-
Notifications
You must be signed in to change notification settings - Fork 3
/
ncupdate.sh
517 lines (455 loc) · 15.5 KB
/
ncupdate.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#!/bin/sh
#
## Piet's Host ## - ©2017, https://piets-host.de
#
# Tested on CentOS 6.8 & 7.4
# Tested on openSUSE Leap 42.1
#
# Some parts of this script are based on the update script of the official Nextcloud VM by Tech and Me:
# https://www.techandme.se/nextcloud-vm
# https://github.com/nextcloud/vm/blob/master/nextcloud_update.
header=' _____ _ _ _ _ _
| __ (_) | | | | | | | |
| |__) | ___| |_ ___ | |__| | ___ ___| |_
| ___/ |/ _ \ __/ __| | __ |/ _ \/ __| __| +-+-+-+-+
| | | | __/ |_\__ \ | | | | (_) \__ \ |_ | v 1.4 |
|_| |_|\___|\__|___/ |_| |_|\___/|___/\__| +-+-+-+-+'
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
# Directories - change the following lines to suit your needs:
html=/var/www/html # root html directory
ncpath=$html/nextcloud1 # name of your subfolder in root html directory, where your nextcloud installation is located
name=nextcloud_install_1 # Define a name for your Instance, which will be upgraded
backupenabled="false" # enable file-backup
datadir="" # provide external data-location, e.g. /home/data
email="[email protected]" # will be used for sending emails, if upgrade was successfull
htuser='apache' # Webserver-User (CentOS: apache, suseLinux: wwwrun, etc..)
htgroup='apache' # Webserver-Group (CentOS: apache, suseLinux: www, etc...)
# Database Variables
dbserver=127.0.0.1 # Database host
database="databasename" # Database name
user="databaseuser" # Database username
pass="S€crEtP@s$" # Database password
# Variables - Do NOT Change!
standardpath=$html/nextcloud
file=nextcloud.sql.`date +"%Y%m%d"`
restore=nextcloud.sql
host=$hostname
ocpath=$ncpath
rootuser='root'
backup=$html/backup_`date +"%Y%m%d"` # name of the backup folder, which will be created
now=`date +"%Y-%m-%d"`
date_diff="7"
old=$(date --date="${now} -${date_diff} day" +%Y-%m-%d)
oldbackup=/home/backup/backupsewikom_$old
ncrepo="https://download.nextcloud.com/server/releases"
red='\e[31m'
green='\e[32m'
yellow='\e[33m'
reset='\e[0m'
redbg='\e[41m'
lightred='\e[91m'
blue='\e[34m'
cyan='\e[36m'
ugreen='\e[4;32m'
show_help() {
cat << EOF
Usage: ${0##*/} [-bd DATA] ...
e.g.: ${0##*/} -bd /home/data
-h display this help and exit
-b enable File-Backup to another folder
-d use external data directory
EOF
}
while getopts 'hbd:' opt # -d {argument}, -b, -h
do
case "$opt" in
d)
if [ -n "$2" ]; then
if [ -d "$2" ]; then
datadir="$2"
externaldata="true"
else
printf $redbg'ERROR: "--data" requires a non-empty option argument.' >&2
printf $reset"\n"
stty echo
exit 1
fi
fi
;;
h) show_help
exit
;;
b) backupenabled="true" ;;
*) echo "ERROR: the options are -d {arg}, -b, -h" >&2; exit 1 ;;
esac
done
shift $(($OPTIND - 1))
printf $green"$header"$reset
echo ""
if [[ "$externaldata" == "true" && "$backupenabled" != "true" ]]; then
echo ""
echo ""
printf $redbg"Backup must be enabled to use external data. Type '${0##*/} -bd /location/of/data'${reset}\n"$reset
echo ""
sleep 3
exit 0
fi
# Versions
chmod +x $ncpath/occ
rm -rf $backup
if [[ "$backupenabled" == "true" ]]; then
rm -rf $oldbackup
mkdir -p $backup
echo ""
echo ""
printf $green"File-Backup is enabled!\n"$reset
echo ""
if [[ "$externaldata" == "true" ]]; then
printf $green"External Data enabled!\n"$reset
echo "Location: $datadir"
echo ""
fi
sleep 1
fi
mkdir -p /var/log/ncupdater
currentversion=$(sudo -u $htuser php $ncpath/occ status | grep "versionstring" | awk '{print $3}')
ncversion=$(curl -s -m 900 $ncrepo/ | tac | grep unknown.gif | sed 's/.*"nextcloud-\([^"]*\).zip.sha512".*/\1/;q')
# Must be root
[[ `id -u` -eq 0 ]] || { echo "Must be root to run script, type: sudo -i"; exit 1; }
# Upgrade Nextcloud
echo ""
echo "Checking latest released version on the Nextcloud download server and if it's possible to download..."
wget -q -T 10 -t 2 $ncrepo/nextcloud-$ncversion.tar.bz2 > /dev/null
if [ $? -eq 0 ]; then
echo ""
printf $ugreen"SUCCESS!\n"$reset
rm -f nextcloud-$ncversion.tar.bz2
else
echo
printf $lightred"Nextcloud $ncversion doesn't exist.\n"$reset
echo "Please check available versions here: $ncrepo"
echo
exit 1
fi
# Downgrade Warning
echo
echo "!! Warning !!"
echo
echo "Downgrading is not supported and risks corrupting your data!"
echo "If you want to revert to an older Nextcloud version,"
echo "make a new, fresh installation and then restore your data from backup."
echo
echo "Checking versions in 5 seconds.."
progress_bar() {
echo -ne ' |====> | (20%)\r'
sleep 1
echo -ne ' |=======> | (40%)\r'
sleep 1
echo -ne ' |===========> | (60%)\r'
sleep 1
echo -ne ' |===============> | (80%)\r'
sleep 1
echo -ne ' |===================>| (100%)\r'
echo -ne '\n'
}
progress_bar
# Check if new version is larger than current version installed.
function version_gt() { local v1 v2 IFS=.; read -ra v1 <<< "$1"; read -ra v2 <<< "$2"; printf -v v1 %03d "${v1[@]}"; printf -v v2 %03d "${v2[@]}"; [[ $v1 > $v2 ]]; }
if version_gt "$ncversion" "$currentversion"
then
echo ""
printf "Latest version is: ${ugreen}${ncversion}${reset}. Current version is: ${ugreen}${currentversion}\n"$reset
echo ""
printf $green"New version available! Upgrade continues...\n"$reset
echo ""
sleep 2
else
echo ""
printf "Latest version is: ${ugreen}${ncversion}${reset}. Current version is: ${ugreen}${currentversion}\n"$reset
echo ""
echo "No need to upgrade, this script will exit..."
echo "Your Nextcloud Version $ncversion is already up to date - No upgrade needed - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
sleep 2
exit 0
fi
if [[ "$backupenabled" == "true" ]]; then
echo "Backing up files and upgrading to Nextcloud $ncversion in 5 seconds..."
else
echo "Upgrading to Nextcloud $ncversion in 5 seconds..."
fi
echo "Press CTRL+C to abort."
progress_bar
if [[ "$backupenabled" == "true" ]]; then
# Backup data
echo ""
printf $yellow"Backing up data... That may take some time - depending on your Installation!\n"$reset
echo ""
rsync -Aax $ncpath/config $backup
rsync -Aax $ncpath/themes $backup
rsync -Aax $ncpath/apps $backup
if [[ "$externaldata" == "true" ]]; then
rsync_param_data="-Aaxv"
rsync "$rsync_param_data" $datadir $backup |\
pv -lep -s $(rsync "$rsync_param_data"n $datadir $backup | awk 'NF' | wc -l)
else
rsync_param_data="-Aaxv"
rsync "$rsync_param_data" $ncpath/data $backup |\
pv -lep -s $(rsync "$rsync_param_data"n $ncpath/data $backup | awk 'NF' | wc -l)
fi
else
{
rm -rf `find $ncpath/. |grep -v "data"|grep -v "config"|grep -v "themes"|grep -v "apps"`
} &> /dev/null
fi
unalias rm 2> /dev/null
rm $ncpath/${file} 2> /dev/null
rm $ncpath/${file}.gz 2> /dev/null
sleep 1
# Database Backup
echo ""
printf $yellow"Starting Database Backup...\n"$reset
echo ""
sleep 1
# use this command for a database server on a separate host:
#mysqldump --opt --protocol=TCP --user=${user} --password=${pass} --host=${dbserver} ${database} > $ncpath/${file}
# use this command for a database server on localhost. add other options if need be.
mysqldump --opt --user=${user} --password=${pass} ${database} > $ncpath/${file}
gzip $file
printf $green"${file}.gz was created\n"$reset
sleep 1
echo ""
function dbrestore {
echo ""
printf $yellow"Restoring Database...\n"$reset
gunzip $ncpath/${file}.gz
echo "$ncpath/${file}"
mv $ncpath/${file} ${restore}
mysql --user=${user} --password=${pass} ${database} < $ncpath/${restore}
echo ""
printf $green"Database restored successfully...\n"$reset
echo ""
echo "NEXTCLOUD UPDATE FAILED - Database restored successfully - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
sleep 1
}
if [[ $? > 0 ]]
then
if [[ "$backupenabled" == "true" ]]; then
printf $red"Backup was not OK.${reset} Please check $backup and see if the folders are backed up properly"
echo "NEXTCLOUD UPDATE FAILED - Backup wasn't successfull - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
exit 1
else
printf $red"Backup was not OK.${reset} Please check $ncpath and see if the folders still exist"
echo "NEXTCLOUD UPDATE FAILED - Backup wasn't successfull - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
dbrestore
exit 1
fi
else
printf $ugreen"Backup OK!\n"$reset
echo ""
sleep 1
fi
echo "Downloading $ncrepo/nextcloud-$ncversion.tar.bz2..."
echo ""
wget -q -T 10 -t 2 $ncrepo/nextcloud-$ncversion.tar.bz2 -P $html
if [ -f $html/nextcloud-$ncversion.tar.bz2 ]; then
printf "$html/nextcloud-$ncversion.tar.bz2 ${green}exists\n"$reset
else
echo "Aborting,something went wrong with the download"
echo "NEXTCLOUD UPDATE FAILED - Download couldn't be completed - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
dbrestore
exit 1
fi
if [[ "$backupenabled" == "true" ]]; then
if [ -d $backup/config/ ]; then
printf "$backup/config/ ${green}exists\n"$reset
else
echo "Something went wrong with backing up your old nextcloud instance, please check in $backup if config/ folder exist."
echo "NEXTCLOUD UPDATE FAILED - /config couldn't be backed up - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
dbrestore
exit 1
fi
else
if [ -d $ncpath/config/ ]; then
printf "$ncpath/config/ ${green}exists\n"$reset
else
echo "Something went wrong with backing up your old nextcloud instance, please check in $ncpath if config/ folder exist."
echo "NEXTCLOUD UPDATE FAILED - /config couldn't be backed up - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
dbrestore
exit 1
fi
fi
if [[ "$backupenabled" == "true" ]]; then
if [ -d $backup/data/ ]; then
printf "$backup/data/ ${green}exists\n"$reset
else
echo "Something went wrong with backing up your old nextcloud instance, please check in $backup if data/ folder exist."
echo "NEXTCLOUD UPDATE FAILED - /data couldn't be backed up - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
dbrestore
exit 1
fi
else
if [ -d $ncpath/data/ ]; then
printf "$ncpath/data/ ${green}exists\n"$reset
else
echo "Something went wrong with backing up your old nextcloud instance, please check in $ncpath if data/ folder exist."
echo "NEXTCLOUD UPDATE FAILED - /data couldn't be backed up - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
dbrestore
exit 1
fi
fi
if [[ "$backupenabled" == "true" ]]; then
if [ -d $backup/apps/ ]; then
printf "$backup/apps/ ${green}exists\n"$reset
else
echo "Something went wrong with backing up your old nextcloud instance, please check in $backup if apps/ folder exist."
echo "NEXTCLOUD UPDATE FAILED - /apps couldn't be backed up - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
dbrestore
exit 1
fi
else
if [ -d $ncpath/apps/ ]; then
printf "$ncpath/apps/ ${green}exists\n"$reset
else
echo "Something went wrong with backing up your old nextcloud instance, please check in $ncpath if apps/ folder exist."
echo "NEXTCLOUD UPDATE FAILED - /apps couldn't be backed up - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
dbrestore
exit 1
fi
fi
if [[ "$backupenabled" == "true" ]]; then
if [ -d $backup/themes/ ]; then
printf "$backup/themes/ ${green}exists\n"$reset
else
echo "Something went wrong with backing up your old nextcloud instance, please check in $backup if apps/ folder exist."
echo "NEXTCLOUD UPDATE FAILED - /apps couldn't be backed up - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
dbrestore
exit 1
fi
else
if [ -d $ncpath/themes/ ]; then
printf "$ncpath/themes/ ${green}exists\n"$reset
else
echo "Something went wrong with backing up your old nextcloud instance, please check in $ncpath if apps/ folder exist."
echo "NEXTCLOUD UPDATE FAILED - /apps couldn't be backed up - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
dbrestore
exit 1
fi
fi
echo ""
if [[ "$backupenabled" == "true" ]]; then
printf $green"All files are backed up.\n"$reset
sleep 1
echo ""
echo "Removing old Nextcloud instance in 5 seconds..."
else
printf $green"All files are okay.\n"$reset
echo ""
echo "Removing old Nextcloud files in 5 seconds..."
fi
progress_bar
if [[ "$backupenabled" == "true" ]]; then
rm -rf $ncpath
echo ""
printf $green"All files removed!\n"$reset
echo ""
sleep 1
fi
printf $yellow"Files are being extracted... \n"$reset
echo ""
pv -w 80 $html/nextcloud-$ncversion.tar.bz2 | tar xjf - -C $html
if [[ "$backupenabled" == "true" ]]; then
mkdir -p $ncpath
mv $standardpath/* $ncpath/
mv $standardpath/.htaccess $ncpath/
mv $standardpath/.user.ini $ncpath/
else
{
rm -rf $standardpath/data
rm -rf $standardpath/config
rm -rf $standardpath/apps
rm -rf $standardpath/themes
rm -f $ncpath/.htaccess
rm -f $ncpath/.user.ini
} &> /dev/null
mv $standardpath/* $ncpath/
mv $standardpath/.htaccess $ncpath/
mv $standardpath/.user.ini $ncpath/
fi
rm $html/nextcloud-$ncversion.tar.bz2
sleep 1
echo ""
printf $green"Extract completed.\n"$reset
echo ""
sleep 1
if [[ "$backupenabled" == "true" ]]; then
printf $yellow"Copying files back to installation.... That may take a long time - depending on your installation!\n"$reset
echo ""
cp -R $backup/themes $ncpath/
cp -R $backup/config $ncpath/
cp -R $backup/data $ncpath/
printf $green"Copying completed.\n"$reset
echo ""
sleep 2
fi
# remove config.sample.php
rm -f $ncpath/config/config.sample.php
printf $yellow"Setting file permissions...\n"$reset
echo ""
# Fix permissions
chown -R ${rootuser}:${htgroup} ${ocpath}/
chown -R ${htuser}:${htgroup} ${ocpath}/apps/
chown -R ${htuser}:${htgroup} ${ocpath}/config/
chown -R ${htuser}:${htgroup} ${ocpath}/data/
chown -R ${htuser}:${htgroup} ${ocpath}/themes/
chown -R ${htuser}:${htgroup} ${ocpath}/updater/
sleep 2
printf $green"File permissions set successfully!\n"$reset
echo ""
# occ upgrade
printf $green"Starting Upgrade Process....\n"$reset
sleep 2
echo ""
chmod +x $ncpath/occ
echo ""
sudo -u $htuser php $ncpath/occ upgrade
if [[ "$backupenabled" == "true" ]]; then
# Change owner of $backup folder to root
chown -R root:root $backup
fi
currentversion_after=$(sudo -u $htuser 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 ""
printf $ugreen"UPGRADE SUCCESS!\n"$reset
echo ""
echo "NEXTCLOUD UPDATE $CURRENTVERSION_after success - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
# Send E-Mail if successfully updated
echo "Hey there!
Your Nextcloud-Update completed successfully!
Host: $host
Name: $name
directory: $ncpath
`date +"%d.%m.%Y-%H:%M:%S"`
Thank you for using Piet's Host Nextcloud-Updater!" | mail -s "NEXTCLOUD UPDATE SUCCESS - `date +"%d.%m.%Y"`" $email
sudo -u $htuser php $ncpath/occ status
echo ""
sudo -u $htuser php $ncpath/occ maintenance:mode --off
echo
echo "Thank you for using Piet's Host Nextcloud-Updater!"
echo ""
## Uncomment, if you want to reboot your server after upgrade
# reboot
exit 0
else
echo
printf "\e[40;38;4;82mLatest: $ncversion \e[30;48;4;82mCurrent: $currentversion_after \n"$reset
sudo -u $htuser php $ncpath/occ status
printf $red"UPGRADE FAILED!\n"$reset
echo "Your files are still backed up at $ncpath. No worries!"
echo "Please report this issue to [email protected]"
echo ""
echo "NEXTCLOUD UPDATE FAILED - `date +"%d.%m.%Y"`" >> /var/log/ncupdater/ncupdater_$name.log
exit 1
fi