⚙️

rsync

2 notes  •  DevOps & CI/CD

rsync Commands Reference

rsync is the standard tool for efficient file synchronisation and remote transfers. It only sends changed blocks, making it much faster than scp for large directories.

Basic syntax

rsync [options] SOURCE DESTINATION

Common flags: -a (archive — preserves permissions, timestamps, symlinks), -v (verbose), -z (compress in transit), -P (show progress + partial).

Sync over SSH on a custom port

rsync -azv --progress -e "ssh -p 16969"   /home/user/public_html/mysite.com/   root@45.76.116.182:/home/user/public_html/mysite.com/

Sync using a specific SSH key

rsync -Pav -e "ssh -i $HOME/.ssh/mykey"   username@hostname:/remote/dir/ /local/dir/

Sync using a specific key and port

rsync -Pav -e "ssh -i $HOME/.ssh/mykey -p 2222"   username@hostname:/remote/dir/ /local/dir/

Delete files on destination that no longer exist in source

rsync -av --delete /source/dir/ user@host:/dest/dir/

Dry run (preview what would change)

rsync -av --dry-run /source/ user@host:/dest/

Backup with timestamp

rsync -av --backup --backup-dir=/backup/$(date +%Y-%m-%d) /source/ /dest/

Cron-based nightly sync

0 2 * * * rsync -az -e "ssh -i /root/.ssh/backup_key"   /var/www/ backup@backup-server:/backups/www/ >> /var/log/rsync.log 2>&1

rsync: Exclude Files and Directories

rsync's --exclude and --exclude-from options let you skip specific files, directories, or patterns during sync.

Exclude a single file or directory

rsync -av --exclude 'node_modules' /source/ /dest/
rsync -av --exclude '*.log' /source/ /dest/

Exclude multiple patterns

rsync -av   --exclude 'node_modules'   --exclude '.git'   --exclude '*.log'   --exclude 'tmp/'   /source/ user@host:/dest/

Exclude using a file (--exclude-from)

Create /etc/rsync-exclude.txt:

node_modules/
.git/
*.log
*.tmp
cache/
uploads/videos/
rsync -av --exclude-from='/etc/rsync-exclude.txt' /source/ user@host:/dest/

Exclude a directory but include its contents (use filter rules)

rsync -av   --filter='- /logs/*.gz'   --filter='+ /logs/*.log'   /source/ /dest/

Include only specific file types

# Only sync PHP and HTML files
rsync -av   --include='*.php'   --include='*.html'   --exclude='*'   /source/ user@host:/dest/

Verify before syncing (dry run)

rsync -av --dry-run --exclude-from='exclude.txt' /source/ /dest/