Advanced Linux Shell Scripting for DevOps Engineers with User management πŸ§πŸš€

Advanced Linux Shell Scripting for DevOps Engineers with User management πŸ§πŸš€

πŸ”„Create a Script to Backup

Create a script named backupData.sh to backup:

#!/bin/bash

function create_backup {
        src_dir=/home/ubuntu/scripts
        tgt_dir=/home/ubuntu/backups

        current_timestamp=$(date "+%Y-%m-%d-%H-%M-%S")

        final_file=$tgt_dir/scripts-backup-$current_timestamp.tgz

        tar czf $final_file -C $src_dir .

}

echo "starting process"
create_backup
echo "BackUp Completed....."

Make the Script Executable by changing File Permission:

chmod [Permission] backupData.sh

Run the script to create a backup:

./backupData.sh

What is Cron?

Cron is a job scheduler that allows users to Schedule tasks.

Cron Syntax:

minute hour day month day_of_week command_to_run
  • minute: The minute of the hour (0 - 59).

  • hour: The hour of the day (0 - 23).

  • day: The day of the month (1 - 31).

  • month: The month (1 - 12).

  • day_of_week: The day of the week (0 - 6, where Sunday is 0 or 7).

  • command_to_run: The command or script to be executed.

What is Crontab?

Crontab is a file that contains a list of cron jobs.

πŸ•° Automate Backup Script Using Cron

open the crontab file:

crontab -e

Example to run the cron job every 6 hours:

0 */6 * * * /path/to/backupData.sh

πŸ‘₯User Management in Linux

User management is a crucial aspect of Linux operating systems, allowing administrators to control access, permissions, and resources for different individuals or processes. In Linux, each user is identified by a unique user ID (UID). The root user, with UID 0, has superuser privileges and can perform administrative tasks, making it a powerful but potentially risky account.

Create 2 users and just display their Usernames πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»:

sudo user add -m UserName1 will create new users

# Create User1
sudo useradd -m UserName1

sudo user add -m UserName2 will create new users

# Create User2
sudo useradd -m UserName2

echo "User1 username: $(id -un User1)" and echo "UserName2 username: $(id -un UserName2)" command will display the Usernames of UserName1 and UserName2

# Display usernames
echo "UserName1 username: $(id -un UserName1)"
echo "UserName2 username: $(id -un UserName2)"
Β