Saturday, June 6, 2015

How to backup your stuff with rsync

As many other people, I have a USB hard disk (let's call it UPDATED) where I store some of my data organized through a hierarchy of folders (docs, notes, projects and the like). A very simple but effective way to keep your data safe is to buy another USB hard disk (let's call it OUTDATED) of the same or greater size and synchronize it from time to time. Personally, I like this flow:
  • Create the data (e.g.: a text file) and leave it in my Linux desktop PC.
  • When I feel that some of that data contains important information, I save it into the UPDATED disk. Also, I move the data into a folder called RECORDED (in my PC) just to know that I already saved it.
  • Sometimes I like re-organizing my UPDATED hard disk so that things are easier to find or to make sure that folder names are coherent.
  • Finally, from time to time (usually every few months) I synchronize the so-called OUTDATED hard disk so that it perfectly mirrors the UPDATED disk.
The last step is the most involved so I use the rsync tool. This tool is quite dangerous so I suggest to start by defining the paths to the UPDATED and OUTDATED hard disks, and making sure they are not the other way around:

export UPDATED=/media/4BF0-F436_/
export OUTDATED=/media/4BF0-F236/


Next, run rsync in simulation (dry) mode in order to check which files will be deleted (or updated):

$ rsync -vanu --delete $UPDATED $OUTDATED | grep deleting

After making sure that everything is as you wanted, apply it for real:

$ rsync -vau --delete $UPDATED $OUTDATED

The options I am using here are:
  • -v verbose mode
  • -a archive mode (preserves owner, timestamps, etc)
  • -u skip files that are newer in OUTDATED (if there were)
  • -n dry-run (to try without doing)
  • --delete deletes files from OUTDATED that are not in UPDATED
There are many other ways to backup your data (automatic, encrypted, or online to name a few), but I feel comfortable with this one because of its simplicity.

No comments: