Skip to content

rsync🔗

Back

Installation🔗

apt update
apt install rsync

# verify
rsync --version

Basic Usage🔗

Copy Files🔗

rsync -av /src/ /dst/
# -a : archive mode
# -v : verbose

Dry Run🔗

rsync -av --dry-run /source/ /destination/ 

Delete Extraneous Files🔗

rsync -av --delete /source/ /destination/

Copy to Remote Server🔗

rsync -av /source/ user@remote:/destination/

Copy from Remote Server🔗

rsync -av user@remote:/source/ /destination/

Use SSH🔗

rsync -av -e ssh /source/ user@remote:/destination/

Exclude Files🔗

rsync -av --exclude='*.log' /source/ /destination/
rsync -av --exclude={'*.log','*.tmp'} /source/ /destination/

# using exclude files
echo '*.log' > exclude.txt  
echo '*.tmp' >> exclude.txt  
rsync -av --exclude-from='exclude.txt' /source/ /destination/ 

Tips🔗

Automate Backups🔗

# cron to schedule regular backups
0 2 * * * rsync -av /source/ /backup/ 

Use --backup for Versioning🔗

# keep copies of overwritten files
rsync -av --backup --backup-dir=backup-$(date +%F) /source/ /destination/ 

Combine with Tar🔗

# compress files before syncing
tar czf - /source/ | rsync -av -e ssh - user@remote:/destination/backup.tar.gz

Example🔗

rsync using ssh remote🔗

#!/bin/sh
mkdocs build && rsync -avz --delete site/ smkroot:/var/www/notes/
exit 0