ππ Day 5 DevOps Challenge - Advanced Linux Shell Scripting for DevOps Engineers with User Management π»π
Table of contents
- ππ Automate Directory Creation with Bash Scripting! ππ Create Dynamic Directories Easily ππ
- ππ¦ Automated Backup Script: Protect Your Work Effortlessly! π‘οΈπΎ Simple Bash Script for Data Safety ππ¦
- π Automating Backup with Cron & CrontabποΈ
- π User Management : Creating and Managing Users
ππ Automate Directory Creation with Bash Scripting! ππ Create Dynamic Directories Easily ππ
π Task: We need to create a bash script called createDirectories.sh
that takes three arguments: a directory name, a start number, and an end number. The script will create directories with unique names based on the given range.
π Step 1: Writing the Script
First, open any text editor and create a new file named createDirectories.sh
. We'll use this file to write our bash script. Ready? Let's go! βοΈ
#!/bin/bash
# Check if three arguments are provided
if [ "$#" -ne 3 ]; then
echo "Usage: ./createDirectories.sh <directory_name> <start_number> <end_number>"
exit 1
fi
# Extract the arguments into variables
directory_name=$1
start_number=$2
end_number=$3
# Loop through the range of numbers and create directories
for ((i = start_number; i <= end_number; i++)); do
# Create a directory with a dynamic name
mkdir "${directory_name}${i}"
done
echo "Directories created successfully! π"
In this, we use the special variable $#
, which holds the number of arguments passed to the script. The if
condition checks if $#
is not equal to 3. If there are not exactly three arguments, the script displays the usage message and exits. Otherwise, it proceeds with creating the directories as before.
π Step 2: Running the Script
Now it's time to see our script in action! Open your terminal, go to the directory where you saved createDirectories.sh
, and run the following commands:
chmod +x createDirectories.sh
./createDirectories.sh mydir 1 5
π Ta-da! The script will create 5 directories named mydir1
, mydir2
, mydir3
, mydir4
, and mydir5
.
ππ¦ Automated Backup Script: Protect Your Work Effortlessly! π‘οΈπΎ Simple Bash Script for Data Safety ππ¦
π Task at Hand: Our goal is to create a bash script that automatically backs up all your important work. This script will create a compressed archive of your chosen directory and save it to a backup location. With this script, you can relax knowing your efforts are secure! π‘οΈπ»
π Step 1: Writing the Script Let's start by opening your favorite text editor and creating a new file called backup_script.sh
. We'll use this file to write our backup script. Ready? Let's get started! βοΈπ
#!/bin/bash
# Set the source directory to back up
source_directory="/path/to/your/work/directory"
# Set the backup destination
backup_destination="/path/to/backup/location"
# Create a unique backup file name with date and time
backup_file_name="backup_$(date '+%Y%m%d_%H%M%S').tar.gz"
# Create the backup archive
tar -czf "$backup_destination/$backup_file_name" "$source_directory"
echo "Backup completed successfully! π¦π"
π Step 2: Understanding the Script Let's break down the script we just wrote:
We set the
source_directory
variable to the path of the directory you want to back up. Replace/path/to/your/work/directory
with the actual path to your work directory.We set the
backup_destination
variable to the location where you want to store the backup file. Replace/path/to/backup/location
with the desired backup storage location.We create a unique backup file name using the current date and time. This way, each backup will have a different name, making it easy to identify and organize multiple backups.
The
tar
command compresses the contents of the source directory into a gzipped archive with the specified name and saves it to the backup destination.Finally, we display a message to let you know that the backup process was successful! π
π Step 3: Running the Script With the script ready, let's run it to perform our first backup! Open your terminal, navigate to the directory where you saved backup_script.sh
, and execute the following commands:
chmod +x backup_script.sh
./backup_script.sh
π Automating Backup with Cron & CrontabποΈ
π‘ What is Cron?
Cron is like a scheduler π for your computer! It allows you to set up automatic tasks π, like backups ποΈ, that run at specific times or intervals. It's commonly used in Unix-like systems to keep things organized and save you time β°. By using Cron, you can focus on other things while it handles repetitive tasks π€.
π‘ What is Crontab?
Think of Crontab as the command center ποΈ for Cron. It's where you create and manage your scheduled tasks. Each user has their own Crontab file, and you can easily edit it using simple commands. By using Crontab, you take control of when your tasks run, making it easy to manage your automated processes π.
βοΈ How to Set Up Cron & Crontab:
- Create a file and paste this code:
#!/bin/bash
a=$(date +%y-%m-%d-%H-%M-%S)
touch /home/ubuntu/$a.deb
This script will create a backup file with a timestamp in its name.
Manage cron jobs:
View existing jobs:
crontab -l
Set up a new task:
crontab -e
and add schedule.Check cron status:
sudo service cron status
Start cron service:
sudo service cron start
Stop a specific job:
crontab -e
, then delete the line.
π User Management : Creating and Managing Users
Hey there! π Ready to learn about User Management? It's like having a bouncer for your computer πΊ, controlling who gets in and who doesn't! Let's dive in:
π What's User Management?
Think of it as the "gatekeeper" π§ of your computer. It handles user accounts πββοΈπββοΈ, deciding who can access what. Super crucial for security! You wouldn't want random strangers peeking at your stuff, right? π
πββοΈ Who's a User?
A user is like a VIP π© in the Linux world! They can do stuff like a magician πͺβmanipulate files, perform tricks (commands), and more! Each user has a special ID π’ that's unique to them. Root user π gets ID 0, and system users get IDs 1 to 999. But for regular local users, IDs start from 1000. Keep it organized and exclusive! πΌ
let's create two users and set passwords for them on Linux:
Open a terminal.
To create the first user, type the following command and press Enter:
sudo adduser user1
set a password for the user1:
Type sudo passwd user1
Then, give a new password and retype the new password.
Then, your password was updated for the user1 successfully
- To create the second user, type the following command and press Enter:
sudo adduser user2
similarly, you can set a password for user2 also.
- To display the usernames of the created users, type the following command and press Enter:
cut -d: -f1 /etc/passwd
You will see a list of usernames displayed on the screen, including "user1" and "user2."