#!/bin/bash

###########################################################################################
#                                                                                         #
# Simple secure automatic backup script by Gothi[c]                                       #
# http://www.donationcoder.com                                                            #
#                                                                                         #
# Takes 1 argument: settings file                                                         #
#                                                                                         #
# Settings file can have the following directives:                                        #
#                                                                                         #
# - backup (which folder to back up)                                                      #
# - scphostname (scp remote machine host name)                                            #
# - scpusername (scp username on remote machine)                                          #
# - scpdestdir (scp destination folder on remote machine)(optional)                       #
# - except files (which files to exclude from the backup) (can have multiple directives)  #
# - logfile (where to log events)                                                         #
# - scpdestfile (optional filename on remote server, if not specified one will be         #
#                generated based on the current date)                                     #
# - enable scp (whether or not to enable scp upload of backups)                           #
# - enable email (whether or not to enable emailing backups)                              #
# - email (email address to send backups to, if enabled)                                  #
# - sendmail (optional)(path to the sendmail program, used to send email)                 #
#                                                                                         #
# Requires following GNU utilities to be installed (these are present on about any GNU    #
# system): cat,sed,grep,tar,bzip2                                                         #
#                                                                                         #
# Requires following GNU utilities to be installed for scp uploading: scp (openssh)       #
#                                                                                         #
# Requires following GNU utilities to be installed for emailing: sendmail, uuencode       #
#                                                                                         #
###########################################################################################

#### first check the sanity of our user. ####

if [ ! -n "$1" ]; then
 echo "ERROR: Missing settings file argument."
 echo "Usage: $0 settingsfile".
 exit $E_BADARGS
fi

#### check if settings file is readable ####

if [ ! -r "$1" ]; then
 echo "ERROR: Could not read provided settings file: $1."
 echo "Please ensure the file exists and that you have read permissions to it."
 exit $E_BADARGS
fi

#### init variables ####

SETTINGS_FILE=$1
LOCAL_SRC_FOLDER=`cat $SETTINGS_FILE | grep "backup:" | sed -e 's/backup:\( \)\?//g' | sed -e 's/\( \)\+//'`
ARCHIVE_FILENAME=`basename $LOCAL_SRC_FOLDER`-`date +%F`.tar.bz2
REMOTE_HOSTNAME=`cat $SETTINGS_FILE | grep "scphostname:" | sed -e 's/scphostname:\( \)\?//g' | sed -e 's/\( \)\+//'`
REMOTE_USERNAME=`cat $SETTINGS_FILE | grep "scpusername:" | sed -e 's/scpusername:\( \)\?//g' | sed -e 's/\( \)\+//'`
REMOTE_DIRECTORY=`cat $SETTINGS_FILE | grep "scpdestdir:" | sed -e 's/scpdestdir:\( \)\?//g' | sed -e 's/\( \)\+//'`
REMOTE_FILENAME=`cat $SETTINGS_FILE | grep "scpdestfile:" | sed -e 's/scpdestfile:\( \)\?//g' | sed -e 's/\( \)\+//'`
LOG_FILE=`cat $SETTINGS_FILE | grep "logfile:" | sed -e 's/logfile:\( \)\?//g' | sed -e 's/\( \)\+//'`
EXCEPTIONS=( `cat $SETTINGS_FILE | grep "except files:" | sed -e 's/except files:\( \)\?//g' | sed -e 's/\( \)\+//' | sed ':a;N;$!ba;$s/\(.*\)\n/\1 /'` )
EMAIL=`cat $SETTINGS_FILE | grep "email:" | sed -e 's/email:\( \)\?//g' | sed -e 's/\( \)\+//'`
SCP_ENABLED=`cat $SETTINGS_FILE | grep "enable scp:" | sed -e 's/enable scp:\( \)\?//g' | sed -e 's/\( \)\+//'`
EMAIL_ENABLED=`cat $SETTINGS_FILE | grep "enable email:" | sed -e 's/enable email:\( \)\?//g' | sed -e 's/\( \)\+//'`
PROG_SENDMAIL=`cat $SETTINGS_FILE | grep "sendmail:" | sed -e 's/sendmail:\( \)\?//g' | sed -e 's/\( \)\+//'`

#### create archive ####

TAR_COMMAND="-cvjf /tmp/$ARCHIVE_FILENAME $LOCAL_SRC_FOLDER"
for exception in $(seq 0 $((${#EXCEPTIONS[@]} - 1))); do
 TAR_COMMAND="$TAR_COMMAND --exclude ${EXCEPTIONS[$exception]}"
done
tar $TAR_COMMAND &> /dev/null

#### scp upload ####

if test "$SCP_ENABLED" != ""; then
 if test "$REMOTE_FILENAME" = ""; then
   SCP_REMOTE_FILE="."
 else
   SCP_REMOTE_FILE=$REMOTE_FILENAME
 fi
 SCP_COMMAND="/tmp/$ARCHIVE_FILENAME $REMOTE_USERNAME@$REMOTE_HOSTNAME:.$REMOTE_DIRECTORY/$SCP_REMOTE_FILE"
 scp $SCP_COMMAND &> /dev/null
fi

#### email upload ####
echo "email enabled = $EMAIL_ENABLED"
if test "$EMAIL_ENABLED" != ""; then
 echo "email enabled"
 if test "$PROG_SENDMAIL" = ""; then
   PROG_SENDMAIL="sendmail"
 fi
 if test "$EMAIL" != ""; then
   cat /tmp/$ARCHIVE_FILENAME | uuencode /tmp/$ARCHIVE_FILENAME | ${PROG_SENDMAIL} $EMAIL
   echo "email sent"
 fi
fi

#### delete temporary archive file ####

rm /tmp/$ARCHIVE_FILENAME

#### logging ####

if test "$LOG_FILE" != ""; then
 touch $LOG_FILE
 if [ -w $LOG_FILE ]; then
   THE_DATE=`date +%F`
   THE_TIME=`date +%T`
   echo "[$THE_DATE] ($THE_TIME): Completed backup for $SETTINGS_FILE." >> $LOG_FILE
 fi
fi

exit 0