Advanced Linux Shell Scripting: Automating Tasks with the Power of Shell Scripts

Automate tasks with BASH Scripts:

In this short blog, we'll explore how to generate multiple directories, create backups, schedule tasks using cron and crontab, and even add users – all through the magic of shell scripting!

  • Generate multiple Directories

    • Write a bash script that will generate multiple directories by giving a number as an argument.

script : vi directories.sh

    #!/bin/bash
    directory_name=$1
    number=$2

    for ((i=1; i<=number; i++))
    do
      directory="${directory_name}${i}"
      mkdir "$directory"
    done

make it executable

    chmod 777 directories.sh

Now run the script by giving arguments

    ./directories.sh Day 5

Output: It will create 5 directories- Day1 Day2 Day3 Day4 Day5

Backup the work done

Create a script that will back up all your work.

create a file take_backup.sh

#!/bin/bash

src=/home/ubuntu/scripts
tgt=/home/ubuntu/backups

filename= $(date "+%d-%m-%Y-%H-%M-%S).tar.gz

echo "Backup started for $filename"

tar -cvf $tgt/$filename $src

echo "Backup completed"

Give execute permissions to the file-

chmod 777 take_backup.sh

Execute the file-

./take_backup.sh

Output-

CRON & CRONTAB to automate the backup script:

Cron and Crontab are essential tools in Linux that allow users to schedule and automate tasks, including backup scripts, at specified intervals.

  1. Cron: Cron is a time-based job scheduler in Unix-like operating systems, including Linux. It runs in the background and executes commands or scripts at predetermined times or intervals. Cron is responsible for managing and triggering these scheduled tasks without any user intervention.

  2. Crontab: Crontab is a configuration file used to define the schedule of tasks for Cron. Each user on a Linux system can have their crontab file, allowing individual users to schedule tasks according to their needs. The crontab file contains lines specifying the timing and the commands or scripts to be executed.

To automate a backup script using Cron and Crontab, follow these steps:

  1. Create the Backup Script: First, create a shell script that performs the backup operations you need. For example, the script might use rsync or tar commands to copy or compress important files and directories.

  2. Edit the Crontab: Use the crontab -e command to open the crontab file for editing. This will open the file in the default text editor specified in your system's environment.

  3. Add the Cron Schedule: In the crontab file, add a new line specifying the schedule for the backup script and the path to the script. The syntax for the cron schedule is as follows:

     m h dom mon dow command
    

    Where:

    • m: Minute (0-59)

    • h: Hour (0-23)

    • dom: Day of the month (1-31)

    • mon: Month (1-12)

    • dow: Day of the week (0-7, where both 0 and 7 represent Sunday)

    • command: The command or script to be executed.

      For example, to run the backup script every day at 5:00 AM, you would add the following line to the crontab file:

        0 5 * * * /path/to/backup_script.sh
      
  4. Save and Exit: Save the changes to the crontab file and exit the text editor.

Cron will now automatically execute your backup script at the specified time and frequency, ensuring that your important data is regularly backed up without requiring manual intervention.

  • ADD USER

    • add a user by using a shell script

      Create a file add-user.sh

        #!/bin/bash
      
        add-user()
        {
            user=$1
            pass=$2
            useradd -m -p $pass $user && echo "User added successfully"
        }
      
        #MAIN
        add-user Ayushi test123
      
    • Root privileges are required to add a user.

        sudo./add-user.sh
      

    • Output

        cat /etc/passwd
      

      In conclusion, shell scripting proves to be a powerful tool for automating tasks and boosting productivity in the Linux environment. Throughout this blog, we have explored the versatility of shell scripts by accomplishing various automation feats.

      Stay tuned for more exciting blogs and tips to expand your shell scripting prowess.

      Happy scripting🚀🐚💻