Rsync
Authorized use only. Offensive reference for systems you own or are explicitly permitted to test. You are responsible for staying within the law.
BASIC SYNTAX#
rsync [options] source destination
BASIC USAGE#
rsync file.txt /backup/ # Copy file rsync -r dir/ /backup/dir/ # Copy directory recursively rsync -a dir/ /backup/dir/ # Archive mode (preserves everything) rsync file.txt user@remote:/path/ # Copy to remote rsync user@remote:/path/file.txt ./ # Copy from remote
IMPORTANT: TRAILING SLASH MATTERS#
rsync -a dir /backup/ # Creates /backup/dir/ rsync -a dir/ /backup/ # Copies contents into /backup/
COMMON OPTIONS#
-a, --archive # Archive mode (-rlptgoD) -r, --recursive # Recurse into directories -v, --verbose # Verbose output -h, --human-readable # Human-readable numbers -z, --compress # Compress during transfer -P # --partial --progress --progress # Show progress during transfer --partial # Keep partial transfers -n, --dry-run # Dry run (no changes) -e ssh # Use SSH for remote shell
ARCHIVE MODE BREAKDOWN (-a)#
-r # Recursive -l # Preserve symlinks -p # Preserve permissions -t # Preserve modification times -g # Preserve group -o # Preserve owner -D # Preserve device and special files
DELETE OPTIONS#
--delete # Delete files not in source --delete-before # Delete before transfer --delete-after # Delete after transfer --delete-during # Delete during transfer (default) --delete-excluded # Delete excluded files too --force # Force deletion of non-empty dirs
INCLUDE/EXCLUDE#
--exclude='pattern' # Exclude files matching pattern --include='pattern' # Include files matching pattern --exclude-from=file # Read exclude patterns from file --include-from=file # Read include patterns from file --filter='rule' # Add filter rule # Patterns *.txt # All .txt files *.log # All .log files dir/ # Specific directory **/cache/ # cache dir at any level # Order matters: first match wins --include='*.txt' --exclude='*' # Only .txt files
PRACTICAL EXAMPLES#
# Basic backup rsync -avh /source/ /backup/ # Backup with progress rsync -avhP /source/ /backup/ # Dry run (test first) rsync -avhn /source/ /backup/ # Mirror (delete extra files) rsync -avh --delete /source/ /backup/ # Remote backup over SSH rsync -avhz -e ssh /source/ user@host:/backup/ # Remote with custom SSH port rsync -avhz -e 'ssh -p 2222' /source/ user@host:/backup/ # Exclude patterns rsync -avh --exclude='*.log' --exclude='cache/' /source/ /backup/ # Include only specific files rsync -avh --include='*.php' --include='*/' --exclude='*' /source/ /backup/ # Backup with compression rsync -avhz /source/ /backup/ # Resume interrupted transfer rsync -avhP --partial /source/ /backup/ # Limit bandwidth (KB/s) rsync -avh --bwlimit=1000 /source/ /backup/ # Preserve hard links rsync -avhH /source/ /backup/
ADVANCED OPTIONS#
-H, --hard-links # Preserve hard links -A, --acls # Preserve ACLs -X, --xattrs # Preserve extended attributes -S, --sparse # Handle sparse files efficiently -x, --one-file-system # Don't cross filesystem boundaries -u, --update # Skip files newer on destination -c, --checksum # Checksum instead of time/size --inplace # Update destination files in-place --append # Append data onto shorter files --append-verify # Append with checksum verification --size-only # Compare by size only --ignore-existing # Skip existing files on destination --remove-source-files # Delete source files after transfer --max-size=SIZE # Don't transfer files larger than SIZE --min-size=SIZE # Don't transfer files smaller than SIZE --modify-window=NUM # Compare mod times with tolerance
BACKUP OPTIONS#
--backup # Make backups of changed files --backup-dir=DIR # Store backups in DIR --suffix=SUFFIX # Backup suffix (default ~) # Example: incremental backup rsync -avh --backup --backup-dir=incremental/$(date +%Y%m%d) /source/ /backup/
LOGGING#
--log-file=FILE # Log to file --log-file-format=FORMAT # Log format -i, --itemize-changes # Output change summary --stats # Print statistics
REMOTE RSYNC#
# SSH (default) rsync -avhz user@host:/remote/path/ /local/path/ rsync -avhz /local/path/ user@host:/remote/path/ # rsync daemon rsync -avhz rsync://host/module/path/ /local/path/ rsync -avhz /local/path/ rsync://host/module/path/ # With password file rsync -avhz --password-file=/path/to/pass user@host::module/
SSH OPTIONS#
# Custom SSH port rsync -avhz -e 'ssh -p 2222' source/ user@host:/dest/ # Custom SSH key rsync -avhz -e 'ssh -i ~/.ssh/mykey' source/ user@host:/dest/ # SSH compression (redundant with -z) rsync -avh -e 'ssh -C' source/ user@host:/dest/ # Multiple SSH options rsync -avhz -e 'ssh -p 2222 -i ~/.ssh/mykey -o StrictHostKeyChecking=no' source/ user@host:/dest/
CRON BACKUP EXAMPLES#
# Daily backup 0 2 * * * rsync -avh --delete /home/ /backup/home/ >> /var/log/backup.log 2>&1 # Weekly full backup 0 3 * * 0 rsync -avhH --delete /data/ /backup/weekly/ # Incremental with date 0 4 * * * rsync -avh --backup --backup-dir=/backup/inc/$(date +\%Y\%m\%d) /data/ /backup/current/
SYNC PATTERNS#
# Mirror (exact copy, delete extra) rsync -avh --delete source/ dest/ # Update only (don't delete) rsync -avh source/ dest/ # One-way sync (source to dest only) rsync -avh --update source/ dest/ # Two directories in sync rsync -avh --delete dir1/ dir2/ rsync -avh --delete dir2/ dir1/ # Copy only newer files rsync -avhu source/ dest/
OUTPUT INTERPRETATION (-i)#
YXcstpoguax ||||||||||| ||||||||||+-- x: extended attribute change |||||||||+--- a: ACL change ||||||||+---- u: update time change |||||||+----- g: group change ||||||+------ o: owner change |||||+------- p: permissions change ||||+-------- t: mod time change |||+--------- s: size change ||+---------- c: checksum change (or creation) |+----------- X: type change +------------ Y: update type # Y values < : file sent > : file received c : local change h : hard link . : unchanged * : message
TROUBLESHOOTING#
# Test with dry run first rsync -avhn --delete source/ dest/ # Verbose output rsync -avvh source/ dest/ # Debug rsync -avh --debug=ALL source/ dest/ # Check what would be deleted rsync -avhn --delete source/ dest/ | grep deleting # Verify after transfer rsync -avhc source/ dest/
PERFORMANCE TIPS#
# Parallel rsync (for many small files)
ls /source/ | parallel -j4 rsync -avh /source/{} /dest/
# Faster for local transfers (no checksum)
rsync -avh source/ dest/
# Faster for remote (compression)
rsync -avhz source/ user@host:/dest/
# Very large files
rsync -avhP --inplace source/ dest/
# Skip checksum (use size/time only)
rsync -avh source/ dest/ # Default behavior