Backup & Restore
Complete guide to backup strategies and data recovery for your VPS. Learn automated backups, disaster recovery, and data protection best practices.
- Root or sudo access to your VPS
- Sufficient storage space for backups
- Understanding of your data structure
- Backup destination (local, remote, or cloud)
- Basic knowledge of compression tools
Regular backups are crucial for data protection and disaster recovery. Understanding backup types and strategies helps ensure your data remains safe and recoverable.
3-2-1 Backup Rule:
- 3 copies of your data
- 2 different media types
- 1 offsite copy
# Check available disk space for backups
df -h
du -sh /var/www/* # Check web directory sizes
du -sh /home/* # Check user directories
# Create backup directory structure
sudo mkdir -p /backups/{daily,weekly,monthly}
sudo chown root:root /backups
sudo chmod 755 /backupsDatabase backups require special attention as they contain dynamic data. Different database systems have different backup methods and considerations.
Backup Types:
- Logical backups - SQL dumps (slower, smaller)
- Physical backups - File system copies (faster, larger)
- Incremental backups - Only changed data
- Differential backups - All changes since last full backup
# MySQL/MariaDB backup
mysqldump -u root -p --all-databases > all_databases.sql
mysqldump -u root -p mydatabase > mydatabase.sql
# PostgreSQL backup
pg_dump -U postgres -h localhost mydatabase > mydatabase.sql
pg_dumpall -U postgres > all_databases.sql
# MongoDB backup
mongodump --db mydatabase --out /backups/mongodb
mongodump --out /backups/mongodb_all # All databases
# Redis backup (if using RDB persistence)
redis-cli save
cp /var/lib/redis/dump.rdb /backups/redis/dump.rdb
# Compress database backups
gzip all_databases.sql
tar -czf databases_backup.tar.gz /backups/databases/File system backups protect configuration files, user data, and application files. Use tools like tar, rsync, or specialized backup software.
Common Backup Targets:
- /etc - System configuration
- /home - User data and configurations
- /var/www - Web applications
- /opt - Custom applications
- /var/lib - Application data
# Create system configuration backup
sudo tar -czf /backups/system_config.tar.gz /etc
# Backup user directories
sudo tar -czf /backups/home_backup.tar.gz /home
# Backup web applications
sudo tar -czf /backups/web_backup.tar.gz /var/www
# Backup specific directories with exclusions
sudo tar -czf /backups/wordpress_backup.tar.gz \
--exclude='wp-content/cache' \
--exclude='wp-content/uploads/cache' \
/var/www/wordpress
# Use rsync for efficient incremental backups
sudo rsync -avz --delete /var/www/ /backups/web_mirror/
# Backup with timestamps
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
sudo tar -czf /backups/backup_$TIMESTAMP.tar.gz /important/data/Automated backups ensure consistency and reduce the risk of human error. Several tools can help automate the backup process.
Popular Automation Tools:
- cron - Simple scheduling
- rsync - Efficient file synchronization
- Bacula - Enterprise backup solution
- Duplicati - Encrypted cloud backups
- restic - Modern backup program
# Create automated backup script
sudo nano /usr/local/bin/backup.sh
#!/bin/bash
# Automated backup script
BACKUP_DIR="/backups"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
LOG_FILE="/var/log/backup.log"
# Function to log messages
log() {
echo "$(date): $1" >> $LOG_FILE
}
log "Starting backup process"
# Database backup
mysqldump -u root -p --all-databases > $BACKUP_DIR/db_$TIMESTAMP.sql
log "Database backup completed"
# File system backup
tar -czf $BACKUP_DIR/files_$TIMESTAMP.tar.gz /var/www /home
log "File system backup completed"
# Clean old backups (keep last 7 days)
find $BACKUP_DIR -name "*.sql" -mtime +7 -delete
find $BACKUP_DIR -name "*.tar.gz" -mtime +7 -delete
log "Old backups cleaned"
log "Backup process completed"
# Schedule with cron
sudo crontab -e
# Add this line for daily backups at 2 AM
0 2 * * * /usr/local/bin/backup.shOffsite backups provide additional protection against local disasters. Cloud storage services offer reliable and scalable backup solutions.
Cloud Backup Options:
- AWS S3 - Scalable object storage
- Google Cloud Storage - Global CDN
- Azure Blob Storage - Enterprise-grade storage
- DigitalOcean Spaces - S3-compatible storage
- rsync.net/Bacula - Specialized backup services
# AWS S3 backup setup
# Install AWS CLI
sudo apt install awscli -y
# Configure AWS credentials
aws configure
# Upload backup to S3
aws s3 cp /backups/backup.tar.gz s3://my-backup-bucket/
# Sync directory to S3
aws s3 sync /backups/ s3://my-backup-bucket/ --delete
# Rclone for multiple cloud providers
sudo apt install rclone -y
# Configure rclone
rclone config
# Copy to cloud storage
rclone copy /backups/ remote:backup-folder
# Mount cloud storage (if needed)
rclone mount remote:backup-folder /mnt/cloud-backup/
# Automated cloud backup script
#!/bin/bash
BACKUP_FILE="/backups/backup_$(date +%Y%m%d).tar.gz"
aws s3 cp $BACKUP_FILE s3://my-backup-bucket/daily/
echo "Backup uploaded to S3: $BACKUP_FILE"Having backups is only half the solution - you need reliable restore procedures. Test your restore process regularly to ensure it works when needed.
Restore Best Practices:
- Test restore procedures regularly
- Have a restore checklist
- Document the restore process
- Keep restore tools ready
- Practice disaster recovery scenarios
# Database restore examples
# MySQL/MariaDB restore
mysql -u root -p < all_databases.sql
mysql -u root -p mydatabase < mydatabase.sql
# PostgreSQL restore
psql -U postgres -d mydatabase < mydatabase.sql
psql -U postgres < all_databases.sql
# MongoDB restore
mongorestore --db mydatabase /backups/mongodb/mydatabase
mongorestore /backups/mongodb_all
# File system restore
tar -xzf backup.tar.gz -C /
# Selective restore
tar -xzf backup.tar.gz -C / --wildcards "*/important-file.txt"
# Verify restore integrity
# For databases
mysql -u root -p -e "SHOW DATABASES;"
psql -U postgres -l
# For files
ls -la /restored/directory/
du -sh /restored/directory/Monitor your backup processes to ensure they're running correctly and your data is being backed up as expected.
Monitoring Tasks:
- Check backup completion status
- Verify backup integrity
- Monitor storage usage
- Test restore procedures
- Review backup logs regularly
# Check backup script logs
tail -f /var/log/backup.log
# Verify backup files exist and are recent
ls -la /backups/
find /backups/ -name "*.tar.gz" -mtime -1 # Files from last 24 hours
# Check backup file integrity
tar -tzf /backups/backup.tar.gz > /dev/null && echo "Backup is valid"
# Monitor disk space for backups
df -h /backups
# Setup backup monitoring with cron
# Add to crontab for monitoring alerts
*/30 * * * * /usr/local/bin/check_backup.sh
# Backup monitoring script
#!/bin/bash
BACKUP_DIR="/backups"
LOG_FILE="/var/log/backup_monitor.log"
ADMIN_EMAIL="admin@example.com"
# Check if recent backup exists
if [ $(find $BACKUP_DIR -name "*.tar.gz" -mtime -1 | wc -l) -eq 0 ]; then
echo "$(date): No recent backup found!" >> $LOG_FILE
# Send alert email
echo "Backup Alert: No recent backup found on $(hostname)" | mail -s "Backup Alert" $ADMIN_EMAIL
fi
echo "$(date): Backup check completed" >> $LOG_FILECan't find what you're looking for? Our support team is here to help.
