5.Advanced Linux Shell Scripting for DevOps Engineers with User management (Day 5 )

5.Advanced Linux Shell Scripting for DevOps Engineers with User management (Day 5 )

All 90 directories within seconds using a simple command.

mkdir day{1..90}

1.You have to do the same using Shell Script i.e using either Loops or command with start day and end day variables using arguments -

So Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is directory name and second is start number of directories and third is the end number of directories ) it creates specified number of directories with a dynamic directory name.

  #!/bin/bash
 dirName=$1
 startNum=$2
 endNum=$3

 for (( i =startNum; i <= endNum; i++))
 do
         mkdir "$dirName$i"
         echo "$dirName$i"
 done

wq! To save the file and provide chmod 700 permission to file , Execute the ./createDirectories.sh

Here $1 is directory_name(d)
$2 is start_directory(1)
$3 is end_directory(90)
After executing this code, we get the output as...

2.Create a Script to backup all your work done till now.

step 1:

create a backup script using the tar command in a Bash script

#!/bin/bash

# Set the source and destination directories
source_dir="/home/mbubur"
backup_dir="/home/mbubur/backup"

# Create a timestamp to use for the backup file name
timestamp=$(date +%Y-%m-%d_%H-%M-%S)

# Set the backup file name
backup_file="backup_${timestamp}.tar.gz"

# Use the tar command to create a compressed backup archive
tar -czvf "${backup_dir}/${backup_file}" "${source_dir}"

# Print a message indicating that the backup has been completed
echo "Backup completed successfully"

In this script, we set the source directory source_dir and the backup directory backup_dir. We then create a timestamp using the date command, which is stored in the timestamp variable.

We then set the backup file name using the backup_file variable. The .tar.gz extension indicates that the archive should be compressed using gzip compression.

output:

3.Read About Cron and Crontab, to automate the backup Script

Job Automation allows us to perform tasks automatically in os by using tools.

This feature is very useful for administration to assign tasks to os whenever he is not present or performs daily basis work.

Two types of job automation

  1. at - at command is used to execute job only one time.

  2. crontab- Crontab command is used to execute the job multiple time.

For set job with "at" command

$at 10:10 AM
at>useradd mbubur
# Ctrl+d (write & quit)

For displaying the pending job

atq

For remove at job

atrm 2

For restricting user from accessing at

vim /etc/at.deny
mbubux #(add user name)
:wq # ( write & quit)

For start crond service

systemctl start crond

For enable crondo service (Permanent on)

systemctl enable crond

For set cron job

crontab -e

For show cron jobs of current user

crontab -l

For remove cron jobs

crontab -r
# or 
# Go to the crontab file and remove job line
crontab -e

For set cron job to other user

crontab -u mbubur -e

For show cron job other user

crontab -u mbubur -l

For restrict user from cron service

vim /etc/cron.deny

For check crontab log file

tail -f /var/log/cron

Steps for run the crontab

step1: type

crontab -l 
# to list the existing cronjobs

step2: type

crontab -e 
#select an editor and write your desired scheduling task
* * * * * /home/mbubur/backup_script.sh > /home/mbubur/backup.log
# add the script below the crontab

step3:

sudo service cron status 
#to check the cronjob status

4.Read about User Management and Let me know on Linkedin if you're ready for Day 6.

Create a user and an account

adduser mbubur

Create a Password for user mbubur

passwd mbubur

Adding new user ( mbubur) to the sudo group

usermod -aG sudo mbubur

Switch User from root to mbubur user

su mbubur

Logout from the current user or press "ctrl + d"

exit

Delete User Account

userdel mbubur

Change user login name

usermod -l mbubur devops

Group Management

A group is a collection of users that makes it simple to manage permissions for identical types of work that are allocated to the same group. A group is a collection of users that makes it simple to manage permissions for identical types of work that are assigned to the same group.

Add Group Account

groupadd devops

Check the group account properties

grep devops /etc/group

Checking the group Admin properties

grep devops /etc/gshadow

Add the mbubur to the devops group

gpasswd -a mbubur devops

Add the multiple member to the devops group

gpasswd -M user1 user2 user3

Remove user from the group

gpasswd -d user1 devops

Make user2 as group admin

gpasswd -A user2 devops

For Delete Group

groupdel devops

5.Create 2 users and just display their Usernames

Added two users by using the useradd command :

sudo useradd mbubur
sudo useradd mbubur2

To display their name use cat /etc/passwd and redirected the output to the tail command to see only the last two lines from the file :