rsync — Incremental Synchronization and Backup
💬 "rsync command"
rsync transfers only the differences. It is a standard tool used for server-to-server backup, migration, and deployment.
Option combinations matter. Use -a(preserve attributes), -v(verbose output), -z(compress during transfer) as defaults.
- Concept: a file synchronization tool that transfers only the differences between source and destination.
- Left argument: the source path. If it ends with a slash, it copies the contents; if not, it copies the directory itself.
- Right argument: the destination path. For remote destinations, use the
user@host:/pathformat. - Deletion: If you use
--delete, files not in the source are also deleted from the destination.
Commonly used options
| Option | Description |
|---|---|
| -a | Preserves permissions, timestamps, ownership, and other attributes (archive mode) |
| -v | Prints the files being transferred |
| -z | Compresses during transfer (useful for remote transfers) |
| --delete | Removes files from the destination that are not in the source, making it a full mirror |
| -n / --dry-run | Shows what would change without actually doing anything |
3 essential command combinations
rsync -avz /var/www/html/ deploy@10.0.0.2:/var/www/html/— incrementally sync the web root to a remote serverrsync -avn --delete /data/ /backup/data/— preview what would be deleted as well (dry-run)rsync -av --exclude '*.log' /srv/app/ /backup/app/— back up while excluding logs
💬 "Show me examples of cost (resource) savings with this"
Here are common situations on live production servers, organized in order.
Step 1 · Preview (dry-run)
First, check what will be transferred or deleted. You should especially preview --delete.
rsync -avn --delete /srv/app/ /backup/app/
Step 2 · Actual synchronization
If there are no problems, run it without -n.
rsync -av --delete /srv/app/ /backup/app/
Step 3 · Verification
Compare file counts, sizes, and representative files.
rsync -avn --delete /srv/app/ /backup/app/ | tail -3
How it is used in practice
1. Server migration
Situation — You need to move /srv/app from the old server to the new server exactly as it is.
rsync -avz --exclude 'cache/' /srv/app/ root@10.0.0.9:/srv/app/
Result — Even for large data, only the changes are transferred, greatly reducing migration time.
Point — Run it once before the migration, then run it once more at the time of the service cutover to minimize downtime.
2. Log archive backup
Situation — You need to move last month's logs to backup storage for retention.
rsync -av --remove-source-files /var/log/nginx/2026-09-* /backup/logs/
Result — After transfer is complete, the source files are deleted, freeing up local disk space.
Point — Only use --remove-source-files after verification. It is essential to confirm success so that the source remains if the transfer fails.
Important notes
- The presence or absence of a trailing slash on the source path completely changes the result. Distinguish between wanting the directory itself and wanting only its contents.
- --delete deletes files from the destination. Always preview with -n first.
- Syncing live DB files can break consistency. Dump the database first and then transfer it.
- Use caution when adding --remove-source-files to automation, because it risks data loss if the transfer fails.
Actual execution screen
This shows only what would be transferred or deleted in a dry-run (no actual changes were made).
!rsync command execution result: dry-run incremental transfer list
Live production server
Source (HTML): https://games.aidebatehub.com/programs/linux/filesystem/rsync.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments