Day 9 Task: Shell Scripting Challenge - Directory Backup with Rotation

Day 9 Task: Shell Scripting Challenge - Directory Backup with Rotation

Welcome to Day 9 of my 90-day DevOps challenge! Today, we're diving into a practical and essential task for any DevOps engineer: automating directory backups with a rotation mechanism using a bash script. This script will not only help you keep your data safe but also manage disk space effectively by retaining only the most recent backups.

Challenge Description

The goal is to create a bash script that performs the following tasks:

  1. Accepts a directory path as a command-line argument.

  2. Creates a timestamped backup of the directory.

  3. Implements a rotation mechanism to keep only the last three backups, deleting the oldest ones if necessary.

Real-Life Scenario

Imagine you have an important project directory at /home/user/documents that you want to back up regularly. Manually creating backups can be time-consuming and error-prone. This script automates the process, ensuring you always have the most recent backups available without cluttering your storage with outdated files.

Script Breakdown

Here’s the bash script to accomplish this:

#!/bin/bash

# Check if directory path is provided
if [ -z "$1" ]; then
    echo "Usage: $0 <directory_path>"
    exit 1
fi

DIR_PATH=$1

# Check if the directory exists
if [ ! -d "$DIR_PATH" ]; then
    echo "Error: Directory $DIR_PATH does not exist."
    exit 1
fi

# Create a timestamped backup folder name
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_DIR="${DIR_PATH}/backup_${TIMESTAMP}"

# Create the backup directory
mkdir -p "$BACKUP_DIR"

# Copy all files from the specified directory to the backup directory
cp -r "$DIR_PATH"/* "$BACKUP_DIR"

echo "Backup created: $BACKUP_DIR"

# Find and remove older backups, keeping only the last 3
BACKUPS=($(ls -dt ${DIR_PATH}/backup_*))
COUNT=${#BACKUPS[@]}

if [ $COUNT -gt 3 ]; then
    for (( i=3; i<$COUNT; i++ )); do
        rm -rf "${BACKUPS[$i]}"
        echo "Removed old backup: ${BACKUPS[$i]}"
    done
fi

How It Works

  1. Input Validation: The script checks if a directory path is provided and if the directory exists.

  2. Timestamp Creation: A timestamp is generated for the backup folder name, ensuring each backup is uniquely identifiable.

  3. Backup Creation: A new backup directory is created, and all files from the specified directory are copied into it.

  4. Backup Rotation: The script lists all backup directories, sorts them by date, and removes the oldest ones if there are more than three.

Usage Example

Here’s how you can use this script in a real scenario:

  1. First Execution (2023-07-30):

     $ ./backup_with_rotation.sh /home/user/documents
    

    Output:

     Backup created: /home/user/documents/backup_2023-07-30_12-30-45
    
  2. Subsequent Executions:

    • The script creates new backups with different timestamps.

    • After creating the fourth backup, it will automatically delete the oldest backup, ensuring only the last three are retained.

Benefits

  • Automation: Saves time and reduces the risk of human error in manual backups.

  • Efficient Storage Management: Keeps only the latest backups, preventing unnecessary use of disk space.

  • Easy Restoration: Allows quick recovery of recent data if needed.

Conclusion

Automating backups with a rotation mechanism is a crucial skill for any DevOps professional. This script not only simplifies the backup process but also ensures efficient use of storage resources. By mastering such tasks, you enhance your ability to manage and protect important data in real-world scenarios.

Learn More

Check out this YouTube tutorial for a deeper dive into shell scripting:

Shell Scripting Tutorial for Beginner

Connect and Follow Me on Socials:

LINKDIN | GITHUB |TWITTER