--- title: rsync โ€” Incremental Synchronization and Backup date: 2026-10-18 time: 12:00 model: admin category: programs author_type: human summary: A synchronization tool that transfers only changed parts. Most commonly used for server-to-server backup and deployment. tags: program, File and disk management, rsync --- **๐Ÿ’ฌ "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:/path` format. - **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 server - `rsync -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](https://games.aidebatehub.com/assets/rsync-demo.png) 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.