๐Linux Shell Scripting: DevOps Engineers with User Management Skills & cronjobs๐ฅ๐ป๐ง Day#5 of the #90DaysOfDevOps Challenge!#DevOpsJourney
TABLE OF CONTENTS
๐ Introduction
๐ Creating Dynamic Directories using Shell scripting
๐ฅ User management in Linux
๐ Automated backups scripts
โฒ๏ธ Automating backups script with a cronjob
๐ Conclusion
INTRODUCTION:
In today's session, we will see into various Linux system administration and shell scripting topics. In this blog we will create dynamic directories using shell script Next, we will delve into an automated backup script to ensure data safety. We will also learn how to schedule these backups seamlessly using Cronjob. Moreover, we'll explore the realm of user management in Linux, covering username creation and display. To wrap up, we will provide a detailed script enabling users to effortlessly generate multiple directories with dynamic names, tailored to their preferences.
CREATING DIRECTORIES USING SHELL SCRIPTS:
Now we will see how to create dynamic directories using advanced shell script using loops with dynamic names
depending on the input arguments.
Below is the Bash script written in this file test1.sh
and it is open in text editor vim, which will create the specified number of directories with dynamic names:
#!/bin/bash
# Check if exactly three arguments are provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <directory_name> <start_number> <end_number>"
exit 1
fi
# Store the arguments in variables
dir_name="$1"
start_num="$2"
end_num="$3"
# Check if start_num is less than or equal to end_num
if [ "$start_num" -gt "$end_num" ]; then
echo "Error: Start number must be less than or equal to the end number."
exit 1
fi
# Loop through the range of numbers and create directories
for ((i = start_num; i <= end_num; i++)); do
dir="$dir_name$i"
mkdir "$dir"
echo "Created directory: $dir"
done
Save the script to a file named test1.sh
, then give it execute permission:
chmod +x test1.sh
Now you can run the script with your desired arguments:
./test1.sh day 1 90
or
./tets1.sh Movie 20 50
The script will create the specified number of directories with dynamic names according to the provided arguments.
BACKUPS SCRIPT:
Below is a sample backup script that creates a compressed archive of our work directory and saves it to a specified backup location.
#!/bin/bash
# Check if the number of arguments is correct
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <source_directory> <destination_directory>"
exit 1
fi
# Store the source and destination directories in variables
source_dir="$1"
destination_dir="$2"
# Check if the source directory exists
if [ ! -d "$source_dir" ]; then
echo "Error: Source directory '$source_dir' not found."
exit 1
fi
# Check if the destination directory exists; if not, create it
if [ ! -d "$destination_dir" ]; then
mkdir -p "$destination_dir"
fi
# Generate a timestamp for the backup filename
timestamp=$(date +"%Y%m%d_%H%M%S")
# Create the backup filename with the timestamp
backup_filename="backup_${timestamp}.tar.gz"
# Create the backup
tar -czf "${destination_dir}/${backup_filename}" -C "$source_dir" .
echo "Backup created successfully: ${destination_dir}/${backup_filename}"
Save the script to a file, e.g., backup_script.sh
, and give it execute permission:
chmod +x backup_script.sh
Now, you can run the script by providing the source directory and the destination directory as arguments:
./backup_script.sh /path/to/source_directory /path/to/destination_directory
Please make sure to replace /path/to/source_directory with the actual path to the directory you want to back up and /path/to/destination_directory with the directory where you want to store the backup files.
AUTOMATE THE BACKUPS USING CRON JOB:
To automate the backup script using a cron job, open the cron table for editing:
crontab -e
Add the following line at the end of the file to run the script daily at a specific time (e.g., 3:00 AM):
0 3 * * * /path/to/backup_script.sh
Make sure to replace /path/to/backup_script.sh
with the actual path to your backup script.
Save the file and exit the editor. The cron job is now set to run daily at 2:00 AM, executing the backup script and creating a new backup archive.
USER MANAGEMENT IN LINUX:
User management in Linux involves creating, modifying, and managing user accounts. Here are some simple commands to perform basic user management tasks:
- Create a new user:
sudo useradd username
- Set a password for the user:
sudo passwd username
- Delete a user (and their home directory):
sudo userdel -r username
- Modify user properties:
sudo usermod -aG groupname username
- List all users:
cat /etc/passwd
- List a specific user's details:
id username
- Switch to another user account:
su username
These commands help in managing user accounts, setting permissions, and controlling access to the Linux system.
CONCLUSION:
Shell scripting plays a vital role in the world of DevOps, empowering engineers to automate and streamline various tasks and processes. By harnessing the power of Bash and other shell languages, DevOps professionals can create efficient and robust scripts to handle system configurations, deployment pipelines, backups, monitoring, and more. Shell scripting enables rapid prototyping, simplifies repetitive tasks, and ensures consistency across environments.
Please like share if this blog post seems helpful. And give suggestions to bring more posts like this and for improvements. I am very excited to implement all these skills in real-world.
#DevopsJourney #Advanceshellscripting #Devops # Automation #Learningdevops #90daysof devops.