π Shell Scripting Challenge: Directory Backup with Rotation .
Automate Backups with Shell Scripting
π Challenge Description
The task is to write a bash script that:
Accepts a directory path as a command-line argument.
Creates a timestamped backup folder in the specified directory.
Copies all the files from the directory into this new backup folder.
Keeps only the last 3 backups and deletes any older ones automatically.
π Shell Script: backup_with_
rotation.sh
Hereβs the script to handle the challenge step-by-step:
#!/bin/bash
# Check if a directory is provided as an argument
if [ -z "$1" ]; then
echo "β Error: Please provide a directory path as an argument!"
exit 1
fi
# Assign the directory path
DIR_PATH="$1"
# Check if the provided path exists and is a directory
if [ ! -d "$DIR_PATH" ]; then
echo "β Error: The specified path does not exist or is not a directory!"
exit 1
fi
# Generate a timestamp for the backup folder
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_FOLDER="backup_${TIMESTAMP}"
# Create the backup folder
mkdir "$DIR_PATH/$BACKUP_FOLDER"
# Copy all files from the specified directory to the backup folder
cp -r "$DIR_PATH"/* "$DIR_PATH/$BACKUP_FOLDER" 2>/dev/null
echo "β
Backup created: $DIR_PATH/$BACKUP_FOLDER"
# Change to the directory containing the backups
cd "$DIR_PATH" || exit
# Get a list of backup folders matching the pattern "backup_*"
BACKUPS=(backup_*)
NUM_BACKUPS=${#BACKUPS[@]} # Count the number of backups
# If more than 3 backups exist, remove the oldest ones
if [ "$NUM_BACKUPS" -gt 3 ]; then
DELETE_COUNT=$((NUM_BACKUPS - 3))
for ((i = 0; i < DELETE_COUNT; i++)); do
rm -rf "${BACKUPS[$i]}"
echo "π Removed old backup: ${BACKUPS[$i]}"
done
fi
𧩠How the Script Works
Step-by-Step Breakdown:
Input Validation
The script checks if the directory path is provided as an argument.
It verifies that the given path exists and is a directory.
Create a Timestamped Backup
A unique backup folder is created using the current timestamp, e.g.,
backup_2025-02-12_12-30-45
.Files from the directory are copied into this backup folder.
Backup Rotation
The script lists all existing backup folders matching the pattern
backup_*
.If there are more than 3 backups, the oldest backups are deleted to retain only the latest 3.
π» Example Execution
Command:
$ ./backup_with_rotation.sh /home/user/documents
First Execution (2025-02-12):
Output:
β
Backup created: /home/user/documents/backup_2025-02-12_10-15-30
β
Backup created: /home/user/documents/backup_2025-02-12_11-45-00
β
Backup created: /home/user/documents/backup_2025-02-12_12-30-45
Directory contents:
backup_2025-02-12_10-15-30
backup_2025-02-12_11-45-00
backup_2025-02-12_12-30-45
file1.txt
file2.txt
...
Second Execution (2025-02-13):
Output:
β
Backup created: /home/user/documents/backup_2025-02-13_08-00-00
π Removed old backup: backup_2025-02-12_10-15-30
Directory contents (after rotation):
backup_2025-02-12_11-45-00
backup_2025-02-12_12-30-45
backup_2025-02-13_08-00-00
file1.txt
file2.txt
...
π― Notice: The oldest backup (backup_2025-02-12_10-15-30
) is deleted to retain only the last 3 backups.
π Key Shell Commands Used
Here are the main commands powering the script:
date
Generates a unique timestamp for each backup.date +"%Y-%m-%d_%H-%M-%S"
mkdir
Creates a new directory for the backup folder.mkdir "$DIR_PATH/$BACKUP_FOLDER"
cp
Copies all files from the source directory to the backup folder.cp -r "$DIR_PATH"/* "$DIR_PATH/$BACKUP_FOLDER"
rm
Deletes the oldest backups to maintain only the last 3 backups.rm -rf "${BACKUPS[$i]}"
β° Scheduling Backups with Cron
Automate the script to run daily at 2:00 AM using the following cron
command:
0 2 * * * /path/to/backup_with_rotation.sh /home/user/documents