Rsync retrospective!
I didn’t fully appreciate how amazing the rsync command is. This page is to quickly highly why the tool is special and some of the more nuanced features.
Significant time saving
Comparing a cp or a mv, rsync can detect whether the file at the destination is the same as source. For large files and over slow networks this pays large dividends.
For example if I wanted to transfer 1TB over a slow network, rsync can identify whether the files are the same. If same, no need to re-transfer!
Ability to copy to different machines
rsync -vaPh /mySrcFiles username@192.168.56.100:~/Destination
This is very useful for example when backing up files.
Compression
I typically leave compression turned off as most of the data I transfer is already compressed. However, if dealing with both uncompressed and very slow networks, this could add value.
Dry Run
This is particularly important when using –delete. Large damage can be caused if not being careful with this one. dry-run should be used to avoid potential snafus.
How does it detect changes?
I encountered a situation where the src and dest contained the same files, but rsync wanted to recopy all the data. To avoid having to spend 24h re-copying, I worked out that although the data on the drives was identical, the timestamps didn’t match and this is how rysnc was estabilishing differences.
To resolve this, it’s possible to only copy the timestamp data over and not the actual data. Once that was done, a standard rsync copy saw the data as the SAME.
rsync -vrt --size-only /src /dest
tldr explanation for rsync
rsync
Transfer files either to or from a remote host (not between two remote hosts).
Can transfer single files, or multiple files matching a pattern.
More information: <https://manned.org/rsync>.
- Transfer file from local to remote host:
rsync path/to/local_file remote_host:path/to/remote_directory
- Transfer file from remote host to local:
rsync remote_host:path/to/remote_file path/to/local_directory
- Transfer file in [a]rchive (to preserve attributes) and compressed ([z]ipped) mode with [v]erbose and [h]uman-readable [P]rogress:
rsync -azvhP path/to/local_file remote_host:path/to/remote_directory
- Transfer a directory and all its children from a remote to local:
rsync -r remote_host:path/to/remote_directory path/to/local_directory
- Transfer directory contents (but not the directory itself) from a remote to local:
rsync -r remote_host:path/to/remote_directory/ path/to/local_directory
- Transfer a directory [r]ecursively, in [a]rchive to preserve attributes, resolving contained soft[l]inks , and ignoring already transferred files [u]nless newer:
rsync -rauL remote_host:path/to/remote_directory path/to/local_directory
- Transfer file over SSH and delete remote files that do not exist locally:
rsync -e ssh --delete remote_host:path/to/remote_file path/to/local_file
- Transfer file over SSH using a different port than the default and show global progress:
rsync -e 'ssh -p port' --info=progress2 remote_host:path/to/remote_file path/to/local_fil