Advanced Shell Scripting in Linux

Advanced Shell Scripting in Linux

Day 05 Task

Task1.Create Directories Using a Bash Script

In this task, we are required to create a bash script that creates directories with dynamic names based on the input arguments. Here is the script:

To use this script, save it in a file named createDirectories.sh, and give it execute permissions using the chmod +x createDirectories.sh command. Then, run the script with the desired input arguments:

Task2.Create a Script to backup all your work done till now.

In this task, we are required to create a script to back up all the work done so far. Backups are essential to ensure that your work is safe and can be recovered in case of any failure. Here is a simple backup script:

create a backup directory -

mkdir backups

now cd.. then create file backup.sh

given permission chmod 700 backup.sh

output-

and as we check backups on backups folder tar file are create

Task3 Read About Cron and Crontab, to automate the backup Script

okay so firstly we are discussing cron and crontab-

Cron is a system daemon run on any Linux system that is responsible for detecting cron jobs and executing them at given intervals. Cron runs every minute and it will inspect a set of pre-defined directories on your filesystem to see if jobs need to be run.

On the other hand, cron jobs are tasks defined to run at given intervals or periods, usually shell scripts or simple bash commands. Cron jobs are usually used in order to log certain events to your Syslog utilities or to schedule backup operations on your host (like database backups or filesystem backups).

Crontab Syntax and Operators

Each line in the user crontab file contains six fields separated by a space followed by the command to be run.

To automate the backup script using cron, you can follow these steps:

  1. Open your crontab file using the following command:

    This will open the crontab file in the default text editor for your system.

  2. Add a new line at the end of the file with the following format:

    This line schedules the backup script to run every minute. You can customize the schedule by modifying the five fields in the line, which represent the minute, hour, day of the month, month, and day of the week, respectively. For example, to run the script every day at 2:00 AM, you can use the following line:

    Save and exit the crontab file.

    After adding the line to your crontab file, cron will automatically run the backup script at the scheduled times. You can check the system logs to see if the script is running successfully and the backups are being created.

    Note that the backup script should be executable and have the correct path specified in the crontab file. Also, it's important to test the backup script and the cron job to ensure that they are working correctly and the backups are being created as expected.