Rsync’s MAN page states – “a fast, versatile, remote (and local) file-copying tool”
Rsync is a very powerful tool used to copy files and directories on a local server or remotely.
Its power lies in its ability to copy recursively, keep file and directory permissions the same as the source and only copy the bits of a file that have changed, called “delta-transfer”.
So not only can you get the destination directory to look exactly the same as the source – as all permissions and users are kept the same, but the copy will be faster each time it is run as only the changed chunks of a file are copied. This helps tremendously with limiting bandwidth usage. Rsync also works great over SSH – so file transfers are encrypted. Links can also be copied as they are.
Rsync also supports running as a daemon – so it can be used if SSH is not available.
Some of the important flags to use in Rsync are as follows :
-v, –verbose increase verbosity
-a, –archive archive mode; equals -rlptgoD (no -H,-A,-X)
-u, –update skip files that are newer on the receiver
–delete delete extraneous files from dest dirs
-z, –compress compress file data during the transfer
-e specify the remote shell to use
Example :
rsync -ave “ssh -p2222” /home/ :/home/
The above command will copy all files in the local /home directory to the destination server at IP address 10.0.0.1, it will use SSH and connect to SSH port 2222. The above command will push the files over, but it can also be used to pull them.
The above command will NOT remove any files that exist only on the destination server. For example, if a file called “index2.php” exists on the destination, but not on the source, it will remain on the destination.
If Rsync is used over SSH, then using SSH-Keys can allow the Rsync command to work without entering a password or passphrase, this will allows it to be run cron automatically. This can be a security risk, so please be aware of that.
Deletion
If you require the 2 directories to be identical, then the flag —delete should be used. This will delete any files on the destination that don’t exist on the source. So it will make the destination directory look exactly the same as the source.
Update
If you require the Rsync to not overwrite any newer files on the destination, then use the flag –update.
This will copy the files from source, but if the same file already exists on the destination and its newer, then it will not be copied. This is useful if file changes have been made on the destination.
Compression
The transfer can be compressed using the flag –compress. This will reduce the bandwidth of files that are compressible, e.g. plain text files, SQL dump files.
Delta-transfer
Files are broken into chunks and these are then compared between source and destination. This allows only those chunks that have changed to be copied. This feature makes Rsync very efficient and lowers bandwidth usage.