灵活性比较强的tar备份脚本

来源:百度文库 编辑:神马文学网 时间:2024/03/29 23:59:44
自定义备份源目录和目标目录
自定义保存包数量(需要修改使用)
三种类型备份(全量/增量/差分)
邮件状态通知
日志记录
错误处理
备份完成通知

#!/bin/bash

# For Usage
usage(){
echo "Uasge:`basename $0` [-t][type] [-s][SRCDIR] [-d][DESDIR] [-i][date] [-n][number]  [-x][extend]"
echo
echo "-t What kind of backup ? type is  full or incr or diff"
echo "   full is full backup"
echo "   incr is incremental backup"
echo "   diff is difference backup"
echo "-s Which directory bakcup from ,use full path"
echo "-d Which directory backup to , use full path"
echo "-n How many days to backup only for incr backup"
echo "-i What time begin to backup only for diff backup"
echo "-x dirctory no to backup"
echo "   as if: grep -v -e lost+found"
echo
echo "For Example:"
echo "bk.sh -t full -s /svn  -d /backup/full -x \"grep -v -e lost+found\""
echo "bk.sh -t incr -s /svn  -d /backup/incremantal -x \"grep -v -e lost+found\"  -n -2 "
echo "bk.sh -t diff -s /svn  -d /bakcup/difference -x \"grep -v -e lost+found\" -i 2006-04-25"
exit
}

if [ ${#} -lt 2 ]; then
usage
fi

# FOr vars
while getopts t:s:d:n:i:x: OPTION
do
  case $OPTION in
        t)   TYPE=$OPTARG   ;;
        s) SRCDIR=$OPTARG   ;;
        d) DESDIR=$OPTARG   ;;
        n)   DAYS=$OPTARG   ;;
        i)  TIMES=$OPTARG   ;;
        x) EXLIST=$OPTARG   ;;
  esac
done

# For file name
TARNAME=`date +'%Y-%m-%d'`.tar

# For backup type
case $TYPE in
        full)
        echo "Begin to full backup ......"
        # From and TO
        if [ -d $SRCDIR -a -d $DESDIR ] ; then
        # Enter directory
        cd $SRCDIR
        # Backup operation
        tar -cvpf  $DESDIR/$TARNAME  $(ls $SRCDIR | $EXLIST )  \
        > /tmp/report.txt
        # Report
        echo "Done!!"
        echo
        else
        # Error
        usage
        exit
        fi
        ;;
        incr)
        echo "Begin to incremental backup ......"
        # For directory From and To
        if [ -d $SRCDIR -a -d $DESDIR ] ;then
        # Enter directory
        cd $SRCDIR
        # Before days .default is one day
        if [ -z "$DAYS" ] ; then
        DAYS="-1"
        fi
        # perference
        OPT=" -N "`date -d "${DAYS} day" +'%Y-%m-%d'`
        echo $OPT ;
        # backup operation
        tar ${OPT} -cvpf $DESDIR/$TARNAME $(ls $SRCDIR | $EXLIST) \
        > /tmp/report.txt
        # Report
        echo "Done!!"
        echo
        else
        # Error
        usage
        exit
        fi

        ;;

        diff)
        echo "Begin to incremental backup ......"
        # For directory From and To
        if [ -d $SRCDIR -a -d $DESDIR ] ;then
        # Enter direcotry
        cd $SRCDIR
        # Times
        if [ -z $TIMES ];then
        TIMES=`date +'%Y-%m-1'`
        fi
        # perference
        OPT=" -N "$TIMES
        # backup operation
        tar ${OPT} -cvpf $DESDIR/$TARNAME $(ls $SRCDIR | $EXLIST) \
        > /tmp/report.txt
        # Report
        echo "Done!!"
        echo
        else
        # Error
        usage
        exit
        fi
        ;;
esac

# if you only a directory for backup , and the same time want
# to keep three bakcups
#cd $DESDIR
#NUM=`ls . | wc -l`
#if [ $NUM -gt 3 ] ; then
#ls . | head -n 1 | xargs rm -f
#fi
#echo "Done!"
#echo
# Mail to root
if [ -x /bin/mail ] ; then
  if [ -x /usr/sbin/sendmail ] ; then
  echo "Begin to mail to root ......"
  SUBJECT="Backup operation is finished on "`date +'%Y-%m-%d'`
  if [ -f /tmp/report.txt ] ;then
    /bin/mail -s $SUBJECT postmaster@zz.cxm < /tmp/report.txt
    rm -f /tmp/report.txt
    echo "Done!"
    echo
  else
    echo "Can't Done!"
    echo
  fi
  fi
fi

# Logger to /var/log/messages
if [ -x /usr/bin/logger ] ; then
echo "Begin to log in /var/log/messages ......"
SUBJECT="Backup operation is finished on "`date +'%Y-%m-%d'`
/usr/bin/logger $SUBJECT
echo "Done!"
echo
fi

# Report
if [ -f $DESDIR/$TARNAME ] ; then
echo "Congratulations!!!"
echo "The backup is finished"
else
echo "Sorry!!!"
echo "The bakcup is failure,please check it out"
fi