#!/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
阅读(522) | 评论(0) | 转发(0) |