#!/bin/bash

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

#-------------
# Set these values
#
#BackMeUp=""
#MYSQLROOTPW=""
#BACKUPDIR=""
#DATE=`date +%Y%M%d`
#EXCLUDE=""
#INCLUDE=""
#------------
# Error message:
function error {
  echo "$1"
  exit 1
}
#------------
if [ -z $BackMeUp    ]; then  error "Missing host to backup (BackMeUp)"             ;fi
if [ -z $MYSQLROOTPW ]; then  error "Missing mysql root password (MYSQLROOTPW)"     ;fi
if [ -z $BACKUPDIR   ]; then  error "Missing backup directory (BACKUPDIR)"          ;fi 
if [ -z $DATE        ]; then  error "Missing date.  No date command? (DATE)"        ;fi

mysql_backup=' -u root --password=$MYSQLROOTPW --all-databases --opt > "/var/backups/mysql-$DATE.sql" '
file_backup=' --file $MOUNTDIR/$BACKUPDIR/backup-$DATE.tbz ' 
if [ ! -z $EXCLUDE   ]; then  file_backup=$files_backup+' --exclude-from=$EXCLUDE ' ;fi
if [ ! -z $INCLUDE   ]; then  file_backup=$files_backup+' $INCLUDE '                ;fi

#------------
#
# Start the backup
#
echo "--- Backup Packages on $BackMeUp:/var/backups/apt-backup-selections ---"
time ssh root@$BackMeUp -c "dpkg --get-selections > /var/backups/apt-backup-selections"

echo "--- Backup MySQL  /var/backups/mysql-$DATE.sql ---"
time ssh root@$BackMeUp -c "mysqldump  $mysql_backup"

echo "--- Backup Files $MOUNTDIR/$BACKUPDIR/backup-$DATE.tbz  ---"
time ssh root@$BackMeUp -c "tar --absolute-names --create -j $file_backup"

#------------
mkdir $BackMeUp_backup.$DATE
pushd $BackMeUp_backup.$DATE/

rsync -avzuR -e ssh root@B:'/root/.bashrc \
/var/backups/apt-backup-selections \
$MOUNTDIR/$BACKUPDIR/backup-$DATE.tbz \
/var/backups/mysql-$DATE.sql'
.

popd
#------------
