#!/bin/bash
# paul@vandervlis.nl
# push backup

echo -e "\n`date +%T` Start backup"
# Remote IP waar backup heen moet:
RIP="10.0.1.3"
# Romote DIR voor backup
RDIR="/backup/naam"
# Local DIR voor backup
LDIR="/backup/localhost"
# Exclude:
EXCLUDE="\
--exclude=/mnt/** \
--exclude=/backup/** \
--exclude=/proc/** \
--exclude=/media/** \
--exclude=/tmp/** \
--exclude=/lost+found \
--exclude=/sys/** \
--exclude=/run/** \
--exclude=$LDIR \
--exclude=/var/cache/apt/archives/** \
--exclude=/home/*/.cache/** \
"

echo -e "\n`date +%T` Dagen opschuiven op backup-server"
ssh $RIP "\
if ! test -e $RDIR; then mkdir -p $RDIR; chmod 700 $RDIR; fi; \
cd $RDIR; \
if test -e backup-06; then rm -r backup-06; fi; \
if test -e backup-05; then mv backup-05 backup-06; fi; \
if test -e backup-04; then mv backup-04 backup-05; fi; \
if test -e backup-03; then mv backup-03 backup-04; fi; \
if test -e backup-02; then mv backup-02 backup-03; fi; \
if test -e backup-01; then mv backup-01 backup-02; fi; \
if test -e backup-00; then cp -al backup-00 backup-01; fi \
"

# mysqldump, elke database apart dumpen
echo -e "\n`date +%T` MySQL dump maken"
/usr/local/sbin/mysqlbackup

# lokaal opschuiven en hardlinks maken
echo -e "\n`date +%T` Hardlinks maken"
if ! test -e $LDIR; then mkdir -p $LDIR; chmod 700 $LDIR; fi
cd $LDIR; if ! test $? = 0; then echo "cd $LDIR ging niet"; exit; fi
if test -e backup-06; then rm -r backup-06; fi
if test -e backup-05; then mv backup-05 backup-06; fi
if test -e backup-04; then mv backup-04 backup-05; fi
if test -e backup-03; then mv backup-03 backup-04; fi
if test -e backup-02; then mv backup-02 backup-03; fi
if test -e backup-01; then mv backup-01 backup-02; fi
if test -e backup-00; then cp -al backup-00 backup-01; fi

# lokaal synchroniseren
echo -e "\n`date +%T` Lokaal synchroniseren"
if test ! -e backup-00; then mkdir backup-00; fi
if test ! -e backup-00;  then echo "aanmaken backup-00 ging niet"; exit; fi
/usr/bin/rsync -a --del --delete-excluded $EXCLUDE / backup-00/
touch backup-00

# syncen naar remote
echo -e "\n`date +%T` Syncen naar backup-server:"
/usr/bin/rsync -azve /usr/bin/ssh --delete \
   backup-00  $RIP:$RDIR

# klaar
echo -e "\n`date +%T` Backup klaar."

