From 0689935da44d2458f191e69433ee693a8ea25ce5 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Wed, 26 Oct 2016 18:03:31 +0800 Subject: [PATCH] log rotate --- make/common/log/logrotate_docker.conf | 7 ------ make/common/log/rotate.sh | 26 ++++++++++++++++++++ make/photon/log/Dockerfile | 11 +++------ make/photon/log/logrotate.conf.photon | 35 --------------------------- make/ubuntu/log/Dockerfile | 11 ++++----- 5 files changed, 35 insertions(+), 55 deletions(-) delete mode 100644 make/common/log/logrotate_docker.conf create mode 100755 make/common/log/rotate.sh delete mode 100644 make/photon/log/logrotate.conf.photon diff --git a/make/common/log/logrotate_docker.conf b/make/common/log/logrotate_docker.conf deleted file mode 100644 index 6a953d4c3..000000000 --- a/make/common/log/logrotate_docker.conf +++ /dev/null @@ -1,7 +0,0 @@ -# Logrotate configuartion file for docker. - -/var/log/docker/*/*.log { - rotate 100 - size 10M - copytruncate -} diff --git a/make/common/log/rotate.sh b/make/common/log/rotate.sh new file mode 100755 index 000000000..3f96df9bc --- /dev/null +++ b/make/common/log/rotate.sh @@ -0,0 +1,26 @@ +#!/bin/bash +set -e +echo "Log rotate starting..." + +#The logs n days before will be compressed. +n=14 +path=/var/log/docker + +list="" +n_days_before=$(($(date +%s) - 3600*24*$n)) +for dir in $(ls $path | grep -v "tar.gz"); +do + if [ $(date --date=$dir +%s) -lt $n_days_before ] + then + echo "$dir will be compressed" + list="$list $dir" + fi +done + +if [ -n "$list" ] +then + cd $path + tar --remove-files -zcvf $(date -d @$n_days_before +%F)-.tar.gz $list +fi + +echo "Log rotate finished." diff --git a/make/photon/log/Dockerfile b/make/photon/log/Dockerfile index 8ae03e95c..ea0759427 100644 --- a/make/photon/log/Dockerfile +++ b/make/photon/log/Dockerfile @@ -1,8 +1,7 @@ FROM library/photon:latest # run logrotate hourly, disable imklog model, provides TCP/UDP syslog reception -RUN tdnf install -y cronie rsyslog logrotate shadow\ - && mv /etc/cron.daily/logrotate /etc/cron.hourly/ \ +RUN tdnf install -y cronie rsyslog shadow tar gzip \ && mkdir /etc/rsyslog.d/ \ && mkdir /var/spool/rsyslog \ && groupadd syslog \ @@ -10,15 +9,13 @@ RUN tdnf install -y cronie rsyslog logrotate shadow\ ADD make/common/log/rsyslog.conf /etc/rsyslog.conf -COPY make/photon/log/logrotate.conf.photon /etc/logrotate.conf - -# logrotate configuration file for docker -ADD make/common/log/logrotate_docker.conf /etc/logrotate.d/ +# rotate logs weekly +# notes: file name cannot contain dot, or the script will not run +ADD make/common/log/rotate.sh /etc/cron.weekly/rotate # rsyslog configuration file for docker ADD make/common/log/rsyslog_docker.conf /etc/rsyslog.d/ - VOLUME /var/log/docker/ EXPOSE 514 diff --git a/make/photon/log/logrotate.conf.photon b/make/photon/log/logrotate.conf.photon deleted file mode 100644 index c28d55b62..000000000 --- a/make/photon/log/logrotate.conf.photon +++ /dev/null @@ -1,35 +0,0 @@ -# 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 -#} - -/var/log/btmp { - missingok - monthly - create 0600 root utmp - rotate 1 -} - -# system-specific logs may be also be configured here. diff --git a/make/ubuntu/log/Dockerfile b/make/ubuntu/log/Dockerfile index f91da3469..3e1c7520e 100644 --- a/make/ubuntu/log/Dockerfile +++ b/make/ubuntu/log/Dockerfile @@ -1,13 +1,12 @@ FROM library/ubuntu:14.04 -# run logrotate hourly, disable imklog model, provides TCP/UDP syslog reception -RUN mv /etc/cron.daily/logrotate /etc/cron.hourly/ \ - && rm /etc/rsyslog.d/* \ - && rm /etc/rsyslog.conf +RUN rm /etc/rsyslog.d/* && rm /etc/rsyslog.conf + ADD make/common/log/rsyslog.conf /etc/rsyslog.conf -# logrotate configuration file for docker -ADD make/common/log/logrotate_docker.conf /etc/logrotate.d/ +# rotate logs weekly +# notes: file name cannot contain dot, or the script will not run +ADD make/common/log/rotate.sh /etc/cron.weekly/rotate # rsyslog configuration file for docker ADD make/common/log/rsyslog_docker.conf /etc/rsyslog.d/