Image

Image

Search This Blog

Sunday, February 07, 2010

Another Backup

Versiunea care foloseste tar (pentru ca cpio din debilian moare cand ti-e lumea mai draga si nu vreau sa pun cpio din surse)



#!/bin/sh
#       @(#) AUTOTBACK 2.8 09/12/09
# 
# 09/12/2009 (sorin@xxxxxxxx.com) mod for debian5. remove hit report. changed to use TAR
# 28/08/2008 (sorin@xxxxxxxx.com) modified for rhel5. skip /proc /sys /tmp
# modified 9/14/2000 to provide hit reporting on web site
#
#       Usage  -Command to be incorporated in cron of root for off Peak
#               automatic system TAR Backup.
#               Generates report in /var/autotback directory, mail and prints it.
#
#

####### Define Local Variables ################
RECIPIENT1=service@xxxxxxxx.com
RECIPIENT2=sorin@xxxxxxxx.com
PRINTER=HP4050
TAPE=/dev/st0
BACKDIR=/
EXCLUDE=/sys/*
EXCLUDE1=/proc/*
EXCLUDE2=/dev/*
EXCLUDE3=/var/run/*
EXCLUDE4=/tmp/*

###############################################

# System variables
LOCK=/var/autotback/backlock
DEFAULTDIR=/var/autotback
REPORTFILE=/var/autotback/backrep
TEMPFILE=/var/autotback/backtmp
TEMPFILE2=/var/autotback/backtmp2
ALLIST=/var/autotback/allist
LIST=/var/autotback/list
ERRORMSG="Another backup is running or the last backup has not finished well. Please check"
DELIMIT="================================================================================"

# define a remove lock and set a trap on exit
remove_lock()
{
       rm -f $LOCK
       }
# make sure we release the lock no matter how we exit, when we exit
trap remove_lock 0
# Check if another backup is running or if the script crashed in such way that the lock is still there, clear the lock
if [ -r $LOCK ]
then
 DIFF=`echo "\`date +%s\`-\`cat $LOCK\`" | bc`
 if [ $DIFF -gt 86400 ]; then echo "More than one day since backup is NOT running, autoclean..." >> $REPORTFILE; rm -f $LOCK; fi
 sed -i -e "1i\ $ERRORMSG" -e "1i\ $DELIMIT" -e "1i\ " $REPORTFILE
 echo $ERRORMSG >> $REPORTFILE
 echo $REPORTFILE | mail -s "Backup Failure on $HOSTNAME" $RECIPIENT1 $RECIPIENT2
 lp -d $PRINTER $REPORTFILE
 exit 5
fi

# Cleanup obsolete traces
cd $BACKDIR
rm -rf $DEFAULTDIR
if [ ! -d $DEFAULTDIR ]
then
       mkdir -p $DEFAULTDIR
fi

# Create lockfile
echo "`date +%s`">$LOCK

# Start backup
echo "                           Automatic Backup Report
(c)1991-2010 Xxxxxxxx Xxxxxxxxx Inc.
$DELIMIT

Backup of $BACKDIR on $HOSTNAME started at `date`
">>$REPORTFILE

# Create a list of what we have to backup and exclude
#cd $BACKDIR
#find . -depth -print  >$ALLIST
#grep -vE ".$EXCLUDE|.$EXCLUDE1|.$EXCLUDE2|.$EXCLUDE3|.$EXCLUDE4" $ALLIST >$LIST
echo "BACKUP started on `date`">$TEMPFILE

# Start saving at the begining of the tape
mt -f $TAPE rewind 2>>$TEMPFILE2
#cat $LIST | cpio -ocvB >$TAPE 2>>$TEMPFILE
tar --exclude=$EXCLUDE --exclude=$EXCLUDE1 --exclude=$EXCLUDE2 --exclude=$EXCLUDE3 --exclude=$EXCLUDE4 -cvpf $TAPE $BACKDIR >>$TEMPFILE 2>>$TEMPFILE2
backstat=$?
tail $TEMPFILE2 >>$REPORTFILE
echo "
Exit status = $backstat
">>$REPORTFILE
if [ $backstat -eq 0 ]
then
 echo "*** Backup COMPLETED OK ***">>$REPORTFILE
fi
if [ $backstat -eq 1 ]
then
 echo "*** Backup may be incomplete or have missing files ***">>$REPORTFILE
fi
if [ $backstat -eq 2 ]
then
 echo "*** Backup has encounter a fatal error. Please check ***">>$REPORTFILE
fi
echo "Tape Save finished on `date`
$DELIMIT
">>$REPORTFILE
# Check total nr of blocks reported by cpio
#set `tail -1 $TEMPFILE`
#BACKTOT=$1
#echo "Total of amount of Backup was $1 blocks.
echo "Verify of $BACKDIR on $HOSTNAME started at `date`
">>$REPORTFILE
# Tape rewind / Verify
mt -f $TAPE rewind 2>>$TEMPFILE2
# cpio -icvtB <$TAPE >>$TEMPFILE 2>>$TEMPFILE2
tar --exclude=$EXCLUDE --exclude=$EXCLUDE1 --exclude=$EXCLUDE2 --exclude=$EXCLUDE3 --exclude=$EXCLUDE4 --exclude=$EXCLUDE -tpvf $TAPE >>$TEMPFILE 2>>$TEMPFILE2
verstat=$?
tail $TEMPFILE2 >>$REPORTFILE
echo "
Exit status = $verstat
">>$REPORTFILE
if [ $verstat -eq 0 ]
then
 echo "***  Verify COMPLETED OK ***
">>$REPORTFILE
if [ $verstat -eq 1 ]
then
 echo "***  Verify may be incomplete ***
">>$REPORTFILE
if [ $verstat -eq 2 ]
then
 echo "***  Verify has encounter a fatal error. Please check ***
">>$REPORTFILE
cat $TEMPFILE >> $REPORTFILE
fi
echo "Tape Verify finished on `date`
">>$REPORTFILE
# Check nr of verified blocks reported by cpio and send warning if not ok
#set `tail -1 $TEMPFILE2`
#VERTOT=$1
#if [ "$BACKTOT" != "$VERTOT" -o $backstat -ne 0 -o $verstat -ne 0 ]
#then
#        echo "*** WARNING BACKUP TRANSFER TOTALS DID NOT MATCH ***">>$REPORTFILE
#        echo "***              PLEASE VERIFY                   ***
# ">>$REPORTFILE
#else
#        echo "*** Transfer Totals Concur ***
# ">>$REPORTFILE
#fi
echo $DELIMIT >>$REPORTFILE

# Send / Print the report
cat $REPORTFILE | mail -s "Backup Report $HOSTNAME" $RECIPIENT1 $RECIPIENT2
lp -d $PRINTER $REPORTFILE

# Remove lock, eject and exit
sleep 5
rm -f $LOCK
mt -f $TAPE rewoffl 2>>$TEMPFILE2
exit 0

No comments:

Post a Comment

Blog Archive