This post appeared originally in our sysadvent series and has been moved here following the discontinuation of the sysadvent microsite

This post appeared originally in our sysadvent series and has been moved here following the discontinuation of the sysadvent microsite
Log-rotation is a key for running a stable server, but removing log files is often an anathema to security, traceability, and server history. In reality, you want a perfect rotation setup in order to maximise the retention of logs.
Instead of trying to continuously trying to balance the number of logs to keep on disk, why not just set the “rotate” value to a higher value and add a small script in cron to handle deletion of old files?
A simple script that does the above can look like this:
#!/bin/bash
LOGPATH=/var/log/myapp # The path to the logs
DFHIGH=80 # The disk usage watermark you want to delete down to, in percent
MAXDAYS=45 # Max logfile age to keep in any case
MINDAYS=7 # Do not delete files newer than this
# Step 1: Remove old files
while [ $( df --output=pcent ${LOGDIR}/ | tail -1 | tr -cd "[0-9\n]" ) -ge ${DFHIGH} -a ${MAXDAYS} -ge ${MINDAYS} ]; do
ionice find ${LOGDIR} -type f -mtime +${MAXDAYS} \! -exec fuser -s '{}' \; -delete >/dev/null 2>/dev/null
let MAXDAYS--
done
# Step 2: Remove empty directories
find ${LOGDIR} -mindepth 1 -type d -empty -delete
Breaking the above script down:
Of course, this is just an example, and the script only a template.
Another case may be to limit disk usage for just one application, but only if the total disk usage exceeds a given limit. This script is designed to be run often as it does not contain a loop and it will only delete the oldest files each time:
#!/bin/bash
LOGPATH=/var/log/myapp # The path to the logs or files
LIMIT=5000 # Max usage in MB
MINAGE=480 # Minimum age to keep in minutes (8 hours = 480)
DELTA=720 # Minutes under max age from where to delete files
# Step 1: Exit if disk use is under LIMIT:
if [ $( du -sm ${LOGPATH}|cut -f1 ) -lt ${LIMIT} ]; then
exit 0
fi
# Step 2: Delete the oldest files
MAXAGE=$(( ( ( $( date +%s ) - $( find ${LOGPATH} -type f -printf "%T@\n"|cut -f1 -d.|sort -n|head -1 ) ) / 60 ) - ${DELTA} ))
if [ ${MAXAGE} -gt ${MINAGE} ]; then
find ${LOGPATH} -type f -mmin +${MAXAGE} -delete
fi
DELTA is the thickness of the time-slice for files we remove from the logpath in each run. It should typically be slightly larger than the time period between runs of this script. Smaller DELTA deletes less files each run, so the script may need to be run more frequently.
Breaking down the above script:
Open source i offentlig sektor - utmaningar, möjligheter och vägen framåt.
Denna artikel beskriver processen och lärdomarna från att släppa ett API som öppen källkod inom offentlig sektor. Projektet, som utvecklades för digitala nationella prov (“DNP”), visar hur öppen källkod kan stärka samarbete, transparens och innovation. Artikeln lyfter fram både möjligheter och utmaningar – från säkerhet och juridiska aspekter till kulturellt motstånd – och ger insikter för andra myndigheter som överväger liknande initiativ.
Slutsatsen är att öppen källkod ... [continue reading]