日志分割

本文介绍了如何配置和使用logrotate进行日志分割,并创建脚本自动化处理日志,包括设置脚本定时运行、服务器间日志同步。通过rsync在日志存储节点上实现从多台Nginx服务器的日志同步。

日志分割

每一台都需要做

vi  /etc/logrotate.conf

 

# see "man logrotate" for details

# rotate log files weekly

monthly

 

# keep 4 weeks worth of backlogs

rotate 4

 

# create new (empty) log files after rotating old ones

create

 

# use date as a suffix of the rotated file

dateext

 

# uncomment this if you want your log files compressed

#compress

 

# RPM packages drop log rotation information into this directory

#注释掉

#include /etc/logrotate.d

 

# no packages own wtmp and btmp -- we'll rotate them here

#注释掉

#/var/log/wtmp {                     

#    monthly

#    create 0664 root utmp

#       minsize 1M

#    rotate 1

#}

 

#/var/log/btmp {

#    missingok

#    monthly

#    create 0600 root utmp

#    rotate 1

#}

 

脚本存储文件夹

mkdir /usr/shell

编写脚本

cd /usr/shell

vi  

具体查看  shell脚本.txt

cd /usr/shell

 

vi  alllog

 

#/bin/bash

#xxx是你脚本名字

/usr/shell/xxx        

/usr/shell/xxx

/usr/shell/xxx

/usr/shell/xxx

/usr/shell/xxx

/usr/shell/xxx

/usr/shell/xxx

/usr/shell/xxx

/usr/shell/xxx

/usr/shell/xxx

/usr/shell/xxx

/usr/shell/xxx

/usr/shell/xxx

 

赋予脚本权限

chmod a+x /usr/shell/*

 

mkdir /opt/log

分割全部日志

/usr/shell/alllog    

 

 

脚本定时运行

crontab  –e

 

加一句

10 00 * * * /bin/bash /usr/shell/alllog

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

服务器文件同步

只在nginx服务器上面做

vi /etc/rsyncd.conf

 

uid = root

gid = root

max connections = 4

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

 

[logs]

path = /opt/log

ignore errors

read only = true

list = true

auth users = backup

secrets file = /etc/rsyncd.pwd

 

 

vi /etc/rsyncd.pwd

 

backup:dadao

 

只在日志储存节点上面做

vi  /usr/shell/daylog

 

#/bin/bash

rsync -avzP --password-file=/etc/rsyncd.pwd backup@第一台nginxip::logs /opt/log-nginx1

rsync -avzP --password-file=/etc/rsyncd.pwd backup@第二胎nginxip::logs /opt/log-nginx2

 

 

mkdir /opt/log-nginx1

mkdir /opt/log-nginx2

 

同步文件

/usr/shell/daylog

 

 

定时

crontab  -e

 

20 00 * * * /bin/bash  /usr/shell/daylog

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值