#!/bin/bash #Fail on errors! set -e set -u ####################################################################### # /etc/cron.daily/backup - a script to backup one hd to # # another on the same machine, using dump # # (see the dump man page for details, and # # http://envgen.nox.ac.uk/envgen/software/archives/000501.html#auto # # # # Written by MIT, 20/6/2002 # # Modified by Dan Swan for Bio-Linux 2.0 17/3/2003 # # Updated for Bio-Linux 5 by Tim Booth 1/8/2008 # # Updated to perform Quarterly ETOH backups by Tony Travis 16/7/2009 # ####################################################################### # Source configuration file CONFIG=/etc/default/backup if [ -r $CONFIG ]; then . $CONFIG fi # Where to backup backup_destination=${DESTINATION:="/backups"} # What to backup backup_filesystems=(${FILESYSTEMS:="/ /home /var /usr"}) # Compression scheme. For large drives bzip2 is probably too slow. compression_mode=${COMPRESSION:="z"} # Dump level can be set on command line or by today's date. if [ -n "$*" ]; then level="$1" else # get the date day=`date +%a` month=`date +%b` dom=`date +%d` # Quarterly Enhanced Towers of Hanoi if [ $day == "Sun" ]; then if [ $dom -lt 7 ]; then case $month in "Jan" | "Apr" | "Jul" | "Oct") level=0 ;; *) level=1 ;; esac elif [ $dom -lt 14 ]; then level=3 elif [ $dom -lt 21 ]; then level=2 elif [ $dom -lt 28 ]; then level=4 elif [ $dom -le 31 ]; then level=3 fi else case $day in "Mon") level=6 ;; "Tue") level=5 ;; "Wed") level=8 ;; "Thu") level=7 ;; "Fri") level=9 ;; "Sat") level=8 ;; esac fi fi # dirs to back up - used to be: # backups=(hda1 hda2 hda5 hda6) # But now I need to be a bit more sophisticated. # List filesystems to back up and use mount to find where they are. backups=() for fs in "${backup_filesystems[@]}" ; do device=`mount | egrep "[^[:space:]]+ on $fs type ext[23]" | awk '{print $1}'` if [ "$device" != "" ]; then backups[${#backups[@]}]=$device fi done #DEBUG #echo ${backups[@]} $level $compression_mode #exit # if backup drive is already mounted then try to unmount it first. # if this is in use the step will fail and exit /bin/mount | grep -q "/backups" && /bin/umount /backups 2>/dev/null # mount necessary drive - must be listed # in /etc/fstab in order to be mounted /bin/mount "$backup_destination" 2>/dev/null || exit 0 # shut down MySQL during backup /etc/init.d/mysql stop # dump each directory, one at a time for item in "${backups[@]}"; do basename=`basename $item` # sync or swim sync /sbin/dump -"$level" -"$compression_mode" -n -u -f "$backup_destination/$basename.bak.$level" "$item" done # start up MySQL after backup /etc/init.d/mysql start # umount drive /bin/umount "$backup_destination"