Cheatsheet#rsync
Rsync
rsync is the golden standard for transferring and synchronizing modified file chunks. It preserves file attributes, symlinks, and permissions.
Essential Flags
-a(--archive) : Enables recursive mode, preserves symlinks, permissions, modification times, group, owner.-v(--verbose) : Displays verbose progress details.-z(--compress) : Compresses data during network transfer (useful for slow connections, skip for local transfers).-h(--human-readable) : Output numbers in human-readable format.-P(--partial --progress) : Keeps partially transferred files (if interrupted) and shows a progress bar.--delete: Mirrors directory by removing files in the destination that are not present in the source.--bwlimit=RATE: Limit network I/O bandwidth (e.g.,--bwlimit=5M).
Basic Synchronization
# Sync local directory to backup directory (trailing slash matters!)
rsync -avh /path/to/source/ /path/to/backup/
# Exact mirror backup (Deletes extra files on destination)
rsync -avh --delete /data/user/ /mnt/external_drive/user/
# Dry run (Preview what will be deleted/copied without modifying anything)
rsync -avh --delete --dry-run /src/ /dst/Note on Trailing Slashes (
/):/source/copies the contents of the directory./sourcecopies the directory itself (creating a new folder inside the destination).
Network Transfers (SSH)
# Push backup to remote server over standard SSH
rsync -avzP /var/www/ [email protected]:/backups/www/
# Push backup using a custom SSH port (e.g., port 2222) and identity file
rsync -avzP -e 'ssh -p 2222 -i ~/.ssh/id_rsa' /var/www/ [email protected]:/backups/www/
# Pull backup from remote server to local machine
rsync -avzP [email protected]:/backups/db/ /local/backups/db/Excludes & Filters
# Exclude specific patterns directly in the command
rsync -avh --exclude 'node_modules' --exclude '.git' --exclude '*.log' /src/ /dst/
# Exclude using a file (create a file named exclude.txt with one pattern per line)
rsync -avh --exclude-from='exclude.txt' /src/ /dst/
# Include only specific files (e.g., only copy .config files, ignore everything else)
rsync -avh --include='*.config' --exclude='*' /src/ /dst/Incremental Backups (Hardlinks)
You can create "Time Machine" style backups using hardlinks. Unchanged files will point to the same inode as yesterday's backup, saving massive disk space.
# Create a new backup folder for today, linked against yesterday's backup
rsync -avh --delete --link-dest=/backups/yesterday/ /src/ /backups/today/