# Define variables
interval=2
source=/mnt/raid3
diskid=backup
log=/var/log/backup-weekly.log
errlcl=254
excludefile=/tmp/exclude.$$
echo "/backup/wstn/backintime" >> $excludefile
echo "/VMWare_defaults" >> $excludefile
echo "/AllKind" >> $excludefile
echo "/Audio" >> $excludefile
echo "/Video/music" >> $excludefile
echo "/Video/Octonauts" >> $excludefile
echo `date +%Y/%m/%d\ %H:%M:%S`" Starting Backup" >> $log
# Find where the backup drive labeled "$diskid" is mounted
#location=`mount | grep \`while read input ; do blkid /dev/$input | grep $diskid | cut -d: -f1 ; done < <(cat /proc/partitions | tail -n +2 | cut -c 26-)\` | cut -d" " -f3`
location=`mount | grep \`blkid | grep $diskid | cut -d: -f1\` | cut -d" " -f3` >/dev/null 2>>$log
# Run the backup only if the backup drive is mounted
if [ -n "$location" ]; then
# Move all the backups one level up (recycling the oldest follewed by cp is indeed faster)
echo `date +%Y/%m/%d\ %H:%M:%S`" Cycling local backups on $location" >> $log
if [ -n $location/backup$interval ]; then
echo `date +%Y/%m/%d\ %H:%M:%S`" Temporary moving $location/backup$interval to $location/backup_tmp" >> $log
mv -fu $location/backup$interval/ $location/backup_tmp >>$log 2>&1
else
echo `date +%Y/%m/%d\ %H:%M:%S`" Older backup not found. Creating an empty $location/backup_tmp" >> $log
mkdir $location/backup_tmp >>$log 2>&1
fi
while [ $interval -ge 1 ]; do
echo `date +%Y/%m/%d\ %H:%M:%S`" Moving $location/backup$[ interval - 1 ] to $location/backup$interval" >> $log
mv -fu $location/backup$[ interval - 1 ]/ $location/backup$interval >>$log 2>&1
interval=$[ interval - 1 ]
done
# Create current backup by recycling the oldest and linking the last backup to it
echo `date +%Y/%m/%d\ %H:%M:%S`" Moving the oldest backup from $location/backup_tmp to $location/backup$interval" >> $log
mv -fu $location/backup_tmp/ $location/backup$interval >>$log 2>&1
echo `date +%Y/%m/%d\ %H:%M:%S`" Using cpio to bring the differences from $location/backup$[ interval + 1 ] to $location/backup$interval" >> $log
cp -falux $location/backup$[ interval + 1 ]/. $location/backup$interval/ >>$log 2>&1
# cleanup old server and router backups
echo `date +%Y/%m/%d\ %H:%M:%S`" Cleaning old backups." >>$log
dest=$location/backup$interval/backup/server/
find $dest -name "bkp_*.img.gz" -type f -mtime +6 -exec rm -v {} \; >>$log 2>&1
# Take the new backup
echo `date +%Y/%m/%d\ %H:%M:%S`" Creating the local backup." >>$log
rsync --quiet --log-file=$log --stats --human-readable --partial --archive --recursive --times --compress --itemize-changes --one-file-system --hard-links --inplace --numeric-ids --del --links --exclude-from=$excludefile $source/ $location/backup$interval >>$log 2>&1 ; errlcl=$?
echo `date +%Y/%m/%d\ %H:%M:%S`" Local Backup finished." >>$log
echo `date` > $location/backup$interval/backup_taken
else
errlcl=255
echo `date +%Y/%m/%d\ %H:%M:%S`" ERROR: Backup disk NOT FOUND!!!" >>$log 2>&1
fi
# Second backup to the offsite drive
rmtdest=/tmp/tmpmnt.$$
rmtsrv=root@rmtserv:/tmp/mnt/sda1
rmtftp=rmtserv:12221/USB/
rmtcifs=\\\\rmtserv\\USB
errlvl=254
echo -e '\n'`date +%Y/%m/%d\ %H:%M:%S`" Remote Backup starting." >>$log
# Check if rsync is available on the destination
needmount=`rsync -av $excludefile $rmtsrv 2>&1 | grep 'rsync: not found'`
if [ -n "$needmount" ]; then
# Rsync is not available, mount the remote destination locally via sshfs (using ssh keys) - fallback to smb (as guest) or curlftp (need a valid ~/.netrc)
echo `date +%Y/%m/%d\ %H:%M:%S`" rsync not available, mounting sshfs $rmtsrv" >>$log
mkdir -p $rmtdest >>$log 2>&1
sshfs $rmtsrv $rmtdest >>$log 2>&1
if [ -z "$(mount | grep $rmtdest)" ]; then
echo `date +%Y/%m/%d\ %H:%M:%S`" sshfs not available, mounting cifs $rmtcifs" >>$log
mount.cifs -o guest $rmtcifs $rmtdest >>$log 2>&1
fi
if [ -z "$(mount | grep $rmtdest)" ]; then
echo `date +%Y/%m/%d\ %H:%M:%S`" cifs not available, mounting ftpfs $rmtftp" >>$log
curlftpfs -o allow_other -o gid=0 -o uid=0 -o umask=000 $rmtftp $rmtdest >>$log 2>&1
fi
# Without rsync we'll use cpio. First we need to make sure the rmtdest has been mounted
if [ -n "$(mount | grep $rmtdest)" ]; then
# cleanup old server and router backups
echo `date +%Y/%m/%d\ %H:%M:%S`" $rmtdest mounted. Cleaning remote folder." >>$log
dest=$rmtdest/backup/server/
find $dest -name "bkp_*.img.gz" -type f -mtime +6 -exec rm -v {} \; >>$log 2>&1
# start syncing the backup
echo `date +%Y/%m/%d\ %H:%M:%S`" Starting cpio data transfer." >>$log
cd $source
find . -depth -print |grep -v -f $excludefile | cpio --pass-through --reset-access-time --make-directories --preserve-modification-time $rmtdest 1>>$log 2> >(grep -vi "newer") >>$log ; errlvl=$?
echo `date +%Y/%m/%d\ %H:%M:%S`" Cpio transfer finished." >>$log
echo `date +%Y/%m/%d\ %H:%M:%S` > $rmtdest/backup_taken
else
errlvl=255
echo `date +%Y/%m/%d\ %H:%M:%S`" ERROR: No rsync available and remote destination NOT MOUNTED!!!" >>$log
fi
else
# If rsync is available we don't need to mount anything. Cleanup old server and router backups over ssh
echo `date +%Y/%m/%d\ %H:%M:%S`" Using rsync. Cleaning remote folder" >>$log
ssh `echo $rmtsrv | cut -d: -f1` `find /tmp/mnt/sda1/backup/server/ -name "bkp_*.img.gz" -type f -mtime +6 -exec rm -v {} \;` >>$log 2>&1
# perform rsync
echo `date +%Y/%m/%d\ %H:%M:%S`" Starting remote rsync." >>$log
rsync --quiet --log-file=$log --stats --human-readable --partial --archive --recursive --times --compress --itemize-changes --one-file-system --hard-links --inplace --numeric-ids --del --links --max-size=4G --exclude-from=$excludefile $source/ $rmtsrv >>$log 2>&1 ; errlvl=$?
ssh `echo $rmtsrv | cut -d: -f1` $(echo `date` > /tmp/mnt/sda1/backup_taken)
echo `date +%Y/%m/%d\ %H:%M:%S`" Remote rsync finished." >>$log
fi
# Cleanup traces
umount -fl $rmtdest >>$log 2>&1
sleep 5
if [ -z "$(mount | grep $rmtdest)" ]; then
rmdir $rmtdest >>$log 2>&1
fi
rm -f $excludefile >>$log 2>&1
echo -e `date +%Y/%m/%d\ %H:%M:%S`" Backup finished."'\n\n' >>$log
# Send backup report
if [ "$errlcl" -eq "0" ] && [ "$errlvl" -eq "0" ]; then
msg="Weekly Backup successfully completed."
else
msg="Weekly Backup might contain errors."
fi
echo "Please see attached Backup Report. The exit codes were: local=$errlcl remote=$errlvl" | mail -r backup@mydomain.tld -s "$msg" -S smtp=smtp://relay.smtpserver.tld -a $log recipient@mydomain.tld
No comments:
Post a Comment