A Quick One-Line Backup

Fri, Jul 17, 2020
5 min read

Because hardware can fail, because software is buggy, and because I am me and I do a lot of mistakes, I do backups. Even more here, as a new user under Linux: I know I’ll break things up.

There are some great dedicated tools to do backups. Like Deja Dup Backup Tool (deja-dup) to automate backups of your home directory, or any other folder — incremental backups, with optional encryption and a list of included/excluded files and folders — and TimeShift that is like Apple’s Time Machine but for the system (not for your files), making it dead easy to restore your installation to any point in time.

These will probably be the tools I’ll end up using later on, but for the time being that is not what I need.

For the moment — even if it only happened once in a month or so — I know I’ll often break stuff while trying new things and since I’ve almost no epxerience I know I can’t repair even simple things and I will have to reinstall everything from scratch to avoid wasting too much time. So, what I need is a way to quickly reinstall all my personal files and all my configurations and tweakings, the only things that matter to me since réinstalling Linux itself is a matter of maybe 15 or 20 minutes. So, I need a way to quickly backup all my files. I don’t need something automated but something I can run when I need it, aka just before trying something new or risky, just in case. Something that is fast, and easy to use and to remember. Something like this:

rsync -a --delete --exclude '.mozilla' --exclude '.cache' --exclude 'Vivaldi' /home/david/ /run/media/david/64/bkp_david/

OK, this is abolutely not easy to use or to remember. The thing is that I don’t need to, that’s where the magic is. But let’s first try to read this odd line and understand what it does.

  • rsync is the command that deals with the r(emote) sync(hronization). It is the smart copy command that will copy a folder (teh source) to somewhere else (the destination). Here, it’s instructed to copy my home folder (/home/david/) to a folder on some external USB storage, in a folder cleverly named bkp_david (/run/media/david/64/bkp_david/). Rsync is quick by default, since it will only copy what has changed since the last time it was run (the first backup will be longest one, since it needs to copy everything).
  • -a is one of the many parameters/options rsync can accept. It is a shortcut for --archive that tells rsync to do some preconfigured stuff with all the files and folders it will copy in order to make sure it doesn’t break anything and that the files and folders are exactly like their originals.
  • --delete, tells rsync to delete whatever it finds in the destination folder that is no longer in the source folder. So, I know everytime I run it, my backup will be an exact copy of my home folder, containing the same files and folders. To be precise, it will be an almost exact copy:
  • The three --exclude thingies tell rsync to ignore specific folders (it could be files too) I don’t want to backup. You can exclude as many thing as you like but each one needs to have its own –exclude command. In my case, the three folders excluded contain temporary stuff or stuff that will often change, that would waste a lot of space on my backup and that would make the backup much slower for no good reason since I don’t need to backup those folders. .mozilla and Vivaldi are tempory folders for my two browsers (Firefox and Vivaldi) and Cache is, well, a cache folder. And together they can be huge.

Are yout talkin' to me? (make rsync tells you what it does)

As it is now, the rsync command I use will display absolutely nothing. It will run then it will close, telling me nothing about what it did. I don’t need it to, since I know it works. But if you’re new to rsync it may be a good idea to see what it does while it’s doing it.

To make it much more more talkative, add a -v or --verbose parameter, maybe like this:

rsync -a -v --delete --exclude '.mozilla' --exclude '.cache' --exclude 'Vivaldi' /home/david/ /run/media/david/64/bkp_david/

Or, since single letter paramaters can be joined together, like this:

rsync -av --delete --exclude '.mozilla' --exclude '.cache' --exclude 'Vivaldi' /home/david/ /run/media/david/64/bkp_david/

Now, each time you’ll run the command, without slowing it down the slightest, rsync will list every file and folder it’s working with.

Great but how do you run this aslmost unwritable command?

Excucting the command

You could type it — just kidding. There is two ways to manage such a command: save it as a script, and then run the script. Or, and its probably faster since it’s a really short command, make it an alias in your ~/.bashrc (the file where your Terminal keeps most of its settings). Like this:

alias bkp="rsync -a --delete --exclude '.mozilla' --exclude '.cache' --exclude 'Vivaldi' /home/david/ /run/media/david/64/bkp_david/"

Reload your bashrc — type source ~/.bashrc in a Terminal — and that’s it. From now on, you just need to remember those three letters, bkp (or whatever name you fancy), and to type them in any Terminal window for all of your home directory, files and settings, to be copied to your backup storage, in a matter of seconds or minutes depending the volume of data that had changed since the last time you ran the backup.

Ok, reading it one last time before I publish it, I realize my title is misleading: this is not a one-line backup, there are two lines. I’m a liar.