forked from nextcloud/vm
-
Notifications
You must be signed in to change notification settings - Fork 5
/
nextcloud_update.sh
303 lines (268 loc) · 9.17 KB
/
nextcloud_update.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/bin/bash
# shellcheck disable=2034,2059
true
# shellcheck source=lib.sh
NCDB=1 && MYCNFPW=1 && NC_UPDATE=1 . <(curl -sL https://raw.githubusercontent.com/techandme/NextBerry/master/lib.sh)
unset NC_UPDATE
unset MYCNFPW
unset NCDB
# Tech and Me © - 2017, https://www.techandme.se/
# Check for errors + debug code and abort if something isn't right
# 1 = ON
# 0 = OFF
DEBUG=0
debug_mode
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
# Put your theme name here:
THEME_NAME=""
# Must be root
if ! is_root
then
echo "Must be root to run script, in Ubuntu type: sudo -i"
exit 1
fi
# System Upgrade
apt-get update -q4 & spinner_loading
export DEBIAN_FRONTEND=noninteractive ; apt-get dist-upgrade -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"
# Update Redis PHP extention
if type pecl > /dev/null 2>&1
then
if [ "$(dpkg-query -W -f='${Status}' php7.0-dev 2>/dev/null | grep -c "ok installed")" == "0" ]
then
echo "Preparing to upgrade Redis Pecl extenstion..."
apt-get install php7.0-dev -y
fi
echo "Trying to upgrade the Redis Pecl extenstion..."
pecl upgrade redis
service apache2 restart
fi
# Update docker images
# This updates ALL Docker images: docker images | grep -v REPOSITORY | awk '{print $1}' | xargs -L1 docker pull
if [ "$(docker image inspect onlyoffice/documentserver >/dev/null 2>&1 && echo yes || echo no)" == "yes" ]
then
echo "Updating Docker container for OnlyOffice..."
docker pull onlyoffice/documentserver
fi
if [ "$(docker image inspect collabora/code >/dev/null 2>&1 && echo yes || echo no)" == "yes" ]
then
echo "Updating Docker container for Collabora..."
docker pull collabora/code
fi
# Cleanup un-used packages
apt-get autoremove -y
apt-get autoclean
# Remove update lists
rm /var/lib/apt/lists/* -r
# Set secure permissions
if [ ! -f "$SECURE" ]
then
mkdir -p "$SCRIPTS"
download_static_script setup_secure_permissions_nextcloud
chmod +x "$SECURE"
fi
# Upgrade Nextcloud
echo "Checking latest released version on the Nextcloud download server and if it's possible to download..."
wget -q -T 10 -t 2 "$NCREPO/$STABLEVERSION.tar.bz2" -O /dev/null & spinner_loading
if [ $? -eq 0 ]; then
printf "${Green}SUCCESS!${Color_Off}\n"
rm -f "$STABLEVERSION.tar.bz2"
else
echo
printf "${IRed}Nextcloud %s doesn't exist.${Color_Off}\n" "$NCVERSION"
echo "Please check available versions here: $NCREPO"
echo
exit 1
fi
# Major versions unsupported
if [ "${CURRENTVERSION%%.*}" == "$NCBAD" ]
then
echo
echo "Please note that updates between multiple major versions are unsupported! Your situation is:"
echo "Current version: $CURRENTVERSION"
echo "Latest release: $NCVERSION"
echo
echo "It is best to keep your Nextcloud server upgraded regularly, and to install all point releases"
echo "and major releases without skipping any of them, as skipping releases increases the risk of"
echo "errors. Major releases are 9, 10, 11 and 12. Point releases are intermediate releases for each"
echo "major release. For example, 9.0.52 and 10.0.2 are point releases."
echo
echo "Please contact Tech and Me to help you with upgrading between major versions."
echo "https://shop.techandme.se/index.php/product-category/support/"
echo
exit 1
fi
# Check if new version is larger than current version installed.
if version_gt "$NCVERSION" "$CURRENTVERSION"
then
echo "Latest release is: $NCVERSION. Current version is: $CURRENTVERSION."
printf "${Green}New version available! Upgrade continues...${Color_Off}\n"
else
echo "Latest version is: $NCVERSION. Current version is: $CURRENTVERSION."
echo "No need to upgrade, this script will exit..."
exit 0
fi
# Make sure old instaces can upgrade as well
if [ ! -f "$MYCNF" ] && [ -f /var/mysql_password.txt ]
then
regressionpw=$(cat /var/mysql_password.txt)
cat << LOGIN > "$MYCNF"
[client]
password='$regressionpw'
LOGIN
chmod 0600 $MYCNF
chown root:root $MYCNF
echo "Please restart the upgrade process, we fixed the password file $MYCNF."
exit 1
elif [ -z "$MARIADBMYCNFPASS" ] && [ -f /var/mysql_password.txt ]
then
regressionpw=$(cat /var/mysql_password.txt)
{
echo "[client]"
echo "password='$regressionpw'"
} >> "$MYCNF"
echo "Please restart the upgrade process, we fixed the password file $MYCNF."
exit 1
fi
if [ -z "$MARIADBMYCNFPASS" ]
then
echo "Something went wrong with copying your mysql password to $MYCNF."
echo "Please report this issue to $ISSUES, thanks!"
exit 1
else
rm -f /var/mysql_password.txt
fi
echo "Backing up files and upgrading to Nextcloud $NCVERSION in 10 seconds..."
echo "Press CTRL+C to abort."
sleep 10
# Check if backup exists and move to old
echo "Backing up data..."
DATE=$(date +%Y-%m-%d-%H%M%S)
if [ -d $BACKUP ]
then
mkdir -p "/var/NCBACKUP_OLD/$DATE"
mv $BACKUP/* "/var/NCBACKUP_OLD/$DATE"
rm -R $BACKUP
mkdir -p $BACKUP
fi
# Backup data
for folders in config themes apps
do
rsync -Aax "$NCPATH/$folders" "$BACKUP"
if [ $? -eq 0 ]
then
BACKUP_OK=1
else
unset BACKUP_OK
fi
done
if [ -z $BACKUP_OK ]
then
echo "Backup was not OK. Please check $BACKUP and see if the folders are backed up properly"
exit 1
else
printf "${Green}\nBackup OK!${Color_Off}\n"
fi
# Backup MARIADB
if mysql -u root -p"$MARIADBMYCNFPASS" -e "SHOW DATABASES LIKE '$NCCONFIGDB'" > /dev/null
then
echo "Doing mysqldump of $NCCONFIGDB..."
check_command mysqldump -u root -p"$MARIADBMYCNFPASS" -d "$NCCONFIGDB" > "$BACKUP"/nextclouddb.sql
else
echo "Doing mysqldump of all databases..."
check_command mysqldump -u root -p"$MARIADBMYCNFPASS" -d --all-databases > "$BACKUP"/alldatabases.sql
fi
# Download and validate Nextcloud package
check_command download_verify_nextcloud_stable
if [ -f "$HTML/$STABLEVERSION.tar.bz2" ]
then
echo "$HTML/$STABLEVERSION.tar.bz2 exists"
else
echo "Aborting,something went wrong with the download"
exit 1
fi
if [ -d $BACKUP/config/ ]
then
echo "$BACKUP/config/ exists"
else
echo "Something went wrong with backing up your old nextcloud instance, please check in $BACKUP if config/ folder exist."
exit 1
fi
if [ -d $BACKUP/apps/ ]
then
echo "$BACKUP/apps/ exists"
else
echo "Something went wrong with backing up your old nextcloud instance, please check in $BACKUP if apps/ folder exist."
exit 1
fi
if [ -d $BACKUP/themes/ ]
then
echo "$BACKUP/themes/ exists"
echo
printf "${Green}All files are backed up.${Color_Off}\n"
sudo -u www-data php "$NCPATH"/occ maintenance:mode --on
echo "Removing old Nextcloud instance in 5 seconds..." && sleep 5
rm -rf $NCPATH
tar -xjf "$HTML/$STABLEVERSION.tar.bz2" -C "$HTML"
rm "$HTML/$STABLEVERSION.tar.bz2"
cp -R $BACKUP/themes "$NCPATH"/
cp -R $BACKUP/config "$NCPATH"/
bash $SECURE & spinner_loading
sudo -u www-data php "$NCPATH"/occ maintenance:mode --off
sudo -u www-data php "$NCPATH"/occ upgrade --no-app-disable
else
echo "Something went wrong with backing up your old nextcloud instance, please check in $BACKUP if the folders exist."
exit 1
fi
# Recover apps that exists in the backed up apps folder
# run_static_script recover_apps
# Enable Apps
if [ -d "$SNAPDIR" ]
then
run_app_script spreedme
fi
# Change owner of $BACKUP folder to root
chown -R root:root "$BACKUP"
# Set max upload in Nextcloud .htaccess
configure_max_upload
# Set $THEME_NAME
VALUE2="$THEME_NAME"
if ! grep -Fxq "$VALUE2" "$NCPATH/config/config.php"
then
sed -i "s|'theme' => '',|'theme' => '$THEME_NAME',|g" "$NCPATH"/config/config.php
echo "Theme set"
fi
# Pretty URLs
echo "Setting RewriteBase to \"/\" in config.php..."
chown -R www-data:www-data "$NCPATH"
sudo -u www-data php "$NCPATH"/occ config:system:set htaccess.RewriteBase --value="/"
sudo -u www-data php "$NCPATH"/occ maintenance:update:htaccess
bash "$SECURE"
# Repair
sudo -u www-data php "$NCPATH"/occ maintenance:repair
sudo -u www-data php "$NCPATH"/occ status | grep "versionstring" | awk '{print $3}' > "$SCRIPTS/.versionnc"
CURRENTVERSION_after=$(sudo -u www-data php "$NCPATH"/occ status | grep "versionstring" | awk '{print $3}')
if [[ "$NCVERSION" == "$CURRENTVERSION_after" ]]
then
echo
echo "Latest version is: $NCVERSION. Current version is: $CURRENTVERSION_after."
echo "UPGRADE SUCCESS!"
echo "NEXTCLOUD UPDATE success-$(date +"%Y%m%d")" >> /var/log/cronjobs_success.log
sudo -u www-data php "$NCPATH"/occ status
sudo -u www-data php "$NCPATH"/occ maintenance:mode --off
echo
echo "If you notice that some apps are disabled it's due to that they are not compatible with the new Nextcloud version."
echo "To recover your old apps, please check $BACKUP/apps and copy them to $NCPATH/apps manually."
echo
echo "Thank you for using Tech and Me's updater!"
## Un-hash this if you want the system to reboot
# reboot
exit 0
else
echo
echo "Latest version is: $NCVERSION. Current version is: $CURRENTVERSION_after."
sudo -u www-data php "$NCPATH"/occ status
echo "UPGRADE FAILED!"
echo "Your files are still backed up at $BACKUP. No worries!"
echo "Please report this issue to $ISSUES"
exit 1
fi