logrotate: 日志切割,备份

1, 定时任务调用链

crontab任务配置使用: https://blog.csdn.net/eyeofeagle/article/details/83217396
在这里插入图片描述

2, 查看帮助:配置文件使用

  • It allows automatic rotation, compression, removal, and mailing of log files
  • Each log file may be handled daily, weekly, monthly, or when it grows too large.
[root@c72 ~]# cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly

# 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
}
...



[root@test-c6 ~]# ls /etc/logrotate.d
ConsoleKit  dracut  httpd  kadmind  krb5kdc  mysql  psacct  syslog  yum

[root@test-c6 ~]# rpm -qf /etc/logrotate.d/mysql
MySQL-server-5.5.62-1.el6.x86_64
[root@test-c6 ~]# rpm -qf /etc/logrotate.d/yum
yum-3.2.29-81.el6.centos.0.1.noarch
[root@test-c6 ~]# rpm -qf /etc/logrotate.d/syslog
rsyslog-5.8.10-12.el6.x86_64

[root@test-c6 ~]# cat /etc/logrotate.d/httpd
/var/log/httpd/*log {
    
    
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}


[root@test-c6 ~]# man logrotate
...
CONFIGURATION FILE
	   logrotate reads everything about the log files it should be handling from the series of configuration files specified on the  command
	     line.  Each configuration file can set global options (local definitions override global ones, and later definitions override earlier
	     ones) and specify logfiles to rotate. A simple configuration file looks like this:

       # sample logrotate configuration file
       compress

       /var/log/messages {
    
    
           rotate 5
           weekly
           postrotate
               /usr/bin/killall -HUP syslogd
           endscript
       }
...
copytruncate
     Truncate the original log file in place after creating a copy, instead of moving the old log file and  optionally  creating  a
       new  one.  It can be used when some program cannot be told to close its logfile and thus might continue writing (appending) to
       the previous log file forever.  Note that there is a very small time slice between copying the file and truncating it, so some
       logging  data  might  be  lost.  When this option is used, the create option will have no effect, as the old log file stays in
       place.
notifempty
    Do not rotate the log if it is empty 

3, 自定义配置:切割/备份指定文件

[root@test-c6 ~]# cat /etc/logrotate.d/1.log
 /var/log/1.txt 
 /var/log/a.txt {
    
    
           missingok
           notifempty
           
           rotate 3
           dateext
           copytruncate

           #size 10M
           maxsize 10M
           compress
       }

#产生26M测试数据
[root@test-c6 ~]# head -c 10M  /dev/urandom |od -x |tr -d ' ' > /var/log/1.txt
[root@test-c6 ~]# head -c 10M  /dev/urandom |od -x |tr -d ' ' > /var/log/a.txt
[root@test-c6 ~]# ll -h /var/log/*.txt
-rw-r--r-- 1 root root 26M Dec  1 07:41 /var/log/1.txt
-rw-r--r-- 1 root root 26M Dec  1 07:41 /var/log/a.txt
[root@test-c6 ~]# head -1  /var/log/a.txt
0000000b59092c99538fa92c4c2ecadc418ae1a

#修改当前时间,以生成新的备份文件名
[root@test-c6 ~]# date -s "$(date -d '2 days' +"%F %T")"
Thu Dec  3 07:42:09 CST 2020


#手动调用rotate,测试:
[root@test-c6 ~]# logrotate  --help
Usage: logrotate [OPTION...] <configfile>
  -d, --debug               Don't do anything, just test (implies -v)
  -f, --force               Force file rotation
  -m, --mail=command        Command to send mail (instead of `/bin/mail')
  -s, --state=statefile     Path of state file
  -v, --verbose             Display messages during rotation
...

[root@test-c6 ~]#  logrotate -v -f /etc/logrotate.d/1.log
reading config file /etc/logrotate.d/1.log
reading config info for /var/log/1.txt /var/log/a.txt

Handling 1 logs

rotating pattern: /var/log/1.txt /var/log/a.txt  forced from command line (3 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/1.txt
  log needs rotating
considering log /var/log/a.txt
  log needs rotating
rotating log /var/log/1.txt, log->rotateCount is 3
dateext suffix '-20201203'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
copying /var/log/1.txt to /var/log/1.txt-20201203
truncating /var/log/1.txt
compressing log with: /bin/gzip
rotating log /var/log/a.txt, log->rotateCount is 3
dateext suffix '-20201203'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
copying /var/log/a.txt to /var/log/a.txt-20201203
truncating /var/log/a.txt
compressing log with: /bin/gzip



#生成压缩文件
[root@test-c6 ~]#  ls /var/log/*.txt* -lh
-rw-r--r-- 1 root root   0 Dec  3 07:51 /var/log/1.txt
-rw-r--r-- 1 root root 14M Dec  3 07:51 /var/log/1.txt-20201203.gz
-rw-r--r-- 1 root root   0 Dec  3 07:51 /var/log/a.txt
-rw-r--r-- 1 root root 14M Dec  3 07:51 /var/log/a.txt-20201203.gz



#解压文件,和原文件对比
[root@test-c6 ~]# gunzip /var/log/a.txt-20201203.gz
[root@test-c6 ~]# ls /var/log/a.txt* -lh
-rw-r--r-- 1 root root   0 Dec  3 07:51 /var/log/a.txt
-rw-r--r-- 1 root root 26M Dec  3 07:51 /var/log/a.txt-20201203
[root@test-c6 ~]# head -1  /var/log/a.txt-20201203
0000000b59092c99538fa92c4c2ecadc418ae1a

猜你喜欢

转载自blog.csdn.net/eyeofeagle/article/details/109996985
今日推荐