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

#在指定日期运行
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
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值