🌱Day 5: Advanced Linux Shell Scripting for DevOps with User Management.

Β·

4 min read

🌱Day 5: Advanced Linux Shell Scripting for DevOps with User Management.

🎬Delving into Advance Shell Scripting Essentials!


πŸ“‚Task 1: Creating a Dynamic Directory

Example #1: When executing ./createDirectories.shday 1 90, the script generates 90 directories labeled from day1 to day90.

#!/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.shMovie 20 50, the script generates 50 directories named from Movie20 to Movie50.

#!/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:

  1. Use of command crontab -e :

    The flag -e option is used to edit the current crontab using the editor.

  2. Selection of Crontab editor:

  3. Scheduling a cronjob backup execution time:

Master the Cron Scheduling Syntax | Blog

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 :) 🌟πŸ”₯

Β