Day Five: More Scripting & Introduction to User Management - The #90DaysOfDevOps Challenge
Welcome to Day 5 of the #90DaysOfDevOps challenge! Today's we focus on more advanced shell scripting and introduce user management in Linux.
Link to GitHub Repo for the challenge.
For Day five of the challenge, the following tasks were to be completed:
Write a bash script that creates directories dynamically.
Create a Script to backup the work.
Automate the backup script.
Create 2 users and display their usernames
Script to create directories dynamically
First, we write the script. We will write the script into a file
createDirectories.sh
as instructed.
#!/bin/bash
# Check if correct number of arguments are provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <directory_name> <start_number> <end_number>"
exit 1
fi
directory_name="$1"
start_number="$2"
end_number="$3"
for (( i=start_number; i<=end_number; i++ )); do
mkdir -p "${directory_name}${i}"
done
echo "Directories created successfully."
We use arguments to get the directory name, start number and end number of the directories to be created. We then use a loop to create directories with the specified range of numbers appended to the directory name.
Note: Brace expansion may give you difficulties when creating the directories dynamically.
When we run the script like so ./createDirectories.sh test_dir 1 10
we get the following result.
Write a backup script
#!/bin/bash
to_back_up="/path/to/directory/to/backup"
backup_dir="/path/to/backup"
mkdir -p "$backup_dir"
cp -r "$to_back_up" "$backup_dir"
echo "Backup completed successfully."
First we define which directory is to be backed up. We then create a backup directory only if it does not exist. We then copy the files from the first directory to the backup directory.
Note: Make sure to replace /path/to/directory/to/backup
with the actual path to your backup script. You can use the command pwd
to help get the path.
When we run the script we get the following results:
The directory backup_test
which is initially not present is created and the files from our previous task copies into it.
Automate the backup script
To automate the backup script we need to do the following:
Open the terminal and type
crontab -e
to edit your user's cron jobs.Add the following line to run the backup script daily at 1:00 AM:
0 1 * /path/to/backup_script.sh
Note 1: Make sure to replace
/path/to/backup_script.sh
with the actual path to your backup script.Note 2: You can use crontab.guru/ to help.
Save and exit the crontab editor. This will schedule your script to run daily at 1:00 AM.
Linux User Creation
To create a user in Linux we use the command
useradd
followed by the username. For this task I used a UTM ubuntu virtual machine.sudo useradd test-user-1
sudo useradd test-user-2
We can then print their usernames using a simple script so we can get the output together
#!/bin/bash echo "User 1: $(getent passwd test-user-1 | cut -d: -f1)" echo "User 2: $(getent passwd test-user-2 | cut -d: -f1)"
getent passwd user1
: This command retrieves information about the user named "user1" . The output usually has several fields separated by colons (:
), including the username, password, user ID, group ID, home directory, and shell.cut -d: -f1
: This command extracts the first field from each line of input, using the colon (:
) as the delimiter. In this case, it extracts the username, which is the first field in the output ofgetent passwd
.
When we run the script we get the following result:
That wraps up Day 4 of the #90DaysOfDevOps challenge! We explored advanced Linux shell scripting techniques and learned how to automate backups using cron and done a bit of user management on Linux. Stay tuned for more challenges and insights as we progress through the journey together. Happy scripting!