#!/bin/bash
backup_target=/data01/backups
d=$backup_target
paths="/etc /var/cpanel /mySQLdump /usr/local/apache/conf/ /etc/ssl/certs/ /etc/ssl/private/ /usr/share/ssl/private/ /usr/share/ssl/certs/"
backupcount=8

echo "Starting Backups..."
#If we have a full set of backups
if [ -d $backup_target/backup.$backupcount ]
then
#Unlink the oldest backup
        rm -rf $backup_target/backup.$backupcount
        echo " Nukeing Backup $backupcount."
fi


echo -n " Shifting Backups... "
for id in `seq $backupcount -1 2`
do
        if [ -d $backup_target/backup.$[id -1]/ ]
        then
                mv $backup_target/backup.$[id -1] $d/backup.$id
        fi
done
echo " Done."

echo -n " Hardlinking Backup 0 to Backup 1... "
if [ -d $backup_target/backup.0/ ]
then
        nice cp -al $d/backup.0 $d/backup.1;
fi
echo " Done."

mkdir -p $backup_target/$path/backup.0/ #Just in case

rm $backup_target/backup.0/{.rsync.log,.done,.started}

touch $backup_target/backup.0/.started


echo " Backing Up... "
for path in $paths
do
        echo -n -e '\t' Syncing $path...
#Just in case it has not already been created
                mkdir -p $backup_target/backup.0/$path
                nice rsync --exclude=.dqbu --delete -vazp /$path/ \
$backup_target/backup.0/$path \
>> $backup_target/backup.0/.rsync.log
        echo " Done."
done
echo " Done."

#Secure the log
chmod 100 $backup_target/backup.0/.rsync.log

#Set the date on the DIR to now
touch $backup_target/backup.0

#Add a file to indicate when we finished
touch $backup_target/backup.0/.done


echo "All Done"


