Table of Contents
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.
Why use rsync instead of SCP?
To copy files between servers you can also use SCP. The problem however with SCP is it’s not as flexible, for example, if doesn’t have a “do not overwrite if exists” mode, nor “do not overwrite if date is never”. Instead, you can use this example:
rsync -au ~/public_html/uploads/ fo[email protected]:/home/forge/dev.example.com/public/uploads/
u flag means do not overwrite if target is newer.
Another example of an
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
The flags means the following:
v = verbose
z = compress
r = recurse into directories
u = skip files that are newer on the receiver
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
-e. These means:
-e 'ssh -p PORT'
a = archive which preserves permissions
a switch is extremely useful as it will copy all the attributes, e.g. directory permissions and so on.
rsync in Real Time
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 ..."
man rsync produces:
-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)