Howtos Automated Apt Updates Debian

From 5dollarwhitebox.org Media Wiki

Jump to: navigation, search

Contents

This Howto is Deprecated

IMPORTANT: This HowTo is deprecated due to the fact that you can easily do the same thing by installing the 'cron-apt' package via apt:

# apt-get update && apt-get install cron-apt


Summary

The basis of this HowTo is for Automated Apt updates/upgrades. Everynight on all of my servers I have a script which runs performing the updates and upgrades to packages currently installed. This sounds simple, but isn't default on Debian installs.


The Script

/root/scripts/apt-get_update.bash. Using 'vi', copy the following code box into the file:

#!/bin/bash

LOG_FILE=/var/log/apt-get_update/apt-get_update.log

export PATH=$PATH:/usr/local/sbin:/usr/sbin:/sbin
exec 2>&1 >> $LOG_FILE

apt-get -y update 2>&1 > /dev/null

echo ""
echo "--------------------------------------------------------"
echo "apt-get_upgrade started at `date`"
echo "--------------------------------------------------------"
echo ""

apt-get -yu upgrade 

echo ""
echo "--------------------------------------------------------" 

Be sure to change the "LOG_FILE" line to where you would like your log to be kept.


You'll need to make the script executable:

chmod +x /root/scripts/apt-get_update.bash


You will also need to create the directory '/var/log/apt-get_update':

mkdir /var/log/apt-get_update


Logrotate The Logfile

/etc/logrotate.d/apt-get_update. Using the 'vi' command copy the following code box into the file:

/var/log/apt-get_update/apt-get_update.log {
        weekly
        rotate 4
        nocompress
        missingok
}


You can edit this to your liking.


The Cron Job

/etc/cron.d/apt-get_update. Using the 'vi' command copy the following code box into the file:

1 3 * * * root /root/scripts/apt-get_update.bash || echo "apt-get update/upgrade failed!" | mail root


The previous cron job will run at 3:01AM every morning... and emails root if it fails. You can adjust this as necessary.


And Thats all you need! You can test the script by executing it:

/root/scripts/apt-get_update.bash


You shouldn't see any output, however you'll want to check your logfile to verify that it outputed correctly:

cat /var/log/apt-get_update/apt-get_update.log

...
--------------------------------------------------------
apt-get_upgrade started at Fri Nov 11 03:01:03 CST 2005
--------------------------------------------------------

Reading Package Lists...
Building Dependency Tree...
The following packages have been kept back:
  gnupg libextutils-parsexs-perl mailutils
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.

--------------------------------------------------------

Your output will very.


You do want to keep an eye on this log file (though you should receive an email if the script fails). The only times that I have issues is when interaction is required for the dpkg post install scripts and such.

Have fun!

Personal tools