rsync stands for remote sync, it’s an utility to synchronize (or copy) the files and directories from one location to another. rsync uses incremental « update » - an algorithm that minimizes the amount of data copied by only transfering the differeneces between sets of files across the network connection.
rsync -azhP dir1/ dir2
-aoption - it stands for « archive » i.e. it syncs recursively and preserves symbolic links, special and device files, modification times, group, owner, and permissions.
-zoption - it adds compression
-Poption - it combines the options
-hoption - it outputs numbers in a human-readable format
COPY/SYNC TO A REMOTE LOCATION
Pushing the contents of dir1 to a dir2 on a remote machine remote1.
rsync -a ~/dir1 remote1:dir2
Pulling the contents of dir2 on a remote machine remote1 to a local directory dir1.
rsync -a remote1:dir2 dir1
KEEP DIRECTORIES SYNCHRONIZED
In order to keep two directories synchronized, it's also necessary to delete files from the destination directory once they are removed from the source. This is not rsync default behaviour though, rsync does not delete anything from the destination directory.
--delete option can be used to overwrite this and allow full directories synchronization.
rsync -a --delete source destination
-noption - it reports the actions it would have taken but
rsyncdoes not transfer any files
--max-sizeoption - it tells
rsyncnot to transfer files that are greater than a specific size
--excludeoption - it tells
rsyncto exclude specified files or directories (comma-separated list) located inside a directory being synchronized
SYNC OVER A SPECIFIC SSH PORT
rsync -azP -e 'ssh -p <port number> ' source destination