How to Master Advanced Linux Shell Scripting for DevOps: User Management and Automation | Day 5 of 90 Days of DevOps Journey
Introduction
In this blog, I will show you some advanced Linux shell scripting techniques that are useful for DevOps engineers. You will learn how to create and manage users, automate backup tasks with cron, and write functions in Bash. These skills will help you perform common system administration tasks more efficiently and securely.
Shell scripting is the art of writing commands or scripts that can be executed by the shell, which is the program that interprets user input and runs other programs on Linux. Shell scripting is essential for DevOps because it allows you to automate repetitive tasks, customize your environment, and interact with other tools and services.
In this blog, I will use the Bash shell, which is the default shell on most Linux distributions. Bash stands for Bourne-Again SHell, and it is an enhanced version of the original Bourne shell. Bash supports many features such as variables, loops, conditionals, arrays, functions, and redirections.
Before we start writing shell scripts, we need to make sure they are executable and have the correct syntax. To make a script executable, we need to use the chmod
command with the +x option. For example:
chmod +x
script.sh
To indicate the syntax of a script, we need to use a special line at the beginning of the file called a shebang. The shebang tells the shell which interpreter to use to run the script. For Bash scripts, the shebang is usually:
#!/bin/bash
Now that we have covered the basics of shell scripting, let’s move on to the tasks for day 5 of our DevOps training.
The tasks are:
You have to create a directory for each day of 90 days using Shell Script i.e. using either Loops or command with start day and end day variables using arguments.
Create a shell script for backing up the parent of all directories where we have a directory for each day of 90 days created in the above task.
Automate the backup script with cron.
User management in Linux.
Let’s see how we can accomplish each task with shell scripting.
Task 1: Creating directories for each day of 90 days using a shell script
#!/bin/bash
if [ $# -ne 3 ]; then
echo "Usage: $0 <string> <start_day> <end_day>"
exit 1
fi
string="$1"
start_day=$2
end_day=$3
if ! [[ "$start_day" =~ ^[0-9]+$ ]] || ! [[ "$end_day" =~ ^[0-9]+$ ]]; then
echo "Start and end day should be integers."
exit 1
fi
if [ "$start_day" -gt "$end_day" ]; then
echo "Start day should be less than or equal to the end day."
exit 1
fi
for ((i=start_day; i<=end_day; i++)); do
directory_name="${string}-${i}"
mkdir "$directory_name"
echo "Created directory: $directory_name"
done
echo "Directories created successfully."
AboveLinux shell script takes three arguments a string, a starting day, and an ending day. It uses a for loop to create folders with names in the format "argument1-number" and increments the number until it reaches the ending day.
Save the above script in a file, for example, "create_folders.sh," and make it executable using the following command:
chmod +x create_folders.sh
You can run the script with the following command:
./create_folders.sh "Day" 1 90
This will create five folders with the names "Day-1," "Day-2," ..., and "Day-90" in the current directory. You can replace "Day" with any string and change the start and end numbers as per your requirement.
Task 2: Create a Script to backup all your work done till now
Linux shell script that takes source and target directories as arguments and creates a backup with the current timestamp.
#!/bin/bash
if [$# -ne 2]; then
echo "Usage: $0 <source_directory> <target_directory>"
exit 1
fi
source_dir="$1"
target_dir="$2"
# Validate if the source directory exists
if [ ! -d "$source_dir" ]; then
echo "Error: Source directory does not exist."
exit 1
fi
# Validate if the target directory exists
if [! -d "$target_dir" ]; then
echo "Error: Target directory does not exist."
exit 1
fi
# Create backup with current time stamp
backup_file="$target_dir/backup_$(date +'%Y%m%d_%H%M%S').tar.gz"
# Create the backup archive
tar -cvf "$backup_file" -C "$source_dir" .
echo "Backup created successfully at : $backup_file"
You can run the script with the following command:
./create_backup.sh /path/to/source_directory /path/to/target_directory
Replace "/path/to/source_directory" and "/path/to/target_directory" with the actual paths of the source and target directories, respectively. The script will create a compressed archive file in the target directory with the current timestamp in the filename. The backup will include all files and subdirectories from the source directory.
Task 3: Automate the backup script with cron
To add a cron job to execute the shell script at 11:00 PM every day, you need to open the crontab file for the user that will run the cron job. Follow these steps to add the cron job:
Open the crontab file for editing
crontab -e
0 23 * /path/to/your/shell/script.sh /path/to/source_directory /path/to/target_directory
Replace "/path/to/your/shell/script.sh" with the actual path to your shell script file. Also, replace "/path/to/source_directory" and "/path/to/target_directory" with the actual paths of the source and target directories.
Save the crontab file and exit the editor.
The cron job is now set to execute the shell script at 11:00 PM every day. It will create a backup with the current timestamp in the specified target directory.
Task 4: User management in Linux
Linux has a built-in user management system. We can use this system to create users, modify user permissions, and delete users.
To create a user, we can use the following command:
sudo useradd username
This command will create a user named
username
.To modify user permissions, we can use the following command:
sudo chmod u+rwx,g+rwx,o+r directory
This command will give the user
username
read, write, and execute permissions to the directorydirectory
.To delete a user, we can use the following command:
sudo userdel username
This command will delete the user
username
.
Conclusion
Advanced Linux Shell Scripting opens a world of possibilities for DevOps engineers. By automating repetitive tasks and managing users efficiently, we enhance productivity and streamline operations. I'm thrilled to have mastered these skills and can't wait to implement them in real-world projects!
Remember to like, comment, and share this post if you find it helpful. Let's inspire and support each other as we venture through the exhilarating world of DevOps!
#DevOpsJourney #AdvancedShellScripting #UserManagement #Automation #TechSkills #LearningCommunity #ShellScripts #BackupAutomation #LinuxCommands #90DaysOfDevOps