Table of contents
π¬Delving into Advance Shell Scripting Essentials!
πTask 1: Creating a Dynamic Directory
Example #1: When executing
./
createDirectories.sh
day 1 90
, the script generates 90 directories labeled fromday1
today90
.
#!/bin/bash
echo "Enter name of the root directory"
read root_dir
mkdir $root_dir && cd $root_dir
for (( i=$2; i<=$3; i++ ))
do
mkdir "$1_${i}"
done
Example #2: When executing
./
createDirectories.sh
Movie 20 50
, the script generates 50 directories named fromMovie20
toMovie50
.
#!/bin/bash
echo "Enter name of the root directory"
read root_dir
mkdir $root_dir && cd $root_dir
for (( i=$2; i<=$3; i++ ))
do
mkdir "$1_${i}"
done
πΎTask 2: Backup Scripting Works
Performing backups is an essential part of a DevOps Engineer's daily tasks. Let's take a look at how it's done.
#!/bin/bash
# Source directory where we want to take the backup
src_dir="/home/ubuntu/Day5"
# Target directory where we want to save the backup
tgt_dir="/home/ubuntu/Day5/Backups"
# Name of the backup file
backup_filename="backup_$(date +%Y-%m-%d_%H-%M-%S).tar.gz"
echo "backup started"
echo "Backing up to $bakup_filename.."
# Command to take backup
tar -czvf "${tgt_dir}/${backup_filename}" "$src_dir"
echo "Backup Completed"
Note:
When using variables, ensure to wrap them in double quotes(" ") to prevent word splitting and pathname expansion.
The tar is a powerful tool that used to compress and decompress files, as well as to archive (collect) and backup the data.
There are different flags that can be used with the tar command. Some of the most common flags:
-c
: Create a new archive file.
-x
: Extract an archive file.
-v
: Display verbose output.
-f
: Specify the name of the archive file.
-z
: Compress the archive file using gzip(gun zip).
πTask 3: Automating Backups with Cron and Crontab
Cron: Cron is a time scheduler(tool) that arranges and manages the timing and execution of tasks or events.
Crontab: Crontab/Cron table is a user file that contains the scheduling information. minute (m), hour (h), day of month (dom), month (mon), and day of week (dow) or use '*' in these fields (for 'any').
Steps to do automation backup using corn and crontab:
Use of command
crontab -e
:The flag
-e
option is used to edit the current crontab using the editor.Selection of Crontab editor:
Scheduling a cronjob backup execution time:
Source: https://tecadmin.net
-> Cron expression syntax:
For example, run a cron backup of a file (backup.sh) every 2 minutes.
Note:
minute (m), hour (h), day of month (dom), month (mon), and day of week (dow) or use '*' in these fields (for 'any').
Take a reference of Cron Guru for cron scheduling expressions.
π User Management Fundamentals :
User management is the process of creating, modifying, and deleting user accounts as well as managing permissions, and controlling access to system resources.
There are different types of user accounts in Linux, such as root, standard, sudo, system, and guest users. Each user account has a unique user ID, a password, a group ID, and a home directory.
π₯ Some Basic Commands of User Management -
π¨ User Creation:
sudo useradd -m <-name of user- ex: Sushil->
Note:
-m
is used to create home user directories, and
sudo
is used as a root/administrator to give grant permission for creating a new user.
π₯οΈ Display User:
whoami
OR
getent passwd | cut -d: -f1
OR
finger -l | grep Login | cut -d" " -f4
whoami
getent passwd | cut -d: -f1
ποΈ Set Password to user:
sudo passwd <-name of user- ex: Sushil->
ποΈ Delete User:
sudo userdel -r <-name of user- ex: Pawan->
Note:
-r
is to remove home directory of user and mail spool.
Happy Coding :) ππ₯