Day 5: Advance Shell Scripting with User Management

Day 5: Advance Shell Scripting with User Management

In this blog, we are learning about how to create backups by writing shell scripts, what is cron and crontab, and at last what is user management and how to create users.

  • Create a script to back up all your work done till now.
#!/bin/bash

date_timestamp=$(date "+%Y-%m-%d-%H-%M-%S")
work_dir="/home/ubuntu/scripts"
tgt_dir="/home/ubuntu/backups"

backup_file=$tgt_dir/$date_timestamp.tgz

tar -cvzf $backup_file --absolute-names $work_dir

echo "Backup is taken on $date_timestamp"
echo "Backup is completed successfully"

So, let's break this script for better understanding:

In the first line, the date_timestamp variable is used to assign the date and time that is used in the echo command. "+%Y-%m-%d-%H-%M-%S", is the format of the timestamp.

In the second line, work_dir is the path of the folder which you want to backup.

In the third line, tgt_dir (target dir) is the path where we want to place the backup folder.

backup_file=$tgt_dir/$date_timestamp.tgz

In this line backup_file variable, I am giving the filename for backup with the .tgz (tar gunzip) extension.

In the fifth line, the tar command will make the zip file that will be created in the backup folder.

In the last two lines, the echo will print the statement.

What is Cron,Crontab ?

Cron is a software utility program that automates the scheduled tasks in a given specified time. It is a daemon process that runs in the background and performs the specified operations in a predefined time.

Crontab stands for cron table which is a list of commands that you want to schedule the tasks. It uses the job scheduler cron to execute the tasks.

It is configured in /etc/crontab.

Format of crontab :

M H DOM M DOW Command

M - Minute

H - Hour

DOM - Date of Month

M - Moth

DOW - Day of Week

Commands to list, edit, and remove the crontab :

crontab -l

This will list all the active crontabs on your machine.

crontab -e

After executing this command, it will ask in which editor you want to write (nano, vim, vi). This command is used to edit the crontab and write scheduled tasks.

crontab -r

This command will remove the job from the crontab.

crontab -u username -e

This command will edit the crontab from the other username.

User Management in Linux:

In Linux, a user can create, modify, and delete any files and can perform several other operations.

ID 0 is assigned to the Root user and from ID 1 to 999 are for system users and after that from 1000 id is assigned to local users. Each user has his own unique id that is assigned by the system.

The root is superuser that can access, and delete any files of any user.

  • To add a user
sudo useradd Amit

sudo is the root, useradd is the command to add user "Amit"

  • To get user id
id Amit

You will get three id's (uid) is the user id and (gid) is the group id and the last one is the groups id and all have same id number.

  • To change the password of user.
sudo passwd Amit

passwd is the command to change the password of Amit user.

  • To get list of all the users.
awk -F':' '{print $1}' /etc/passwd

awk command is used to extract the data from the file. Here by using awk command, -F is a file that is separated by : and prints the first column of the /etc/passwd file. (/etc is configuration files)

  • To delete users
userdel Amit

Now, let's understand with example :

Create 2 users and just display their Usernames

useradd AmitMaurya
useradd Himanshu

These two commands will create the user Amit and Himanshu and to see whether these usernames are created or not you can execute them.

id Amit
id Himanshu

Or, you can see by awk command.

awk -F ':' '{print $1}' /etc/passwd

At last, you can the name of users that you had created.

So, That's it for the Day 5 Task. I hope you all have learned something and I am loving this #90DaysofDevOps challenge more day by day as I am learning also some new commands and topics.

Follow for more amazing Linux and DevOps upcoming blogs.

Connect me on Twitter too !!

THANK YOU :)