-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_update
executable file
·37 lines (32 loc) · 1.55 KB
/
auto_update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/sh
# This is a script made to keep your Ubuntu server up to
# date placing this script to /etc/cron.weekly/auto_update
# This script updates your server automatically and informs
# you via email if the update was successful or not.
# 11/03/15 - Change mail to mutt
# 01/30/17 - Now we check to see if the homepage is equal
# 02/26/17 - Use curl and more advanced options for homepage checking
SERVER_NAME="$(hostname)"
FILE_BASE=/var/log/$(basename "$0")_$(date +"%F_%T")
LOG_FILE=$FILE_BASE.log
TEST_PAGE=${FILE_BASE}_before.html
curl -LSs $SERVER_NAME -o $TEST_PAGE >> $LOG_FILE 2>&1
echo "apt update" >> $LOG_FILE
DEBIAN_FRONTEND=noninteractive apt -y update >> $LOG_FILE 2>&1
echo "\napt full-upgrade" >> $LOG_FILE
DEBIAN_FRONTEND=noninteractive apt -y full-upgrade >> $LOG_FILE 2>&1
echo "\napt autoremove" >> $LOG_FILE
DEBIAN_FRONTEND=noninteractive apt -y autoremove >> $LOG_FILE 2>&1
echo "\napt clean" >> $LOG_FILE
apt clean >> $LOG_FILE 2>&1
TEST_PAGE_AFTER=${FILE_BASE}_after.html
curl -LSs $SERVER_NAME -o $TEST_PAGE_AFTER >> $LOG_FILE 2>&1
if grep -q "E: \|W: " $LOG_FILE; then
(printf "To: admin@$SERVER_NAME\nSubject: ERROR: Upgrade of $SERVER_NAME $(date)\n\n"; cat $LOG_FILE) | sendmail -t
elif ! cmp -s $TEST_PAGE $TEST_PAGE_AFTER; then
(printf "To: admin@$SERVER_NAME\nSubject: WARNING: Upgrade of $SERVER_NAME completed with different homepage $(date)\n\n"; cat $LOG_FILE) | sendmail -t
else
(printf "To: admin@$SERVER_NAME\nSubject: SUCCESS: Upgrade $SERVER_NAME $(date)\n\n"; cat $LOG_FILE) | sendmail -t
rm -f $TEST_PAGE
rm -f $TEST_PAGE_AFTER
fi