Day -> 5 of #90DaysOfDevOps

Advanced Linux Shell Scripting for DevOps Engineers with User Management

Create Directories using Shell Script

Create a createDirectories.sh file using nano text editor

nano createDirectories.sh

Write the script inside the createDirectories.sh

for ((i=1; i<=90; i++ ));

do

dirname="day$i"

mkdir "$dirname"

echo "created directory : $dirname"

done

If you are using nano, save the file by pressing Ctrl + X, then Y, and Enter.

Executing createDirectories.sh with ./createDirectories.sh on the terminal, it created day1 to day90 directories.sh.


Create a Script to Backup All Your Work

Backups are an important part of a DevOps Engineer's day-to-day activities...

Create a backup shell file the same way mention above

Add script into the file carefully

backup_dir="/path/to/backup" -> holding the path to the backup directory.

source_dir="/path/to/source" -> holds the path to the source directory to be backed up.

tar -czf "$backup_dir/backup_$(date +%Y%m%d_%H%M%S).tar.gz" "$source_dir" -> Creates a compressed archive tar.gz with a timestamp into the backup directory $backup_dir, that contains the data of $source_dir .

echo "Backup successfully done" -> prints the message.


Cron and Crontab to Automate the Backup Script

Cron ->

Cron is a time-based job scheduling program in Linux. It allows us to schedule and automate the repetitive tasks, for example running a backup script, at specific intervals.

Crontab ->

Crontab is the configuration file used to define the tasks scheduled by Cron.

Explanation of Cron Syntax

* * * * * /path/to/backup.sh /path/to/source_directory /path/to/destination_directory Above script specifies the time and frequency for the cron job.

  • * - Minute (0–59)

  • * - Hour (0–23)

  • * - Day of the month (1–31)

  • * - Month (1–12)

  • * -Day of the week (0–7) (both 0 and 7 represent Sunday)

Example ->

Explanation for every hour->

0 * * * * * /path/to/backup.sh/path/to/source_directory /path/to/destination_directory

0 - At minute 0

* - Every hour

* - Every day of month

* - Every month

* - Every day of the week


Explanation for Sunday 2 AM->

0 2 * * 0 /path/to/backup.sh /path/to/source_directory /path/to/destination_directory

0 - At minute 0

2 - At 2 AM

* - Every day of month

* - Every month

0 - Sunday


Read About User Management

In a Linux operating system, users are entities that can manipulate files and perform various operations. Each user is identified by a unique user ID (UID).

Types of Users

  1. Root User

    • The root user has a User ID of 0.

    • This user has full administrative access to the system and can perform any operation.

  2. System User

    • These users have User ID's that range from 1 to 999.

    • System users are used by system services and daemons (background processes) to function correctly.

  3. Local User

    • Local users have User ID's that starts from 1000 onwards.

    • These are the users created by the system administrator for normal operations.

User Management in Linux ->

Add new user -> sudo useradd -m newUser

Set a password -> sudo passwd newUser

View user information -> id newUser

List all users -> cat /etc/passwd

Modify a user -> sudo usermod -u new_uid username

Delete a user -> sudo userdel -r username


#90DaysOfDevOps