Backup FULL and DIFF estilo Time Machine (Apple)

De Wiki Hackstore
#!/bin/bash
# Script de Backup
# Author - Raphael Bastos

ROOTDIR="/backup"
DAY=`date +%u`
NDAY=`date +%d`
HOUR=`date +%H`
H=$HOUR;
DATE2=`date +'%Y-%m-%d'`
DATA=`date +%d-%m-%Y`;
DATEMT=`date +'%Y-%m'`
ERRORLOG="$ROOTDIR/logs/error.log"
BKPLOG="$ROOTDIR/logs/$DATEMT/bkp.log"
BKPDIR="$ROOTDIR/local/"
FULLDIR="$BKPDIR"
ERROR=0;
ERROR2=0;
MAILSCRIPT="$ROOTDIR/mail.sh"
SUCESSO="SUCESSO"
FALHA="FALHA"
MNT="/mnt"
ROOTDIR="/backup"
YEAR=`date +%Y`;
ERROR=0;
ERROR2=0;
ERRORWIN="$ROOTDIR/logs/errorwin.log"
DIRSMB="/data"
DIRBKP="/backup/files/samba"
FULL="${DIRBKP}/FULL"
DIFF="${DIRBKP}/DIFF"
MACHINE="server01"

############################################
# cria diretórios necessários caso não existam

if [ ! -d $ROOTDIR/logs ]; then
        mkdir -p $ROOTDIR/logs
fi

if [ ! -d $BKPDIR/$DATE2 ]; then
        mkdir -p $BKPDIR/$DATE2
fi

if [ ! -d ${DIRBKP} ]; then
        mkdir -p ${DIRBKP}
fi

if [ ! -d ${FULL} ]; then
        mkdir -p ${FULL}
fi

############################################

# Backup FULL
if [ "$NDAY" = "3" -o  "$NDAY" = "13" -o  "$NDAY" = "25" ]; then
        rsync -azv --delete ${DIRSMB} ${FULL} 2> $ERRORLOG
        if [ "$?" -gt 1 ]; then
                cat $ERRORLOG | python $MAILSCRIPT "$FALHA ao sincronizar arquivos do compartilhamento SAMBA. `date`";
                ERROR=1;
                ERROR2=1;
        fi

# Backup DIFF
else

        DIFFCURRENT="$DIFF/$DATA"
        mkdir -p ${DIFFCURRENT}
        sleep 3
        rsync -azv --compare-dest="${FULL}" --delete ${DIRSMB} ${DIFFCURRENT} 2> $ERRORLOG
        if [ "$?" -gt 1 ]; then
                cat $ERRORLOG | python $MAILSCRIPT "$FALHA ao sincronizar arquivos diferenciais do compartilhamento SAMBA. `date`";
                ERROR=1;
        fi

fi

############################################
cd /

#####################################################
# fim