This is simplified version of script I'm using for doing my backups. I have prepared it only recently because manual invocation of rsync was enough for me. But as the number of separately rsynced folders increased I was forced to introduce some automation and I had to solve some interesting problems along the way.
Normally a separate SSH connection is established for each rsync invocation.
That may become irritating if, for some reason, establishing an SSH connection requires
manual intervention (e. g. password, touch id...). A solution is to set up
multiplexing, which is available in OpenSSH. Connection
is then set up only once (see ControlMaster=yes
) and rsync is then
forced to use that connection (see --rsh
argument). It's important
to close the connection at the end of the script (see ssh -O exit...
).
Sometimes (after huge changes in backuped data or on slow network) it may be
better not to run backup over network, but instead connect the storage itself
directly to backuped computer and do the backup over fast local interface
(USB 3, SATA...). Rsync encodes storage type directly in source and target
argument so it's not necessary to create separate scripts for those situations
(see TARGET
variable).
Rsync is sensitive to zero length arguments (""
) which most of the time triggers
unintentional synchronization of current working directory. This means it's
problematic to use conditionaly empty command line arguments (see variable
DRY_RUN
). Solution is to use eval
shell builtin. This also
allows to have multiple arguments stored in one variable (see variable OPTIONS
).