#!/bin/bash

#### usage: ####
###  backup.sh <hostname> local

#### Dependencies: ######
## rsync - for backing up files
## ssmtp - for emailing the summary
## mysql - if you want to backup mysql db's

### NOTE: ####
## To backup locally without ssh, type local after the hostname.
## To use ssh keys, setup your ~/.ssh/config for the hostname.
# ------------------ begin config --------------------------

email=email@email.address.com
smtp_login=user@smtp.hostname.com

# Local directory where we'll be doing work and keeping copies of all archived files
backupdir="/media/backup/$1"

# The user and the address of machine to be backed up via ssh
login="$1"   

# The number of days after which old backups will be deleted
days="180"					

# MySQL username and password and db's
#MYSQLUSER="root"
#MYSQLPASS="mysqlpassword"

# excludes
excludesdir="/root"
excvar="backup_excludes.var"
excusr="backup_excludes.usr"
exchome="backup_excludes.$1"

# ---------------------  end config ---------------------- 

## check for var excludes file
if [ -f $excludesdir/$excvar ];
then
   excv="$excludesdir/$excvar"
else
   excv="/dev/null"
fi

## check for usr excludes file
if [ -f $excludesdir/$excusr ];
then
   excu="$excludesdir/$excusr"
else
   excu="/dev/null"
fi

## check for home excludes file
if [ -f $excludesdir/$exchome ];
then
   exch="$excludesdir/$exchome"
else
   exch="/dev/null"
fi


# Prints date of the format YYYY/MM/DD
year=$(date +%Y)
month=$(date +%m)
day=$(date +%d)
today="$year/$month/$day"

# get location of ssmtp
ssmtp=$(type -P ssmtp)

# get list of databases to backup
#DBS=$(ssh $login "mysql -u $MYSQLUSER --password=$MYSQLPASS -Bse 'show databases'")

# check if doing backup over ssh or locally
if [ "$2" = "local" ]; then
   via=""
else
   via="-e ssh $login:" ; ssh=1
fi
echo "$via"
# check for backup media
if [ ! -d $backupdir ];
then
   echo "NO BACKUP MEDIA LOCATED! \n HALTING BACKUP!"
   $ssmtp -oi $smtp_login << EOF
To: $email
Subject: [BACKUP FAILED!] $1 for $(date +%m-%d-%y)

NO BACKUP MEDIA LOCATED! 
HALTING BACKUP!"

EOF
   exit 0
else
   cd $backupdir
fi

# check for remote machine
if [[ $via != "" ]]; then
up=`ssh -q $1 echo "1"`
else
up="1"
fi

if [[ $ssh = "1" && $up != 1 ]]; then
echo "Hostname $1 not accessible!"
echo "HALTING BACKUP!"
   $ssmtp -oi $smtp_login << EOF
To: $email
Subject: [BACKUP FAILED!] $1 for $(date +%m-%d-%y)

Hostname $1 not accessible! 
HALTING BACKUP!"

EOF
   exit 0
else
echo "SSH Connection established to $1"
fi

# clear the screen
clear

# make backup directories.
current="$backupdir/current"
old="$backupdir/old"
if [ ! -d $current ];
then
   mkdir $current
fi
if [ ! -d $old ];
then
   mkdir $old
fi

# CREATING current DATE / TIME
now="$old/$today"
mkdir -p $now

# get list of home directories
if [ "$2" = "local" ]; then
   homelist=$(ls /home)
else
   homelist=$(ssh $login "ls /home/")
fi

echo "**********************"
echo " Backup started at "
date
echo "**********************"


## HOME      
echo "== backing up /home =="
for i in $homelist;do echo "==== backing up $i ====";mkdir -p $current/home/$i;rsync -aphvz --delete --stats --progress --compress --exclude-from="$exch" $via/home/$i/ $current/home/$i;echo "";done
rsync -dvpz --delete --stats --compress --progress $via/home/ $current/home/

## root
echo "== backing up /root/ =="
mkdir -p $current/root/
rsync -aphvz --delete --stats --compress --progress $via/root/ $current/root/

## etc
echo "== backing up /etc/ =="
mkdir -p $current/etc/
rsync -aphvz --delete --stats --compress --progress $via/etc/ $current/etc/

## var
echo "== backing up /var/ =="
mkdir -p $current/var/
rsync -aphvz --delete --delete-excluded --progress --exclude-from="$excv" --stats --compress $via/var/ $current/var/

## usr
echo "== backing up /usr/ =="
mkdir -p $current/usr/
rsync -aphvz --delete --delete-excluded --progress --exclude-from="$excu" --stats --compress $via/usr/ $current/usr/

## Backup MYSQL databases
#mkdir $now/dbs
#echo "=== backing up Databases... ==="
#for i in $DBS;do echo "backing up $i...";ssh $login mysqldump -u $MYSQLUSER --password=$MYSQLPASS $i | gzip > $now/dbs/$i.$today1.sql.gz;done

# Update the mtime to reflect the snapshot time
echo "Updating mtime to reflect the snapshot time..."
touch $backupdir/current

# Make hardlink copy
echo "Making hardlink copy...."
cp -al $current/* $now

# Remove old backups
echo "Removing old backups... "
find $old/$(date +%Y) -maxdepth 2 -mindepth 2 -type d -mtime +$days -exec rm -rf {} \;
#find $old/$(($(date +%Y)-1)) -maxdepth 2 -mindepth 2 -type d -mtime +$days -exec rm -rf {} \;

echo "**********************"
echo " Backup ended at "
date
echo "**********************"

echo "done"

echo "Sending mail..."
body1=$(grep -A1 "Backup started at" $backupdir/backup.log)
body2=$(egrep "backing up|Number of files t|Total transferred file size" $backupdir/backup.log|grep -v -- ": 0")
body3=$(grep -A1 "Backup ended at" $backupdir/backup.log)

$ssmtp -oi $smtp_login << EOF
To: $email
Subject: [BACKUP OK] $1 for $(date +%m-%d-%y)

$body1

$body2

$body3

EOF

echo "done"

sleep 2
cp $backupdir/backup.log $now/

exit 0

