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:
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).
COMPRESS TRAFFIC (more CPU):
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.
DELETE DELETED FILES IN SOURCE (Dangerous!!):
KEEP FILES IN DESTINATION IF THEY ARE NEWER THAN THE SOURCE:
-u, to avoid overwriting destination files that are more recent than the source
INCREMENTAL BACKUP (Saves a copy of modified files):
REMOTE SYNCHRONIZATION (SSH):
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.
.ssh
public_html/web/images/users/
public_html/app/logs
public_html/app/cache
NOTE: Exclusions are always relative to the source directory!!!!
CONSIDERATIONS:
The slash / can make RSync behave differently:
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