cheatsheet.zwischenspeicher.info

Some tech documentation and snippets, finally organized.
Posts tagged as logrotate

Auto-upgrade for Debian based systems

A reliable auto-updater for home use. It can be executed e.g. at boot from /etc/rc.local to keep apt based systems up-to-date. Some output will be logged to /var/log/safe-upgrade.log, in case of upgraded or held packages as well as errors a report will be sent to root@localhost.

#!/bin/sh

PINGTEST=debian.org
HOSTNAME=`hostname`
LOGFILE=/var/log/safe-upgrade.log
MAILTO=root@localhost

sleep 5

echo ----- `date` ----- >> "$LOGFILE"

ping -c 1 "$PINGTEST" > /dev/null 2>> "$LOGFILE" ||\
    {
        echo "System upgrade cancelled: No internet connection." |\
        tee -a "$LOGFILE" |\
        mailx -s "$HOSTNAME"\ update\ \*\*\*ERROR\*\*\* "$MAILTO"
        exit 0
    }

DEBIAN_FRONTEND=noninteractive

apt-get -q=2 update 2>&1 |\
    tee -a "$LOGFILE" |\
    mailx -E -s "$HOSTNAME"\ update\ \*\*\*ERROR\*\*\* "$MAILTO"

apt-get upgrade -y -q 2>&1 |\
    grep -ve "^(\{0,1\}Reading" \
         -e "^Building" |\
    tee -a "$LOGFILE" |\
    grep -ve "^0 upgraded.*0 not upgraded\.$" |\
    mailx -E -s "$HOSTNAME"\ upgrade\ log "$MAILTO" &&

echo ----- DONE ----- >> "$LOGFILE"

exit 0

To tame the daily growing log file, add the following lines to /etc/logrotate.conf or into an own file in /etc/logrotate.d/. This will keep compressed archives of the last three months' log files.

### File: /etc/logrotate.conf

/var/log/safe-upgrade.log {
    monthly
    create
    rotate 3
}

If you want the script to be executed by cron, it is necessary to set the $PATH variable - in the script itself, or in the crontab. This example runs every three hours at 45':

### File: root crontab, open to edit with "crontab -e"

# m h  dom mon dow   command
45 */3 * * * PATH='/usr/sbin:/usr/bin:/sbin:/bin' /path/to/safe-upgrade.sh