Day 9 Task: Shell Scripting Challenge Directory Backup with Rotation

Manav RautManav Raut
3 min read

Challenge Description

The task is to create a bash script that takes a directory as a command-line argument and performs a backup. The script should create timestamped backup folders and copy all the files from the specified directory into the backup folder. Additionally, it should implement a rotation mechanism to keep only the last 3 backups.


Steps ๐Ÿš€

1. Create a Folder and Make Some Files

First, you'll need to create a directory with some files to simulate the data that will be backed up. ๐Ÿ“‚ Use the following commands to create the directory and files: ๐Ÿ’ป

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

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

Before running the script, ensure that zip is installed, as it will be used to compress the backups. To check if zip is installed, type: ๐Ÿ”

zip

If zip is not installed, install it using:

sudo apt install zip

3. crontab Job Scheduling ๐Ÿ•’

You can schedule your backup script to run periodically using crontab. For example, if you want the script to run every hour, do the following: โฐ

  1. Open crontab for editing:

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

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

This will run your backup script every hour and store backups in /root/backup.


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

Here is the bash script that will create timestamped backups and implement a rotation mechanism to delete older backups, keeping only the last 3 backups. ๐Ÿ”„๐Ÿ“…

#!/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 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
BACKUP_COUNT=$(ls -1 "$BACKUP_DIR" | grep "backup_.*.zip" | wc -l)

if [ "$BACKUP_COUNT" -gt 3 ]; then
    # Delete the oldest backups
    ls -1t "$BACKUP_DIR"/backup_*.zip | tail -n +4 | xargs rm
    echo "Old backups removed, keeping only the last 3 backups."
fi

Explanation: ๐Ÿ“„

  1. Backup Creation: ๐Ÿ’พ

    • The script takes two arguments: the source directory and the backup directory. ๐Ÿ“‚

    • It generates a timestamp and creates a zip file of the source directory in the backup directory with a name like backup_YYYY-MM-DD_HH-MM-SS.zip. ๐Ÿ•’

    • It uses the zip command to compress the backup. ๐Ÿ“ฆ

  2. Backup Rotation: ๐Ÿ”„

    • The script checks how many backup files exist in the backup directory. ๐Ÿ“Š

    • If there are more than 3 backups, the script deletes the oldest backups and retains only the 3 most recent backups. ๐Ÿ—‘๏ธโœจ


5. Example Crontab Job ๐Ÿ•’

To run this script automatically every hour, add the following line to your crontab: โฐ

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

This will ensure that backups are created every hour โฐ and that older backups are rotated out ๐Ÿ”„, keeping only the latest three. ๐Ÿ“‚โœจ

0
Subscribe to my newsletter

Read articles from Manav Raut directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manav Raut
Manav Raut