Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management
Table of contents
- Task 1. Write a bash script createDirectories.sh i.e. using either Loops or command with start day and end day variables using arguments to create a specified number of directories with a dynamic directory name.
- Task 2. Create a Script to backup all your work done till now.
- Task 3. What is Cron and Crontab, to automate the backup Script?
- Task4. What is User Management?
- Task5. Create 2 users and just display their Usernames.
Task 1. Write a bash script createDirectories.sh i.e. using either Loops or command with start day and end day variables using arguments to create a specified number of directories with a dynamic directory name.
#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <base_name> <start_day> <end_day>"
exit 1
fi
# Get the arguments
base_name=$1
start_day=$2
end_day=$3
# Loop through the range and create directories
for (( day = start_day; day <= end_day; day++ )); do
# Construct the directory name
dir_name="${base_name}${day}"
# Create the directory
mkdir "$dir_name"
# Display a message
echo "Created directory: $dir_name"
done
echo "Directories created successfully."
Let's break down the script step by step:
#!/bin/bash
: This is called a shebang and it tells the system that this script should be executed using the Bash shell.if [ "$#" -ne 3 ]; then ... fi
: This checks if the script was called with three arguments (base name, start day, end day). If not, it displays a usage message and exits.base_name=$1
,start_day=$2
,end_day=$3
: These lines store the provided arguments in variables.for (( day = start_day; day <= end_day; day++ )); do ... done
: This is a loop that iterates from the start day to the end day.dir_name="${base_name}${day}"
: This constructs the directory name by combining the base name and the current day number.mkdir "$dir_name"
: This creates the directory using the constructed name.echo "Created directory: $dir_name"
: This displays a message indicating that the directory was created.echo "Directories created successfully."
: This prints a final message once all directories are created.
To use the script, save it in a file named createDirectories.sh
, and give it execute permissions using chmod +x
createDirectories.sh
, and then run it as shown in your example:
./createDirectories.sh day 1 90
This will create directories named day1
, day2
, up to day90
in the current directory.
./createDirectories.sh Movie 20 50
This will create directories named Movie20
, Movie21
, up to Movie50
in the current directory.
Task 2. Create a Script to backup all your work done till now.
#!/bin/bash
# Set the source directory and backup directory
source_dir="$HOME/my_work"
backup_dir="$HOME/work_backup"
# Create the backup directory if it doesn't exist
mkdir -p "$backup_dir"
# Create a timestamp for the backup folder name
timestamp=$(date +"%Y%m%d%H%M%S")
backup_folder="$backup_dir/backup_$timestamp"
# Copy the contents of the source directory to the backup folder
cp -r "$source_dir" "$backup_folder"
echo "Backup completed successfully. Your work has been backed up to: $backup_folder"
We can customize the source_dir
variable to point to your actual work directory. Save this script in a file named backupWork.sh
and make it executable using chmod +x
backupWork.sh
.
You can then run the script as follows:
./backupWork.sh
This will create a backup of your work in the specified backup directory with a timestamped folder name. Remember to customize the source_dir
variable to match the actual directory where your work is stored.
Task 3. What is Cron and Crontab, to automate the backup Script?
What is Cron and Crontab:
Cron is a time-based job scheduler in Unix-like operating systems. It allows you to schedule tasks or commands to run at specific intervals or times automatically. These tasks can include anything from running scripts, performing system maintenance, sending emails, and more.
Crontab is a command used to interact with the cron daemon (scheduler). It allows you to view, edit, and manage the scheduled tasks (also known as cron jobs) that you want to run automatically at specified times or intervals. A crontab file contains lines of instructions, each representing a scheduled task, along with its timing details.
In essence, cron and crontab provide a way to automate repetitive tasks on your computer or server by setting up schedules for when those tasks should be executed.
How to set up an automated backup using cron and crontab
Setting up an automated backup using cron and crontab is a great way to ensure your data is regularly backed up without manual intervention.
Step 1: Create a Backup Script Write a script that performs the backup. This script should copy your important files or directories to a backup location. Save this script in a location where you'll remember, such as /home/user/backup_script.sh
. Make sure the script is executable by running chmod +x /home/user/backup_script.sh
.
Step 2: Open Your Crontab Crontab is the tool you'll use to schedule when the backup script runs. To open your user's crontab, open a terminal and run:
crontab -e
This will open the crontab file in your default text editor.
Step 3: Add a Cron Job In the crontab file, each line represents a scheduled task. To schedule your backup script, add a line like this:
0 2 * * * /home/user/backup_script.sh
In this example:
0
represents the minute when the task will run (0 means the start of the hour).minute
: The minute when you want the backup to run (0-59).2
is the hour when the task will run (2 means 2 AM).hour
: The hour when you want the backup to run (0-23).*
for the other fields means "every" (i.e., every day of the month, every month, and every day of the week).day
: The day of the month when you want the backup to run (1-31).month
: The month when you want the backup to run (1-12).day_of_week
: The day of the week when you want the backup to run (0-6, where Sunday is 0)./home/user/backup_script.sh
is the path to your backup script.
This example means the backup script will run every day at 2 AM.
Step 4: Save and Exit After adding the line, save and close the crontab file. The changes will be automatically applied.
Step 5: Verify To verify that the cron job was added successfully, you can list your current cron jobs by running:
crontab -l
You should see the line you added.
Your backup script will now run automatically at the scheduled time, creating backups of your important data. Just ensure that the paths in your script and crontab are accurate, and your backup process should work smoothly.
Task4. What is User Management?
User management, in simple words, refers to the process of creating, organizing, modifying, and controlling user accounts on a computer system or online platform. It involves tasks related to who can access the system, what permissions they have, and how they interact with the system.
Think of user management as similar to how you manage user profiles on social media or accounts on an online shopping website. In the context of a computer system:
Creating Users: It involves setting up accounts for individuals who will be using the system. Each user typically has a unique username and password.
Modifying User Access: You can adjust what a user can do and see. For instance, some users might have permission to access certain files while others can't.
Organizing Users: Sometimes, users are grouped for easier management. For example, a company might have different user groups like "Employees" and "Managers."
Deactivating or Deleting Users: When someone leaves an organization or you no longer want someone to have access, you can deactivate or delete their user account.
Security and Permissions: User management ensures that only authorized individuals can access sensitive information. It also controls who can change system settings or install software.
Authentication and Authorization: User management involves verifying the identity of users (authentication) and then determining what they're allowed to do (authorization).
User Profiles: Like a profile on a social media platform, user management may involve storing information about users, such as their preferences and settings.
In essence, user management is about maintaining a structured, secure, and organized environment for users to interact with a computer system or digital service. It's essential for maintaining security, privacy, and efficient operation of systems and services.
Task5. Create 2 users and just display their Usernames.
To create two users and display their usernames in a Linux terminal, you can use the following commands. You'll need root or superuser privileges to create new users.
Open a terminal window.
Create the first user:
sudo useradd user1
This command creates a new user named "user1." You can replace "user1" with your desired username.
- Create the second user:
sudo useradd user2
This command creates a new user named "user2." You can replace "user2" with your desired username.
- To display their usernames, you can use the
awk
command to extract the usernames from the/etc/passwd
file:
awk -F: '{print $1}' /etc/passwd
This command will list all the usernames on your system, including the two you just created.
I appreciate your time in reading this blog. Thank you for engaging with the content.
I trust you found the information both helpful and insightful. Enjoy your learning journey, and feel free to reach out with any further questions!