Skip to content

ajay-mall13/Backup-rotation-shell-script

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ Automated Backup Script with Retention Policy

This Bash script automatically creates a compressed (.zip) backup of a given source directory and retains only the latest 5 backups by deleting older ones.


πŸ“‚ Features

  • βœ… Zip compression of a source folder
  • πŸ•’ Timestamp-based backup naming
  • 🧹 Rotation policy: keeps only the latest 5 backups
  • πŸ” Suitable for automation via cron jobs

πŸ› οΈ Usage

./backup.sh <path to your source directory> <path to your backup folder>

Example:

./backup.sh /home/ubuntu/devops /home/ubuntu/backups

πŸ“¦ Backup Output

Backups are stored in the format:

backups_YYYY-MM-DD_HH-MM-SS.zip

e.g., backups_2025-06-20_10-45-30.zip


πŸ”„ Rotation Logic

If there are more than 5 backups in the backup folder, the oldest ones are automatically deleted to retain only the 5 most recent.


πŸ•°οΈ Setup Automated Backups via Crontab

  1. Open crontab:
crontab -e
  1. Add the following line to schedule daily backups at every minute:
* * * * * /bin/bash /path/to/backup.sh /path/to/source /path/to/backup_folder

Screenshot 2025-06-20 120922

  1. Save and exit.

πŸ“ Notes

  • Make sure the script has execution permission:
chmod 764 backup.sh
  • Ensure zip is installed:
sudo apt install zip -y   # Debian/Ubuntu systems

πŸ“ Example Directory Structure

.
β”œβ”€β”€ backup.sh
└── backups/
    β”œβ”€β”€ backups_2025-06-20_10-45-30.zip
    β”œβ”€β”€ backups_2025-06-19_10-45-30.zip
    └── ...

πŸ‘¨β€πŸ’» Author

Ajay Mall β€” Automated backup shell script for Linux

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages