Day 5 : Advanced Linux Shell Scripting for DevOps Engineers

ยท

3 min read

Create a Bash Script to Create Directories:

Write a script createDirectories.sh that takes three arguments: a directory name prefix, a start number, and an end number. The script will create directories with names based on these inputs.

Example 1:

./createDirectories.sh day 1 90

This command creates 90 directories: day1, day2, ..., day90.

Example 2:

./createDirectories.sh Movie 20 50

This command creates 31 directories: Movie20, Movie21, ..., Movie50.

Automating Backups with Shell Scripting

Backups are crucial for ensuring data integrity and availability in any DevOps environment. Today, I created a shell script to automate the backup of all my work done so far.

#!/bin/bash

#Define source and destination
source="/home/ubuntu"
destination="/home/ubuntu/backup_dir"
time_stamp=$(date +"%Y%m%d%H%M%S")
backup_name="backup_$time_stamp.tar.gz"

#create the backup
echo "creating backup of $source to $destination/$backup_name"
tar czf $destination/$backup_name  $source

sleep 2

echo "Backup Completed"

Why Automate Backups?

  • Reliability: Ensures regular and consistent backups.

  • Efficiency: Saves time and reduces manual effort.

  • Data Integrity: Protects against data loss.

What is Cron?

Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule jobs (commands or scripts) to run automatically at specified intervals. This could be anything from running a backup script every night at 2 AM to sending out reminder emails every Monday morning or cleaning up log files every hour.

Cron starts when the system boots up and runs in the background, checking the Crontab files to see if there are any jobs to execute.

What is Crontab?

Crontab (Cron table) is a configuration file that specifies shell commands to run periodically on a given schedule. Each user on a system can have their own Crontab file, allowing for personalized automation.

The Crontab file consists of lines where each line represents a job, with the time and date fields followed by the command to be executed.

Crontab Syntax

Crontab format:

* * * * * command_to_execute
- - - - -
| | | | |
| | | | ----- Day of the week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of the month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Example

To schedule a backup script to run daily at 2:30 AM, you would use:

30 2 * * * /path/to/backup.sh

Editing Crontab

To edit your Crontab file, use the crontab -e command. This opens your Crontab file in the default text editor, allowing you to add, modify, or remove jobs.

codecrontab -e

To view your current Crontab entries, use:

crontab -l

To remove all Crontab entries for the current user, use:

crontab -r

Automating the Backup Script with Crontab

Make the backup script executable:

#/path/to/backup
chmod +x  "/home/ubuntu/backup_dir"

Schedule the Script with Crontab

  1. Open Crontab for editing:

     crontab -e
    
  2. Add a new cron job to run the script daily at 2 AM:

     0 2 * * * bash /home/ubuntu/backup_dir
    
  3. Save and exit the editor.

Your backup script will now run automatically every day at 2 AM.

ย