#!/bin/bash

# Title:     DebianBackup.bash
# Author:    John Stile
# License:   GPL
# Reference: http://www.debian-administration.org/articles/55

#-------------
#
# Set these
#
#MYSQLROOTPW=""
#BACKUPDIR=""
#DATE=`date +%Y%M%d`
#EXCLUDE=""
#INCLUDE=""
#------------
# Error message:
function error {
  echo "$1"
  echo 1
}
#------------
if [ -z $MYSQLROOTPW ]; then  error "Missing mysql root password (MYSQLROOTPW)"; fi
if [ -z $BACKUPDIR   ]; then  error "Missing backup directory ()"              ; fi
if [ -z $DATE        ]; then  error "Missing date.  No date command? (DATE)"   ; fi
if [ ! -z $EXCLUDE   ]; then  file_backup_exclude=" --exclude-from=$EXCLUDE "  ; fi
if [   -z $INCLUDE   ]; then  error "Missing files to backup (INCLUDE)."       ; fi
#------------
#
# Start the backup
#
echo "--- Backup Packages ---"
time dpkg --get-selections > $BACKUPDIR/apt-backup-selections

echo "--- Backup MySQL ---"
time mysqldump  -u root --password=$MYSQLROOTPW --all-databases --opt > $BACKUPDIR/mysql-$DATE.sql

echo "--- Backup Files ---"
time tar -cjvpf $BACKUPDIR/backup-$DATE.tar.bz --absolute-names $file_backup_exclude  $INCLUDE
#------------
