Chinaunix首页 | 论坛 | 博客
  • 博客访问: 694402
  • 博文数量: 148
  • 博客积分: 4086
  • 博客等级: 上校
  • 技术积分: 1766
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-06 23:27
文章分类

全部博文(148)

文章存档

2013年(19)

2012年(9)

2011年(106)

2009年(14)

分类: LINUX

2011-10-24 17:37:30

#!/bin/bash
#author: renxiaobin
#date: 2011-10-18
#role: backup files periodically.
##################################################
LogFile=/var/www/wikibackup/log/`date +"%Y-%m"`.log
DATE=`date +"%Y-%m-%d"`
Year=`date +"%Y"`
Month=`date +"%m"`
Day=`date +"%d"`
SourceDir=/var/www
BakDir=/var/www/wikibackup
RetainDay=7
ProjectLst=/var/www/wikibackup/project.lst
##################################################
echo "backup start at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
echo "--------------------------------------------------" >> $LogFile
cd $BakDir
PROJECTLIST=`cat $ProjectLst`
for Project in $PROJECTLIST
do
 ProjectData=$SourceDir/$Project
 DestDir=$BakDir/$Project
 PackFile=$DATE.$Project.tgz
 if [ -f $BakDir/$PackFile ]
 then
  echo "backup file have exist !" >>$LogFile
 else
  cp -RHpf $ProjectData $DestDir > /dev/null
  tar -zcvf $PackFile $Project > /dev/null
  echo "backup $Project done into $PackFile ">>$LogFile
  if [ "$Day" = "01" ]
  then
   PackMonthFile=$Year$Month.$Project.tgz
   tar -zcvf $PackMonthFile $Project > /dev/null
   echo "backup $Project done into $PackMonthFile ">>$LogFile   
  fi
  if [ "$Month" = "01" ]
  then
   PackYearFile=$Year.$Project.tgz
   tar -zcvf $PackYearFile $Project > /dev/null
   echo "backup $Project done into $PackYearFile ">>$LogFile    
  fi
  rm -rf $Project
 fi

 OldFile="$BakDir/"$(date --date="$RetainDay days ago" +"%Y-%m-%d").$Project.tgz
 if [ -f $OldFile ]
 then
  rm -f $OldFile > /dev/null
  echo "[$OldFile],Delete File 7 Days Ago Success!" >> $LogFile
 fi
 echo " " >> $LogFile
 let "tempValue=Year-1"
 OldMonthFile="$BakDir/"$tempValue$Month.$Project.tgz
 if [ -f $OldMonthFile ]
 then
  rm -r $OldMonthFile > /dev/null
  echo "[$OldMonthFile],Delete Month File 1 year Ago Success!" >> $LogFile
 fi
done
echo "--------------------------------------------------" >> $LogFile
echo "backup end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
echo " " >> $LogFile
exit 0

阅读(534) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~