- 论坛徽章:
- 0
|
带注释版- #!/bin/bash
- #Copyright(c) 2003-2008 Wang Pengcheng(qianjigui@gmail.com)
- #Backup the important files to the remove-disk
- #Version 0.1.0
- #Support the mtime change, and using the 'find' to increase the behaviour.
- #version 0.0.1
- #basic method
-
- ##############Print the version and the usage#############
- echo 'BackupInfo version 0.1.0'
- echo 'Copyright 2001-2008 by Wang Pengcheng(qianjigui@gmail.com)'
- echo 'Usage: backupInfo [mtime]'
- ###################################################
-
- #the arguments for the rsync
- opts="-a --progress --delete"
- #######################
-
- ######Get the src,dist root Dir##########################
- srcRoot='/home/wpc' //源路径
- distRoot='/media/WPC_/backup' //目标路径
- ##################################################
-
- #########Get backup file List##########################
- runPath='/home/wpc/workspace/MyLib/Shell/bin' //应该是作者自己的一个shell脚本文件路径
- dicListPath="$runPath/dicList.txt" //
- dicList=`cat $dicListPath` //需要备份的路径
- lastBackTime="$runPath/backupTime.log" //最近一次备份的时间
- if test -e $lastBackTime -a $# -lt 1 //判断一储存最近一次备份时间的文件存在并且参数个数小于1
- then
- mMin=`cat $lastBackTime` //获取上次备份时间
- nowTimeS=`date +%s` //获取当前时间自unix纪元起的总秒数
- mMin=`expr $nowTimeS - $mMin` //获取时间差
- mMin=`expr $mMin / 60` //换成分钟
- mMin=`expr $mMin + 10` //加十分钟
- findOpts="-amin +$mMin" //用作find判断的比较时间,距现在mMin分钟前
- else //开始判断二$#等于1
- if [ $# -eq 1 ]
- then
- mDay=$1 //mDay等于第一个参数
- else //
- mDay=3 //mDay等于3
- fi //结束判断二
- findOpts="-mtime +$mDay" //用作find判断的比较时间,距现在mDay天前
- fi //结束判断一 这一段主要是判断要备哪段时间的数据
- findOpts="$findOpts -type f" //find -type f
- echo "The exclude files FIND Pattern: $findOpts"
-
- #################Backup the file#####
- for dirName in $dicList //遍历所有需要备份的目录
- do
- echo
- echo "***Backup the dictionary $dirName***"
- srcD="$srcRoot/$dirName/" //拼凑需备份的绝对目录名
- distD="$distRoot/$dirName" //拼凑目标绝对目录名
-
- if [ -e $distD ] //判断目标文件夹是否已经存在
- then
- echo "The $distD exists."
- else
- echo "Create the dictionary: $distD..."
- mkdir -p $distD //递归建立目录
- fi
-
- ##Create the exclude patterns
- timeC=`date +%s` //时间,格式同前
- regPatternFile="/tmp/regPatterFile$timeC.txt" //定义储存备份文件名的文件
-
- find $srcD $findOpts | sed "s|$srcD||" > $regPatternFile //取出要备份的文件名部分,看不懂得可以参考sed的用法
- excludePattern="--exclude-from=$regPatternFile" //拼凑参数
-
- #echo "rsync $opts $excludePattern $srcD $distD" //这种就不用说了
- rsync $opts $excludePattern $srcD $distD | grep -n '[^/] //备份并顺便输出行号
-
- #delete tmp files //
- rm $regPatternFile //删除临时文件
- echo "***Backup Finish***"
- done
-
- #Write information to the log file
- echo
- echo "Write Log to the $lastBackTime ..." //更新最后一次备份时间
- date +%s > $lastBackTime
- echo "Save Log finished."
复制代码 |
|