#!/bin/bash
# Path: /root/backup/create_backup.sh
# created by Johan Huysmans <johan.huysmans@x-tend.be>
# modified by Frederic Descamps <fred@x-tend.be>
# modified by Raf Nijskens <raf@x-tend.be>
# modified by Jos Leurs <jos@x-tend.be>
# version 0.8


# -----  Start configuration  -----
#
#


# -----  Reporting  -----
# If 0 the reporting is disabled
REPORTING=1
REPORT_TO="someone@somecompany.org"
SUBJECT="Backup on "$HOSTNAME


# -----  HA  -----

# If machine is a node of HA environment HA=1. Default HA=0.
HA=0

# IP of the heartbeat
HA_IP=0.0.0.0


# -----  Backup strategy  -----

# WEEKLY_DAILY
# If 1 backup structure is DEST_DIR/daily/20070711 and DEST_DIR/weekly/20070711
# If 0, DOWFB and HIST settings are ignored
WEEKLY_DAILY="1"

# X: day of week to perform the full backup
# day of week (0..6);  0 represents Sunday
DOWFB="6"

# Keep the backup for X weeks
# There will be X weekly's available
HIST="4"


# -----  Files and Folders  -----
# File or directory that will be backuped
# If you add a / at the end of a directory,
#  the content of the directory will be copied.
# If you don't add a / at the end of a directory,
#  the directory will be copied.
BACKUP_DIR="/"

EXCLUDE_FILE="/root/.rsync/excludes"

WEEK_DIR="weekly"

DAY_DIR="daily"

DEST_USER="root"
DEST_SERV_IP="xxx.xxx.xxx.xxx"
DEST_DIR="/backup"

LOG_DIR="/root/backup/logs/"

# 
#
# -----  End configuration  -----

# -----  Vars  -----
#
#

# YYYYMMDD: name of the directory
DATE=`date +%Y%m%d`

# X: day of week (0..6);  0 represents Sunday
DOW=`date +%w`

# YYYYMMDD: name of the directory of X weeks ago
HIST=`expr $HIST + 1`
DATE_HIST=`date +%Y%m%d --date="$HIST weeks ago"`


# -----  Script  -----
#
#

LOGFILE=$LOG_DIR$DATE

echo "Now is `date`." > $LOGFILE
echo "I am `uname -a`." >> $LOGFILE


# Check if the machine is the master else die
if [ $HA -eq "1" ]
then
        /sbin/ifconfig | grep "$HA_IP" >/dev/null 2>&1
        if [ "$?" -eq "1" ] 
        then
                echo "I'm not the master node. No need to backup me." >> $LOGFILE
                # check size of BACKUP_DIR
                echo " - check size of local data: `date`" >> $LOGFILE
                du -sh $BACKUP_DIR >> $LOGFILE
                
                if [ $REPORTING -eq "1" ]
                then
                        cat $LOGFILE | mail -s "$SUBJECT" $REPORT_TO 
                fi
                exit 1
        fi
fi

TYPE="full"

if [ $WEEKLY_DAILY -eq "0" ] 
then
        DEST=$DEST_USER@$DEST_SERV_IP:$DEST_DIR
        OPTIONS="-WaHz"
        CHECK_DIR=$DEST_DIR
else 
        if [ $DOWFB -eq $DOW ]
        then  
                DIR=$WEEK_DIR
                OPTIONS="-WaHz"
        else
                DIR=$DAY_DIR    
                OPTIONS="-baHz --link-dest=$DEST_DIR/latest_weekly"
                TYPE="incremential"
        fi
        CHECK_DIR=$DEST_DIR/$DIR/$DATE
        DEST=$DEST_USER@$DEST_SERV_IP:$CHECK_DIR
fi

# create the backup 
echo " - start $TYPE backup at: `date`" >> $LOGFILE

rsync $OPTIONS --numeric-ids --exclude-from=$EXCLUDE_FILE $BACKUP_DIR $DEST 2>> $LOGFILE 1>> $LOGFILE

if [ $WEEKLY_DAILY -eq "1" ] 
then
        # create a link pointing to the latest daily backup
        echo " - create link at: `date`" >> $LOGFILE
        ssh $DEST_USER@$DEST_SERV_IP "cd $DEST_DIR && ln -snf $DIR/$DATE latest_daily" >> $LOGFILE

        if [ $DOWFB -eq $DOW ]
        then  
           # create a link pointing to the latest full backup
                echo " - create link at: `date`" >> $LOGFILE
                ssh $DEST_USER@$DEST_SERV_IP "cd $DEST_DIR && ln -snf weekly/$DATE latest_weekly" >> $LOGFILE
           # delete all previous daily backups
                echo " - remove daily backups at: `date`" >> $LOGFILE
                ssh $DEST_USER@$DEST_SERV_IP "rm -rf $DEST_DIR/daily/*" >> $LOGFILE
           # remove the X weeks old full backup
                echo " - remove the full backup at: `date`" >> $LOGFILE
                ssh $DEST_USER@$DEST_SERV_IP "rm -rf $DEST_DIR/weekly/$DATE_HIST" >> $LOGFILE
        fi

        # list content of BACKUP_DIR to know where links are pointing to 
        # list day and week dir to check content

        echo " - check links and content at: `date`" >> $LOGFILE
        ssh $DEST_USER@$DEST_SERV_IP "ls -al $DEST_DIR | grep latest" >> $LOGFILE

        echo " - Check content of $DEST_DIR/$WEEK_DIR:" >> $LOGFILE
        ssh $DEST_USER@$DEST_SERV_IP "ls  $DEST_DIR/$WEEK_DIR" >> $LOGFILE

        echo " - Check content of $DEST_DIR/$DAY_DIR:"  >> $LOGFILE
        ssh $DEST_USER@$DEST_SERV_IP "ls  $DEST_DIR/$DAY_DIR" >> $LOGFILE
fi

# check size of BACKUP_DIR
echo " - check size of local data: `date`" >> $LOGFILE
du -sh $BACKUP_DIR >> $LOGFILE

# check size of backup
echo " - check size of backup" >> $LOGFILE
ssh $DEST_USER@$DEST_SERV_IP "du -sh $CHECK_DIR" >> $LOGFILE

# check free space on destination
echo " - check free space @ $DEST_SERV_IP" >> $LOGFILE
ssh $DEST_USER@$DEST_SERV_IP "df -h" >> $LOGFILE

echo " -- Backup finished at `date`" >> $LOGFILE

if [ $REPORTING -eq "1" ]
then
        cat $LOGFILE | mail -s "$SUBJECT" $REPORT_TO 
fi

