Backing up your data doesn’t have to be a headache, even if you’re new to Linux. In this guide we’ll walk you through setting up an automated backup solution using two of the most dependable tools on any Unix‑like system: rsync for copying files efficiently, and cron for scheduling the job. By the end you’ll have a hands‑free system that protects your home directory, web server files, or any folder you choose.
What You’ll Need
- A Linux machine (any distribution that supports
rsyncandcron) - Root or sudo access to edit cron jobs
- A destination for your backups – another local drive, an external USB stick, or a network share (NFS, SMB, etc.)
- Basic familiarity with the terminal
Step 1: Install rsync (if it isn’t already)
Most modern Linux distributions ship with rsync pre‑installed. Verify its presence by running:
rsync --version
If you see a version string, you’re good to go. Otherwise install it with your package manager:
Debian/Ubuntu: sudo apt-get update && sudo apt-get install rsync
Fedora/CentOS/RHEL: sudo dnf install rsync or sudo yum install rsync
Arch Linux: sudo pacman -S rsync
Installation typically takes less than a minute.
Step 2: Choose and Prepare a Backup Destination
Decide where you want your backups to live. For a local external drive mounted at /mnt/backup, make sure the mount point exists and is writable:
sudo mkdir -p /mnt/backup
sudo chown $(whoami):$(whoami) /mnt/backup
If you are using a network share, mount it first (e.g., using mount.cifs for SMB or mount -t nfs for NFS). Test write permission with a simple touch /mnt/backup/testfile and delete the file afterwards.
Step 3: Write Your rsync Command
Craft a command that copies exactly what you need while preserving permissions, timestamps, and symbolic links. A solid starting point looks like this:
rsync -av --delete /home/$(whoami)/ /mnt/backup/home_$(whoami)/
Explanation of the flags:
-a(archive) – recursive copy, preserves permissions, owners, timestamps, and symlinks.-v(verbose) – shows what’s being transferred; you can replace it with-hfor human‑readable numbers.--delete– removes files from the destination that no longer exist in the source, keeping both sides in sync.
Run the command manually first to make sure it behaves as expected. If you see a flood of “file unchanged” messages, add --dry-run to preview without touching any data:
rsync -av --delete --dry-run /home/$(whoami)/ /mnt/backup/home_$(whoami)/
Step 4: Create a Shell Script (Optional but Recommended)
Wrapping the command in a script makes future edits easier and lets you add logging. Create a file called ~/backup.sh:
#!/bin/bash
# Simple backup script using rsync
LOGFILE="/var/log/rsync-backup.log"
DATE=$(date "+%Y-%m-%d %H:%M:%S")
echo "--- Backup started at $DATE ---" >> $LOGFILE
rsync -av --delete /home/$(whoami)/ /mnt/backup/home_$(whoami)/ >> $LOGFILE 2>&1
STATUS=$?
if [ $STATUS -eq 0 ]; then
echo "Backup completed successfully" >> $LOGFILE
else
echo "Backup encountered errors (exit code $STATUS)" >> $LOGFILE
fi
echo "--- Backup finished at $(date "+%Y-%m-%d %H:%M:%S") ---n" >> $LOGFILE
Make it executable:
chmod +x ~/backup.sh
If you prefer not to write a script, you can place the raw rsync command directly in the cron entry (see Step 5).
Step 5: Schedule the Job with cron
Open your personal crontab with:
crontab -e
Add a line that runs the backup every night at 2 am – a time when most desktops are idle:
0 2 * * * /home/$(whoami)/backup.sh
If you skipped the script, the line would look like this:
0 2 * * * rsync -av --delete /home/$(whoami)/ /mnt/backup/home_$(whoami)/ >> /var/log/rsync-backup.log 2>&1
Save and exit the editor. cron will automatically pick up the new schedule. To verify, list your crontab again with crontab -l.
Step 6: Verify Backups and Monitor Logs
After the first scheduled run (or after you manually trigger the script), inspect the log file:
cat /var/log/rsync-backup.log
You should see a list of transferred files, a summary, and the “Backup completed successfully” message. Also, browse the backup directory to confirm the expected files are present.
For ongoing health checks, consider adding a simple email alert to the script using mail or sendmail. Example snippet:
if [ $STATUS -ne 0 ]; then
echo "Backup failed on $(date)" | mail -s "rsync backup error" you@example.com
fi
Common Mistakes to Avoid
1 Backing up to the same filesystem. If your source and destination live on the same partition, a failure could corrupt both copies. Always use a separate drive or network share.
2 Using --delete carelessly. While useful, --delete will erase anything not present in the source. Test with --dry-run first, especially when backing up a newly created directory.
3 Running the job as root unintentionally. Root‑owned files may be copied with permissions that break later restores. Run the backup as the regular user whenever possible.
4 Neglecting log rotation. Log files grow indefinitely. Set up logrotate or truncate the log at the start of each run.
5 Forgetting to mount the backup drive. If the external drive isn’t mounted when cron fires, rsync will create a local directory named /mnt/backup and silently copy data there. Use the @reboot cron entry or a systemd mount unit to guarantee the mount.
Tips and Tricks
• Incremental backups with --link-dest. Create daily snapshots that share unchanged files via hard links, saving space while keeping a full history.
• Compress during transfer. Add -z to the rsync flags if the backup destination is remote (e.g., over SSH).
• Encrypt sensitive data. Pipe rsync through gpg or use an encrypted LUKS volume for the backup drive.
• Test restores. A backup is only as good as your ability to recover. Periodically restore a random file or directory to a temporary location.
Frequently Asked Questions
Can I back up to a remote server?
Yes. Replace the local destination with an SSH target, e.g., rsync -avz /home/user/ user@remote:/backup/home_user/. Ensure SSH keys are set up for password‑less login.
What if I need to back up databases?
Database files should be dumped first (e.g., mysqldump or pg_dump) and then included in the rsync set. Automate the dump in your script before the rsync line.
How do I back up multiple directories with one cron job?
List them sequentially in the script, or use a loop:
for DIR in /etc /var/www /home/$(whoami); do
rsync -av --delete "$DIR" /mnt/backup$(basename "$DIR")/
done
Conclusion
Setting up automated backups with rsync and cron is a straightforward, low‑cost solution that scales from a single laptop to a multi‑server environment. By following the steps above, testing your configuration, and watching out for common pitfalls, you’ll protect your data without ever lifting a finger again. Remember: backups are only useful when they’re reliable and restorable, so schedule regular tests and keep your backup media in good condition. Happy backing up!




