linux应用日志归档,使用rsync归档收集Tomcat日志

随着业务的不断的增加,使得多台的Tomcat的日志不断剧增,以前有利用bzip2压缩历史数据的脚本压缩过,所以历史日志格式都是统一的,所以打算把部分历史日志数据收集下来,再做具体分析,选选取一台磁盘比较大的服务器用rsync来做日志中心服务器用于收集日志,需要的可以参看:http://www.linuxidc.com/Linux/2017-08/146524.htm 。于是就写了一个脚本在不同的服务器上用于历史日志数据的归档,实现也很简单,如下:

#!/bin/bash

#rsync_logs.sh

#writer jim

#Used for historical log collection

#Need to use rsync you must install rsync

#00 00 01 * * /usr/local/scripts/rsync_logs.sh

#history

#2017.08.26

one_month_ago=$(date -d '-1 month' +%Y-%m)

tomcat_dir="/data/tomcat"

######rsync服务器相关配置变量####

port=873

rsyncd_user="root"

rsyncd_host="172.16.1.170"

DEST_name="backup"

password_file="/etc/.rsync.passwd"

##################################

rpm -qa | grep rsync

if [ $? -ne 0 ];then

yum -y install rsync

fi

for i in $(ls $tomcat_dir)

do

cd ${tomcat_dir}/${i}/logs

for n in $(ls *${one_month_ago}*.bz2)

do

mv $n "${i}_${n}"

#由于有多个Tomcat所以需要对历史压缩的日志数据改名

rsync -vzrLtopg --progress --port=${port} "${i}_${n}" --password-file=${password_file} ${rsyncd_user}@${rsyncd_host}::${DEST_name} && rm -rf "${i}_${n}"

done

done

Rsync 的详细介绍:请点这里

Rsync 的下载地址:请点这里

0b1331709591d260c1c78e86d0c51c18.png