This page looks best with JavaScript enabled

Directory synchronization using RSync

 ·  🎃 kr0m

Rsync is an essential tool for any self-respecting sysadmin. It allows us to synchronize remote directories very easily and will only synchronize changes, which means that subsequent synchronizations will be faster ;). There are countless uses such as file synchronization between web servers, backups, and any other purpose that your fevered minds can imagine.

COPYING DIRECTORIES:

rsync -av "SOURCE_PATH DESTINATION_PATH"

It is equivalent to: -rlptgoDv

-r: Recorre de forma recurisva la estructura de directorios que le indiquemos  
-l: Copia enlaces simbólicos como enlaces simbólicos  
-p: Mantiene permisos  
-t: Mantiene hora del fichero  
-g: Mantiene el grupo  
-o: Mantiene el propietario  
-D: Mantiene los ficheros de dispositivo (solo para root)  
-v modo verbose

AVOID FULL COPY:

By default, Rsync copies the entire file as soon as it detects that something has changed. If you want to avoid this and only copy the difference (more CPU).

rsync -av --no-whole-file "SOURCE_PATH DESTINATION_PATH"

COMPRESS TRAFFIC (more CPU):

rsync -avz --no-whole-file "SOURCE_PATH DESTINATION_PATH"

CHECK CRC:

By default, Rsync only looks at the date and size. If nothing changes, it is considered that the file did not change. You can force the check to be done by CRC.

rsync -avZc --no-whole-file "SOURCE_PATH DESTINATION_PATH"

DELETE DELETED FILES IN SOURCE (Dangerous!!):

rsync -av --delete "SOURCE_PATH DESTINATION_PATH"

KEEP FILES IN DESTINATION IF THEY ARE NEWER THAN THE SOURCE:

rsync -avu "SOURCE_PATH DESTINATION_PATH"

-u, to avoid overwriting destination files that are more recent than the source

INCREMENTAL BACKUP (Saves a copy of modified files):

rsync -avvb --delete --backup-dir=/rsync/backup_$(date +%y%m%d%H%M) "SOURCE_PATH DESTINATION_PATH"

REMOTE SYNCHRONIZATION (SSH):

rsync -avz --rsh='ssh -p22' "USER@HOST:SOURCE_PATH DESTINATION_PATH"
rsync -avz --delete --rsh='ssh -p22' "USER@HOST:SOURCE_PATH DESTINATION_PATH"

EXCLUSIONS:

You can exclude directories by specifying them in a file or in the synchronization command.

rsync -avz --delete --exclude-from /root/.scripts/excludes_rsync --rsh='ssh -p22' "USER@HOST:SOURCE_PATH DESTINATION_PATH"

cat /root/.scripts/excludes_rsync
.ssh
public_html/web/images/users/
public_html/app/logs
public_html/app/cache
rsync -avz --delete --exclude .ssh --exclude public_html/web/images/users/ --exclude public_html/app/logs --exclude public_html/app/cache --rsh='ssh -p22' "USER@HOST:SOURCE_PATH DESTINATION_PATH"

NOTE: Exclusions are always relative to the source directory!!!!

CONSIDERATIONS:

The slash / can make RSync behave differently:

rsync -av "SOURCE_PATH DESTINATION_PATH" –> Copies directory 1 to 2
rsync -av "SOURCE_PATH/ DESTINATION_PATH" –> Copies files from directory 1 to 2

CRON:

A typical use is to include an RSync in the CRON to periodically synchronize the contents of a certain directory.

*/5 * * * * /usr/bin/rsync -avz -\-delete -\-rsh=\'ssh -p22\' \"USER@HOST:SOURCE_PATH DESTINATION_PATH\" >/dev/null 2>&1
If you liked the article, you can treat me to a RedBull here