| 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 |
| metadata | {"author":"secure-server-skill","version":"1.0","category":"disaster-recovery"} |
| allowed-tools | Bash(tar:*, gzip:*, rsync:*, cron:*, aws:*, scp:*) |
Backup Strategy Skill
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.
Prerequisites
- Root or sudo access to the server
- Sufficient disk space for temporary backups
- Off-server storage solution (S3, remote server, NAS, etc.)
- Understanding of what needs to be backed up
- Credentials for remote storage (if applicable)
What to Back Up
Critical Directories
/home
/etc
/var/www
/var/lib/mysql
/root
/opt
/usr/local
What NOT to Back Up
/tmp
/var/tmp
/proc
/sys
/dev
/run
/var/cache
Basic Backup Script
Simple Tar-Based Backup
Create /usr/local/bin/backup.sh:
#!/bin/bash
BACKUP_DIR="/backup"
DATE=$(date +%Y-%m-%d)
BACKUP_NAME="backup-$DATE.tar.gz"
RETENTION_DAYS=7
mkdir -p "$BACKUP_DIR"
echo "Creating backup: $BACKUP_NAME"
tar -czf "$BACKUP_DIR/$BACKUP_NAME" \
--exclude='/backup' \
--exclude='/proc' \
--exclude='/sys' \
--exclude='/dev' \
--exclude='/run' \
--exclude='/tmp' \
--exclude='/var/tmp' \
--exclude='/var/cache' \
/home \
/etc \
/var/www \
/root \
2>/var/log/backup-error.log
if [ $? -eq 0 ]; then
echo "Backup completed successfully"
echo "Backup saved to: $BACKUP_DIR/$BACKUP_NAME"
else
echo "Backup failed! Check /var/log/backup-error.log"
exit 1
fi
echo "Cleaning up old backups (keeping last $RETENTION_DAYS days)..."
find "" -name -mtime + -delete
Make it executable:
sudo chmod +x /usr/local/bin/backup.sh
Advanced Backup Strategies
Database Backups
MySQL/MariaDB:
#!/bin/bash
DB_USER="root"
DB_PASS="your_password"
BACKUP_DIR="/backup/mysql"
DATE=$(date +%Y-%m-%d)
mkdir -p "$BACKUP_DIR"
mysqldump -u"$DB_USER" -p"$DB_PASS" --all-databases \
--single-transaction \
--quick \
--lock-tables=false \
> "$BACKUP_DIR/all-databases-$DATE.sql"
gzip "$BACKUP_DIR/all-databases-$DATE.sql"
find "$BACKUP_DIR" -name "all-databases-*.sql.gz" -mtime +7 -delete
PostgreSQL:
#!/bin/bash
BACKUP_DIR="/backup/postgresql"
DATE=$(date +%Y-%m-%d)
mkdir -p "$BACKUP_DIR"
sudo -u postgres pg_dumpall > "$BACKUP_DIR/pg-backup-$DATE.sql"
gzip "$BACKUP_DIR/pg-backup-$DATE.sql"
find "$BACKUP_DIR" -name "pg-backup-*.sql.gz" -mtime +7 -delete
Incremental Backups with rsync
#!/bin/bash
BACKUP_DIR="/backup/incremental"
CURRENT="$BACKUP_DIR/current"
DATE=$(date +%Y-%m-%d-%H%M%S)
SNAPSHOT="$BACKUP_DIR/$DATE"
mkdir -p "$BACKUP_DIR"
rsync -av --delete \
--link-dest="$CURRENT" \
--exclude='/backup' \
--exclude='/proc' \
--exclude='/sys' \
/home \
/etc \
/var/www \
"$SNAPSHOT"
rm -f "$CURRENT"
ln -s "$SNAPSHOT" "$CURRENT"
ls -1dt "$BACKUP_DIR"/2* | tail -n +11 | xargs rm -rf
Off-Server Storage
AWS S3 Backup
#!/bin/bash
BACKUP_DIR="/backup"
S3_BUCKET="s3://my-backups/server-name"
DATE=$(date +%Y-%m-%d)
BACKUP_FILE="backup-$DATE.tar.gz"
tar -czf "$BACKUP_DIR/$BACKUP_FILE" /home /etc /var/www
aws s3 cp "$BACKUP_DIR/$BACKUP_FILE" "$S3_BUCKET/"
if [ $? -eq 0 ]; then
echo "Backup uploaded to S3 successfully"
rm "$BACKUP_DIR/$BACKUP_FILE"
else
echo "S3 upload failed!"
exit 1
fi
SCP to Remote Server
#!/bin/bash
BACKUP_DIR="/backup"
REMOTE_USER="backup"
REMOTE_HOST="backup-server.example.com"
REMOTE_DIR="/backups/webserver"
DATE=$(date +%Y-%m-%d)
BACKUP_FILE="backup-$DATE.tar.gz"
tar -czf "$BACKUP_DIR/$BACKUP_FILE" /home /etc /var/www
scp "$BACKUP_DIR/$BACKUP_FILE" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/"
if [ $? -eq 0 ]; then
echo "Backup transferred successfully"
rm "$BACKUP_DIR/$BACKUP_FILE"
else
echo "Transfer failed!"
exit 1
fi
Encrypted Backups
#!/bin/bash
BACKUP_DIR="/backup"
DATE=$(date +%Y-%m-%d)
BACKUP_FILE="backup-$DATE.tar.gz"
ENCRYPTED_FILE="backup-$DATE.tar.gz.gpg"
GPG_RECIPIENT="admin@example.com"
tar -czf "$BACKUP_DIR/$BACKUP_FILE" /home /etc /var/www
gpg --encrypt --recipient "$GPG_RECIPIENT" \
--output "$BACKUP_DIR/$ENCRYPTED_FILE" \
"$BACKUP_DIR/$BACKUP_FILE"
rm "$BACKUP_DIR/$BACKUP_FILE"
echo "Encrypted backup created: $ENCRYPTED_FILE"
Scheduling Backups with Cron
Edit Crontab
sudo crontab -e
Common Schedules
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
0 3 * * 0 /usr/local/bin/backup.sh
0 2 * * * /usr/local/bin/backup.sh && find /backup -name "backup-*.tar.gz" -mtime +30 -delete
0 */6 * * * /usr/local/bin/backup.sh
0 0 1 * * /usr/local/bin/backup.sh
Cron with Logging
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
BACKUP_FILE="/backup/backup-2024-01-31.tar.gz"
gzip -t "$BACKUP_FILE"
if [ $? -eq 0 ]; then
echo "Backup archive is valid"
else
echo "Backup archive is corrupted!"
exit 1
fi
tar -tzf "$BACKUP_FILE" > /dev/null
if [ $? -eq 0 ]; then
echo "Tar archive structure is valid"
else
echo "Tar archive has errors!"
exit 1
fi
List Backup Contents
tar -tzf /backup/backup-2024-01-31.tar.gz | less
tar -tzf /backup/backup-2024-01-31.tar.gz | grep "config.php"
Restoration Procedures
Full System Restore
#!/bin/bash
BACKUP_FILE="/backup/backup-2024-01-31.tar.gz"
echo "WARNING: This will restore files and may overwrite existing data!"
read -p "Continue? (yes/no): " CONFIRM
if [ "$CONFIRM" != "yes" ]; then
echo "Aborted"
exit 1
fi
cd /
tar -xzf "$BACKUP_FILE"
echo "Restore complete. Review extracted files and restart services."
Restore Specific Directory
tar -xzf /backup/backup-2024-01-31.tar.gz -C / etc/
tar -xzf /backup/backup-2024-01-31.tar.gz -C / etc/nginx/nginx.conf
Restore Database
gunzip < /backup/mysql/all-databases-2024-01-31.sql.gz | mysql -uroot -p
gunzip < /backup/postgresql/pg-backup-2024-01-31.sql.gz | sudo -u postgres psql
Monitoring and Alerting
Email Notifications
#!/bin/bash
BACKUP_SCRIPT="/usr/local/bin/backup.sh"
ADMIN_EMAIL="admin@example.com"
if $BACKUP_SCRIPT; then
echo "Backup completed successfully on $(date)" | \
mail -s "Backup Success - $(hostname)" "$ADMIN_EMAIL"
else
echo "Backup failed on $(date)" | \
mail -s "BACKUP FAILED - $(hostname)" "$ADMIN_EMAIL"
fi
Check Last Backup Age
#!/bin/bash
BACKUP_DIR="/backup"
MAX_AGE_HOURS=26
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" ]; then
echo "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 ]; then
echo "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
- Version backups - Keep multiple generations
- Document procedures - Maintain restoration runbooks
- Separate credentials - Don't store backup credentials on the server being backed up
Common Mistakes to Avoid
- ❌ Only backing up to the same server (single point of failure)
- ❌ Not testing restore procedures
- ❌ Backing up cached/temporary files (waste of space)
- ❌ Not encrypting backups containing sensitive data
- ❌ Setting retention too short (can't recover from old issues)
- ❌ Not monitoring backup success/failure
- ❌ Including backup directory in backup (infinite loop!)
- ❌ Not documenting what's backed up and how to restore
Additional Resources
See references/backup-locations.md for storage provider comparison.
See scripts/backup-full.sh for comprehensive backup script.
See scripts/backup-mysql.sh for database-specific backup.
Related Skills
auto-updates - Keep backup tools updated
ssh-hardening - Secure SSH for remote backups
firewall-configuration - Protect backup storage access