Breakdown of the 'scpr' alias
As a follow-up to ‘making rsync easy’, here’s a breakdown of what the options do.
-P — Shows the progress of the transfer, and keeps partially transferred files. Useful if you’re on an unstable connection, as you can just run the same command again if the transfer fails.
-h — Outputs numbers in a human-readable format.
-a — ‘Archive mode’.
-v — Verbose output.
-z — Compress the data being transferred.
-e ssh — Use ssh when sending data over a network.
--modify-window=1 — This option allows timestamps to differ by up to one second. It’s necessary if you ever need to copy to or from FAT partitions, which represent timestamps with 2-second resolution.
Making rsync easy
rsync is an invaluable tool for copying or uploading files. From the manual:
It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. … It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination. Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.
It’s incredibly useful, but it can be rather daunting to use for simple tasks, due to the overwhelming number of options available. Because of this, I have written a general purpose alias for bash that takes the effort out of most rsync jobs.
Add this to your .bashrc file:
alias scpr='rsync -Phavze ssh --modify-window=1'
This will give you a simple rsync source/ destination/ command, which will copy everything from source to destination. No need to worry about which files have already been copied; rsync will only change what’s needed.
For example, the following command might be used to update a user’s website:
$ scpr ~/Sites/"my awesome website"/ user@host:www/
The --delete option can be added to remove files at the destination that do not exist at the source. The command scpr --delete x/ y/ essentially means “make y an exact copy of x”. Use with care!
(Note: Omitting the trailing slash from the source folder alters the behaviour of rsync. See the ‘USAGE’ section in the rsync manual. If you’re unsure, just leave it in.)