Implement automated backup strategy for VPS servers with regular snapshots, off-server storage, and retention policies to enable quick disaster recovery.
Instrucciones de origen · Vista previa de solo lectura
name
backup-strategy
description
Implement automated backup strategy for VPS servers with regular snapshots, off-server storage, and retention policies to enable quick disaster recovery.
license
MIT
compatibility
Ubuntu, Debian, CentOS, RHEL, and most Linux distributions
Implement automated backup solutions for VPS servers to ensure quick recovery from security incidents or system failures.
What This Skill Does
This skill helps AI agents configure automated backup systems on VPS servers. Security isn't just prevention - it's recovery. If your server gets compromised, you need to rebuild quickly. Regular, off-server backups are essential for business continuity and disaster recovery.
Key capabilities:
Create automated backup scripts
Schedule regular backups with cron
Implement retention policies (keep N days of backups)
Compress and encrypt backup archives
Store backups off-server (S3, remote server, etc.)
Verify backup integrity
Document restoration procedures
When to Use
Use this skill when you need to:
Set up new server with backup strategy
Implement disaster recovery plan
Comply with data retention requirements
Protect against ransomware and data loss
Enable quick server rebuilds
Meet business continuity requirements
Critical understanding: The backup must NOT be on the same server. If the server is compromised, local backups can be deleted or encrypted by attackers.
/home # User home directories
/etc # System and application configuration
/var/www # Web server content
/var/lib/mysql # MySQL databases (if using file-based)
/root # Root user home (if used)
/opt # Optional software installations
/usr/local # Locally installed software
# Daily at 2 AM
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
# Weekly on Sunday at 3 AM
0 3 * * 0 /usr/local/bin/backup.sh
# Daily at 2 AM, keep 30 days
0 2 * * * /usr/local/bin/backup.sh && find /backup -name "backup-*.tar.gz" -mtime +30 -delete
# Every 6 hours
0 */6 * * * /usr/local/bin/backup.sh
# Monthly on the 1st at midnight
0 0 1 * * /usr/local/bin/backup.sh
Cron with Logging
# Daily backup with logging and email on failure
0 2 * * * /usr/local/bin/backup.sh > /var/log/backup-$(date +\%Y\%m\%d).log 2>&1 || mail -s "Backup Failed" admin@example.com < /var/log/backup-$(date +\%Y\%m\%d).log
Backup Verification
Check Backup Integrity
#!/bin/bash# Verify backup archive integrity
BACKUP_FILE="/backup/backup-2024-01-31.tar.gz"# Test gzip integrity
gzip -t "$BACKUP_FILE"if [ $? -eq 0 ]; thenecho"Backup archive is valid"elseecho"Backup archive is corrupted!"exit 1
fi# Test tar contents
tar -tzf "$BACKUP_FILE" > /dev/null
if [ $? -eq 0 ]; thenecho"Tar archive structure is valid"elseecho"Tar archive has errors!"exit 1
fi
List Backup Contents
# List files in backup
tar -tzf /backup/backup-2024-01-31.tar.gz | less
# Search for specific file
tar -tzf /backup/backup-2024-01-31.tar.gz | grep "config.php"
Restoration Procedures
Full System Restore
#!/bin/bash# Restore from backup
BACKUP_FILE="/backup/backup-2024-01-31.tar.gz"# WARNING: This will overwrite existing files!echo"WARNING: This will restore files and may overwrite existing data!"read -p "Continue? (yes/no): " CONFIRM
if [ "$CONFIRM" != "yes" ]; thenecho"Aborted"exit 1
fi# Extract to rootcd /
tar -xzf "$BACKUP_FILE"echo"Restore complete. Review extracted files and restart services."
Restore Specific Directory
# Restore only /etc
tar -xzf /backup/backup-2024-01-31.tar.gz -C / etc/
# Restore specific file
tar -xzf /backup/backup-2024-01-31.tar.gz -C / etc/nginx/nginx.conf
#!/bin/bash# Backup with email notification
BACKUP_SCRIPT="/usr/local/bin/backup.sh"
ADMIN_EMAIL="admin@example.com"# Run backupif$BACKUP_SCRIPT; thenecho"Backup completed successfully on $(date)" | \
mail -s "Backup Success - $(hostname)""$ADMIN_EMAIL"elseecho"Backup failed on $(date)" | \
mail -s "BACKUP FAILED - $(hostname)""$ADMIN_EMAIL"fi
Check Last Backup Age
#!/bin/bash# Alert if backup is too old
BACKUP_DIR="/backup"
MAX_AGE_HOURS=26 # Alert if no backup in last 26 hours
LATEST_BACKUP=$(find "$BACKUP_DIR" -name "backup-*.tar.gz" -type f -printf'%T@ %p\n' | sort -n | tail -1 | cut -d' ' -f2-)
if [ -z "$LATEST_BACKUP" ]; thenecho"No backups found!" | mail -s "BACKUP ALERT" admin@example.com
exit 1
fi
AGE_SECONDS=$(($(date +%s) - $(stat -c %Y "$LATEST_BACKUP")))
AGE_HOURS=$((AGE_SECONDS / 3600))
if [ $AGE_HOURS -gt $MAX_AGE_HOURS ]; thenecho"Last backup is $AGE_HOURS hours old!" | \
mail -s "BACKUP TOO OLD" admin@example.com
fi
Security Best Practices
Off-server storage - Never rely solely on local backups
Encryption - Encrypt sensitive backups, especially if storing remotely
Access control - Restrict backup file permissions (600 or 640)
Test restores - Regularly test that backups can be restored
Monitor backup jobs - Alert on failures
Retention policy - Balance storage costs with recovery needs