Skip to content

Instantly share code, notes, and snippets.

@jlogsdon
Created February 26, 2010 19:13
Show Gist options
  • Save jlogsdon/316042 to your computer and use it in GitHub Desktop.
Save jlogsdon/316042 to your computer and use it in GitHub Desktop.
#!/bin/bash
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
BDIR=/mnt/backup # Backup directory
TDIR=/var/trac # Trac directory
SDIR=/var/svn # SVN directory
MDIR=/var/lib/mysql # MySQL directory
HDIR=/home # Home directory
DATE=`date +%Y-%m-%d`
TOTALSIZE=0
DRYRUN=0
while getopts ":dl" option; do
case $option in
d) DRYRUN=1;;
esac
done
LFILE=${BDIR}/${DATE}/backup.log
# Shortcut to prefix a timestamp to a message
function log {
TIME=`date +%H:%M:%S`
if [[ $DRYRUN -eq 1 ]]; then
echo "[${TIME}] ${@}"
else
echo "[${TIME}] ${@}" >> $LFILE
fi
}
# INIT
if [[ $DRYRUN -eq 0 ]]; then
df -h | grep ${BDIR} >> /dev/null
MOUNT_STATUS=?@
if [ $MOUNT_STATUS == "1" ]; then
log "${BDIR} is not mounted. Mounting..."
mount ${BDIR}
fi
# Now we're going to check to make sure the backup drive is actually mounted so we
# don't fill up the / partition...
if [ ! -d ${BDIR}/lost+found ]; then
log "${BDIR} is still not mounted. Exiting."
exit
fi
BDIR=${BDIR}/${DATE}
BSDIR=${BDIR}/svn
BTDIR=${BDIR}/trac
BMDIR=${BDIR}/mysql
BHDIR=${BDIR}/home
if [ ! -d ${BDIR} ]; then
mkdir ${BDIR}
fi
if [ ! -d ${BSDIR} ]; then
mkdir ${BSDIR}
fi
if [ ! -d ${BTDIR} ]; then
mkdir ${BTDIR}
fi
if [ ! -d ${BMDIR} ]; then
mkdir ${BMDIR}
fi
if [ ! -d ${BHDIR} ]; then
mkdir ${BHDIR}
fi
fi
# END INIT
# TRAC BACKUP
log "Creating backup of Trac projects"
for PROJECT in `ls ${TDIR}`; do
if [ -f $TDIR/$PROJECT/VERSION ]; then
SIZE=`du -sb ${TDIR}/${PROJECT} | cut -f1`
let "TOTALSIZE += ${SIZE}"
log " ${PROJECT} (${SIZE})"
if [[ $DRYRUN -eq 0 ]]; then
trac-admin ${TDIR}/${PROJECT} hotcopy ${BTDIR}/${PROJECT}
fi
fi
done
# END TRAC BACKUP
# SUBVERSION BACKUP
log "Creating backup of Subversion projects"
for REPO in `ls ${SDIR}`; do
if [ -d $SDIR/$REPO/db ]; then
SIZE=`du -sb ${SDIR}/${REPO} | cut -f1`
if [[ $DRYRUN -eq 0 ]]; then
svnadmin --quiet dump ${SDIR}/${REPO} > ${BSDIR}/${REPO}.dump
SIZE=`ls -l ${BSDIR}/${REPO}.dump | cut -d' ' -f5`
fi
log " ${REPO} (${SIZE})"
let "TOTALSIZE += ${SIZE}"
fi
done
# END SUBVERSION BACKUP
# DATABASE BACKUP
log "Creating backup of MySQL databases"
for DB in `ls ${MDIR}`; do
if [ -d ${MDIR}/${DB} ]; then
SIZE=`du -sb ${MDIR}/${DB} | cut -f1`
if [[ $DRYRUN -eq 0 ]]; then
mysqldump -uroot -pswingline ${DB} > ${BMDIR}/${DB}.sql
gzip ${BMDIR}/${DB}.sql
SIZE=`ls -l ${BMDIR}/${DB}.sql.gz | cut -d' ' -f5`
fi
log " ${DB} (${SIZE})"
let "TOTALSIZE += ${SIZE}"
fi
done
# END DATABASE BACKUP
# HOME DIRECTORY BACKUP (non-committed files only)
log "Creating backup of non-versioned files in /home"
for USER in `ls ${HDIR}`; do
# Only users with a dev directory
if [ ! -d $HDIR/$USER/dev ]; then
log " ${USER} has no dev directory"
continue
fi
# Loop through the users sites
for SITE in `ls $HDIR/$USER/dev`; do
REPO=$HDIR/$USER/dev/$SITE
# Only doing subversion checks
if [ ! -d $REPO/.svn ]; then
log " ${USER}/${SITE} is not a subversion project"
continue
fi
cd $REPO
FILES=$(svn status | grep '?')
DEST=$BHDIR/$USER-$SITE.tar
if [ "$FILES" == "" ]; then
log " ${USER}/${SITE} is all committed"
continue
fi
CFILES=`echo "$FILES" | wc -l`
echo "$FILES"|while read STATUS FILE; do
SIZE=`ls -l "${FILE}" | cut -d' ' -f5`
let "TOTALSIZE += ${SIZE}"
if [ -f $DEST ]; then
CMD=r
else
CMD=c
fi
if [[ $DRYRUN -eq 0 ]]; then
tar ${CMD}f ${DEST} "${FILE}"
fi
done
log " ${USER}/${SITE} has ${CFILES} files"
done
done
# END HOME DIRECTORY BACKUP
log "Total backup size: ${TOTALSIZE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment