Linux For Devops
Day 5 : Advanced Linux Shell Scripting for DevOps Engineers with User management
Creating Directories Dynamically:
To create directories dynamically using a bash script, we will utilize command-line arguments to pass the necessary information. The script, createDirectories.sh
, will take three arguments: directory name, start number, and end number. Here's an example of how the script would look:
#!/bin/bash
directory_name=$1
start_number=$2
end_number=$3
for ((i=start_number; i<=end_number; i++)); do
dir_name="${directory_name}${i}"
mkdir $dir_name
done
echo "Directories created successfully."
In this script, we store the command-line arguments in corresponding variables (directory_name
, start_number
, end_number
). We then use a for
loop to iterate from the start number to the end number and create directories with dynamically generated names by appending the current iteration number to the specified directory name. Finally, we display a success message once the directories are created.
To execute the script, run the command: ./
createDirectories.sh
mydir 1 5
, where mydir
is the desired directory name, and 1
and 5
are the start and end numbers, respectively. This will create five directories named mydir1
, mydir2
, mydir3
, mydir4
, and mydir5
.
Script to Backup Work Done:
Creating a backup script allows us to safeguard our work and prevent data loss. Here's an example of a backup script called backup.sh
:
#!/bin/bash
backup_dir="backup"
mkdir -p $backup_dir
cp -R path_to_work $backup_dir
echo "Backup created successfully."
In this script, we define a variable backup_dir
to specify the name of the backup directory. The mkdir -p
command ensures that the directory is created if it doesn't exist. The cp -R
command copies the contents of the path_to_work
directory to the backup directory, preserving the directory structure. Finally, we display a success message once the backup is created.
To execute the script, replace path_to_work
with the actual path to your work directory and run the command: ./
backup.sh
. This will create a backup of your work in the specified backup_dir
.
Understanding Cron and Crontab:
Cron is a time-based job scheduler in Unix-like operating systems. It allows you to schedule commands or scripts to run at specific intervals or times. Crontab, short for "Cron Table," is the configuration file that stores the scheduled tasks for each user.
Creating a Backup Script: Before we automate the backup process, let's create a backup script called
backup.sh
. Here's an example:
#!/bin/bash
backup_dir="backup"
source_dir="path_to_work"
mkdir -p $backup_dir
cp -R $source_dir $backup_dir
echo "Backup created successfully."
In this script, we define the variables backup_dir
to specify the name of the backup directory and source_dir
to denote the directory you want to back up. The mkdir -p
command ensures that the backup directory is created if it doesn't exist. The cp -R
command copies the contents of the source directory to the backup directory while preserving the directory structure. Finally, a success message is displayed once the backup is created.
- Creating a Cron Job: A cron job is a task scheduled to run at specific intervals. To create a cron job for our backup script, follow these steps:
Open the terminal and run the
crontab -e
command to edit the crontab file.Choose your preferred text editor if prompted.
Add the following line to schedule the backup script to run daily at 2 AM:
0 2 * * * /bin/bash /path/to/backup.sh
- Save and exit the file.
- Understanding the Cron Syntax: The cron syntax consists of five fields separated by spaces:
* * * * * command_to_be_executed
The first field represents minutes (0-59).
The second field represents hours (0-23).
The third field represents the day of the month (1-31).
The fourth field represents the month (1-12).
The fifth field represents the day of the week (0-7, where both 0 and 7 represent Sunday).
Using asterisks (*) allows the cron job to run at every possible value in that field.
Editing Crontab Entries: To edit your crontab entries, you can use the
crontab -e
command. This will open the crontab file in your default text editor. You can then add, modify, or delete entries as needed. Remember to save the file after making changes.Verifying and Managing Cron Jobs: To list your cron jobs, you can use the
crontab -l
command. It will display all the scheduled tasks for your user.
To remove a cron job, use the crontab -e
command to edit the crontab file, delete the corresponding line, and save the file.
Testing the Backup Cron Job: Once the cron job is set up, it will run automatically according to the specified schedule. To test the backup cron job, you can either wait until the scheduled time or manually trigger it by modifying the cron schedule to run in a few minutes.
Monitoring Cron Job Output: Cron jobs execute in the background, and their output is usually sent via email to the owner of the crontab. To redirect the output to a file, you can modify the cron job line as follows:
0 2 * * * /bin/bash /path/to/backup.sh >> /path/to/logfile.log 2>&1
This will append both standard output and standard error to the specified logfile.
Creating Users and Displaying Usernames:
Managing users is a common task in a Linux environment. Here's an example script, createUsers.sh
, to create two users and display their usernames:
#!/bin/bash
users=("user1" "user2")
for user in "${users[@]}"; do
useradd $user
echo "Created user: $user"
done
In this script, we define an array called users
that contains the desired usernames. We then use a for
loop to iterate over the array and create users using the useradd
command. After creating each user, we display their username using the echo
command.
To execute the script, run the command: ./
createUsers.sh
. This will create two users with usernames "user1" and "user2" and display their names on the console.
Conclusion: Bash scripting empowers DevOps professionals to automate various tasks and streamline workflows. In this blog post, we explored the process of creating a bash script, createDirectories.sh
, to dynamically create directories based on user inputs. We also covered creating a backup script to safeguard our work and creating users while displaying their usernames. By harnessing the power of bash scripting, we can enhance our productivity, reduce manual effort, and ensure consistency in our operations.