Difference between revisions of "Limit the size of .log files & the journal"
Limit the size of .log files & the journal (view source)
Revision as of 13:09, 16 August 2013
, 10 years ago→Some uses for Logrotate
imported>Handy |
imported>Handy |
||
Line 144: | Line 144: | ||
We can use the above script block as a template, easily removing parts & modifying its relatively simple settings. It can duplicated in a script with each script block specifying custom settings tailored for individual files. | We can use the above script block as a template, easily removing parts & modifying its relatively simple settings. It can duplicated in a script with each script block specifying custom settings tailored for individual files. | ||
<br clear="all"/> | |||
== An Example that you can modify to suit == | |||
I'll show how I have my system set, you can use the information already given on this page & other available on the web to fine tune your set up to suit your needs (if you have the need anyway). | |||
=== Firstly - Be sure this file is here /etc/cron.daily/logrotate === | |||
#!/bin/sh | |||
# nicenesses range from -20 (most favorable scheduling) to 19 (least favorable) | |||
NICE=19 | |||
# 0 for none, 1 for real time, 2 for best-effort, 3 for idle | |||
IONICE_CLASS=2 | |||
# 0-7 (for IONICE_CLASS 1 and 2 only), 0=highest, 7=lowest | |||
IONICE_PRIORITY=7 | |||
CMD_LOGROTATE="/usr/bin/logrotate /etc/logrotate.conf" | |||
if [ -x /usr/bin/nice ]; then | |||
CMD_LOGROTATE="/usr/bin/nice -n ${NICE:-19} ${CMD_LOGROTATE}" | |||
fi | |||
if [ -x /usr/bin/ionice ]; then | |||
CMD_LOGROTATE="/usr/bin/ionice -c ${IONICE_CLASS:-2} -n ${IONICE_PRIORITY:-7} ${CMD_LOGROTATE}" | |||
fi | |||
${CMD_LOGROTATE} | |||
exit 0 | |||
<br clear="all"/> | |||
=== Secondly - Create /etc/logrotate.d/rotate.logs using the following === | |||
## rotate all /var/log files with names ending in log | |||
/var/log/*log { | |||
## cycle through these commands once per day | |||
daily | |||
## keep the results of 7 cycles | |||
rotate 7 | |||
## use gzip to compress each rotated (copied) log file | |||
compress | |||
## compress the file on the next cycle | |||
delaycompress | |||
## copy the contents of the log file to a new file <name>.log.1 | |||
## & then delete the contents of the original log file | |||
copytruncate | |||
## do nothing to empty files | |||
notifempty | |||
## create no errors if a file is missing | |||
missingok | |||
## after the files have been rotated run the following command | |||
postrotate | |||
## move any files with the .gz extension to /var/log/old | |||
mv /var/log/*.gz /var/log/old | |||
## closes the postrotate section | |||
endscript | |||
## ends the command sequence of this block of script | |||
} | |||
You also need to create the /var/log/old directory if it doesn't already exist. This is where the .gzipped daily backup files will be kept. | |||
<br clear="all"/> | |||
=== A Summary of the above example thus far === | |||
The First step puts a file into '''/etc/cron.daily''' which is an easy way to add the script to a daily cron job. Which means that script will be run everyday. | |||
It basically runs this command: | |||
logrotate /etc/logrotate.conf | |||
As logrotate.conf goes through its list of commands it calls this one: | |||
include /etc/logrotate.d | |||
Which means that any scripts that are inside of '''/etc/logrotate.d''' are also run. | |||
This brings us to the second step (above), where we created '''/etc/logrotate.d/rotate.logs''' . This script will be run everyday. The comments I added to the rotate.logs file above give a general idea of what it does. You can delete, modify & add to that script, but do it carefully. | |||
==== The effect of running /etc/logrotate.d/rotate.logs everyday ==== | |||
Is that any file in /var/log that had '''log''' at the end of its name will be processed by the commands in the '''rotate.logs''' script. This will back up these files to a new file '''<name>.log.1''' & empty the original file to size 0. Any previous copies of with '''<name>.log.<number>''' will have their numbers bumped up one, until the day when they would have been given an 8, that is the day that they are deleted. | |||
As well as this rotating (copying) & renaming of files, all files will be compressed in gzip format on the next rotation. Which means that you always have the current file & yesterdays file in /var/log in uncompressed format. | |||
No files that are empty will be processed, & a file being missing will throw no errors. | |||
Any gzip compressed file, (they have a .gz file extension) will be copied to /var/log/old where it will stay, with the number in its name being bumped everyday until it reaches 7. on the next day, instead of going to 8 it will be deleted. | |||
<br clear="all"/> | <br clear="all"/> |