定期备份指定目录下多种日志类型的脚本(Bash Shell)_其他_操作系统_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 操作系统 > 其他 > 定期备份指定目录下多种日志类型的脚本(Bash Shell)

定期备份指定目录下多种日志类型的脚本(Bash Shell)

 2010/11/20 11:19:40  SavageGarden  http://savagegarden.javaeye.com  我要评论(0)
  • 摘要:#在指定日期运行today=$(date+%Y-%m-%d)month=${today:5:2}date=${today:8:2}if["$date"!="4"];thenecho"itwillberunatyyyy-MM-04"exitfi#只保留两个月functioncheck_dir(){dir_name=$1if["${today:0:4}"="${dir_name:0:4}"];thenmdstr=${dir_name#*"-"}mstr=${mdstr%"-"*}if["$
  • 标签:定期备份 日志类型
#在指定日期运行
today=$(date +%Y-%m-%d)
month=${today:5:2}
date=${today:8:2}
if [ "$date" != "4" ]; then
	echo "it will be run at yyyy-MM-04"
	exit
fi
#只保留两个月
function check_dir()
{
  dir_name=$1
	if [ "${today:0:4}" = "${dir_name:0:4}" ]; then
    mdstr=${dir_name#*"-"}
    mstr=${mdstr%"-"*}
		if [ "${mstr:0:1}" = "0" ]; then
			mstr=${mstr#*"0"}
		fi
		res=$(($month-$mstr))
		if [ $res -lt 2 ]; then
			return 1
    fi
	fi
	return 0
}

backuplog="/backup/backup_report.log"
over="/log/over_"
back="/backup/report/"

logtype1="logtype1"
logtype2="logtype2"
logtype3="logtype3"
logtype4="logtype4"

logtype_array=($logtype1 $logtype2 $logtype3 $logtype4)
length=${#logtype_array[*]}

while [ $index -lt $length ]
do
	over_path=$over${logtype_array[$index]}"/"
	back_path=$back${logtype_array[$index]}"/"
	`echo "------------------------------------------------------" >> $backuplog`
	`echo ${logtype_array[$index]}" backup start!" >> $backuplog`
	`echo "------------------------------------------------------" >> $backuplog`
	`echo "" >> $backuplog`
	for dir in `ls $over_path`
	do
		if `check_dir $dir`; then
			nowdate=`date`
			`echo "$nowdate	now backup $dir" >> $backuplog`
			cd $back_path
			if [ ! -d "$dir" ]; then
				`mkdir $dir`
			fi
			cd $over_path$dir
			`tar -zcf $dir".tar.gz" *`
			`mv $dir".tar.gz" $back_path$dir"/"`
			cd $over_path
			`rm -rf $dir`
		fi
	done
	`echo "" >> $backuplog`
	`echo "------------------------------------------------------" >> $backuplog`
        `echo ${report_array[$index]}" backup end!" >> $backuplog`
        `echo "------------------------------------------------------" >> $backuplog`
	index=`expr $index + 1`
done
?
发表评论
用户名: 匿名