Rsync Backup Data: How to Use It
Rsync is a powerful command-line tool that synchronizes files and directories between two locations, making it ideal for backups. You enter a source path, a destination path, and optional flags, and rsync copies only the changed parts of files, saving time and bandwidth. This article explains how rsync works, how to use it for backups, and common pitfalls to avoid.
What Is Rsync and Why Use It for Backups?
Rsync (remote sync) is a Unix/Linux utility that copies files locally or over a network using SSH. It was first released in 1996 and has become a standard tool for system administrators and power users. Its key advantage for backups is its delta-transfer algorithm: it compares the source and destination files and transfers only the differences, not the whole file. This makes incremental backups fast and efficient, especially for large files that change slightly (e.g., database files or virtual machine images).
Rsync also preserves permissions, timestamps, symlinks, and other metadata with the -a (archive) flag, which is essential for a faithful backup. It can run over SSH for secure remote backups, and it supports resuming interrupted transfers, so a dropped connection doesn’t force you to start over.
How Rsync Works: The Basics
At its core, rsync compares files by size and modification time (unless you use --checksum to force a full content check). If a file differs, it splits the source file into blocks, computes checksums for each block, and sends them to the destination. The destination then identifies which blocks it already has and requests only the missing ones. This is the delta-transfer algorithm.
For a typical backup, you’ll use a command like:
rsync -av --delete /source/ /backup/
Here:
-a(archive): preserves permissions, times, symlinks, and recurses into directories.-v(verbose): shows what’s being copied.--delete: removes files in the destination that no longer exist in the source, making the backup a mirror.
The trailing slash on the source path (/source/) is crucial: it means “copy the contents of the directory,” not the directory itself. Without it, rsync creates a nested directory like /backup/source/.
Choosing the Right Rsync Options
Selecting flags depends on your backup needs. Here are common scenarios:
- Local backup to an external drive:
rsync -av /home/user/Documents/ /mnt/backup/Documents/ - Remote backup over SSH:
rsync -avz -e ssh /home/user/Documents/ user@remote:/backup/(the-zflag compresses data during transfer). - Incremental backup with hard links: Use
rsync -av --link-dest=to create a snapshot backup that only stores changed files, saving space. - Exclude unnecessary files:
--exclude='*.tmp'or--exclude-from='exclude-list.txt'. - Dry run: Add
-n(or--dry-run) to see what would be copied without actually doing it.
For a simple backup, -av is usually enough. Add --delete if you want the destination to exactly match the source. If you’re backing up over a network, -z helps with slow links. For maximum safety, consider using --backup to keep old versions of changed files instead of overwriting them.
Common Problems and How to Avoid Them
Rsync is reliable, but mistakes happen. The most common issues include:
- Forgetting the trailing slash on the source path, which creates an extra directory level.
- Using
--deletecarelessly on a destination that isn’t a true mirror—you could delete files you wanted to keep. - Not preserving ownership or permissions when backing up as a regular user; use
sudoor run as root if you need to preserve them. - Transferring over an unencrypted connection (without SSH) exposes your data; always use
-e sshfor remote transfers. - Ignoring exit codes: rsync returns 0 on success, but other codes indicate errors. Check with
echo $?after the command.
Another misconception is that rsync is only for Unix. While it’s native to Linux and macOS, Windows users can use it via WSL (Windows Subsystem for Linux) or tools like cwRsync, though those are third-party implementations.
Rsync vs. Other Backup Tools
Rsync is not the only backup tool. Here’s how it compares to common alternatives:
- cp -a: Simple copy, but always copies everything, no delta transfer, and no network support.
- tar: Creates archives, good for compression, but not ideal for incremental backups without extra scripting.
- rsync: Best for syncing directories, efficient with changes, supports remote transfers, and is scriptable.
- GUI tools like Deja Dup or Time Machine: Easier for beginners, but less control and often use rsync under the hood (e.g., Deja Dup uses duplicity, which uses rsync).
For a simple, powerful command-line backup, rsync is hard to beat. For non-technical users, GUI tools may be more approachable, but they often lack the granular control that rsync offers.
Setting Up an Automated Backup with Rsync
To make backups regular, you can combine rsync with cron (on Linux/macOS) or Task Scheduler (on Windows via WSL). Here’s a simple cron job that runs daily at 2 AM:
0 2 * * * rsync -av --delete /home/user/Documents/ /mnt/backup/Documents/
For more robust backups, consider a script that:
- Creates a timestamped directory for each backup.
- Uses
--link-destto point to the previous backup, so unchanged files are hard-linked, saving space. - Logs the output to a file for review.
Example script snippet:
#!/bin/bash BACKUP_DIR="/mnt/backup/$(date +%Y%m%d)" PREV="$(ls -1d /mnt/backup/*/ | tail -n 1)" rsync -av --link-dest="$PREV" /home/user/Documents/ "$BACKUP_DIR"
This creates a full backup the first time, then only stores changed files in subsequent runs, while still presenting a full directory tree.
Safety and Best Practices
Backups are your safety net, so treat them with care:
- Test your backups regularly by restoring a few files to a different location.
- Keep a copy offsite (e.g., on a remote server) to protect against local disasters.
- Use
--dry-runbefore a major backup to review what will happen. - Monitor disk space on the destination; rsync won’t warn you if it runs out.
- Encrypt sensitive data before transferring over the network, or use SSH with strong keys.
Remember that rsync is not a versioning system; it overwrites files. If you need point-in-time recovery, combine rsync with a snapshot tool or use --backup to keep old versions.
Frequently Asked Questions
What does the -a flag do in rsync?
The -a (archive) flag is a combination of several options: it preserves permissions, timestamps, symlinks, and recurses into subdirectories. It’s the most common flag for backups because it ensures the copy is a faithful replica of the source.
Can I use rsync to backup from Windows to Linux?
Yes, but you need a Windows version of rsync, such as the one included in WSL (Windows Subsystem for Linux). Alternatively, you can use a third-party port like cwRsync. The command syntax is the same, but you’ll need to adjust paths (e.g., /mnt/c/Users/...) in WSL.
Is rsync safe for backing up a running database?
Rsync alone is not safe for a live database because files may change during the copy, leading to corruption. It’s better to use a database-specific backup tool (e.g., mysqldump) or ensure the database is in a consistent state (e.g., using LVM snapshots) before running rsync.
How do I schedule rsync backups automatically?
On Linux/macOS, use cron by adding a line to your crontab: 0 2 * * * rsync -av /source/ /dest/. On Windows with WSL, you can use Task Scheduler to run a bash script that contains the rsync command.
What is the difference between –delete and –backup?
--delete removes files in the destination that are not in the source, making it a mirror. --backup moves existing destination files to a backup directory (default ~ or specified with --backup-dir) before overwriting, so you keep old versions. They serve different purposes: mirroring vs. versioning.