Use rsync for a simple incremental backup of a folder

  • March 23, 2008
  • Avatar for peter
    Peter
    Upfold

rsync is a very useful and powerful program for doing incremental transfers of files, whether that is locally or remotely. It can, however, take a little while to familiarise yourself with how rsync works before you can get started with it however.

In this quick tip, I'll show you how to do a simple incremental local backup of a folder on your system.

So, say I have a folder full of important OpenOffice documents, that I want to backup to my USB memory stick so I have an up-to-date copy of them wherever I go. My source folder here is ~/Documents and my memory stick is mounted at /media/disk right now.

Initially, you will have to use rsync to copy all the files across, since an incremental copy doesn't yet exist. Let's do that now:

$ rsync -avh ~/Documents /media/disk

That sets up an archive copy. Depending on how much stuff you have and where you're copying, it might take some time, but the verbose option we used (-v) should show you the name of each file as it is copied over.

Once that's done, you've got your initial copy done. Now when you need to synchronise the two copies, simply run the same command again. This time, rsync will go off and find only the files that have changed, and copy those over. When it has finished, both copies will then be up-to-date, and you can get back on the move.

This particular method won't delete files from either copy when you delete them from another. Put another way, if I create Document3.odt on my memory stick, then sync back to my computer, but then later delete it from the memory stick, it will remain on my computer.

If you'd prefer both copies to remain completely synchronised, even if that means deleting files from either copy, add the --delete option, like so:

$ rsync --delete -avh ~/Documents /media/disk

Do be careful with that option, however, as you don't want to be deleting things you might need.

That's it for this quick tip!

Avatar for peter Peter Upfold

Home » Articles »