#!/bin/bash
#设置日志文件,前提建好了/backup/log目录
LogFile=/backup/log/`date +"%Y-%m"`.log
#备份源目录
SourceDir=/cvs
#备份目标
BakDir=/backup
#保存20天过期自动删除
RetainDay=20
#备份的内容可以写进project.lst,如cvs目录下有a,b,c三个目录,project.lst填写多少就备份多少,下面是备份的
#具体脚本,其核心是tar打包,并把重要的内容记录到日志文件里
ProjectLst=/backup/project.lst
##################################################
DATE=`date +"%Y-%m-%d"`
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
done
echo "--------------------------------------------------" >>$LogFile
echo "backup end at $(date +"%Y-%m-%d %H:%M:%S")" >>$LogFile
echo " " >> $LogFile
##################################################
#下面的内容就是把刚才备份的内容传到服务器上,前提是你有一个可以访问到底FTP服务器
#put backup to ftp server
HOST=192.168.110.111
FTP_USERNAME=ftpuser
FTP_PASSWORD=123456
cd $BakDir
echo "start open ftp serverat $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
echo "--------------------------------------------------" >>$LogFile
/usr/bin/ftp -in <<EOF
open $HOST
user $FTP_USERNAME $FTP_PASSWORD
put $DATE.$Project.tgz
bye
EOF
echo "put ftp end at $(date +"%Y-%m-%d %H:%M:%S")" >>$LogFile
#最后上传完毕后再查看本地备份大于20天的自动删除,这样就可以实现本地异地双备份
find $Bakdir -type f -mtime +$RetainDay -name "*.$Project.tgz" -exec rm {} \; >/dev/null
exit 0
#最后我们还可用crontab做个周期性计划,比如每周一次全备份
#59 23 * * 6 /home/backup.sh
Linux下自动备份脚本并上传到ftp服务器
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.heiqu.com/ca313172d6cc9b49eeea577bcddb6aee.html