Backup & Restore Using rsync
Objective: Implement a local backup solution using rsync and demonstrate restore capability.
-
1. Verify Backup Destination
lsblk df -hConfirm that
/backupexists and is mounted.
-
2. Create Test Source Directory
sudo mkdir -p /data sudo touch /data/file1.txt sudo touch /data/file2.txtSimulates production data.
-
3. Perform Initial Backup
sudo rsync -av --delete /data/ /backup/data/Explanation:
- -a → archive mode (preserves permissions, ownership, timestamps)
- -v → verbose output
- --delete → removes files in backup deleted from source
- Trailing slash on
/data/ensures contents are copied, not the directory itself
-
4. Verify Backup Integrity
ls /backup/data df -hConfirm that files exist in the backup location.
-
5. Test Incremental Behavior
sudo rm /data/file2.txt sudo touch /data/file3.txt sudo rsync -av --delete /data/ /backup/data/Verify:
- file2.txt removed from backup
- file3.txt added to backup
-
6. Log Backup Output
sudo rsync -av --delete /data/ /backup/data/ >> /var/log/rsync-backup.log 2>&1Records stdout and stderr for auditing and troubleshooting.
-
7. Simulate Data Loss and Restore
sudo rm -rf /data/* sudo rsync -av /backup/data/ /data/ ls /dataVerify that the data has been restored from backup.
-
8. Optional: Automate Backup with Cron
sudo crontab -eExample daily backup at 2:00 AM:
0 2 * * * /usr/bin/rsync -a --delete /data/ /backup/data/ >> /var/log/rsync-backup.log 2>&1