Day5 #90DaysOfDevOps Challenge

Advanced Shell Scripting

Advanced Shell Scripting

Advanced Linux Shell Scripting for DevOps Engineers with User Management

Tasks:

  1. 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*.*

Example: When the script is executed as

./createDirectories.sh day 1 5

Then it creates 5 directories as day1 day2 day3 .... day5

No alt text provided for this image

Here we are using C-Styled for loops, which allows us to use variables inside for loops to iterate over a range of elements.

C-styled for loops are loops that have 3 parts, the initializing iterator, the incrementor/decrementer, and the end condition. And the spaces between double braces are part of the syntax.

2. Create a Script to back up all the work done.

-- Firstly we defined the directory for which we are taking backup - src_dir.

-- Then the dir where the backup will be saved - target_dir.

-- We are saving the backup file name as per the timestamp so defined the curr_timestamp using the date command output as a variable.

-- The backup file name with the target path where we are placing it, has to be defined.

-- Now the tar command will archive the files saved into the source directory and place them into the target directory with the current timestamp. The archive will only have the script folder and not the whole src directory structure inside. -C flag is used to change the dir to the src directory and only archive the script dir and files inside it.

No alt text provided for this image

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

Cron is a service that helps in scheduling jobs in Linux. And the schedules are defined in a configuration file called crontab. These tasks can be anything like system maintenance and backups or running periodic scripts for various applications etc.

Cron job runs as a daemon in the background and checks the /etc/crontab and /etc/cron.d directories for scheduled jobs. Once any schedule is reached, cron executes the job using the specified user's account privileges.

In the crontab file, we can define multiple jobs, with their scheduled time and the account under which the job should run.

Format of setting schedules:

No alt text provided for this image

Command used:

crontab -l \=> To see all active crontabs

crontab -e \=> To select an editor and open a crontab where we will define our cron schedule for a job.

crontab -r \=> to remove a crontab file.

Automate the backup script created in task 2:

-- created a new crontab using crontab -e.

-- defined a corn job to be run every second of every minute of every hour of every day of every week and every month.

No alt text provided for this image

-- The script is running every second and backups are created

No alt text provided for this image

  1. User Management in Linux-

A Linux user is an account that logs in to the system and performs various operations. Each user has their home directory where they can store their files, and they have their own set of permissions that determine what actions they are allowed to perform on the system.

Users have a unique user ID that identifies them in the system. ID 0 is assigned to the root user and the IDs 1 to 999 (both inclusive) are assigned to the system users hence the ids for local user begins from 1000 onwards.

Some important commands for user management-

  • Add a new user

\> useradd username

  • Assign a password to a user

\> passwd username

  • Change the user ID for a user

\> usermod -u new_id username

  • Modify the group ID of a user

\> usermod -g new_group_id username

  • Change the user login name

\> usermod -l new_login_name old_login_name

  • Change the Home directory of a user

\> usermod -d new_home_dir_path username

  • Delete a username

\> userdel -r username

  • Get the ID of any user

\> id username

  • User configuration file

\> cat /etc/passwd

  • Print all the users in Linux by printing the first column of the user configuration file

\> awk -F ' : ' '{ print $1 }' /etc/passwd

5. Create 2 users and just display their Usernames.

No alt text provided for this image


Thank you for reading! 📘