Command linemediumBash

Rsync for Backup and Synchronization

Powerful file synchronization and backup using rsync with various options.

01

The problem

You need to sync files between directories/servers or create incremental backups.

02

The solution

Bash
# Basic sync (local)
rsync -av source/ destination/

# Sync with progress
rsync -av --progress source/ destination/

# Dry run (simulate without copying)
rsync -av --dry-run source/ destination/

# Delete extra files at destination (mirror)
rsync -av --delete source/ destination/

# Exclude files/folders
rsync -av --exclude='*.log' --exclude='node_modules/' source/ destination/

# Use exclude list file
rsync -av --exclude-from='exclude.txt' source/ destination/

# Sync over SSH
rsync -avz -e ssh source/ user@remote:/destination/
rsync -avz user@remote:/source/ destination/

# Preserve permissions, ownership, timestamps
rsync -avpog source/ destination/

# Limit bandwidth (KB/s)
rsync -av --bwlimit=1000 source/ destination/

# Resume partial transfers
rsync -av --partial source/ destination/

# Remove source files after transfer (move)
rsync -av --remove-source-files source/ destination/

# Compare files (checksum, not just time/size)
rsync -av --checksum source/ destination/

# Copy symbolic links as links
rsync -av -L source/ destination/   # Follow links
rsync -av -l source/ destination/   # Copy as links

# Set maximum file size
rsync -av --max-size='100M' source/ destination/

# Skip files larger than size
rsync -av --exclude='*.iso' source/ destination/

# Backup with timestamp
rsync -av --backup --backup-dir=../backup_$(date +%Y%m%d) source/ destination/

# Include only specific patterns
rsync -av --include='*.txt' --include='*.pdf' --exclude='*' source/ destination/

# Compress during transfer (good for slow connections)
rsync -avz source/ destination/

# Two-way sync (bidirectional, requires Unison)
# Not native to rsync, use: unison profile

03

Put it to work

Example
# Backup home directory to external drive
rsync -av --progress --delete /home/user/ /media/user/backup/

# Sync website to server
rsync -avz -e ssh ~/website/ user@server:/var/www/html/

# Create incremental backup with timestamp
rsync -av --backup --backup-dir=/backups/$(date +%Y-%m-%d) /data/ /backups/current/

# Exclude cache and temp files
rsync -av --exclude='.cache/' --exclude='*.tmp' ~/ /backup/

# Test before actual sync
rsync -av --dry-run --delete source/ destination/

Worth knowing

Trailing slash matters: source/ copies contents, source copies the folder itself. Use -n for dry runs. -a archive mode includes -rlptgoD (recursive, links, perms, times, group, owner, devices).