A simple host backup script

This is an adaptation of something I’ve had running on my Linux boxes at home for a while. Hope others find it useful. NOTE: Updated to make a bit more portable.

The idea here is to make something simple and portable. This really only makes sense if the /data/ volume is on second hard disk or mount point (e.g. NFS or SMB). For this particular machine it is in fact all on one local disk, but a separate rsync job will be scheduled to archive these files to another server (a NAS device on the network). For now these backups just provide a convenient set of compressed files for recovery.

# Backup selected volumes for emergency recovery
HOST=$(hostname|cut -f1 -d.)
echo "Nightly backup of workstation ${HOST}"

cd /

ls -l ${BUDIR}/${HOST}

echo root
tar czf ${BUDIR}/${HOST}/${HOST}_root.tar.gz etc 2>&1

echo usr
rm -f ${BUDIR}/${HOST}/${HOST}_usr.tar.gz 2>&1
tar cf ${BUDIR}/${HOST}/${HOST}_usr.tar usr/share/applications 2>&1
tar uf ${BUDIR}/${HOST}/${HOST}_usr.tar usr/share/pixmaps 2>&1
gzip ${BUDIR}/${HOST}/${HOST}_usr.tar 2>&1

echo local
tar czf ${BUDIR}/${HOST}/${HOST}_local.tar.gz usr/local 2>&1

echo var
rm -f ${BUDIR}/${HOST}/${HOST}_var.tar.gz 2>&1
tar cf ${BUDIR}/${HOST}/${HOST}_var.tar var/www 2>&1
tar uf ${BUDIR}/${HOST}/${HOST}_var.tar var/named 2>&1
tar uf ${BUDIR}/${HOST}/${HOST}_var.tar var/lib/mysql 2>&1
tar uf ${BUDIR}/${HOST}/${HOST}_var.tar var/spool 2>&1
tar uf ${BUDIR}/${HOST}/${HOST}_var.tar var/cache 2>&1
tar uf ${BUDIR}/${HOST}/${HOST}_var.tar var/log 2>&1
gzip ${BUDIR}/${HOST}/${HOST}_var.tar 2>&1

echo opt
tar czf ${BUDIR}/${HOST}/${HOST}_opt.tar.gz opt 2>&1

echo app
rm -f /${BUDIR}/${HOST}/${HOST}_app.tar.gz 2>&1
tar cf ${BUDIR}/${HOST}/${HOST}_app.tar data/app/opendj/ds-user1 2>&1
tar uf /${BUDIR}/${HOST}/${HOST}_app.tar data/app/apache/tomcat6 2>&1
tar uf ${BUDIR}/${HOST}/${HOST}_app.tar data/app/calibre 2>&1
tar uf ${BUDIR}/${HOST}/${HOST}_app.tar data/app/tekkit 2>&1
gzip ${BUDIR}/${HOST}/${HOST}_app.tar 2>&1

echo home
tar czf ${BUDIR}/${HOST}/${HOST}_home.tar.gz home 2>&1

ls -l ${BUDIR}/${HOST}


Most Korn and Bourne derived shells recognize braces, “{ }”, as delineating shell variables. It’s a good idea to use them whenever combining variable text with symbols or other characters (like “_”) to avoid confusing the shell into thinking you’re declaring a new variable.

Not sure why but like many sysadmins (or former sysadmins in my case) I actually like doing shell programming — even though even something as simple as this script could probably be written just as easily (or more easily?) in perl. I guess there’s a certain amount of satisfaction to be derived from being “bi-lingual”.