Day 5 Advanced Linux Shell Scripting for DevOps Engineers with User management
Harsh Rajotya is a technical blogger and a cloud DevOps engineer with a BE in Electronics and Computers from M.B.M. Engineering College, Jodhpur. He holds multiple certifications in AWS, Python, and RHCSA8, and is proficient in various cloud computing, infrastructure provisioning, containerization, CI/CD, and configuration management technologies and tools.
As a DevOps engineer at DevOpsFarm Inc, he deploys and manages end-to-end applications using Jenkins, Git, Terraform, Docker, and Kubernetes on AWS. He has experience in working with several AWS services, such as EC2, EBS, S3, VPC, autoscaling, and load balancing, and implements security policies using IAM, inline policies, and bucket policies. He also handles scheduled Cron jobs for snapshots and backups and is familiar with Ansible and Chef. He is passionate about learning new technologies and sharing his knowledge through technical blogging on Medium, where he has worked on projects involving deploying full-stack, Node.js, and MongoDB applications.
Write a bash script create directories.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.
#!/bin/bash
#############################################################
#Purpose: This script will create 90 directories at once
#############################################################
#Intializing variables
Name_of_dir=$1
start_no=$2
end_no=$3
#Command to create directories
eval mkdir $Name_of_dir{$start_no..$end_no}
Here, the “eval” command takes arguments as input to the command and stores it as one single command.
Execution,
./directories.sh day 1 90
Here,
day, — First argument
1, — Second argument
2, — Third Argument
create a Script to backup all your work done till now
##########################################################################
#Purpose: This script will take backup
##########################################################################
#Creating Variables
src=/home/ubuntu/day5
dest=/home/ubuntu/backups
time=$(date +"%Y-%m-%d-%H-%M-%S")
backupfile=$dest/$time.tgz
#Taking Backup
echo "Taking backup on $time"
tar zcvf $backupfile --absolute-names $src
if [ ${?} -eq 0 ]
then
echo "Backup Complete"
else
exit 1
fi
Above, the script will take backups.
Read About Cron and Crontab, to automate the backup Script
crontab -e
Enter the below code in the last line of crontab,
* * * * * sh /home/ubuntu/day5/backup.sh
first *, — represents minutes
second *, — represents hours
third *, — represents day of month
fourth *, — represents month
fith *, — day of week
Read about User Management
User management is a very important part of a DevOps engineer’s journey.
User management includes some basic tasks, like
Creating Users
Deleting Users
Password reset
Adding user in a group
Create 2 users and just display their Usernames
##########################################################################
#Purpose: This script will take two argument as username and create user
##########################################################################
#Creating variables
user1=$1
user2=$2
#Creating Users
echo "Adding Users $user1 and $user2"
useradd -p test -m $user1
useradd -p test -m $user2
if [ ${?} -eq 0 ]
then
echo "Users added successfully"
else
exit 1
fi




