Mars backup

From Teknologisk videncenter
Revision as of 20:21, 12 November 2011 by Rael (talk | contribs) (Reverted edits by Rael (talk) to last revision by Heth)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

cron

Note
See the backup script below
0 2 * * * /home/heth/wiki_incremental_backup >/dev/null 2>/tmp/buerror.log

Restore

Restore fx. the 7'th of december 2011

cd /
tar -g /mnt/mercstor/mars/backup/2011/12/mars07112011.snar -xvzf /mnt/mercstor/mars/backup/2011/12/bu07112011.tar

Check backup

Check and compare backup from the 7'th of december 2011 to current

mkdir /tmp/restore
cd /tmp/restore
tar -g /mnt/mercstor/mars/backup/2011/12/mars07112011.snar -xvzf /mnt/mercstor/mars/backup/2011/12/bu07112011.tar
BACKUP="/var/www/data /var/www/wiki /var/www/mrbs /var/www/robots.txt /etc/apache2 /usr/local/etc/sphinx.conf"
for i in $BACKUP
do
  echo "Comparing /$i and `pwd`/$i"
  diff -rq /$i ./$i
done

Backup script

/usr/heth/wiki_incremental_backup

#!/bin/bash
#Backupscript for mars.tekkom.dk on ubuntu
#heth@mercantec.dk 03.11.2011
#Version 1.1
#Revision log: 07.11.2011 Changed tar to differential backup
BACKUP="/var/www/data /var/www/wiki /var/www/mrbs /var/www/robots.txt /etc/apache2 /usr/local/etc/sphinx.conf"
LOG="/tmp/bulog.txt"
ERRORLOG=/tmp/buerror.log
BUDIR="/mnt/mercstor/mars/backup"
BUFS="/mnt/mercstor"
DATE=`date +%d`
MONTH=`date +%m`
YEAR=`date +%Y`
SUBJECT="Mars backup succeded..."
TIMESTART=`date +%s`
MAILTO="heth@mercantec.dk"

if test -f $LOG
then
  rm $LOG
fi

if test -f $ERRORLOG
then
 rm $ERRORLOG
fi

echo "=================================================" > $LOG
if cmp -s $0 $BUDIR/backup_script
then
 echo "Program: $0" >> $LOG
else
 cp $0 $BUDIR/backup_script
 echo "Program: $0 (Updated)" >> $LOG
fi
echo "Time: `date`" >> $LOG
echo "Backing up $BACKUP" >> $LOG
TIMESTAMP=`date +%d%m%Y-%H%M`
#Check remote filesystem mounted
if df $BUFS >> /dev/null
then echo "$BUFS mounted" >> $LOG
else
  echo "ERROR: $BUFS not mounted" >> $LOG
echo "=================================================" > $LOG
 cat $LOG | mail -s "Mars backup FAILED" $MAILTO
 exit 1
fi

#Backups are organized in $BUDIR/year/month directories. Fx. $BUDIR/2011/12
#Check that directory for this year exist
if test ! -d $BUDIR/$YEAR
then mkdir $BUDIR/$YEAR
fi

#Check that directory for this month exist
if test ! -d $BUDIR/$YEAR/$MONTH
then mkdir $BUDIR/$YEAR/$MONTH
fi

########## Wiki database backup
#Close mediawiki databse for writing
cp /var/www/mediawiki/LocalSettings.php /var/www/mediawiki/LocalSettings.php.org
echo "$wgReadOnly = '<b>Backup operation in progress. Please wait a few minutes.</b>'" >>/var/www/mediawiki/LocalSettings.php

if mysqldump -u USERNAME -pPASSWORD --default-character-set=latin1 wikidb -c\
  > $BUDIR/$YEAR/$MONTH/wikidb${DATE}${MONTH}${YEAR}.dump
then
  sync
  echo -en "WIKIDB database backup succeded\tSize: " >> $LOG
  chmod 0400 $BUDIR/$YEAR/$MONTH/wikidb${DATE}${MONTH}${YEAR}.dump
  du -h $BUDIR/$YEAR/$MONTH/wikidb${DATE}${MONTH}${YEAR}.dump | cut -f1 >> $LOG
