Setting Up Automated Server Backups

Setting Up Automated Server Backups

A robust backup strategy protects your data against accidental deletion, hardware failure, and ransomware. This guide covers a practical daily backup setup.

What to Back Up

  • Web files: /var/www/
  • Databases: use mysqldump
  • Configuration files: /etc/nginx/, /etc/apache2/, /etc/
  • Application data and uploads

Create a Backup Script

nano /root/backup.sh
#!/bin/bash
DATE=$(date +%Y-%m-%d)
BACKUP_DIR="/backup/$DATE"
mkdir -p "$BACKUP_DIR"

# Back up web files
rsync -az /var/www/ "$BACKUP_DIR/www/"

# Back up all databases
mysqldump --all-databases -u root -pYOUR_DB_PASSWORD > "$BACKUP_DIR/all-databases.sql"

# Compress the backup
tar -czf "/backup/backup-$DATE.tar.gz" "$BACKUP_DIR"
rm -rf "$BACKUP_DIR"

# Remove backups older than 7 days
find /backup/ -name "backup-*.tar.gz" -mtime +7 -delete

echo "Backup complete: /backup/backup-$DATE.tar.gz"
chmod +x /root/backup.sh

Schedule the Backup with Cron

crontab -e
# Run nightly at 3 AM:
0 3 * * * /root/backup.sh >> /var/log/backup.log 2>&1

Test the Script

/root/backup.sh

Offsite Backups with rclone

rclone can sync backups to Backblaze B2, AWS S3, Google Drive, and more:

apt install rclone -y
rclone config   # Set up your cloud storage provider
# Then add to backup.sh:
rclone copy /backup/ remote:my-server-backups/
  • 0 Utilizadores acharam útil
Esta resposta foi útil?

Artigos Relacionados

Managing Users and Groups

Managing Users and Groups on Linux Proper user management is essential for server security. Avoid...

Managing Services with systemctl

Managing Services with systemctl systemctl is the standard tool for managing services on modern...

Monitoring Server Resources

Monitoring Server Resources Keeping an eye on your server's resource usage helps you identify...

Scheduling Tasks with Cron

Scheduling Tasks with Cron Cron allows you to schedule commands to run automatically at set...

Installing and Configuring Nginx

Installing and Configuring Nginx Nginx is a high-performance web server and reverse proxy widely...