Set up backups in Linux

This page is outdated.
Please see http://www.eng.famu.fsu.edu/~dommelen/backup/ for improved scripts.

Having solid backups is essential for anyone who has used computers for some time. This describes how I do them. It may give you some ideas, but it is not by any means a turnkey solution.

First consider what can be called solid backups. Suppose you have a USB disk with a folder 1/ on it, and on Sundays you (or rather the system does it for you) copy the entire contents of your hard disk to that directory. If your hard disk crashes on Thursday, or you get a disabling virus, or you mess up your system hopelessly (did you login as root or use sudo?), or you lose just a single file, but an important one, you can restore your system, or just that one file, to the exact state it was on on Sunday.

But what about the files you created on Monday through Thursday? They would be gone. To avoid that, you can have the system do daily (or rather nightly) incremental backups. They simply add to Sunday's backup only the files that were added or changed since Sunday. With incremental backups, only Thursday's files will be gone. Well, neither you nor the system can do backups every minute. (Actually, it is possible to do continuous backups, but I find it intrusive.)

Now what happens the next Sunday? The system will clean out the folder 1/ to start the new week's backup. So, if your hard disk decides to crash during the new backup you are left with no system disk and no backup. To avoid that, you want to move the previous week's backup to another folder 2/ before doing the new full backup. This has the added advantage that you now have backups going back up to two weeks in time. Sometimes you do not immediately realize you have lost a file or have a virus or system problem.

I find it does happen on occasion that I want to go back way in time to files that I optimistically "cleaned up", or that my DVD has been having a problem with write for many months. So I want backups that go way back in time. Now if I just save every backup that is removed from 2/ in a directory 3/, every one removed from 3/ in a directory 4/ and so on to directory 6/, I still only save about the last 6 weeks of backups. But instead I can save only every second backup that I remove from 2/ to 3/. That makes the backup in 3/ about 4 weeks old. And I can only save only every second backup that I remove from 3/ in 4/ making that one about 8 weeks old. Then if I only save every third backup from 4/ in 5/, that one will be about half a year old. And so on. It produces a set of backups going back way in time.

Since I did not know any software that does that in a way that I can easily control, I wrote my own shell script for it. (It needs packages tcsh and dar_static installed.) The template is backtemplate. This template must be edited to reflect the machine that is actually being backed up.

The edited version that backs up almost my complete machine is script backup. Downloaded package files are backed up with a separate script backapt. Looking at them might give you an idea how you could adapt them to your machine. But they are not a turn-key solution. To use them you must read them, understand them, and change them for your system.

One note: on Debian I found internal e-mail to be correctly set up using exim4. It allows the system to send the user an e-mail warning if a backup fails. On Ubuntu, Postfix was not operational, and simply starting it up did not produce a working internal e-mail system. Therefor I left the e-mail address blank. The scripts will still put icons on your Desktop telling you the status of your backups, and FAILED icons if something goes wrong, but I do think an e-mail is so much more invasive. If you have a simple procedure to enable internal e-mail on Ubuntu, let me know. On Debian however, the system is set up not to send mail to root, and there I login as root. To fix that up, create a mailbox for the non privileged user and link that to root:

set a > /var/mail/dommelen
ln -s /var/mail/dommelen /var/mail/root

I have also a script backsct that backs up the contents of the encrypted disk mounted on /sct to an encrypted backup file. It requires files dar.dat and darn.dat to be on the encrypted disk. Maybe this is paranoid. But there is nobody home during the day, so it is conceivable that someone would steal both computer and backup disk.

Also, you will need the Fortran executable that does the basic computations to ensure that the right backups are kept at each stage (through setting the right filename.) On 32 bit Intel kernels you can use backupex, on other machines you will need to compile the source backupex.f See the notes on the discontinued g77 compiler in Linux. In Linux, you will have to compile it something like

gfortran -o backupex backupex.f iargc.f getarg.f
Place the adjusted backup script(s) and backupex in folder ~/bin. From a terminal
mkdir ~/bin  (if it does not yet exist)
mv wherever/backup ~/bin
mv wherever/backupex ~/bin
chmod u+x ~/bin/backup ~/bin/backupex
and so on for the other scripts.

Then you must tell the system to execute the backup script(s) on the right times. And if you are not logged in as root, you must tell the system to run the backup as root, because there are many files that only root can access. In a terminal window

sudo -u root crontab -e -u root
Add:
HOME=/home/username
  0 1 * * * /home/username/bin/backapt auto
 30 1 * * * /home/username/bin/backsct 1
  0 2 * * * /home/username/bin/backup 7
where for username you substitute your username on the computer. If you login as root, use /root instead of /home/username, and the HOME line is not needed. In order of appearance, the first line tells the system that at 0 minutes after 1 am on each day script backapt auto must be run, that at 30 minutes past 1 am backsct 1 must be run, and at 2 am backup 7. Backapt auto will do full backups only when it runs out of incremental ones. Backsct 1 will do full backups on Mondays and backup 7 on Sundays, with incremental ones the other days.

So what if my computer and backup disk get stolen or my house burns down? Periodically, I take my computer to work and backup to the backup disk there using the command backup /i new where /i is the mount point of that disk. The same way I keep a backup of my work computer at home. I am not sure what to do if someone throws an atomic bomb on Tallahassee and both places get destroyed. It is the capital of Florida, you know. Then again, I would probably not be around to care.


Applies to software obtained May 2009.
Return to Linux on Lifebook S6520