#!/bin/bash

function PrintData(){
        echo $1
        logger $1
}

DirsToBkp="/etc /root /home/m1 /home/m2 /home/mtn"
StartDate="$(date +%Y%m%d-%H%M%S)"
LastFullBkp=$(ls -1t /backups/ | head -n1)

# ${TypeBkp} backup
if [[ "${1}" == '-g' ]]
        then
                TypeBkp=Inc
                DataToBkp=$(find ${DirsToBkp} -newer /backups/${LastFullBkp} -type f)
                if [[ -z ${DataToBkp} ]]
                        then
                                PrintData "No New Data to backup in ${DirToBkp} From ${LastFullBkp}"
                                exit 2
                fi
        else
                TypeBkp=Full
                DataToBkp=${DirsToBkp}
fi
tar czf /backups/${TypeBkp}_${StartDate}.tar.gz ${DataToBkp} &> /dev/null
if [ $? -ne 0 ]
        then
                PrintData "Backup ${TypeBkp} ${StartDate} FAILED"
                exit 1
        else
                PrintData "Backup ${TypeBkp} ${StartDate} SUCCESS /backups/${TypeBkp}_${StartDate}.tar.gz"

fi