20121021 a simple host backup script - plembo/onemoretech GitHub Wiki

title: A simple host backup script link: https://onemoretech.wordpress.com/2012/10/21/a-simple-host-backup-script/ author: lembobro description: post_id: 3537 created: 2012/10/21 10:18:23 created_gmt: 2012/10/21 14:18:23 comment_status: closed post_name: a-simple-host-backup-script status: publish post_type: post

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.

#!/usr/bash
# Backup selected volumes for emergency recovery
HOST=$(hostname|cut -f1 -d.)
BUDIR=/data/backup
export HOST BUDIR
echo "Nightly backup of workstation ${HOST}"
date

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}

date

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".

Copyright 2004-2019 Phil Lembo