Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management

Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management

Task1:- Create a backup

#!/bin/bash

function create_backup {
src_dir=/home/linux/scripts/src_dir
tgt_dir=/home/linux/scripts/tgt_dir

current_timestamp=$(date  "+%y-%m-%d-%H-%M-%S")
echo "taking backup for timestamp : " $current_timestamp

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

# echo $final_file

# czf (compress zip file)
tar czf $final_file -C $src_dir .

#echo "backup completed...."
}
echo "starting backup processssssss"
create_backup
echo "backup completeddd............"
~                                       if you want then you can  take backup at regular intervals, crontab is

Then you just need to change file permission 700 and run the script

./script.sh

Task2:- create muiltiple folder depending upon the command line arguments

create file createfolder.sh

#!/bin/bash
export dirs_to_create="$1\{$2..$3\}"
seq -f "$1%1.0f" $2 $3 | xargs mkdir

then change the file permission to 777 and then execute the code

./createfolder.sh folder 1 5

what is cron and crontab? what the purpose of using these in the script?


Cron
is a time-based job scheduler in Unix-like systems. It allows users to schedule jobs (commands or scripts) to run periodically.

Crontab is a file that contains a list of cron jobs. It is a text file that is typically located in the /etc/cron.d/ directory for system-wide jobs or in the .crontab directory in the user's home directory for user-specific jobs.

Purpose of using cron and crontab in scripts:

Cron and crontab can be used to automate a wide variety of tasks, such as:

  • Backing up files and databases

  • Running system maintenance tasks

  • Sending email notifications

  • Generating reports

  • Processing data

  • Deploying software updates

Example of a cron job:

The following cron job will run the script /path/to/script.sh every day at midnight:

0 0 * * * /path/to/script.sh

The fields in the cron job entry are as follows:

  • Minute: 0-59

  • Hour: 0-23

  • Day of month: 1-31

  • Month: 1-12

  • Day of week: 0-7 (0 is Sunday)

  • Command: The command or script to be run

Benefits of using cron and crontab:

  • Automation: Cron and crontab can be used to automate a wide variety of tasks, which can save time and effort.

  • Reliability: Cron and crontab are reliable and will run jobs on schedule, even if the user is not logged in.

  • Scalability: Cron and crontab can be used to schedule jobs on multiple systems, which can be useful for large-scale deployments.

Overall, cron and crontab are powerful tools for automating tasks in Unix-like systems. They can be used to simplify system administration and improve the efficiency of workflows.

You can also visit that website for generating code based on the cron tab :- https://crontab.guru/

Create 2 users and just display their Usernames

sudo adduser heelo1
sudo adduser heelo2
ls /home