Day 9 : Shell Scripting Challenge Directory Backup with Rotation

Day 9 : Shell Scripting Challenge Directory Backup with Rotation

Β·

4 min read

Directory Backup with Rotation πŸš€πŸ“‚

Hey DevOps Enthusiasts! πŸ‘‹

Welcome to Day 9 of our 90dayofDevOps journey. Today, we’re tackling a shell scripting challenge where we create a script to backup a directory and implement a rotation mechanism to keep our backups organized and clutter-free. Let's dive in!

Challenge Description πŸ“

Our task is to create a bash script that:

  1. Takes a directory path as a command-line argument and performs a backup of the directory.

  2. Creates timestamped backup folders and copies all files from the specified directory into the backup folder.

  3. Implements a rotation mechanism to keep only the last 5 backups, removing the oldest ones if there are more than 5.

Script Breakdown πŸ› οΈ

Below is the complete script followed by a detailed explanation of each function.

#!/bin/bash

<<readme
This is day 9 of 90DaysofDevops
It takes backup of scripts with rotation key
readme

source_directory=$1
target_directory=$2
timestamp=$(date '+%Y-%m-%d-%H:%M:%S')
backup_directory="$target_directory/backup_$timestamp"

function create_backup {
    zip -r "${backup_directory}.zip" "${source_directory}" > /dev/null

    if [ $? -eq 0 ]; then
        echo "Backup done successfully"
    else
        echo "Backup not done for $timestamp"
    fi
}

function perform_rotation {
    backups=($(ls -t "$target_directory/backup_"*.zip))

    if [ "${#backups[@]}" -gt 5 ]; then
        backups_to_remove=("${backups[@]:5}")
        for backup in "${backups_to_remove[@]}"; do
            rm "$backup"
        done
    fi
}

create_backup
perform_rotation

Explanation πŸ“–

Initial Setup

source_directory=$1
target_directory=$2
timestamp=$(date '+%Y-%m-%d-%H:%M:%S')
backup_directory="$target_directory/backup_$timestamp"
  • source_directory: The directory to be backed up, passed as the first argument.

  • target_directory: The directory where the backups will be stored, passed as the second argument.

  • timestamp: A timestamp to uniquely identify each backup.

  • backup_directory: The path for the new backup folder, incorporating the timestamp.

Function: create_backup πŸ“‚

function create_backup {
    zip -r "${backup_directory}.zip" "${source_directory}" > /dev/null

    if [ $? -eq 0 ]; then
        echo "Backup done successfully"
    else
        echo "Backup not done for $timestamp"
    fi
}

  • zip -r "${backup_directory}.zip" "${source_directory}" > /dev/null: Creates a compressed backup of the source directory.

    • -r: Recursively includes files and directories.

    • > /dev/null: Suppresses the output for a cleaner console.

  • if [ $? -eq 0 ]: Checks if the last command (zip) was successful.

    • echo "Backup done successfully": Prints success message.

    • echo "Backup not done for $timestamp": Prints failure message if the backup failed.

OUTPUT

Function: perform_rotation πŸ”„

bashCopy codefunction perform_rotation {
    backups=($(ls -t "$target_directory/backup_"*.zip))

    if [ "${#backups[@]}" -gt 5 ]; then
        backups_to_remove=("${backups[@]:5}")
        for backup in "${backups_to_remove[@]}"; do
            rm "$backup"
        done
    fi
}

  • backups=($(ls -t "$target_directory/backup_"*.zip)): Lists all backup files in the target directory, sorted by modification time (newest first).

  • if [ "${#backups[@]}" -gt 5 ]: Checks if there are more than 5 backups.

    • backups_to_remove=("${backups[@]:5}"): Selects the backups to remove, starting from the 6th backup.

    • for backup in "${backups_to_remove[@]}"; do rm "$backup"; done: Loops through the backups to remove and deletes them.

OUTPUT :

This will create a timestamped backup of your source directory in the target directory and ensure only the last 5 backups are kept.

Setting Up a Cron Job for Automated Backups ⏲️

To automate this backup process, we can set up a cron job that runs the script at regular intervals. Here’s how you can do it:

What is a Cron Job?

A cron job is a time-based job scheduler in Unix-like operating systems. It allows users to schedule scripts or commands to run at specific intervals.

Setting Up the Cron Job

  1. Open the Crontab File: Open the crontab file by running the following command in your terminal:

     crontab -e
    
  2. Add the Cron Job: Add the following line to the crontab file:

     * * * * * /path/to/backup_script.sh /path/to/source_directory /path/to/target_directory
    

    This cron job will run the script every minute.

Cron Job Syntax Explanation

The cron job syntax consists of five fields representing time intervals followed by the command to be executed:

  • * * * * *: Represents every minute. The fields are:

    • Minute (0-59)

    • Hour (0-23)

    • Day of the month (1-31)

    • Month (1-12)

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

By using * in all fields, we specify that the command should run every minute.

Conclusion πŸŽ‰

Today, we learned how to create a bash script for backing up directories with a rotation mechanism to manage backups efficiently. We also set up a cron job to automate the backup process, ensuring regular and timely backups. This is a crucial skill for any DevOps engineer, ensuring that backups are organized and storage is managed effectively.

Keep practicing and experimenting with these concepts to enhance your scripting skills. See you on Day 10 of our DevOps journey! πŸš€

✨Happy scripting! 😊

Β