Advanced Linux Shell Scripting for DevOps Engineers with User Management (Day-5 Task)

Advanced Linux Shell Scripting for DevOps Engineers with User Management (Day-5 Task)

1. Task 01

Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is the directory name and the second is the start number of directories and the third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.

As we know that if we want to pass any argument at the time of bash script execution we used to denote it as like $1(first arg),$2(2nd arg), etc...So here we will do the same thing in a bash script like :

name of the file as createDirec.sh

start_day as $2 and end_day as $3 and day as $1

Execute as ./createDirec.sh day 1 90

Output

2. Task 02

Create a Script to back up all your work done till now.

Backups are an important part of DevOps Engineers' day to Day activities. Usually, when we create any backup of a file we used to create a zip file by using the tar command: tar -czvf file.tar.gz directory_name

Here is a simple shell script to take a backup of recent work:

First, make a directory called backup and another directory which is already created previously shell_scripts( previous work done within this folder). We want to take backup daily so printed a current timestamp.

3. Read About Cron and Crontab, to automate the backup Script

Cron is a time-based job scheduler that runs in the background and triggers scripts or commands according to a specified schedule. It utilizes a configuration file known as the Crontab (short for "cron table") to define the tasks and their associated schedules. Each user on a Unix-like system can have their own crontab file, enabling them to schedule and manage their individual set of tasks.

Reasons for using Cron jobs:

  • Helps OS to take a scheduled backup of log files or database.

  • Delete old log files

  • Archive and purge database tables

  • Send out any notification email such as Newsletters, Password expiration email

  • Regular clean-up of cached data

  • Crontab is an ideal option to automate Unix jobs.

  • It is used to automate system maintenance

Crontab syntax:

MIN HOUR DOM MON DOW CMD

Ranges :

Here's a brief overview of how Cron and Crontab work together to automate backup scripts:

1/ Crontab Syntax: The Crontab file contains entries specifying the schedule and the command(s) to be executed. Each entry consists of six fields, separated by spaces, representing the minute, hour, day of the month, month, day of the week, and the command to be executed.

* * * * * command_to_be_executed

The asterisks (*) represent wildcards that can be used to match any value. For example, using an asterisk in the minute field would match every minute. Additionally, you can use specific numeric values or ranges to define precise schedules.

2/ Editing Crontab: To edit your Crontab file, you can use the command crontab -e, which opens the file in the default text editor. Each user's Crontab file is stored in a separate location on the system.

3/ Scheduling Backup Scripts: To automate a backup script, you would typically create a shell script that performs the backup operations you require. For example, it might copy specific files or directories to a backup location.

Once you have your backup script ready, you can schedule it using Crontab. For instance, if you want to run the script every day at 3:00 AM, you would add the following entry to your Crontab file:

0 3 * * * /path/to/backup_script.sh

In this example, "0 3 *" represents the schedule: at 3:00 AM every day. The "/path/to/backup_script.sh" is the command to be executed.

4/ Additional Crontab Features: Crontab provides additional features to further customize the scheduling of tasks. You can use the environment variables, redirect output to log files, and specify email addresses to receive notifications upon task completion or errors.

For example, you can redirect the output and error streams to log files like this:

0 3 \ * \ /path/to/backup_script.sh >> /path/to/backup.log 2>&1

This example appends the output and error messages to the "backup.log" file, allowing you to review them later.

5/ Verifying and Listing Crontab Entries: To view the current entries in your Crontab file, you can use the command crontab -l. It displays the existing cron jobs for the current user.

You can also remove all the entries in your Crontab file by running crontab -r.

Cron and Crontab provide a flexible and reliable method to automate backup scripts and other routine tasks in Unix-like systems. By leveraging the scheduling capabilities of Cron and the customizable nature of Crontab, you can ensure that your backup operations are performed automatically and according to your desired schedule.

4. Read about User Management

User management includes everything from creating a user to deleting a user on your system.

root

The root user is the superuser and has all the powers for creating a user, deleting a user and can even login in with the other user's account. The root user always has userid 0.

useradd

1. With useradd command, you can add a user.

Syntax: sudo adduser username [you should have the super user power to add a user]

Then Enter the password for the new account and confirm

You can check the newly added user from the file /etc/passwd

Command: cat /etc/passwd

2. For disabling an account using Terminal, remove the password set on the account.

sudo passwd -l 'username'

3. To delete an account, use the command –

sudo userdel -r 'username

5. Create 2 users and just display their Usernames

Added two users by using the useradd command :

$ sudo useradd Sam

$ sudo useradd Vinod

To display their name use cat /etc/passwd and redirected the output to the tail command to see only the last two lines from the file :

cat /etc/passwd | tail -2