Day-06/100 - Automating Backups with Rotation using ZIP method | Comparision with rsync method
Backups are essential for protecting data from accidental deletion, corruption, or hardware failures. However, managing backups efficiently requires automating both the backup creation process and the rotation (deletion of old backups). In this blog, we'll explore a simple Bash script that performs these tasks seamlessly. Overview of the Backup Script This Bash script automates the process of creating compressed backups of a specified directory and retains only the latest five backups. Older backups are automatically deleted to save storage space. Features: Creates a zip backup of a specified directory. Uses a timestamp to uniquely name each backup. Retains only the latest five backups and removes older ones. Runs efficiently with minimal user intervention. Script Breakdown Here's the script: #! /bin/bash /dev/null if [ $? -eq 0 ]; then echo "Backup generated successfully for ${timestamp}" else echo "Backup failed!" >&2 fi } function perform_rotation() { # Using glob instead of `ls` to avoid errors if no backups exist backups=("${backup_dir}"/backup_*.zip) backups=($(ls -t "${backups[@]}" 2>/dev/null)) # Sort backups by timestamp echo "Existing backups: ${backups[@]}" if [ "${#backups[@]}" -gt 5 ]; then echo "Performing rotation for 5 days" backups_to_remove=("${backups[@]:5}") # Keep latest 5 backups echo "Removing backups: ${backups_to_remove[@]}" for backup in "${backups_to_remove[@]}"; do rm -f "${backup}" done fi } create_backup perform_rotation Understanding the Script 1. Usage Instructions The script includes a usage guide in a commented block to inform users how to execute it. Usage: ./backup_and_rotation.sh This ensures that users provide the required arguments: the source directory and the backup folder. 2. Checking for User Input Before proceeding, the script verifies that the required arguments are provided. If not, it displays a usage message and exits. if [ $# -eq 0 ]; then display_usage exit 1 fi 3. Backup Creation The create_backup function compresses the specified directory into a .zip file, using a timestamp in the filename for uniqueness. zip -r "${backup_dir}/backup_${timestamp}.zip" "${source_dir}" > /dev/null This ensures backups do not overwrite each other and makes it easier to track backup dates. 4. Backup Rotation The script retains only the latest five backups. It sorts the existing backups by timestamp and removes older ones. if [ "${#backups[@]}" -gt 5 ]; then backups_to_remove=("${backups[@]:5}") for backup in "${backups_to_remove[@]}"; do rm -f "${backup}" done fi This ensures that old backups do not consume unnecessary storage space. Running the Script To execute the script, use: chmod +x backup_and_rotation.sh ./backup_and_rotation.sh /path/to/source /path/to/backup For example: ./backup_and_rotation.sh /home/user/Documents /home/user/Backups Comparision When to Use Zip-based Backup Best for: Situations where you need complete, standalone backups that can be easily transferred, archived, or restored as a whole. ✅ Use Cases: Periodic Full Backups – If you need a full copy of the data every time (e.g., weekly full backups). Cloud Storage or Remote Archival– When backing up data to cloud storage (Google Drive, S3, etc.) where a single compressed file is easier to manage. Disaster Recovery– If you need to store long-term snapshots of your system that can be restored independently. File Versioning and Portability – When you need to download or send backups via email, FTP, or portable storage. Backup Before System Updates – Creating a complete archive before running major software updates or upgrades. ⚠ Downside: Requires more storage space and can be slower due to compression overhead. When to Use Rsync-based Backup Best for: Situations where efficiency, speed, and incremental backups are needed. ✅ Use Cases: Daily or Frequent Backups – Since rsync only copies changed files, it's perfect for daily or hourly backups. Server or Web App Backups – For Linux servers, web applications, and databases, where only the latest changes matter. Large File Systems – If you're backing up large directories, rsync saves time and disk space. Network or Remote Backups – When syncing files between machines (e.g., from a local machine to a remote server). Local System Synchronization – If you need to keep two directories synchronized without unnecessary duplication. ⚠ Downside: Does not create a single compressed file; you need to manually zip it later if needed.

