因为需要日志的备份,所以就在以前的脚本的基础上重新改了下(http://jim123.blog.51cto.com/4763600/1912312),以实现日志的压缩备份和清理:
#!/bin/bash #backup_log.sh:Used to compress the backup logs #note:Need to install the system bizp2 or you must configure the yum source #00 00 1 * * /usr/local/scripts/backup_log.sh #writer jim #histor #2017.4.1 ########PATH######## app_dir=/usr/local/scripts/text_app_dir logs_dir=/data/logs/clrnup_logs logs_back_dir=/data/logs/logs_backup one_month_ago=$(date -d ‘-1 month‘ +%Y-%m) datetime=$(date +"%Y-%m-%d") datetime_dir=$logs_dir/"$datetime" logs_dir_path=$(find $app_dir -type d -name ‘logs‘ -print) logs_bz2_path=$(find $logs_back_dir -name "*${two_month_ago}*.bz2" -print) if [ ! -e "$datetime_dir" ];then mkdir -p $datetime_dir fi if [ ! -e "$logs_back_dir" ];then mkdir -p $logs_back_dir fi ########PATH######## rpm -qa | grep bzip2 if [ $? -ne 0 ];then yum -y install bzip2 fi echo "============================ start move logs `date +"%Y-%m-%d %H:%M:%S"`" | tee -a $datetime_dir/logs_clenup_${datetime}.log for i in $logs_dir_path do tmp_dirname=$(dirname $i) tmp_basename=$tmp_dirname if [ ! -e "${logs_back_dir}/${tmp_basename}" ];then mkdir -p ${logs_back_dir}/${tmp_basename} fi /usr/bin/bzip2 -z $i/*${one_month_ago}* $i/*.bz2 ${logs_back_dir}/${tmp_basename} echo "`date +"%Y-%m-%d %H:%M:%S"` $i move logs success" | tee -a $datetime_dir/logs_clenup_${datetime}.log done if [ $? -eq 0 ];then echo "============================ end move logs `date +"%Y-%m-%d %H:%M:%S"`" | tee -a $datetime_dir/logs_clenup_${datetime}.log else echo "move logs fail!" | tee -a $datetime_dir/logs_clenup_${datetime}.log fi echo "============================ start clean logs `date +"%Y-%m-%d %H:%M:%S"`" | tee -a $datetime_dir/logs_clenup_${datetime}.log for i in $logs_bz2_path do rm -rf $(dirname $i) echo "`date +"%Y-%m-%d %H:%M:%S"` $i cleanup bz2 success" | tee -a $datetime_dir/logs_clenup_${datetime}.log done if [ $? -eq 0 ];then echo "============================ end clean logs `date +"%Y-%m-%d %H:%M:%S"`" | tee -a $datetime_dir/logs_clenup_${datetime}.log else echo "clean logs fail!" | tee -a $datetime_dir/logs_clenup_${datetime}.log fi find $logs_dir -type f -ctime +90 -name "logs_clenup_*" -exec rm -vf {} \;
本文出自 “技术随笔” 博客,谢绝转载!
原文地址:http://jim123.blog.51cto.com/4763600/1912512