rsync Cheat Sheet

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.

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/ [email protected]:/home/forge/dev.example.com/public/uploads/

The u flag means do not overwrite if target is newer.

Another 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)

Other references

See also

  • https://kb.vander.host/operating-systems/how-to-get-rsync-to-not-overlap-because-the-previous-job-has-not-completed/

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top