Backups are essential for protecting data from accidental deletion, corruption, or hardware failures. However, managing backups efficiently requires automating both the backup creation process and the rotation (deletion of old backups). In this blog, we'll explore a simple Bash script that performs these tasks seamlessly.
Overview of the Backup Script
This Bash script automates the process of creating compressed backups of a specified directory and retains only the latest five backups. Older backups are automatically deleted to save storage space.
Features:
- Creates a zip backup of a specified directory.
- Uses a timestamp to uniquely name each backup.
- Retains only the latest five backups and removes older ones.
- Runs efficiently with minimal user intervention.
Script Breakdown
Here's the script:
#! /bin/bash
<< readme
This is a script for backup with 5-day rotation
Usage:
./backup_and_rotation.sh
readme
function display_usage {
echo "Usage: ./backup_and_rotation.sh "
}
if [ $# -eq 0 ]; then
display_usage
exit 1
fi
source_dir=$1
timestamp=$(date '+%Y-%m-%d-%H-%M-%S')
backup_dir=$2
function create_backup() {
zip -r "${backup_dir}/backup_${timestamp}.zip" "${source_dir}" > /dev/null
if [ $? -eq 0 ]; then
echo "Backup generated successfully for ${timestamp}"
else
echo "Backup failed!" >&2
fi
}
function perform_rotation() {
# Using glob instead of `ls` to avoid errors if no backups exist
backups=("${backup_dir}"/backup_*.zip)
backups=($(ls -t "${backups[@]}" 2>/dev/null)) # Sort backups by timestamp
echo "Existing backups: ${backups[@]}"
if [ "${#backups[@]}" -gt 5 ]; then
echo "Performing rotation for 5 days"
backups_to_remove=("${backups[@]:5}") # Keep latest 5 backups
echo "Removing backups: ${backups_to_remove[@]}"
for backup in "${backups_to_remove[@]}"; do
rm -f "${backup}"
done
fi
}
create_backup
perform_rotation
Understanding the Script
1. Usage Instructions
The script includes a usage guide in a commented block to inform users how to execute it.
Usage: ./backup_and_rotation.sh
This ensures that users provide the required arguments: the source directory and the backup folder.
2. Checking for User Input
Before proceeding, the script verifies that the required arguments are provided. If not, it displays a usage message and exits.
if [ $# -eq 0 ]; then
display_usage
exit 1
fi
3. Backup Creation
The create_backup function compresses the specified directory into a .zip file, using a timestamp in the filename for uniqueness.
zip -r "${backup_dir}/backup_${timestamp}.zip" "${source_dir}" > /dev/null
This ensures backups do not overwrite each other and makes it easier to track backup dates.
4. Backup Rotation
The script retains only the latest five backups. It sorts the existing backups by timestamp and removes older ones.
if [ "${#backups[@]}" -gt 5 ]; then
backups_to_remove=("${backups[@]:5}")
for backup in "${backups_to_remove[@]}"; do
rm -f "${backup}"
done
fi
This ensures that old backups do not consume unnecessary storage space.
Running the Script
To execute the script, use:
chmod +x backup_and_rotation.sh
./backup_and_rotation.sh /path/to/source /path/to/backup
For example:
./backup_and_rotation.sh /home/user/Documents /home/user/Backups
Comparision
When to Use Zip-based Backup
Best for: Situations where you need complete, standalone backups that can be easily transferred, archived, or restored as a whole.
✅ Use Cases:
Periodic Full Backups – If you need a full copy of the data every time (e.g., weekly full backups).
Cloud Storage or Remote Archival– When backing up data to cloud storage (Google Drive, S3, etc.) where a single compressed file is easier to manage.
Disaster Recovery– If you need to store long-term snapshots of your system that can be restored independently.
File Versioning and Portability – When you need to download or send backups via email, FTP, or portable storage.
Backup Before System Updates – Creating a complete archive before running major software updates or upgrades.
⚠ Downside: Requires more storage space and can be slower due to compression overhead.
When to Use Rsync-based Backup
Best for: Situations where efficiency, speed, and incremental backups are needed.
✅ Use Cases:
Daily or Frequent Backups – Since rsync only copies changed files, it's perfect for daily or hourly backups.
Server or Web App Backups – For Linux servers, web applications, and databases, where only the latest changes matter.
Large File Systems – If you're backing up large directories, rsync saves time and disk space.
Network or Remote Backups – When syncing files between machines (e.g., from a local machine to a remote server).
Local System Synchronization – If you need to keep two directories synchronized without unnecessary duplication.
⚠ Downside: Does not create a single compressed file; you need to manually zip it later if needed.