Day- 5 : Advanced Linux Shell Scripting for DevOps Engineers with User management
Table of contents
๐ Introduction ๐
Welcome to Day 5 of the #90DaysOfDevOps challenge! Today, we'll dive deep into advanced Linux shell scripting, where we'll focus on essential topics like Creating Dynamic Directories, Automated Backup scripts, Automating the Backup Script with Cron, User Management in Linux, and Creating and Displaying Usernames. As DevOps engineers, mastering shell scripting is vital as it empowers us to automate tasks and optimize processes. ๐ป๐
๐ Creating Dynamic Directories
Before we dive into the script, let's first understand how to create dynamic directories using a simple command in Linux: mkdir day{1..N}
The above command will create N directories named day1, day2, day3, and so on, up to dayN. This demonstrates a quick and efficient way to create multiple directories with sequential names.
๐ Creating Dynamic using Shell Scripting Directories
Let's create a user-friendly shell script named task1.sh that generates directories with dynamic names.
#!/bin/bash
# Check if three arguments are provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <directory_name> <start_number> <end_number>"
exit 1
fi
# Extract arguments
directory_name="$1"
start_number="$2"
end_number="$3"
# Check if start_number and end_number are integers
if ! [[ "$start_number" =~ ^[0-9]+$ ]] || ! [[ "$end_number" =~ ^[0-9]+$ ]]; then
echo "Error: Start and end numbers must be integers."
exit 1
fi # we can also skip this part but it is good practice to do so
# Loop to create directories
for ((i=start_number; i<=end_number; i++)); do
directory="${directory_name}${i}"
mkdir "$directory"
done
echo "Directories created successfully."
this can also be used to check the condition whether the numbers are integers or not
if [[ "$start_number" =~ ^[0-9]+$ && "$end_number" =~ ^[0-9]+$ ]]; then
# Loop to create directories
for ((i=start_number; i<=end_number; i++)); do
directory="${directory_name}${i}"
mkdir "$directory"
done
else
echo "Error: Start and end numbers must be integers."
exit 1
fi
Shebang (
#!/bin/bash
): The first line of the script starts with#!/bin/bash
. It's called a shebang, and it tells the system that the script should be interpreted and executed using the Bash shell.Checking Arguments: The script begins by checking if it received exactly three arguments when it was executed. Arguments are pieces of information that you provide when running a script. In this case, the script expects three arguments: the directory name, the start number, and the end number of directories to create.
Extracting Arguments: If the script receives three arguments, it proceeds to extract those arguments into separate variables for later use. For example, if you run the script with
./
createDirectories.sh
day 1 90
, the variables will be set as follows:directory_name
: "day"start_number
: 1end_number
: 90
Checking Integer Input: The script then checks whether the
start_number
andend_number
are valid integers. It ensures that they contain only digits (0-9) and no other characters. If either of the numbers is not an integer, the script will display an error message and exit.Loop to Create Directories: Once the input is validated, the script enters a loop. This loop will run from the
start_number
to theend_number
(both inclusive) to create the desired number of directories. Inside the loop, it constructs the directory name by combining thedirectory_name
and the loop index.Creating Directories: Inside the loop, for each iteration, the script creates a new directory with the constructed name. For example, if the loop index is 1, it creates a directory named "day1"; if the index is 2, it creates "day2", and so on, up to the specified
end_number
.Printing Success Message: After the loop completes, the script prints a success message to indicate that all the directories have been created successfully.
Running the Script: To run the script, you make it executable using the
chmod 700
createDirectories.sh
command. This gives permission to execute the script. Then, you run the script by providing the directory name and the range of numbers you want to create directories for, like./
createDirectories.sh
day 1 10
.
๐พ Automated Backup Script
Let's create a script that will do system backups by compressing and archiving a source directory to a target location. It automates data safety, ensuring recovery options in case of data loss or system failures. Users can conveniently back up important files and directories with ease.
#!/bin/bash
# Define source and target directories
src=/home/ubuntu/scripts
trg=/home/ubuntu/backups
# Get the current timestamp
curr_timestamp=$(date "+%d-%m-%Y-%H-%M-%S")
# Create backup file name with timestamp
backup=$trg/$curr_timestamp.tgz
# Inform the user about the backup process
echo "Taking backup on $curr_timestamp"
#echo $backup
# Create the backup archive using tar
tar czf $backup --absolute-names $src
echo "backup complete"
This Bash script is designed to create a backup of the files in the "home/ubuntu/scripts" directory. It generates a timestamp to form a unique filename for the backup and saves it in the "home/ubuntu/backups" directory. The user is informed about the backup process with echo statements displaying emojis representing a folder (๐) and a clock (๐). The tar
command is then used to compress and create the backup archive, and once the backup is complete, the user is notified with a message
๐ฐ๏ธ Understanding Cron and Crontab to Automate Your Backup Script ๐๐
Cron is like a timekeeping wizard ๐ฎ that helps your computer perform tasks at scheduled intervals, like clockwork โฐ. Crontab (CRON TABle) is its trusty assistant, the one who writes down the schedule for Cron to follow ๐๏ธ. Together, they automate the backup process, making it hassle-free! ๐
Cron ๐ฐ๏ธ: Cron is a magical ๐ช Unix-based utility that runs in the background of your computer, keeping an eye on the clock โ. It's like having a personal assistant who can handle repetitive tasks without any reminders!
Crontab ๐ : Crontab is like Cron's planner ๐ . It's a text file where you jot down the to-do list, telling Cron when and how often to perform a specific task. Each line in the Crontab represents a different task and its schedule.
Automating with Cron and Crontab ๐๐๏ธ: Now, let's schedule our backup script using Cron and Crontab!
Open the Crontab file by typing
crontab -e
in the terminal. ๐๏ธYou'll see a text editor where you can add your backup task. Each line in Crontab has a specific format:
* * * * * command_to_be_executed
The five
*
represent the schedule for minute, hour, day of the month, month, and day of the week, respectively. Use numbers to specify the exact time for each.To run the backup script every day at 3 AM, add this line to Crontab: ๐
0 3 * * * /path/to/your/backup_script.sh
Here,
0
represents the minute (0-59) and3
represents the hour (0-23).Save the Crontab and exit the editor.
That's it! ๐ Now, your backup script will automatically run every day at 3 AM ๐, making sure your precious files stay safe and sound.
Cron and Crontab may sound intimidating, but they are your loyal timekeepers, ready to automate repetitive tasks ๐คโฐ. With our simple backup script and a touch of Cron magic, you can have peace of mind knowing your files are protected! ๐พ๐ So go ahead and automate your backup process like a tech pro! ๐ช๐ฉโ๐ป
๐ค User Management in Linux ๐ค
In a Linux operating system, a user is like a unique character with special powers! ๐ They can work with files and do many other cool things. Each user has a special ID that makes them one of a kind in the OS. ๐
๐ User ID Range: After installing the OS, the first user is super special โ we call them the "root user" ๐ โ and they have the ID 0. Then there are some other special users with IDs from 1 to 999. These users help the system run smoothly. But the real party starts after ID 999! That's where local users come in, and their IDs start from 1000 and go up. ๐
๐ Commands for User Info: Now, let's talk about some commands to know more about our users! ๐ต๏ธโโ๏ธ
whoami
: This command reveals the name of the user who is currently using the system. It's like asking "Who am I?" and getting an answer! ๐คid
: When you want to know your special ID, just type this command! It shows both the user and group IDs. ๐๐ฅcat /etc/passwd
: Don't worry; there's no cat involved! ๐บ This command spills the beans about all the users on the system, including their IDs and other info.cat /etc/group
: Similar to the previous one, but this time, it's all about the groups. It tells you which users are part of each group. ๐งโ๐คโ๐งTo create a user, use the "useradd" command. For example:
sudo useradd username
๐๏ธ To delete a user, use the "userdel" command. For example:
sudo userdel username
โ๏ธ To modify user properties, such as their password or home directory, use the "usermod" command. For example:
sudo usermod -d /new/home/directory username
๐ To display user information, use the "id" or "finger" command. For example:
id username finger username
Remember to run these commands with administrative privileges using "sudo" to ensure proper user management. ๐ก๏ธ๐
๐ก๏ธCreating and Displaying Usernames ๐
To create users and display their names, follow these simple steps:
๐ฅ๏ธ Open a terminal or command prompt.
โจ Run the following command to create the first user:
sudo adduser user1
Fill in the required details as prompted.
๐ Repeat the previous step to create the second user:
sudo adduser user2
Again, provide the necessary information.
๐ To display the usernames, use this command:
awk -F: '{print $1}' /etc/passwd
This command fetches the usernames from the /etc/passwd file.
By learning these user management commands, you can efficiently handle user accounts on Linux systems. ๐๐ง
๐ Conclusion ๐
๐ Congratulations on successfully completing Day 5 of the #90DaysOfDevOps challenge!
๐ Today's topics included dynamic directories, automated backup scripts, and user management in Linux. You've learned valuable skills to structure data efficiently, ensure data safety with automated backups, and manage users effectively. Keep up the excellent work and stay committed to your DevOps journey. Each day brings new opportunities for growth, so embrace the challenges ahead with enthusiasm. Together, we'll continue this exciting adventure of becoming proficient DevOps professionals! ๐๐ป๐ง๐ฉโ๐ป๐ Happy DevOps-ing! ๐๐
Stay in the loop with my latest insights and articles on cloud โ๏ธ and DevOps ๐ by following me on Hashnode, LinkedIn linkedin.com/in/sahil-kamble-40898b208
Thank you for reading! ๐ Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.