else
 echo "ERROR: Backup of WIKIDB failed" >> $LOG
 SUBJECT="Mars backup failed..."
fi
cp /var/www/mediawiki/LocalSettings.php.org /var/www/mediawiki/LocalSettings.php

########## MRBS database backup
if mysqldump -u USERNAME -pPASSWORD --default-character-set=latin1 mrbs -c\
  > $BUDIR/$YEAR/$MONTH/mrbs${DATE}${MONTH}${YEAR}.dump
then
  sync
  echo -en "MRBS database backup succeded\tSize: " >> $LOG
  chmod 0400 $BUDIR/$YEAR/$MONTH/mrbs${DATE}${MONTH}${YEAR}.dump
  du -h  $BUDIR/$YEAR/$MONTH/mrbs${DATE}${MONTH}${YEAR}.dump | cut -f1 >> $LOG
else
 echo "ERROR: Backup of MRBS failed" >> $LOG
 SUBJECT="Mars backup failed..."
fi

#Daily backups for the entire month exists in the $BUDIR/$YEAR/$MONTH
#Incremental backups with tar using --listed-incremental
# Changed to differential using copy of level 0 snar file each day

if test ! -f $BUDIR/$YEAR/$MONTH/mars${MONTH}${YEAR}.snar
then #First backup this month create level 0 snar file
  if tar -czf $BUDIR/$YEAR/$MONTH/bu${DATE}${MONTH}${YEAR}.tar\
   -g $BUDIR/$YEAR/$MONTH/mars${MONTH}${YEAR}.snar $BACKUP
  then
   sync
   echo -en "WIKI files level 0 backup succedded\tsize: " >> $LOG
   chmod 0400 $BUDIR/$YEAR/$MONTH/bu${DATE}${MONTH}${YEAR}.tar
   chmod 0400 $BUDIR/$YEAR/$MONTH/mars${MONTH}${YEAR}.snar
   du -h $BUDIR/$YEAR/$MONTH/bu${DATE}${MONTH}${YEAR}.tar | cut -f1 >> $LOG
  else
   echo "ERROR: Level 0 Backup of WIKI files failed" >> $LOG
   SUBJECT="Mars backup failed..."
  fi
else #Create daily level 1 backup from copy of level 0 snar file
  cp $BUDIR/$YEAR/$MONTH/mars${MONTH}${YEAR}.snar $BUDIR/$YEAR/$MONTH/mars${DATE}${MONTH}${YEAR}.snar
  # On /mnt/merstor logged in as heth user 1001:1001
  chmod 600 $BUDIR/$YEAR/$MONTH/mars${DATE}${MONTH}${YEAR}.snar
  if tar -czf $BUDIR/$YEAR/$MONTH/bu${DATE}${MONTH}${YEAR}.tar\
   -g $BUDIR/$YEAR/$MONTH/mars${DATE}${MONTH}${YEAR}.snar $BACKUP
  then
   sync
   echo -en "WIKI files level 1 backup succedded\tsize: " >> $LOG
   chmod 0400 $BUDIR/$YEAR/$MONTH/bu${DATE}${MONTH}${YEAR}.tar
   chmod 0400 $BUDIR/$YEAR/$MONTH/mars${DATE}${MONTH}${YEAR}.snar
   du -h $BUDIR/$YEAR/$MONTH/bu${DATE}${MONTH}${YEAR}.tar | cut -f1 >> $LOG
  else
   echo "ERROR: Level 1 Backup of WIKI files failed" >> $LOG
   SUBJECT="Mars backup failed..."
  fi
fi
TIMEEND=`date +%s`
echo -e "Backup finished at `date` it took `expr $TIMEEND - $TIMESTART` seconds." >> $LOG
echo "=================================================" >> $LOG
sync
sleep 1
if test -f $ERRORLOG
then
 SUBJECT="$SUBJECT - ERRORLOG attached.."
 echo -e "\n ERRORLOG:">> $LOG
 cat $ERRORLOG >> $LOG
 echo "=================================================" >> $LOG
fi
cat $LOG | mail -s "$SUBJECT" $MAILTO
exit 0

</source>