Linux Day 9

Linux Day 9

Shell Scripting Challenge: Directory Backup with Rotation

Introduction

Shell scripting is a powerful way to automate tasks in Unix-like environments, and one common use case is creating backups. In this challenge, we’ll dive into creating a backup with rotation feature using a Bash script. This script will not only create timestamped backups but also manage them by keeping only the last three backups. This task is perfect for automating directory backups and preventing excessive storage usage.


Challenge Overview

In this task, you’ll build a bash script that takes a directory path as a command-line argument and performs two key operations:

  1. Backup Creation: Copies the contents of the specified directory into a new backup folder with a timestamp.

  2. Backup Rotation: Automatically deletes older backups, ensuring only the three most recent backups are kept.

This type of automation is useful for system administrators, developers, or anyone who needs to periodically back up a directory without having to manually manage old backups.


The Script: How it Works

The script will:

  • Create a timestamped backup folder inside the specified directory.

  • Copy all files from the target directory to the newly created backup folder.

  • Check for existing backup folders and remove the oldest ones, keeping only the last three backups.


Steps to Write the Script

Here’s the breakdown of the script logic:

  1. Get the current timestamp:

    • Use the date command to generate a timestamp that includes the date and time.
  2. Create a new backup directory:

    • Use mkdir to create a directory named with the timestamp (e.g., backup_2023-08-01_09-15-30) within the specified directory.
  3. Copy the files:

    • Use cp to copy all files from the specified directory to the newly created backup folder.
  4. Check for existing backups:

    • Use ls -d to list all backup folders in the directory, and sort them by name (since the timestamps are included in the name, sorting by name will also sort by date).
  5. Delete the oldest backups:

    • Use rm -rf to remove the oldest backups if there are more than three existing backups.

The Code

Here’s the bash script that implements the above logic:

#!/bin/bash

# Check if the directory is passed as an argument
if [ -z "$1" ]; then
  echo "Usage: $0 <directory_path>"
  exit 1
fi

# Assign the first argument as the directory path
DIR="$1"

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

# Create a timestamp for the backup folder
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")

# Create a new backup directory with the timestamp
BACKUP_DIR="$DIR/backup_$TIMESTAMP"
mkdir -p "$BACKUP_DIR"

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

# Print success message
echo "Backup created: $BACKUP_DIR"

# Find all backup directories, sort them, and remove the oldest if more than 3 exist
BACKUPS=($(ls -d "$DIR"/backup_* 2> /dev/null))

# Check if there are more than 3 backups
if [ ${#BACKUPS[@]} -gt 3 ]; then
  # Calculate how many backups need to be deleted
  DEL_COUNT=$((${#BACKUPS[@]} - 3))

  # Loop over the oldest backups and delete them
  for ((i=0; i<$DEL_COUNT; i++)); do
    rm -rf "${BACKUPS[$i]}"
    echo "Old backup removed: ${BACKUPS[$i]}"
  done
fi

Script Breakdown

Let’s walk through the script section by section:

  1. Argument Validation:

    • We first check if a directory path is provided as an argument. If not, the script exits with an error message.
  2. Directory Existence Check:

    • We verify if the provided directory exists. If not, the script exits with an error.
  3. Timestamp Creation:

    • Using the date command, we generate a timestamp in the format YYYY-MM-DD_HH-MM-SS, which will be used as the name for the backup folder.
  4. Backup Creation:

    • The script creates a new directory named with the timestamp and copies all files from the target directory into this new directory.
  5. Backup Rotation:

    • The script lists all existing backup directories, sorts them, and checks if there are more than three. If so, it deletes the oldest backups to maintain only the last three.

Example Usage

  1. First Execution (e.g., on 2023-07-30):
$ ./backup_with_rotation.sh /home/user/documents
Backup created: /home/user/documents/backup_2023-07-30_12-30-45
Backup created: /home/user/documents/backup_2023-07-30_15-20-10
Backup created: /home/user/documents/backup_2023-07-30_18-40-55

After this execution, the /home/user/documents directory contains:

  • backup_2023-07-30_12-30-45

  • backup_2023-07-30_15-20-10

  • backup_2023-07-30_18-40-55

  • file1.txt

  • file2.txt

  • ...

  1. Second Execution (e.g., on 2023-08-01):
$ ./backup_with_rotation.sh /home/user/documents
Backup created: /home/user/documents/backup_2023-08-01_09-15-30

Now, the /home/user/documents directory will contain:

  • backup_2023-07-30_15-20-10

  • backup_2023-07-30_18-40-55

  • backup_2023-08-01_09-15-30

  • file1.txt

  • file2.txt

Conclusion————

This simple yet powerful Bash script automates the process of backing up directories with a built-in rotation mechanism to ensure only the latest backups are kept.