This is the v 0.2 of the backup script, which does basically the same as v 0.1 but it has been modify to use variable for the path, instead of typing each path, and it is also backing up to a external NAS mounted through NFS, so it performs the mount, check whether it works and performs the backup.
#!/bin/bash
#Copyright (C) 2014 Josep Manel Andrés Moscardó http://terradelfoc.blogspot.com
#Version 0.2
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published
#by the Free Software Foundation, either version 3 of the License,
#or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful, but
#WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT
#ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
#Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see http://www.gnu.org/licenses/.
#
DAILYPATH=/backup/daily/
WEEKLYPATH=/backup/weekly/
MONTHLYPATH=/backup/monthly/
#PATHBACKUP=/backup
CAL=/usr/bin/cal
#Format= 2013-03-20
YESTERDAY=`date --date=yesterday +%Y-%m-%d`
TODAYFULL=`date +%Y-%m-%d`
#Int (where Monday is 1 and Sunday is 7)
TODAY=`date +%u`
#Format= 2013-03-20
SEVENDAYSAGO=`date --date '7 days ago' +%Y-%m-%d`
FOURWEEKSAGO=`date --date '28 days ago' +%Y-%m-%d`
FIVEWEEKSAGO=`date --date '35 days ago' +%Y-%m-%d`
#These variables will be used to find out which day is the backup from 5months ago that we have to remove
FIVEMONTHSAGOMONTH=`date --date='5 month ago' +%m`
FIVEMONTHSAGOYEAR=`date --date='5 month ago' +%Y`
#This function will give us the date of the first Monday of 5 months ago month.
function date5monthsago(){
#Exact day for the backup from 5months ago
local aux=`$CAL $FIVEMONTHSAGOMONTH $FIVEMONTHSAGOYEAR |
awk '
NR == 1 { next }
NR == 2 { next }
NF <= 5 { next }
NF == 6 { print $1 ; exit }
NF == 7 { print $2 ; exit }
'`
echo $aux
}
#AUX used for checking whether the backup from 28 days ago is the first Monday of the month backup, if so We'll keep it in monthly directory, if not, we'll remove it.
AUX=`date --date '35 days ago' +%d`
#Do daily backup and store it on /home/backup/daily
#To log Starting time
START=$(date +%s)
#Mounting NFS to backup IC3HOME, we'll be umounting the FS at the end of the script
echo "###########Start mounting NFS############" > /tmp/mount.log 2>>1
mount lamarr:/volume1/backup /backup/ &>> /tmp/mount.log
if [ $(echo $?) -eq 0 ]; then
echo "mounted OK" >> /tmp/mount.log
cat /tmp/mount.log >> $DAILYPATH$TODAYFULL.log
##########################################################
echo "#########################Starting ic3home backup#####################" >> $DAILYPATH$TODAYFULL.log 2>>1
###########################################################
###########################################################
rsync -avz --link-dest=/backup/daily/$YESTERDAY --progress /export/ic3home/ /backup/daily/$TODAYFULL >> $DAILYPATH$TODAYFULL.log 2>&1
###########################################################
###########################################################
#To log Finishing time
END=$(date +%s)
TOTALTIME=$(( $END - $START ))
echo "############################################################################################" >> $DAILYPATH$TODAYFULL.log
echo "Start time:$START, finish time:$END, seconds:$TOTALTIME" >> $DAILYPATH$TODAYFULL.log
#We will check if today is Monday, if so we'll move last monday's backup 1 directory up
if [ "$TODAY" -eq "1" ]; then
if [ -d "$DAILYPATH$SEVENDAYSAGO" ]; then
mv $DAILYPATH$SEVENDAYSAGO $WEEKLYPATH
mv $DAILYPATH$SEVENDAYSAGO.log $WEEKLYPATH
fi
#Checking for a backup older than 28 days and also from first Monday of the month.If the backup from 28 days ago is not the firstMondayOfTheMonth we'll expire, or else we'll keep it within the monthly directory.
if [ -d "$WEEKLYPATH$FIVEWEEKSAGO" ]; then
#Is it from first Monday of the month?
if [ "$AUX" -le "7" ]; then
mv $WEEKLYPATH$FIVEWEEKSAGO $MONTHLYPATH
mv $WEEKLYPATH$FIVEWEEKSAGO.log $MONTHLYPATH
#Execute function to find out exact date for 5 months ago
FIVEMONTHSAGODAY=$(date5monthsago)
#Expire backups older than 4 months ago. Note that FIVEMONTHSAGODAY has only 1 digit,that is why we add "0" before.
if [ -d "$MONTHLYPATH${FIVEMONTHSAGOYEAR}-${FIVEMONTHSAGOMONTH}-0${FIVEMONTHSAGODAY}" ]; then
rm -rf $MONTHLYPATH${FIVEMONTHSAGOYEAR}-${FIVEMONTHSAGOMONTH}-0${FIVEMONTHSAGODAY}
rm $MONTHLYPATH${FIVEMONTHSAGOYEAR}-${FIVEMONTHSAGOMONTH}-0${FIVEMONTHSAGODAY}.log
fi
#If 35 days ago backups is not the first monday of the month, expire it
else
rm -rf $WEEKLYPATH${FIVEWEEKSAGO}
rm $WEEKLYPATH${FIVEWEEKSAGO}.log
fi
fi
#If today is NOT Monday we'll expire old daily backups
else
#Is there a backup from 1weekago? If so we'll exxpire it
if [ -d "$DAILYPATH$SEVENDAYSAGO" ]; then
rm -rf $DAILYPATH$SEVENDAYSAGO
#Remove log file also
rm $DAILYPATH${SEVENDAYSAGO}.log
fi
fi
#Umount the NFS
umount /backup
else
echo "NFS couldn't be mounted" >> /tmp/mount.log
fi