-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathopkg-upgrade.sh
256 lines (218 loc) · 9.2 KB
/
opkg-upgrade.sh
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/bin/sh
#
# Upgrade packages with specific ipk
#
# crontab -e
# # Packages upgrade daily @05:55
# 55 5 * * * /root/opkg-upgrade.sh
#
# Launch command:
# /root/opkg-upgrade.sh --auto
# /root/opkg-upgrade.sh --manual
#
FILE_PATH=$(readlink -f $(dirname $0)) #/root
FILE_NAME=$(basename $0) #opkg-upgrade.sh
FILE_NAME=${FILE_NAME%.*} #opkg-upgrade
FILE_DATE=$(date +'%Y%m%d-%H%M%S')
FILE_LOG="/var/log/$FILE_NAME.log"
FILE_LOG_ERRORS="/var/log/$FILE_NAME.err"
###############################################################################
### Functions
function fCmd() {
local cmd=$@
$cmd 2>&1 1> /dev/null | tee -a $FILE_LOG_ERRORS
if [ $? -ne 0 ]; then
echo "* " | tee -a $FILE_LOG
echo "* $cmd" | xargs | tee -a $FILE_LOG
opkgStatus="Upgrade ending with errors!"
fSendMail "$opkgStatus\nOS: $OPENWRT_RELEASE" "$(cat $FILE_LOG | grep -Ev "^Downloading|^Configuring|resolve_conffiles|^\.|^$")"
exit 1
fi
}
function fSendMail() {
local MSG_HEAD=$1 MSG_BODY=$2
echo -e "Subject: [$HOSTNAME@$DOMAIN] Upgrade\n\n$MSG_HEAD\n\n$MSG_BODY" | msmtp $(id -un)
#echo -e "$MSG_HEAD\n\n$MSG_BODY" | mailx -s "[$HOSTNAME@$DOMAIN] Upgrade" -- $(whoami)
}
###############################################################################
### Environment Variables
# Source under this script directory
cd $(readlink -f $(dirname $0))
source ./.env
source /etc/os-release
###############################################################################
### Pre-Script
REBOOT=0
UPG_AUTO=1
[ $# -gt 0 ] && ([ "$1" == "-m" ] || [ "$1" == "--manual" ]) && UPG_AUTO=0
###############################################################################
### Script
runstart=$(date +%s)
rundate="$(date)"
echo "* Start time: $(date)" | tee $FILE_LOG
# Free buffers cache (pagecache, dentries and inodes)
free > /dev/null 2>&1 && sync && echo 3 > /proc/sys/vm/drop_caches
rm -Rf /var/opkg-lists/*
echo "* " | tee -a $FILE_LOG
echo "* Checking for updates, please wait..." | tee -a $FILE_LOG
fCmd opkg update
opkgStatus=""
opkgDowngradeOn=0
opkgDowngradeList=""
opkgDowngradeNb=0
if [ -f opkg-downgrade.conf ]; then
opkgDowngradeList=$(cat opkg-downgrade.conf | grep -v '^#' | cut -d' ' -f1 | xargs | sed -e 's/ /|/g')
opkgDowngradeNb=$(cat opkg-downgrade.conf | grep -v '^#' | cut -d' ' -f1 | wc -l)
if [ $UPG_AUTO -eq 0 ] && [ $opkgDowngradeNb -gt 0 ]; then
echo "* "
echo -n "* Downgrade these packages <$opkgDowngradeList>? [y/N] "
read answer
if [ -n "$(echo $answer | grep -i '^y')" ]; then
opkgDowngradeOn=1
cat opkg-downgrade.conf | while read line
do
# Skip line starts with #
if [ -n "$(echo $line | grep -v '^#')" ]; then
pkg_name=$(echo $line | cut -d' ' -f1)
pkg_url=$(echo $line | cut -d' ' -f2)
# Evaluate variable inside the url
pkg_url=$(eval echo $pkg_url)
if [ -n "$(echo $pkg_url | grep $OPENWRT_ARCH)" ]; then
if [ -n "$(echo $pkg_url | grep '^http')" ]; then
rm -f $pkg_name*.ipk
echo "* Downloading $pkg_url"
wget --no-check-certificate -q --timeout=5 $pkg_url
else
echo "* Using $pkg_url"
fi
echo "* Installing package $pkg_name"
opkg install --force-downgrade $pkg_name*.ipk
find /etc -name *-opkg -print | xargs rm > /dev/null 2>&1
else
echo "* Skipping $pkg_name... Not compatible with $OPENWRT_ARCH!"
fi
fi
done
fi
echo "* "
fi
elif [ $UPG_AUTO -eq 0 ]; then
echo "* File not found: $(pwd)/opkg-downgrade.conf"
echo "* "
fi
rm -f $FILE_LOG_ERRORS
opkgInstalled="$(opkg list-installed 2> /dev/null | wc -l)"
opkgUpgradable=$(opkg list-upgradable 2> /dev/null | wc -l)
opkgUpgradable=$(($opkgUpgradable - $opkgDowngradeNb))
echo "* Packages installed: $opkgInstalled" | tee -a $FILE_LOG
echo "* Packages upgradable: $opkgUpgradable" | tee -a $FILE_LOG
if [ $opkgUpgradable -gt 0 ]; then
echo "* " | tee -a $FILE_LOG
if [ $opkgDowngradeNb -eq 0 ]; then
echo "* Running full opkg upgrade" | tee -a $FILE_LOG
# Running in backgroud
#(opkg list-upgradable | cut -d' ' -f1 | xargs opkg upgrade && find /etc -name *-opkg -print | xargs rm > /dev/null 2>&1 && rm -f /tmp/opkgCheckDate.txt)&
# Print upgradable packages list
opkg list-upgradable | cut -d' ' -f1 | sed 's/^/- /' | tee -a $FILE_LOG
# Do the packages upgrade
opkg list-upgradable | cut -d' ' -f1 | xargs opkg upgrade > /dev/null 2> $FILE_LOG_ERRORS
# Fix packages install issue
if [ -f $FILE_LOG_ERRORS ] && [ $(cat $FILE_LOG_ERRORS | grep "check_data_file_clashes" | wc -l) -gt 0 ]; then
# * check_data_file_clashes: Package openwrt-keyring wants to install file /etc/opkg/keys/f94b9dd6febac963
cat $FILE_LOG_ERRORS | awk '/Package/{print $4}' | sed 's/^/- /' | sed 's/$/ (f)/'
opkg install --force-reinstall $(cat $FILE_LOG_ERRORS | awk '/Package/{print $4}')
REBOOT=1
fi
if [ -f $FILE_LOG_ERRORS ] && [ $(cat $FILE_LOG_ERRORS | grep "opkg_install_cmd" | wc -l) -gt 0 ]; then
# * opkg_install_cmd: Cannot install package openwrt-keyring.
cat $FILE_LOG_ERRORS | awk '/Cannot install package/{print $6}' | sed 's/\.//g' | sed 's/^/- /' | sed 's/$/ (f)/'
opkg install --force-reinstall $(cat $FILE_LOG_ERRORS | awk '/Cannot install package/{print $6}' | sed 's/\.//g')
REBOOT=1
fi
if [ $? -ne 0 ]; then
echo "* " | tee -a $FILE_LOG
echo "* Retrying upgrade..." | tee -a $FILE_LOG
opkg list-upgradable | cut -d' ' -f1 | xargs opkg upgrade 2>> $FILE_LOG
fi
else
echo "* Running partial opkg upgrade and skipping <$opkgDowngradeList>" | tee -a $FILE_LOG
# Running in backgroud
#(opkg list-upgradable | cut -d' ' -f1 | grep -vE "$opkgDowngradeList" | xargs opkg upgrade && find /etc -name *-opkg -print | xargs rm > /dev/null 2>&1 && rm -f /tmp/opkgCheckDate.txt)&
# Print upgradable packages list
opkg list-upgradable | cut -d' ' -f1 | grep -vE "$opkgDowngradeList" | sed 's/^/- /' | tee -a $FILE_LOG
# Do the packages upgrade
opkg list-upgradable | cut -d' ' -f1 | grep -vE "$opkgDowngradeList" | xargs opkg upgrade > /dev/null 2> $FILE_LOG_ERRORS
# Fix packages install issue
if [ -f $FILE_LOG_ERRORS ] && [ $(cat $FILE_LOG_ERRORS | grep "check_data_file_clashes" | wc -l) -gt 0 ]; then
# * check_data_file_clashes: Package openwrt-keyring wants to install file /etc/opkg/keys/f94b9dd6febac963
cat $FILE_LOG_ERRORS | awk '/Package/{print $4}' | sed 's/^/- /' | sed 's/$/ (f)/'
opkg install --force-reinstall $(cat $FILE_LOG_ERRORS | awk '/Package/{print $4}')
REBOOT=1
fi
if [ -f $FILE_LOG_ERRORS ] && [ $(cat $FILE_LOG_ERRORS | grep "opkg_install_cmd" | wc -l) -gt 0 ]; then
# * opkg_install_cmd: Cannot install package openwrt-keyring.
cat $FILE_LOG_ERRORS | awk '/Cannot install package/{print $6}' | sed 's/\.//g' | sed 's/^/- /' | sed 's/$/ (f)/'
opkg install --force-reinstall $(cat $FILE_LOG_ERRORS | awk '/Cannot install package/{print $6}' | sed 's/\.//g')
REBOOT=1
fi
if [ $? -ne 0 ]; then
echo "* " | tee -a $FILE_LOG
echo "* Retrying upgrade..." | tee -a $FILE_LOG
opkg list-upgradable | cut -d' ' -f1 | grep -vE "$opkgDowngradeList" | xargs opkg upgrade 2>> $FILE_LOG
fi
fi
if [ $? -eq 0 ]; then
opkgStatus="Upgrade completed."
echo "* $opkgStatus" | tee -a $FILE_LOG
else
opkgStatus="Upgrade ending with errors!"
echo "* $opkgStatus" | tee -a $FILE_LOG
fi
echo "* " | tee -a $FILE_LOG
echo "* Remove duplicated conffile" | tee -a $FILE_LOG
find /etc -name *-opkg -print | xargs rm > /dev/null 2>&1
rm -f /tmp/opkgCheckDate.txt
elif [ $opkgDowngradeOn -eq 1 ]; then
opkgStatus="Downgrade completed."
echo "* " | tee -a $FILE_LOG
echo "* $opkgStatus" | tee -a $FILE_LOG
else
opkgStatus="OpenWrt is up to date."
echo "* " | tee -a $FILE_LOG
echo "* $opkgStatus" | tee -a $FILE_LOG
fi
# Free buffers cache (pagecache, dentries and inodes)
free > /dev/null 2>&1 && sync && echo 3 > /proc/sys/vm/drop_caches
# Clean unused commands
sed -i '/acme start/d' /etc/crontabs/root
#if [ $opkgUpgradable -gt 0 ] || [ $opkgDowngradeOn -eq 1 ] || [ $REBOOT -eq 1 ]; then
if [ $REBOOT -eq 1 ]; then
if [ $UPG_AUTO -eq 1 ]; then
echo "* " | tee -a $FILE_LOG
echo "* " | tee -a $FILE_LOG
echo "* " | tee -a $FILE_LOG
echo "* Rebooting to complete the upgrade..." | tee -a $FILE_LOG
REBOOT=1
else
echo "* "
echo "* "
echo "* "
echo -n "* Reboot to complete the upgrade? [Y/n] "
read answer
if [ -n "$(echo $answer | grep -i '^y')" ] || [ -z "$answer" ]; then
REBOOT=1
fi
fi
fi
echo "* " | tee -a $FILE_LOG
echo "* End time: $(date)" | tee -a $FILE_LOG
runend=$(date +%s)
runtime=$((runend-runstart))
echo "* Elapsed time: $(($runtime / 3600))hrs $((($runtime / 60) % 60))min $(($runtime % 60))sec" | tee -a $FILE_LOG
if [ $UPG_AUTO -eq 1 ] && ( [ $opkgUpgradable -gt 0 ] || [ $opkgDowngradeOn -eq 1 ] ); then
[ -f $FILE_LOG ] && fSendMail "$opkgStatus\nOS: $OPENWRT_RELEASE" "$(cat $FILE_LOG | grep -Ev "^Downloading|^Configuring|resolve_conffiles|^\.|^$")"
fi
if [ $REBOOT -eq 1 ]; then
reboot
fi
exit 0