linux下备份脚本主之 遍历目录备份所有二级目录文件

linux下备份脚本主之 遍历目录备份所有二级目录文件

1,备份脚本如下,我这里以备份CVS目录为例,总CVS目录下有几个Project,统统备份

并且保留20份原始文档,恢复也很简单,拷贝回去接压缩即可,特别是权限等也不受到影响,

另外还有日志文件全程跟踪,你可以了解备份的具体时间,和期间遇到的问题。

 

 #!/bin/bash

#author:        JerryMin
#date:          2013-03-08
#role:          backup files periodically.
##################################################
LogFile=/root/cvsbakup/log/`date +"%Y-%m-%d"`.log
SourceDir=/cvs
BakDir=/cvsbak
RetainDay=20
ProjectLst=/root/cvsbakup/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 "--------------------------------------------------" >>$LogFile
  echo " backup file have exist !" >>$LogFile
  echo "--------------------------------------------------" >>$LogFile
 else
  echo "`date +"%Y-%m-%d %H:%M:%S"` cp log begin !" >>$LogFile
  echo "--------------------------------------------------" >>$LogFile
  cp -a $ProjectData $DestDir  >/dev/null  2>&1
  echo "--------------------------------------------------" >>$LogFile
  echo "`date +"%Y-%m-%d %H:%M:%S"` tar log begin !" >>$LogFile
  echo "--------------------------------------------------" >>$LogFile
  tar -zcvf $PackFile $Project  >>$LogFile 2>&1
  echo "--------------------------------------------------" >>$LogFile
  echo "`date +"%Y-%m-%d %H:%M:%S"` backup $Project done into $PackFile" >>$LogFile
  echo "--------------------------------------------------" >>$LogFile
  echo "`date +"%Y-%m-%d %H:%M:%S"` begin delete project" >>$LogFile
  rm -rf $Project
 fi
done
echo "--------------------------------------------------" >>$LogFile
echo "`date +"%Y-%m-%d %H:%M:%S"` find file retainday over 20 days" >>$LogFile
find $Bakdir -type f -mtime +$RetainDay -name "*.$Project.tgz" -exec rm {} \;  >/dev/null 2>&1
echo "--------------------------------------------------" >>$LogFile
echo "backup end at $(date +"%Y-%m-%d %H:%M:%S")" >>$LogFile
exit 0
2,上面这个脚本是本地备份,如果想异地备份
我这里也有几个办法,一个是NFS,另一个是SAMBA,还有一个是FTP,想NFS,SAMBA只需要挂载过来
即可,比如我们这里的/cvsbak,可是是挂载网络上的NFS,SAMBA目录,只要权限设置妥当,完全没有问题。
最后FTP,其实用smbclient访问时跟FTP差不多,在脚本的最后加上,登录的用户名密码再PUT上去即可,比如FTP的脚本如下:
 

你可能感兴趣的:(linux,linux备份脚本)