Creating weekly automatic upgrade script which tells if the upgrade was succesful

This script is designed to be scheduled to run with /etc/cron.* and it doesn’t just send you the log file via email but tells you immediately if the upgrade was succesful or not; the temporary log file posted will be filtered for errors and warnings and if such things are found, the subject of the mail will be “Upgrade of your server failed”.

It should be saved as /etc/cron.daily/autoupdate.sh or /etc/cron.weekly/autoupdate.sh depending on how often the upgrade needs to be done. When the script is saved in a proper location under /etc/cron.* it must be remembered to be given execution privileges (sudo chmod +x /etc/cron.*/autoupdate.sh). You also should set your email in the $admin_mail variable to make the mail feature working.

!/bin/sh # This is a script made to keep your Ubuntu server up to # date placing this script to /etc/cron.weekly/autoupdate.sh. # This script updates your server automatically and informs # you via email if the update was succesful or not. # create a .sussess.txt file somewere with the text "Upgrade successful" # do the same for .fail.txt #change the sucfile and failfile path # Set the variable $admin_email as your email address. admin_mail="[email protected]" Hostname=$(hostname) # Create a temporary path in /tmp to write a temporary log # file. No need to edit. tmpfile=/tmp/autoupdate.txt sucfile=/path/to/.success.txt failfile=/path/to/.fail.txt # Run the commands to update the system and write the log # file at the same time. echo "aptitupde update" >> ${tmpfile} aptitude update >> ${tmpfile} 2>&1 echo "" >> ${tmpfile} echo "aptitude full-upgrade" >> ${tmpfile} aptitude -y full-upgrade >> ${tmpfile} 2>&1 echo "" >> ${tmpfile} echo "apt-get autoremove" >> ${tmpfile} apt-get -y autoremove >> ${tmpfile} 2>&1 echo "" >> ${tmpfile} echo "update-grub" >> ${tmpfile} update-grub >> ${tmpfile} 2>&1 echo "" >> ${tmpfile} echo "aptitude clean" >> ${tmpfile} aptitude clean >> ${tmpfile} 2>&1 # Send the temporary log via mail. The fact if the upgrade # was succesful or not is written in the subject field. if grep -q 'E: |W: ' ${tmpfile} ; then sendemail -f ${Hostname}@domain.com -t ${admin_mail} -u Upgrade of your server Failed $(date) -a ${tmpfile} -m < ${failfile} -s smtpserver.domain.com -o tls=no -xu username -xp pass else sendemail -f ${Hostname}@domain.com -t ${admin_mail} -u Upgraded your server succesfully $(date) -a ${tmpfile} -m < ${sucfile} -s smtpserver.domain.com -o tls=no -xu username -xp pass fi # Remove the temporary log file in temporary path. rm -f ${tmpfile} reboot

To make the email function work install the following

apt-get install libio-socket-ssl-perl libnet-ssleay-perl sendemail

UPDATE: added a “autoremove” , “grub-update” and “reboot”