Background
rsync is powerful and easy to use utility to keep files in sync between two servers. The principal way of rsync‘s command structure is to push files from point A to point B.
A Basic Example of an rsync
Command
rsync -vzru /home/location_A/ [email protected]:/home/location_B/
In the command above, files from location_A
on a local disk and server is `pushed` to location_B
on server2
.
The flags means the following:
v = verbose
z = compress
r = recurse into directories
u = skip files that are newer on the receiver
The r
is really useful because it means all directories and subdirectories will be copied.
u
means that it won’t copy files again which are the same as the destination receiver
Rsync Example with Custom SSH Port and Preserve Directory Permissions and Attributes
rsync -vrzua -e 'ssh -p 34229' /home/location_A/ [email protected]:/home/location_B/
In the above example, two additional flags were added, namely a
and -e
. These means:
-e 'ssh -p PORT'
a = archive which preserves permissions
The a
switch is extremely useful as it will copy all the attributes, e.g. directory permissions and so on.
Using rsync
in Real Time
Typically rsync
commands are stored in a CRON
to run every XX minutes.
If you want continuous rsync
, and you are well aware of what it’s going to do to your network traffic, then you need to use an utility called flock
because running one rsync
if the previous one hasn’t completed will cause problems.
Here is an example of the flock command:
flock -n lock_file -c "rsync ..."
References
man rsync
produces:
-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)