Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management

Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management

  • If you noticed that there are a total 90 sub-directories in the directory '2023' of this repository. What did you think, how did I create 90 directories? Manually one by one or using a script, or a command?

  • All 90 directories within seconds using a simple command mkdir day-{1..90} .

Tasks

  • You have to do the same using Shell Script i.e using either Loops or command with start day and end day variables using arguments So Write a bash script create createdirectories.sh that when the script is executed with three given arguments (one is the directory name and second is start number of directories and third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.

    Example 1: When the script is executed as ./createdirectories.shday 1 90 then it creates 90 directories as day1 day2 day3 .... day90

  • To create and run this script, follow these steps:

    1. Create a file of scripts and then open a text editor (e.g., nano, vim, or a code editor like VSCode).

    2. Copy and paste the script into the editor.

    3. Save the file with a .sh extension, for example, createdirectories.sh.

    4. Give permission to that file using chmod 777 createdirectories.sh

    5. Open your terminal and navigate to the directory where the script is saved.

    6. Run the script using the command: ./createdirectories.sh day.

Bash

# vim createdirectories.sh
#!/bin/bash

if [ "$#" -ne 3 ]; then
    echo "Usage: $0 directories day-1 day-90"
    exit 1
fi

directory_name=$1    # Directory name provided as the first argument
start=$2             # Start number provided as the second argument
end=$3               # End number provided as the third argument

# Loop through the range of numbers and create directories
for (( i = $start; i <= $end; i++ )); do
    dir_name="${directory_name}${i}"  # Concatenate the directory name with the number
    mkdir -p "$dir_name"              # Create the directory
    echo "Created directory: $dir_name"
done

Explanation of script:

  1. #!/bin/bash: This is the shebang line, specifying that the script should be executed by the Bash shell (/bin/bash).

  2. if [ "$#" -ne 3 ]; then: This line checks if the number of arguments provided when executing the script is not equal to 3.

    • $# refers to the number of arguments passed to the script.

    • If the number of arguments is not equal to 3, it displays usage instructions and exits with an exit code of 1.

  3. directory_name=$1, start=$2, end=$3: These lines assign the first, second, and third arguments provided when executing the script to variables directory_name, start, and end respectively.

  4. for (( i = $start; i <= $end; i++ )); do: This starts a for loop that iterates from the start number to the end number.

  5. dir_name="${directory_name}${i}": Inside the loop, this line concatenates the directory_name (provided as the first argument) with the current value of i to form the name of the directory to be created.

  6. mkdir -p "$dir_name": This command creates the directory with the name specified in dir_name.

    • -p flag ensures that parent directories are created if they don't exist.
  7. echo "Created directory: $dir_name": This line displays a message indicating that the directory has been created, showing the name of the directory created.

Example 2: When the script is executed as ./createDirectories.shMovie 20 50 then it creates 50 directories as Movie20 Movie21 Movie23 ...Movie50

NOTE:*same as example 1*

🔒 Create a Script to backUp all your work done till now

  • Backups are an important part of DevOps Engineer's day to Day activities The video in References will help you to understand How a DevOps Engineer takes backups (it can feel a bit difficult but keep trying, Nothing is impossible.)

  • Let's create a simple Bash script that backs up all your important work files and directories to a specified backup location. We'll use emojis and bullet points to make it easy to understand:

  • Script Description: The script will create a compressed backup of specified work files and directories, storing them in a backup directory with a timestamp in the filename. The script will be interactive, allowing you to choose which files and directories to back up. here is the full detail of backups

Here will start the backup script

  • To create a backup script, first, create a backups folder. Enter the folder and check your current directory using pwd. To come back, use cd.

  • Navigate to the folder you want to back up, and create a file named backup.sh, and begin writing the script.

  • Now, I want to back up a script folder. Go to that folder, print your working directory using pwd and within that folder, create a backup.sh file to start writing the script for backing up your script folder.

Bash

# vim backup.sh
-------------------------------------------------------------------------------
#!/bin/bash

src_dir=/home/ubuntu/scripts # past your script folder pwd
tgt_dir=/home/ubuntu/backups # past your backups folder pwd

curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%s")

backup_file=$tgt_dir/$curr_timestamp.tgz

echo "Taking backup on $curr_timestamp"

echo "$backup_file"

tar czf $backup_file --absolute-names $src_dir

echo "Backup complete"

Explanation of this script:

  1. #!/bin/bash: This line is known as a shebang. It tells the system that this script should be executed using the Bash shell.

  2. src_dir=/home/ubuntu/scripts: This line initializes a variable named src_dir and sets its value to /home/ubuntu/scripts, which is the source directory containing the files to be backed up.

  3. tgt_dir=/home/ubuntu/backups: This line initializes a variable named tgt_dir and sets its value to /home/ubuntu/backups, which is the target directory where the backup will be stored.

  4. curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%s"): This line uses the date command to get the current date and time in a specific format (%Y-%m-%d-%H-%M-%s, which represents year-month-day-hour-minute-seconds) and assigns it to the variable curr_timestamp.

  5. backup_file=$tgt_dir/$curr_timestamp.tgz: This line creates a variable backup_file which contains the path and name of the backup file. It combines the target directory path ($tgt_dir) with the current timestamp ($curr_timestamp) and appends the .tgz extension to denote a compressed tar archive.

  6. echo "Taking backup on $curr_timestamp": This line simply prints a message indicating that a backup is being taken along with the current timestamp.

  7. echo "$backup_file": This line prints the full path and filename of the backup file that will be created.

  8. tar czf $backup_file --absolute-names $src_dir: This line uses the tar command to create a compressed archive (tgz format) of the files in the source directory ($src_dir).

    • czf options: c creates a new archive, z compresses the archive using gzip, and f specifies the archive filename.

    • --absolute-names: This option includes absolute pathnames when creating the archive.

  9. echo "Backup complete": This line prints a message indicating that the backup process is complete.

  • Grant permission to the backup.sh file using chmod 777 backup.sh and execute that script using ./backup.sh. Press Enter after executing the command.

  • Return to your backups folder and run ls. You'll notice a red-colored .tgz file. To extract this file, use tar xf and hit Enter. Then, execute ls to view the contents. You'll find a home folder; navigate into it to discover backups of your script's folder.

Now you have a handy backup script to safeguard all your important work files and directories! 🌟 Remember to run the script regularly to keep your data safe and secure.

Read About Cron and Crontab, to automate the backup Script 🔄

  • Cron is the system's main scheduler for running jobs or tasks unattended. A command called crontab allows the user to submit, edit or delete entries to cron. A crontab file is a user file that holds the scheduling information.

what is Cron:

  • Cron is a time-based job scheduling program in Linux-like operating systems.

  • It allows users to schedule repetitive tasks at specified intervals.

  • These tasks can include running scripts, commands, or other processes automatically.

what is Crontab:

  • Crontab is the configuration file used to manage cron jobs.

  • Each user on the system can have their crontab file.

  • It lists the schedule and commands for the tasks to be executed.

As a DevOps engineer, you should be aware of the five components in a cron job: minute, hour, day of the month, month, day of the week, and the command to execute.

  • Let's create a crontab using crontab -e

Bash

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
00 6 * * * echo "this is first cronjob" > /home/ubuntu/crontab-backup.txt

Read about User Management📜

  • A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system. In this post, we will learn about users and commands which are used to get information about the users. After installation of the operating system, the ID 0 is assigned to the root user and the IDs 1 to 999 (both inclusive) are assigned to the system users and hence the ids for local user begins from 1000 onwards.

Create 2 users and just display their Usernames

To create users and display their usernames on a Linux system using the command line, you can use the following steps:

  1. Create the Users: create file useradd.sh Use the adduser command (or useradd depending on the Linux distribution) to create two users. For example, let's create users named cat and dog.

    Bash

     sudo adduser cat
     sudo adduser dog
    

    This will prompt you to set passwords and other user information. Follow the prompts to complete the user creation process.

  2. Display Usernames: Once the users are created, you can display their usernames using the awk command in conjunction with /etc/passwd file, which contains user account information.

    Bash

     cat /etc/passwd | awk -F: '{ print $1 }'
    

    This command reads the /etc/passwd file and prints the first field (the usernames) separated by a colon (:) delimiter.

  3. Give permission using chmod 777 useradd.sh and execute with this command ./useradd.sh

If you run this command, it will display the usernames of all users created on the system, including the cat and dog accounts you just created.

Bash

# vim useradd.sh
-------------------------------------------------------------------------------
#!/bin/bash
sudo adduser cat
sudo adduser dog
cat /etc/passwd | awk -F: '{ print $1 }'


Happy Learning

Thanks For Reading! :)

-SriParthu💝💥