devork

E pur si muove

Backup script in 43 lines

Saturday, March 24, 2007

This is no more then my personal backup prefference at this moment. It is full of personal preferences and might not be what other people like. It also isn't written with portability in mind. But I think it is rather elegant.

My use case: on Friday evening I get home, switch on my laptop and boot into single user mode. I attach an extrenal hard drive to it and power that on too. Once logged in (as root, we're still in single user mode) I type mkbackup.sh && shutdown -h +5. When I get up in the morning all should be fine i.e. the laptop is switched off, but if it's not the laptop will still be running and I can investigate what went wrong.

Here the script:

#! /bin/bash

set -e -x


#### CONFIGURATION ITEMS ####
# The UUID of the filesystem the backup lives on.
UUID=5ffcbedc-f49d-4e17-8b07-88f6b62247f6
# Subdirectory on the backup media to store backup in.
SUBDIR=backups
#### END CONFIGURATION ITEMS ####


# Read which filesystems to backup from /etc/fstab.
SFS=''
while read filesystem mountpoint type options dump pass; do
        grep -qs '^$' <<<$filesystem && continue
        grep -qs '^ *#' <<<$filesystem && continue
        test $dump -eq 1 && SFS="$SFS $mountpoint"
done < /etc/fstab

# Mount backup media when needed.
do_unmount=false
if ! grep -qs /dev/disk/by-uuid/$UUID /proc/mounts; then
        mount -U $UUID
        do_unmount=true
fi

# Prepare file and directory variables.
dir_prefix=$(grep -s /dev/disk/by-uuid/$UUID | cut -d ' ' -f 2)
ddir=$dir_prefix/$SUBDIR
file_prefix=$(date --utc --rfc-3339=date)

# Do the backup.
for $fs in $SFS; do
        name=tr -d '/' <<<$fs
        find $fs -xdev | afio -o -Z $ddir/$file_prefix_$name.afio.Z
done

# Unmount backup media when we did mount it.
if $do_unmount; then
        umount $dir_prefix
fi

For completeness I should be showing my /etc/fstab too:

# /etc/fstab: static file system information.
#
# <file system>  <mount point> <type> <options>                    <dump> <pass>
## Virtual filesystems:
proc             /proc         proc   defaults                         0      0

## Local filesystems:
/dev/hda1        /boot         ext3   defaults                         1      2
/dev/signy/swap0 none          swap   none                             0      0
/dev/signy/root  /             ext3   defaults,errors=remount-ro       1      1
/dev/signy/home  /home         ext3   defaults                         1      2

## Removable media:
/dev/hdc         /media/cdrom0 udf,iso9660 user,noauto                 0      0
UUID=5ffcbedc-f49d-4e17-8b07-88f6b62247f6 /media/icybox ext3 noauto    0      0

## Chroot stuff
#proc         /chroots/sid/proc        proc   none                     0      0
#/home/flub   /chroots/sid/home/flub   ext3   bind                     0      0

The only thing that I'd like to figure out is how to switch off that external hard drive too, some sort of syscall I assume.

Subscribe to: Posts (Atom)