Day 9 Task: Shell Scripting Challenge Directory Backup with Rotation

ยท

3 min read

Table of Contents

  • Challenge Description

  • Steps ๐Ÿš€

    1. Create a Folder and Make Some Files

    2. Install zip (if not already installed) ๐Ÿ“ฆ

    3. crontab Job Scheduling ๐Ÿ•’

    4. Bash Script for Backup and Rotation ๐Ÿ› ๏ธ

    5. Example Crontab Job ๐Ÿ•’

  • Explanation ๐Ÿ“„

Challenge Description

The challenge is to create a Bash script that accepts a directory as a command-line argument and performs a backup. The script should:

  • Create timestamped backups in a specified directory.

  • Implement a rotation mechanism to retain only the last three backups.


Steps ๐Ÿš€

1. Create a Folder and Make Some Files

Start by simulating the data to be backed up. Use the following commands to create a directory and some sample files:

mkdir /root/datafile
touch /root/datafile/file1.txt
touch /root/datafile/file2.txt

2. Install zip (if not already installed) ๐Ÿ“ฆ

Ensure that the zip command is available, as it will be used for compression. Check if zip is installed by typing:

zip

If zip is not installed, install it using:

sudo apt install zip

3. crontab Job Scheduling ๐Ÿ•’

You can automate your backup process by scheduling the script using crontab. For instance, to run the script every hour:

  1. Open crontab for editing:

     crontab -e
    
  2. Add the following line to schedule the job:

     * 1 * * * bash /root/backup.sh /root/datafile /root/backup
    

This will execute the backup script every hour, storing backups in /root/backup.

4. Bash Script for Backup and Rotation ๐Ÿ› ๏ธ

Below is the script that performs timestamped backups and implements backup rotation:

#!/bin/bash

# Check if the required directories are provided
if [ -z "$1" ] || [ -z "$2" ]; then
    echo "Usage: $0 <source_directory> <backup_directory>"
    exit 1
fi

SOURCE_DIR="$1"
BACKUP_DIR="$2"
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_NAME="backup_$TIMESTAMP.zip"
BACKUP_PATH="$BACKUP_DIR/$BACKUP_NAME"

# Create the backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"

# Create a backup of the source directory
zip -r "$BACKUP_PATH" "$SOURCE_DIR"

# Output the backup creation message
echo "Backup created: $BACKUP_PATH"

# Implement backup rotation - Keep only the last 3 backups
BACKUPS=("$BACKUP_DIR"/backup_*.zip)
BACKUP_COUNT="${#BACKUPS[@]}"

if [ "$BACKUP_COUNT" -gt 3 ]; then
    ls -1t "${BACKUPS[@]}" | tail -n +4 | xargs -r rm
    echo "Old backups removed, keeping only the last 3 backups."
fi

5. Example Crontab Job ๐Ÿ•’

To automate this process, add the following line to your crontab configuration:

* 1 * * * bash /root/backup.sh /root/datafile /root/backup

This will ensure that:

  • Backups are created every hour. ๐Ÿ•’

  • Old backups are rotated out, keeping only the latest three. ๐Ÿ”„


Explanation ๐Ÿ“„

Backup Creation ๐Ÿ’พ

  • The script accepts two arguments: the source directory and the backup directory.

  • It generates a timestamp and creates a compressed backup file named backup_YYYY-MM-DD_HH-MM-SS.zip.

  • The backup is stored in the specified backup directory using the zip command.

Backup Rotation ๐Ÿ”„

  • The script calculates the number of backup files in the backup directory.

  • If more than three backups exist, it deletes the oldest ones to maintain only the last three.

  • This ensures efficient storage management while preserving recent backups.


By following these steps, you can create a robust and automated backup system for your files! ๐Ÿš€๐Ÿ”„

ย