Let's continue our blog from where we left off, Shell Scripting. We shall discuss how shell scripts are used regularly in a DevOps engineer's life. To get some context, the previous blog's link is here: Easy Shell Scripting | Linux.
Script for creating multiple directories using for loop
The Problem Statement: So Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is the directory name and second is the start number of directories and third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.
My Approach:
The script is as simple as given in the image. To make things easier, let's break down the script.
#!/bin/bash
It's the shebang.
dir_name=$1
first_dirnum=$2
last_dirnum=$3
We are assigning the variable names with the argument numbers, so we can act dynamically.
if [ $# -ne 3 ]; then
echo " More Arguments! Enter only directory name and the range as 3 arguments!"
exit 1
fi
We are using the 'if' condition first to verify, if the total number of arguments passed while executing the script is equal to the ones specified in the script, in this case, it's 3. If the condition is met, the script goes to the next step, the 'For' loop else it prints the message in the script. The prompt might look like this:
for ((i=$first_dirnum ; i<=$last_dirnum ; i++))
do
mkdir -p "$1${i}"
done
In the next part, we are doing this activity using for loop. The first line is the syntax of for loop. Until the range is not completed, the loop is executed. "mkdir -p" is used to create multiple directories.
echo "Directories are created!"
Voila, and we are done with this shell script!
Output:
To execute the script, use the following command:
./createdirectoy1.sh day 1 90
Heads Up: If you face an issue while executing your shell script saying "Permission Denied", make sure you give adequate permissions using the command:
chmod +x filename.sh
Script for Backup of all the work done till now
The script looks like this:
Let's break down this script for easy understanding.
#!/bin/bash
It's the shebang
filename="backup_`date +%d`_`date +%m`_`date +%Y`.tar";
The date is the bash keyword that stores parameters like day, date, month, time, and timezone.
date +%d
defines the current date.date +%m
defines the current month.date +%Y
defines the current year. And all of that is compressed to a tar file. This approach of taking backups eases the job of an engineer and accessibility also increases as the backups are taken and named according to their date of creation.
tar cvf /home/ec2-user/backup/$filename /home/ec2-user
This command helps in taking the backup of the specified folder and storing it in the specified folder.
cd /backup
ls
du -sh
These commands help in moving to the backup folder, listing the contents of that folder and the disk usage also.,
This same job can be done as a cron job. We will discuss the cron and crontab in the next section.
The commands for backup through crontab will be:
Become a root user and then execute:
crontab -e
* * * * * ./backup.sh
Let's discuss the above commands in detail.
Cron and Crontab
Cron is a service/utility that allows the tasks to be scheduled at the intervals required. Crontab is a file that has a set of commands that needs to be executed at particular intervals. The main usage of Crontab is it helps in performing the tasks that are repetitive and needs to be performed at regular intervals.
/etc/crontab is the location of the crontab.
Syntax of cron:
* * * * * user-name command
The five asterisks in the order specified are Minutes (0-59), Hours (0-23), Day of the Month, Month, and Day of Week.
crontab -l
Lists all the crontab.
crontab -e
Open the crontab editor.
Some of the crontab's applications are to check disk space and take regular backups.
User Management
Let's discuss briefly User management in this blog. A user is an entity in a Linux OS, that can perform several operations like file editing, writing etc. Each user is assigned an ID that is unique for each user in the operating system. After installation of the operating system, the ID 0 is assigned to the root user and the IDs 1 to 999 (both inclusive) are assigned to the system users hence the ids for local user begins from 1000 onwards.
To create a user:
sudo adduser user1
sudo adduser user2
To display just the usernames and not the password we can use either of the commands below:
grep home /etc/passwd |grep bash
Using two calls to grep, the output received will be containing both from the home directory and bash as the default shell.
cat /etc/passwd| grep home |cut -d: -f1
This is the most used command where the cut keyword helps in exporting the username.
Well, this was for this blog. Haven't been active for two weeks, but hopefully, will stay consistent. In the next blog, I am going to discuss File Permissions and ACL in detail.
To help me improve my blog and correct my mistakes, I am available on LinkedIn as Sneha K S. Do reach me and I am open to suggestions and corrections.
#Day5 #90DaysofDevops