This script automates the process of creating backups for a specified directory and implements a backup rotation mechanism to manage storage space efficiently.
Backup rotation is a process of systematically managing backups by retaining only the most recent copies and deleting older ones. This approach ensures:
- Efficient Storage Utilization: Prevents disk space from being consumed by redundant backups.
- Quick Recovery: Ensures access to recent backups without shifting through outdated copies.
- Streamlined Maintenance: Simplifies the process of managing backup files.
By limiting the number of backups retained (e.g., the last 5), backup rotation achieves a balance between redundancy and storage efficiency.
- Limited Storage Capacity: Disk space is finite; retaining too many backups can exhaust available space quickly.
- Prevent Redundancy: Keeping excessive backups often results in duplication and unnecessary clutter.
- Focus on Relevance: Only the most recent backups are typically useful for recovery.
- Maintenance Efficiency: Automating the rotation reduces manual intervention, saving time and effort.
Without backup rotation, backup systems may become unsustainable over time, leading to storage constraints and inefficiencies.
This script creates compressed backups of a source directory and retains only the latest 5 backups in the target directory. The older backups are automatically deleted to optimize storage space.
- Automated Backup Creation: Compresses the source directory into a zip file with a timestamped name.
- Backup Rotation: Ensures only the 5 most recent backups are retained.
- Error Handling: Provides clear error messages if something goes wrong during backup creation or file deletion.
-
Source Directory Setup:
- A directory named
datawas created at/home/ubuntu/. - This directory (
/home/ubuntu/data) contains 5 files:file1.txt,file2.txt,file3.txt,file4.txt, andfile5.txt.
- A directory named
-
Script File:
- A script file named
backup.shwas created in/home/ubuntu/. - This file contains the backup and rotation code described in this document.
- A script file named
-
Destination Directory Setup:
- A directory named
backupswas created at/home/ubuntu/. - This directory (
/home/ubuntu/backups) is where all the backups are stored. - After running the script, the backups are compressed into zip files and saved in this directory. The rotation mechanism ensures that only the latest 5 backups are retained.
- A directory named
- Usage Check: Validates that the source and backup directories are provided as arguments.
- Backup Rotation:
- Lists all existing backups in the target directory.
- Deletes backups if there are more than 5, starting with the oldest.
- Backup Creation:
- Creates a compressed zip file of the source directory, named with the current timestamp.
-
display_usage:- Displays usage instructions if incorrect arguments are provided.
-
perform_rotation:- Checks the number of backups in the target directory.
- Deletes backups exceeding the 5 most recent files.
-
create_backup:- Creates a timestamped zip file of the source directory in the backup directory.
- Validate arguments.
- Execute
perform_rotationto clean up old backups. - Execute
create_backupto generate a new backup.
<path to source>: The directory to be backed up.<path to backup>: The directory where the backup will be stored.
./backup.sh /home/ubuntu/data /home/ubuntu/backupsBefore running the script, ensure it has executable permissions:
chmod +x backup.sh- Automates regular backups with minimal user intervention.
- Prevents clutter in the backup directory by keeping only relevant files.
- Ideal for systems with limited storage space.
- Requires
zipto be installed on the system. - Backup rotation is limited to 5 files by default (modifiable in the script).
- Add email notifications for backup completion or failures.
- Support for incremental backups to reduce storage space further.
- Make the number of backups retained configurable through arguments or a configuration file.