#!/bin/bash
#設定環境預設語言
export LANG=C
#獲取執行時的星期
week=`date | cut -d" " -f1`
#取得執行時間
run_time=`date +%Y%m%d%H%M`
#備份來源目錄
sdir="/home"
#備份存放目錄
ddir="/bk"
#寄信通知
mailto="ben@ub-net.com.tw"
#備份保留天數
ex_day='14'
#檢查備份存放目錄
[ ! -d "$ddir" ] && mkdir "$ddir"
#建立備份函式
full_bk () {
 tar jcpvf "full-$(basename $1)-$run_time".tar.bz2  "$1" &> /tmp/mail.txt
 touch $1/full-bak.lock
 mail -s "$HOSTNAME backup in $run_time stat" "$mailto" < /tmp/mail.txt
 [ -f "full-$(basename $1)-$run_time".tar.bz2 ] && mv "full-$(basename $1)-$run_time".tar.bz2 "$2"
    }

diff_bk () {
 if [ -f  "$1/full-bak.lock" ]
  then
   find "$1" -cnewer "$1/full-bak.lock" -a -type f   -exec tar -rpvf "diff-$(basename $1)-$run_time".tar {} \; &> /tmp/mail.txt
   touch "$1/diff-bak.lock"
   [ -f "diff-$(basename $1)-$run_time".tar ] && bzip2 "diff-$(basename $1)-$run_time.tar"
          mail -s "$HOSTNAME backup in $run_time stat" "$mailto" < /tmp/mail.txt
          [ -f "diff-$(basename $1)-$run_time".tar.bz2 ] && mv "diff-$(basename $1)-$run_time".tar.bz2 "$2"

  else
          tar jcpvf "full-$(basename $1)-$run_time".tar.bz2  "$1" &> /tmp/mail.txt
          touch $1/full-bak.lock
          mail -s "$HOSTNAME backup in $run_time stat" "$mailto" < /tmp/mail.txt
          [ -f "full-$(basename $1)-$run_time".tar.bz2 ] && mv "full-$(basename $1)-$run_time".tar.bz2 "$2"
 fi
    }

inc_bk () {
 if [ ! -e  "$1/full-bak.lock"  -a  ! -e  "$1/diff-bak.lock" ]
  then
          tar jcpvf "full-$(basename $1)-$run_time".tar.bz2  "$1" &> /tmp/mail.txt
          touch "$1/full-bak.lock"
          mail -s "$HOSTNAME backup in $run_time stat" "$mailto" < /tmp/mail.txt
          [ -f "full-$(basename $1)-$run_time".tar.bz2 ] && mv "full-$(basename $1)-$run_time".tar.bz2 "$2"
  elif [ "$1/full-bak.lock" -nt "$1/diff-bak.lock" ]
   then
                         find "$1" -cnewer "$1/full-bak.lock" -a -type f  -exec tar -rpvf "inc-$(basename $1)-$run_time".tar {} \; &> /tmp/mail.txt
                         touch "$1/diff-bak.lock"
    [ -f "inc-$(basename $1)-$run_time".tar ] && bzip2 "inc-$(basename $1)-$run_time".tar
                         mail -s "$HOSTNAME backup in $run_time stat" "$mailto" < /tmp/mail.txt
                         [ -f "inc-$(basename $1)-$run_time".tar.bz2 ] && mv "inc-$(basename $1)-$run_time".tar.bz2 "$2"

  else
                                find "$1" -cnewer "$1/diff-bak.lock" -a -type f  -exec  tar -rpvf "inc-$(basename $1)-$run_time".tar {} \; &> /tmp/mail.txt
                                touch "$1/diff-bak.lock"
    [ -f "inc-$(basename $1)-$run_time".tar ] && bzip2 "inc-$(basename $1)-$run_time".tar
                                mail -s "$HOSTNAME backup in $run_time stat" "$mailto" < /tmp/mail.txt
                                [ -f "inc-$(basename $1)-$run_time".tar.bz2 ] && mv "inc-$(basename $1)-$run_time".tar.bz2 "$2"
  fi 
   }

case $week in
  Sun)
 full_bk "$sdir" "$ddir"
 find "$ddir" -type f -name "*.tar.bz2" -mtime +"$ex_day" -exec rm {} \;
 ;;
  Mon)
 inc_bk "$sdir" "$ddir"
 find "$ddir" -type f -name "*.tar.bz2" -mtime +"$ex_day" -exec rm {} \;
 ;;
  Tue)
 inc_bk  "$sdir" "$ddir"
 find "$ddir" -type f -name "*.tar.bz2" -mtime +"$ex_day" -exec rm {} \;
 ;;
  Wed)
 diff_bk "$sdir" "$ddir"
 find "$ddir" -type f -name "*.tar.bz2" -mtime +"$ex_day" -exec rm {} \;
 ;;
  Thu)
 inc_bk "$sdir" "$ddir"
 find "$ddir" -type f -name "*.tar.bz2" -mtime +"$ex_day" -exec rm {} \;
 ;;
  Fri)
 inc_bk "$sdir" "$ddir"
 find "$ddir" -type f -name "*.tar.bz2" -mtime +"$ex_day" -exec rm {} \;
 ;;
  Sat)
 inc_bk "$sdir" "$ddir"
 find "$ddir" -type f -name "*.tar.bz2" -mtime +"$ex_day" -exec rm {} \;
esac

創作者介紹

Ben 的 IT 學習之不歸路網誌

ben88168 發表在 痞客邦 PIXNET 留言(0) 人氣()