rsync: copy files the intelligent way

http://yalneb.blogspot.com.es/2017/05/rsync.html

You probably heard about last Friday's massive cyber attack that affected millions of outdated machines that still run Windows XP, but also newer version. Luckily the pandemic got halted almost by sheer luck (read more at hackaday).

Anyway, it could have affected any of us (maybe dual-boot?), which alerted me of the fact that I had not backed up my files for quite some time now. This gave me two options. Either plug in my external backup HDD and copy everything with my file browser (overwriting old files), or do it the intelligent way: an incremental update to my backup with rsync to only copy modified files.

rsync is an open source utility that provides fast incremental file transfer. its main advantage over simply copying files with a file browser is that is is capable of skipping files that are already present at the destination (i.e. continue an interrupted copy process) and deleting files at the source which are not present at the source (i.e. updating an old system backup). Making it a great choice to copy large and highly nested folder structures to a remote system without fear of connection loss, or easily keep an updated system backup.


How to use rsync 


$ rsync [options] [origin] [destiny]

Interesting options are:

-a, --archive: archive mode; equals -rlptgoD.
-r, --recursive: recurse into directories.
-l, --links: copy symlinks as symlinks.
-p, --perms: preserve permissions.
-t, --times: preserve modification times.
-g, --group: preserve group.
-o, --owner: preserve owner (super-user only).
-D, --devices: preserve device files.

--delete: delete extraneous files from destiny dirs. Useful to update an existing backup.

-z, --compress
: compress file data during the transfer. Use only if your connection is more constricted that you CPU power on both sides. So DON'T use this for an USB drive.

-h, --human-readable
: output numbers in a human-readable format.

-P, --progress
: show progress during transfer.

-b, --backup
: preexisting destination files are renamed as each file is transferred or deleted. Useful to keep a detailed backup over time by creating individual "snapshots", but keep in mind that it grows with each new commit.

-v, --verbose
: increase verbosity and provide a summary after syncing.

My preferred way of using rsync is with the options -avhP, which comes very handy to manage large backups while visualizing what is being copied.


Remote rsync-ing


Copying to a remote server is as easy as the following example. Suppose we have a local folder `/home/andy/photos/` and a remote folder `/home/backups/` at a server located in our LAN at 192.168.1.20 (where my user name is admin). If you want to copy your photos over, simply:

$ rsync -avhP /home/andy/photos admin@192.168.1.20

2 comments :

  1. Hey Andres, thank you for this post. The manpage for rsync is a pain to read through. I don't have enough reputation on ServerFault to post, so I posted here.

    ReplyDelete
    Replies
    1. Hi! I'm glad you found this cheatsheet useful. But yeah, there is even a worse manpage out in the wild, the one for "tar" jajajaja

      I don't post as much as I'd like to, but stay tuned for more linux-voodoo ;)

      Delete