Bashbashbackupautomationsystem-administrationproduction-ready
Backup Rotation Script
Automated backup with compression, rotation policies (daily/weekly/monthly), integrity verification, and S3 upload support.
title: "Backup Rotation Script" description: "Automated backup with compression, rotation policies (daily/weekly/monthly), integrity verification, and S3 upload support." category: "Bash" language: "bash" tags: ["bash", "backup", "automation", "system-administration", "production-ready"] createdAt: "2025-03-10" updatedAt: "2025-03-30"
Overview
An automated backup script with grandfather-father-son rotation (daily/weekly/monthly), compression options, SHA256 integrity verification, and optional S3 offsite storage. Designed to run as a daily cron job.
Code
BACKUP_SOURCE=/var/lib/data BACKUP_DEST=/backups S3_BUCKET=my-backups ./backup.shNotes
- The rotation policy keeps 7 daily, 4 weekly, and 6 monthly backups by default
- SHA256 checksums verify backup integrity before and after transfer
zstdoffers better compression ratio and speed than gzip for large datasets- S3
STANDARD_IAstorage class reduces costs for infrequently accessed backups - Run as a cron job:
0 2 * * * /opt/scripts/backup.sh >> /var/log/backup.log 2>&1