Server ManagementIntermediate18 min
How to Set Up Automated Backups
Configure automated backups for your server data using rsync and cron jobs
Introduction
Regular backups are essential for data protection. This guide shows you how to automate backups using rsync and cron.
Step 1: Install Rsync
Ensure rsync is installed on your system.
sudo apt update
sudo apt install rsyncStep 2: Create Backup Script
Create a bash script to handle the backup process.
#!/bin/bash
BACKUP_DIR="/backup"
SOURCE_DIR="/var/www"
DATE=$(date +%Y-%m-%d)
rsync -avz $SOURCE_DIR $BACKUP_DIR/backup-$DATE
find $BACKUP_DIR -type d -mtime +7 -exec rm -rf {} \;Step 3: Make Script Executable
Set proper permissions for the backup script.
chmod +x /usr/local/bin/backup.shStep 4: Schedule with Cron
Add a cron job to run the backup script daily at 2 AM.
sudo crontab -e
# Add: 0 2 * * * /usr/local/bin/backup.shStep 5: Test the Backup
Run the backup script manually to ensure it works correctly.
sudo /usr/local/bin/backup.shConclusion
Your server now has automated daily backups. Consider storing backups off-site for additional protection.
#Backup#Rsync#Cron#Linux#Data Protection