You know how when someone accidentally deletes their files or their hard drive crashes or some other apocalyptic event occurs, the first thing people ask is "where is your backup"? Of course, we've all seen it (*ahem* been there :/ ). It's a bit unintuitive, because backups have no equivalent in the real world. If you drive your car into a lake, there's no way to get it back. But making backups is the single best way to prevent losing your stuff. So do it!
Don't backup "my files"
But don't just backup "my documents, my pictures, my whatever". If you computer crashes and you have a backup of "my files", then sure, it's not a total loss. It's better than nothing. But it's not what you actually need. You need the whole thing
This "my files" nonsense is born out of the fact that the delightful company that produced your operating system doesn't want you to be able to make a backup of it. Because if you did, you could make trivial copies of the operating system, and they don't like that idea. Have you ever asked yourself why in 30 years, through all manner of viruses, blue screens of death and hardware crashes Microsoft has never given you or sold you a full system backup program? It's not because they never thought of it. (Or because no one asked for it).
Making full backups is easy
If you've ever installed Gentoo manually (ie. not with one of the automated installers)... yes, the demographic for this one is not immense. But then that's why we're here, to spread the happy message! :happy: Anyway, if you have, then you know immensely easy (this is astonishing especially if you have a Windows background) it is to make a full system backup. In the course of a Gentoo install (and yes, I'm about to reveal the big secret here...*drumroll*), you boot from the livecd, you mount your root partition, you download a tar of a minimal Gentoo filesystem that has your basic /bin/ls
and so on, and then you just.... untar it. That's it. No magic, no voodoo, no secret foobared locations on the filesystem that can't be written to, just extract the archive and you're done!
To put it bluntly, this is all you have to do:
tar zcvf backup.tar.gz /
And to restore the backup:
tar zxpvf backup.tar.gz -C /mnt/root_partition
Put that in perspective to the Windows world where a whole industry has sprung up to solve problems that Microsoft deliberately introduced with their *ahem* novel engineering. Idiotic programs like Norton Ghost that you have to get your hands on just to do the same simple thing that you can do with tar
on a decent operating system.
Making it more convenient
Granted, you could just use the above tar command, but you may want something a little more convenient. For starters, you may want to skip some files on your file system. The method I use is inspired by a script posted on the gentoo forums a long time ago. I used that script for years without really understanding it, but a while back I decided to rewrite it to suit me better.
Besides just tarring the files it also writes a log file that you can grep
to see if some particular file of interest is in the backup, it timestamps the backup with the current date/time and it keeps track of how many backups you want to keep.
Backups are made in a special backup_dir
location. This directory is supposed to hold lists of files (recipes, if you like) you want to backup. For example, a simple recipe could be called full.lst
:
/
--exclude=/backup/*.tgz*
--exclude=/proc/*
--exclude=/sys/*
--exclude=/tmp/*
--one-file-system
The syntax for the file is that of tar
, and it's a list of things to backup. /
means the full file system will be included. But certain directories are excluded, /backup
because we don't want to include our old backup files in new backups, /proc
and /sys
because they are virtual file systems and don't contain "real" files, and we don't care about /tmp
. Finally, we say --one-file-system
, which prevents mounted disks, cds and things like that to be included.
And here is the script that makes this possible. Run it, it will produce a backup file that is compressed. Try to get it below 4.3gb and write it on a dvd+rw, now you have a backup system. :party:
#!/bin/bash
#
# Author: Martin Matusiak <numerodix@gmail.com>
# Licensed under the GNU Public License, version 3.
backup_dir=/backup
num_backups=1
verbose="$@"
lists=$backup_dir/*.lst
ext=tgz
date_params="%Y-%m-%d-%H%M"
nice_val="nice -n20"
# colors
wh="\e[1;37m"
pl="\e[m"
ye="\e[1;33m"
cy="\e[1;36m"
re="\e[1;31m"
if [[ "$verbose" && "$verbose" != "-v" ]]; then
echo "Usage: $0 [-v]"
exit 1
fi
if [ ! -d $backup_dir ]; then
echo -e "${re}Backup dir $backup_dir does not exist.${pl}"; exit 1
fi
for list in $(ls $lists); do
name=$(basename $list .lst)
file_root=$backup_dir/$name.$(date +$date_params)
stdout="1> /dev/null"
stderr="2> $file_root.$ext.err"
if [ "$verbose" ]; then
stdout=""
fi
cmd="cat $list | $nice_val xargs tar zlcfv \
$file_root.$ext $stderr | tee $file_root.$ext.log $stdout"
trap 'echo -e "${re}Received exit signal${pl}"; exit 1' INT TERM
echo " * Running \`$name\` job..."
if [ "$verbose" ]; then echo -e ${ye}$cmd${pl}; fi
echo -en $cy; bash -c "$cmd" ; echo -en $pl
status_code=$?
if [ $status_code -gt 0 ]; then
# Dump error log
echo -en $re ; cat $file_root.$ext.err
echo -en $pl ; echo "Tar exit code: $status_code"
else
# Kill error file
rm $file_root.$ext.err
fi
# Evict old backups we don't want to keep
num=$num_backups
for evict in $(ls -t $backup_dir/$name.*.$ext); do
if [ $num -le 0 ]; then
rm -f "$evict"
else
num=$(($num-1))
fi
done
# Report number of files in backup
echo -n "$(wc -l < $file_root.$ext.log) files"
echo ", $(ls -l $file_root.$ext | awk '{ print $5 }') bytes"
done
Worse is better
I've been thinking about how to handle backups most effectively, and it occurs to me that backups are a case of "worse is better". The thing is you could make a really nice and easy application to make backups, but .tar.gz
is still the optimal format to store them in. Wherever you are, you have tar
and gzip
available, and restoring backups usually happens under somewhat constricted conditions, sometimes without network access. So you want to avoid introducing dependencies, it's safer to make do with the tools that are there already.
So it may not be the most elegant system, but it's damn reliable.
Limitations (NEW)
Basically what I'm saying is that if you have no backup system then using tar is a pretty decent system. At the very least it has worked well for me the last 5 years. That isn't to say you shouldn't use a different method if you have different needs.
What about scaling? Well, I think this works quite well up to backups of say 4gb or so. My root partition is using 12gb of space at the moment. The purpose of this method is to back up your working system with all the configuration, applications and so on. Not to back up your mp3 collection, I would exclude that (not least because it's pointless to compress mp3 files and other formats that already are well compressed).
What about the bootloader? (NEW)
Some people have asked how this backup method concerns the bootloader. The answer is that it does backup the files that belong to the bootloader (in /boot
). It does not backup the actual boot sector of your hard drive (which isn't represented as a file). So if, for example, you want to restore the backup on another computer (which I've done lots of times), you'll still need to use grub/lilo to update the boot sector.
UPDATE: Apologies to the indignant Windows users. I pretty much wrote this in rant mode, without doing any research and what I wrote about Windows is just from my own experience. I would have been more thorough if I had known this would make the frontpage of digg and draw so many hits.