Day 5 - Advanced Linux Shell Scripting for DevOps Engineers with User Management
Tasks

💼 Senior Software Engineer
Create Directories Using Shell Script:
ubuntu@ip-172-31-45-42:~/devops/ketan$ vim create_folders.sh#!/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."ubuntu@ip-172-31-45-42:~/devops/ketan$ chmod 700 create_folders.shubuntu@ip-172-31-45-42:~/devops/ketan$ ./create_folders.sh "Day" 1 90 Created directory: Day-1 Created directory: Day-2 Created directory: Day-3 Created directory: Day-4 Created directory: Day-5 Directories created successfully.Create a Script to Backup All Your Work:
#!/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"./create_backup.sh /path/to/source_directory /path/to/target_directoryubuntu@ip-172-31-45-42:~/devops$ mkdir backupfolder ubuntu@ip-172-31-45-42:~/devops$ ls backfolder backupfolder create_backup.sh create_bakup.sh ketan ubuntu@ip-172-31-45-42:~/devops$ ./create_backup.sh ketan backupfolder ./Day-22/ ./Day-79/ ./Day-84/ ./Day-24/ Backup created successfully at: backupfolder/backup_20240706_142339.tar.gzRead About Cron and Crontab to Automate the Backup Script:
To add a cron job to execute the shell script at 11:00 PM every day, you must 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 -e0 23 * /path/to/your/shell/script.sh /path/to/source_directory /path/to/target_directoryReplace "/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 will now run the shell script at 11:00 PM every day. It will create a backup with the current timestamp in the specified target directory.
Read About User Management:
Linux includes a built-in user management system to create, modify, and delete users.
To create a user, we can use the following command:
sudo useradd usernameubuntu@ip-172-31-45-42:~/devops$ sudo useradd ketanhiray ubuntu@ip-172-31-45-42:~/devops$ cat /etc/passwd ketanhiray:x:1005:1007::/home/ketanhiray:/bin/shTo modify user permissions, we can use the following command:
ubuntu@ip-172-31-45-42:~/devops$ chmod 700 ketanThis command will give the user
usernameread, write, and execute permissions to the directorydirectory.To delete a user, we can use the following command:
sudo userdel username
Post Your Progress:
Mastering Advanced Linux Shell Scripting opens up a world of possibilities for DevOps engineers. Automating repetitive tasks and efficiently managing users can enhance productivity and streamline operations. I'm thrilled to have honed these skills and am eager to put them into practice in real-world projects!
Remember to like, comment, and share this post if you find it helpful, so we can inspire and support each other in our DevOps journey!




