Thanks to a fun and productive weekend in Philadelphia with Nathan, I've finally written a script with which to perform regular backups of the important goodies from my server. Scheduling this to run regularly is left as an exercise for the reader, as is the decision of what to do with the tarballs so as to make the whole thing worthwhile. The script itself goes like so:
#!/bin/sh
BACKUP_IDENTIFIER="`hostname`"
BACKUP_STASH_SUBDIR="backups"
DEFAULT_TASKS="home mysql etc"
HOME_EXCLUDE_SUBDIRS="${BACKUP_STASH_SUBDIR} builds netbsd .netbsd"
ETC_DIRS="/etc /var/backups /var/cron /var/mysql"
MYSQL_DATABASES="schmonz schmonz_smf schmonz_textpattern"
MYSQL_USER="XXX"
MYSQL_PASS="XXX"
backup_home()
{
local tarball find_prune
tarball="$1"
find_prune="-path ''"
for i in ${HOME_EXCLUDE_SUBDIRS}; do
find_prune="${find_prune} -o -path ./$i"
done
cd && find . \( ${find_prune} \) -prune -o \! -type d -print \
| pax -wzf ${tarball} || die 1 "find/pax trouble"
}
backup_mysql()
{
local tarball sqlfile
tarball="$1"
sqlfile=`basename ${tarball} .tar.gz`.sql
cd ~/${BACKUP_STASH_SUBDIR}
mysqldump --opt -u${MYSQL_USER} -p${MYSQL_PASS} --databases \
${MYSQL_DATABASES} > ${sqlfile} || die 2 "mysqldump trouble"
pax -wzf ${tarball} ${sqlfile} || die 3 "pax trouble"
rm ${sqlfile}
}
backup_etc()
{
local tarball
tarball="$1"
sudo pax -wzf ${tarball} ${ETC_DIRS} || die 4 "pax trouble"
}
get_backup_tarball_path()
{
local task
task="$1"
echo ~/${BACKUP_STASH_SUBDIR}/`date '+%Y%m%d'`-${BACKUP_IDENTIFIER}-${task}.tar.gz
}
die()
{
local exitcode
exitcode=$0; shift
echo >&2 "error: $@"
exit ${exitcode}
}
main()
{
local tasks tarball_path
tasks=${DEFAULT_TASKS}
[ $# -gt 0 ] && tasks="$@"
for i in ${tasks}; do
tarball_path=`get_backup_tarball_path $i`
echo backup_$i:
backup_$i ${tarball_path}
echo ' '${tarball_path}
done
}
main "$@"
exit $?