Being new to Linux, I’ve been trying to discover the “right” way to do things without a) becoming an expert hacker overnight, b) installing and configuring a bunch of complex programs, c) destroying my computer.
Well, here’s one task down: Turns out there is a very easy way to do simple backups on Linux. There’s a command for it called rsync. rsync basically “syncs” the contents of one folder with the contents of another. If you have many gigs of data already synced, and have changed only a few files, rsync will very quickly update those few files, skipping over the unchanged files. Advanced rsync users can set up backups automatically and keep daily snapshots going back years so that if they want to know exactly what their computer looked like on Valentine’s Day 2008 at 9pm, it’s there under 2008/02/14/singlesblues/.
We’re not going to be so ambitious — at least, not yet. We’ll just assume we have an external hard drive that we can hook up to our computer (or leave hooked up), and trust ourselves to remember to back our stuff up every day or two. Every once in a while, we can save everything to a different folder and start backing up to a new directory.
Are you more ambitious than that? With scripting experience, you should check out this blog post on using rsync just like Mac’s Time Machine. Or try rsnapshot. But if you want to do it yourself, make one simple script and be done, here is how I did it.
We’ll be making a short script file so we can just type ‘backup’ into the command line and be done for the day. But our ‘backup’ command will really just be a wrapper for the function doing the dirty work: rsync.
The best way to get to know a Linux command like rsync is to go to the command line and type
man rsync. But I can give you the gist. Running
rsync -options /src /dest
syncs the contests of the folder /dest with the contents of /src, using the options passed in. Rsync has lots of options; check out the ‘man’ page for the full list. But ‘-a’, archive, turns on everything we need to make a backup. We can also pass in ‘-v’, verbose, to have rsync tell us everything it’s doing. Depends if you’re a backseat backupper, or if you trust rsync to do its thing.
I’m backing up data that is shared by Windows and Linux. Unfortunately, Windows saves the time modified of a file in multiples of 2. So I have to add a special option, –modify-window=1, to tell rsync not to update a file if its “last modified” time has changed by 1 second or less.
The Script (Scripts are easy — no experience required)
A script is just a collection of commands that you run all at once, rather than typing them in to the command line. Here’s what my “backup” file looks like:
#!/bin/bash # Back up /home and /media/Data folders to an external HD src="/home" dest="/media/BO'S DATA/Backups/Bo-PC/Current" rsync -av --modify-window=1 "$src" "$dest" src="/media/Data" rsync -av --modify-window=1 "$src" "$dest"
This is the file “/home/bo/usr/bin/backup” (no file extension). You can store yours in /usr/bin or /home/username/usr/bin. No matter.
The first line is called a shebang line. Script files all have it, and it tells the computer what program to use in running the script. This script runs in bash.
We define two variables, src and dest. We then rsync from src to dest. I do this twice, because I’m saving two different folders: my /home folder and my /media/Data folder where I keep all my documents etc. My external hard drive is called BO’S DATA.
Notice I’m backing up into a folder on my external called “Current.” If I decide to save a snapshot, I can rename “Current” to “Backup-on-2011-02-21”. Then I create a new “Current” folder. The next time I run ‘backup’, it’ll fill that folder with my data and then will continue rsyncing as usual. This is slow: a new backup of 50 gigs or so of data takes around a half hour. It’s also costly in memory. But I don’t really delete stuff off my computer, and I don’t care what the contents of my browser cache were on Valentine’s night 2008, so I probably won’t leave too many of these around at a time anyway.
A word of caution: You should probably not set src = “/” and try to back up your whole filesystem. I’ve heard it doesn’t work. If you have several folders you need to back up, just copy and paste the rsync lines and change the value of ‘src’ each time. Also, by the bye, it’s normal to get a lot of “operation failed” notifications when backing up /home/ for files like symlinks or files for which you don’t have permissions set. That’s fine — the important thing is to backup your documents.
That’s it! Let me know if this worked (or didn’t) for you!
Link of the day: I didn’t use a single one of these today.