#backup.sh
-- 重要变量名 作用
LogFile 日志文件
SourceDir 需要备份的目录
BakDir 备份至此目录
RetainDay 备份档案文件保留天数
ProjectLst 备份项目文件配置文件
代码:
#!/bin/bash
#author: InBi
#date: 2011-07-28
#role: backup files periodically.
#website: http://www.itwhy.org/2011/07-28/707.html
##################################################
LogFile=/backup/log/`date +"%Y-%m"`.log
DATE=`date +"%Y-%m-%d"`
SourceDir=/var/RDfile
BakDir=/backup
RetainDay=20
ProjectLst=/backup/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
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 Old File Success!" >> $LogFile
fi
echo " " >> $LogFile
done
echo "--------------------------------------------------" >> $LogFile
echo "backup end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
echo " " >> $LogFile
exit 0
# project.lst :
这个文件比较好理解,就是一个text文件,它的每一行表示一个项目,也就是你需要备份的目录下面的文件或目录。
onu_arm
onu_voip
onu_wifi
5xxx
7xxx
8xxx
nms
crontab定时执行backup.sh