forked from nextcloud/vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nextcloud_update.sh
977 lines (887 loc) Β· 33 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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
#!/bin/bash
#################################################################################################################
# DO NOT USE THIS SCRIPT WHEN UPDATING NEXTCLOUD / YOUR SERVER! RUN `sudo bash /var/scripts/update.sh` INSTEAD. #
#################################################################################################################
true
SCRIPT_NAME="Nextcloud Update Script"
# shellcheck source=lib.sh
source <(curl -sL https://raw.githubusercontent.com/MaximumFish/vm/master/lib.sh)
# Get all needed variables from the library
ncdb
nc_update
# T&M Hansson IT AB Β© - 2021, https://www.hanssonit.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
# Must be root
root_check
# Check if dpkg or apt is running
is_process_running apt
is_process_running dpkg
# Check for pending-snapshot
if does_snapshot_exist "NcVM-snapshot-pending"
then
msg_box "Cannot proceed with the update currently because NcVM-snapshot-pending exists.\n
It is possible that a backup is currently running.\n
Advice: don't restart your system now if that is the case!"
# Don't exit the script while the snapshot exists to disable any automatic restart during backups
while does_snapshot_exist "NcVM-snapshot-pending"
do
print_text_in_color "$ICyan" "Waiting for NcVM-snapshot-pending to vanish... (press '[CTRL] + [C]' to cancel)"
sleep 5
done
print_text_in_color "$ICyan" "Continuing the update..."
fi
# Create a snapshot before doing anything else
check_free_space
if ! [ -f "$SCRIPTS/nextcloud-startup-script.sh" ] && (does_snapshot_exist "NcVM-startup" \
|| does_snapshot_exist "NcVM-snapshot" || [ "$FREE_SPACE" -ge 50 ] )
then
SNAPSHOT_EXISTS=1
if is_docker_running
then
check_command systemctl stop docker
fi
nextcloud_occ maintenance:mode --on
if does_snapshot_exist "NcVM-startup"
then
check_command lvremove /dev/ubuntu-vg/NcVM-startup -y
elif does_snapshot_exist "NcVM-snapshot"
then
if ! lvremove /dev/ubuntu-vg/NcVM-snapshot -y
then
nextcloud_occ maintenance:mode --off
start_if_stopped docker
notify_admin_gui "Update failed!" \
"Could not remove NcVM-snapshot - Please reboot your server! $(date +%T)"
msg_box "It seems like the old snapshot could not get removed.
This should work again after a reboot of your server."
exit 1
fi
fi
if ! lvcreate --size 5G --snapshot --name "NcVM-snapshot" /dev/ubuntu-vg/ubuntu-lv
then
nextcloud_occ maintenance:mode --off
start_if_stopped docker
notify_admin_gui "Update failed!" \
"Could not create NcVM-snapshot - Please reboot your server! $(date +%T)"
msg_box "The creation of a snapshot failed.
If you just merged and old one, please reboot your server again.
It should then start working again."
exit 1
fi
if ! lvrename /dev/ubuntu-vg/NcVM-snapshot /dev/ubuntu-vg/NcVM-snapshot-pending
then
nextcloud_occ maintenance:mode --off
start_if_stopped docker
msg_box "Could not rename the snapshot before starting the update. Please reboot your system!"
exit 1
fi
nextcloud_occ maintenance:mode --off
start_if_stopped docker
fi
# Check if /boot is filled more than 90% and exit the script if that's
# the case since we don't want to end up with a broken system
if [ -d /boot ]
then
if [[ "$(df -h | grep -m 1 /boot | awk '{print $5}' | cut -d "%" -f1)" -gt 90 ]]
then
msg_box "It seems like your boot drive is more than 90% full. \
You can't proceed to upgrade, as it would likely break your system.
To be able to proceed with the update you need to delete some old Linux kernels. If you need support, please visit:
https://shop.hanssonit.se/product/premium-support-per-30-minutes/"
exit
fi
fi
# Ubuntu 16.04 is deprecated
check_distro_version
# Remove leftovers
if [ -f /root/php-upgrade.sh ]
then
rm -f /root/php-upgrade.sh
elif [ -f /tmp/php-upgrade.sh ]
then
rm -f /tmp/php-upgrade.sh
fi
# Hold PHP if Ondrejs PPA is used
print_text_in_color "$ICyan" "Fetching latest packages with apt..."
apt update -q4 & spinner_loading
if apt-cache policy | grep "ondrej" >/dev/null 2>&1
then
print_text_in_color "$ICyan" "Ondrejs PPA is installed. \
Holding PHP to avoid upgrading to a newer version without migration..."
apt-mark hold php*
#check_php
#apt-mark unhold php"$PHPVER"*
fi
# Don't allow MySQL/MariaDB
if ! grep -q pgsql /var/www/nextcloud/config/config.php || dpkg -l | grep -q mariadb-common
then
msg_box "MySQL/MariaDB is not supported in this script anymore. Please contact us to get support \
for upgrading your server: https://shop.hanssonit.se/product/premium-support-per-30-minutes/"
exit 0
fi
# Check if the DIR actually is a file
if [ -f /var/log/nextcloud ]
then
rm -f /var/log/nextcloud
fi
# Move all logs to new dir (2019-09-04) # updated 2021-01-27
mkdir -p "$VMLOGS"
find_log() {
NCLOG=$(find / -type f -name "nextcloud.log" 2> /dev/null)
if [ "$NCLOG" != "$VMLOGS/nextcloud.log" ]
then
# Might enter here if no OR multiple logs already exist, tidy up any existing logs and set the correct path
print_text_in_color "$ICyan" "Unexpected or non-existent logging configuration - \
deleting any discovered nextcloud.log files and creating a new one at $VMLOGS/nextcloud.log..."
xargs rm -f <<< "$NCLOG"
# Set logging
nextcloud_occ config:system:set log_type --value=file
nextcloud_occ config:system:set logfile --value="$VMLOGS/nextcloud.log"
nextcloud_occ config:system:set loglevel --value=2
touch "$VMLOGS/nextcloud.log"
chown www-data:www-data "$VMLOGS/nextcloud.log"
fi
}
if [ -d /var/log/ncvm/ ]
then
rsync -Aaxz /var/log/ncvm/ "$VMLOGS"
rm -Rf /var/log/ncvm/
rm -f "$NCDATA"/*.log*
elif [ -d /var/ncdata/ ] && [ -n "$(find /var/ncdata -maxdepth 1 -name "*.log")" ]
then
rsync -Aaxz /var/ncdata/*.log "$VMLOGS"
rm -f /var/ncdata/*.log*
elif [ -n "$(find "$NCDATA" -maxdepth 1 -name "*.log")" ]
then
rsync -Aaxz "$NCDATA"/*.log "$VMLOGS"
rm -f "$NCDATA"/*.log*
fi
# Set secure permissions
if [ ! -f "$SECURE" ]
then
mkdir -p "$SCRIPTS"
download_script STATIC setup_secure_permissions_nextcloud
chmod +x "$SECURE"
else
rm "$SECURE"
download_script STATIC setup_secure_permissions_nextcloud
chmod +x "$SECURE"
fi
# Remove the local lib.sh since it's causing issues with new functions (2020-06-01)
if [ -f $SCRIPTS/lib.sh ]
then
rm -f $SCRIPTS/lib.sh
fi
# Update updatenotification.sh
if [ -f $SCRIPTS/updatenotification.sh ]
then
download_script STATIC updatenotification
chmod +x $SCRIPTS/updatenotification.sh
fi
# Make sure everyone gets access to menu.sh
download_script MENU menu
# Make sure fetch_lib.sh is available
download_script STATIC fetch_lib
# Update docker-ce to overlay2 since devicemapper is deprecated
if [ -f /etc/systemd/system/docker.service ]
then
if grep -q "devicemapper" /etc/systemd/system/docker.service
then
print_text_in_color "$ICyan" "Changing to Overlay2 for Docker CE..."
print_text_in_color "$ICyan" "Please report any issues to $ISSUES."
run_script STATIC docker_overlay2
elif grep -q "aufs" /etc/default/docker
then
apt-mark hold docker-ce
run_script STATIC docker_overlay2
fi
fi
export DEBIAN_FRONTEND=noninteractive ; apt dist-upgrade -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"
# Update Netdata
if [ -d /etc/netdata ]
then
print_text_in_color "$ICyan" "Updating Netdata..."
NETDATA_UPDATER_PATH="$(find /usr -name 'netdata-updater.sh')"
if [ -n "$NETDATA_UPDATER_PATH" ]
then
install_if_not cmake # Needed for Netdata in newer versions
bash "$NETDATA_UPDATER_PATH"
fi
fi
# Fix PHP error message
mkdir -p /tmp/pear/cache
# Update Redis PHP extension (18.04 --> 20.04 since 16.04 already is deprecated in the top of this script)
print_text_in_color "$ICyan" "Trying to upgrade the Redis PECL extension..."
# Check current PHP version
check_php
# Do the upgrade
if pecl list | grep redis >/dev/null 2>&1
then
if is_this_installed php"$PHPVER"-common
then
install_if_not php"$PHPVER"-dev
fi
pecl channel-update pecl.php.net
yes no | pecl upgrade redis
systemctl restart redis-server.service
fi
# Remove old redis
if grep -qFx extension=redis.so "$PHP_INI"
then
sed -i "/extension=redis.so/d" "$PHP_INI"
fi
# Check if redis is enabled and create the file if not
if [ ! -f $PHP_MODS_DIR/redis.ini ]
then
touch $PHP_MODS_DIR/redis.ini
fi
# Enable new redis
if ! grep -qFx extension=redis.so $PHP_MODS_DIR/redis.ini
then
echo "# PECL redis" > $PHP_MODS_DIR/redis.ini
echo "extension=redis.so" >> $PHP_MODS_DIR/redis.ini
check_command phpenmod -v ALL redis
fi
# Upgrade APCu and igbinary
if [ "${CURRENTVERSION%%.*}" -ge "17" ]
then
if [ -f "$PHP_INI" ]
then
print_text_in_color "$ICyan" "Trying to upgrade igbinary, smbclient, and APCu..."
if pecl list | grep igbinary >/dev/null 2>&1
then
yes no | pecl upgrade igbinary
# Remove old igbinary
if grep -qFx extension=igbinary.so "$PHP_INI"
then
sed -i "/extension=igbinary.so/d" "$PHP_INI"
fi
# Check if igbinary is enabled and create the file if not
if [ ! -f $PHP_MODS_DIR/igbinary.ini ]
then
touch $PHP_MODS_DIR/igbinary.ini
fi
# Enable new igbinary
if ! grep -qFx extension=igbinary.so $PHP_MODS_DIR/igbinary.ini
then
echo "# PECL igbinary" > $PHP_MODS_DIR/igbinary.ini
echo "extension=igbinary.so" >> $PHP_MODS_DIR/igbinary.ini
check_command phpenmod -v ALL igbinary
fi
fi
if pecl list | grep -q smbclient
then
yes no | pecl upgrade smbclient
# Check if smbclient is enabled and create the file if not
if [ ! -f $PHP_MODS_DIR/smbclient.ini ]
then
touch $PHP_MODS_DIR/smbclient.ini
fi
# Enable new smbclient
if ! grep -qFx extension=smbclient.so $PHP_MODS_DIR/smbclient.ini
then
echo "# PECL smbclient" > $PHP_MODS_DIR/smbclient.ini
echo "extension=smbclient.so" >> $PHP_MODS_DIR/smbclient.ini
check_command phpenmod -v ALL smbclient
fi
# Remove old smbclient
if grep -qFx extension=smbclient.so "$PHP_INI"
then
sed -i "/extension=smbclient.so/d" "$PHP_INI"
fi
fi
if pecl list | grep -q apcu
then
yes no | pecl upgrade apcu
# Remove old igbinary
if grep -qFx extension=apcu.so "$PHP_INI"
then
sed -i "/extension=apcu.so/d" "$PHP_INI"
fi
# Check if apcu is enabled and create the file if not
if [ ! -f $PHP_MODS_DIR/apcu.ini ]
then
touch $PHP_MODS_DIR/apcu.ini
fi
# Enable new apcu
if ! grep -qFx extension=apcu.so $PHP_MODS_DIR/apcu.ini
then
echo "# PECL apcu" > $PHP_MODS_DIR/apcu.ini
echo "extension=apcu.so" >> $PHP_MODS_DIR/apcu.ini
check_command phpenmod -v ALL apcu
fi
# Fix https://help.nextcloud.com/t/nc-21-manual-update-issues/108693/4?$
if ! grep -qFx apc.enable_cli=1 $PHP_MODS_DIR/apcu.ini
then
echo "apc.enable_cli=1" >> $PHP_MODS_DIR/apcu.ini
check_command phpenmod -v ALL apcu
fi
fi
if pecl list | grep -q inotify
then
# Remove old inotify
if grep -qFx extension=inotify.so "$PHP_INI"
then
sed -i "/extension=inotify.so/d" "$PHP_INI"
fi
yes no | pecl upgrade inotify
if [ ! -f $PHP_MODS_DIR/inotify.ini ]
then
touch $PHP_MODS_DIR/inotify.ini
fi
if ! grep -qFx extension=inotify.so $PHP_MODS_DIR/inotify.ini
then
echo "# PECL inotify" > $PHP_MODS_DIR/inotify.ini
echo "extension=inotify.so" >> $PHP_MODS_DIR/inotify.ini
check_command phpenmod -v ALL inotify
fi
fi
fi
fi
# Make sure services are restarted
restart_webserver
# Update adminer
if [ -d $ADMINERDIR ]
then
print_text_in_color "$ICyan" "Updating Adminer..."
rm -f "$ADMINERDIR"/latest.php "$ADMINERDIR"/adminer.php
curl_to_dir "http://www.adminer.org" "latest.php" "$ADMINERDIR"
ln -s "$ADMINERDIR"/latest.php "$ADMINERDIR"/adminer.php
fi
# Get newest dat files for geoblock.sh
if grep -q "^#Geoip-block" /etc/apache2/apache2.conf
then
get_newest_dat_files
check_command systemctl restart apache2
fi
# Update docker containers and remove Watchtower if Bitwarden is present due to compatibility issue
# If Watchtower is installed, but Bitwarden is missing, then let watchtower do its thing
# If Watchtower is installed together with Bitwarden, then remove Watchtower and run updates
# individually depending on which docker containers that exist.
if is_docker_running
then
# To fix https://github.com/nextcloud/vm/issues/1459 we need to remove Watchtower
# to avoid updating Bitwarden again, and only update the specified docker images above
if docker ps -a --format '{{.Names}}' | grep -Eq "bitwarden";
then
if [ -d /root/bwdata ] || [ -d "$BITWARDEN_HOME"/bwdata ]
then
if does_this_docker_exist 'containrrr/watchtower'
then
docker stop watchtower
WATCHTOWER=1
elif does_this_docker_exist 'v2tec/watchtower'
then
docker stop watchtower
WATCHTOWER=1
fi
docker container prune -f
docker image prune -a -f
docker volume prune -f
if [ -n "$WATCHTOWER" ]
then
notify_admin_gui "Watchtower removed" "Due to compatibility issues with Bitwarden and Watchtower, \
we have removed Watchtower from this server. Updates will now happen for each container separately."
fi
fi
fi
# Update selected images
# Bitwarden RS
docker_update_specific 'bitwarden_rs' "Bitwarden RS"
# Collabora CODE
docker_update_specific 'code' 'Collabora'
# OnlyOffice
docker_update_specific 'onlyoffice' 'OnlyOffice'
# Full Text Search
docker_update_specific 'fts_esror' 'Full Text Search'
# Plex
docker_update_specific 'plex' "Plex Media Server"
fi
# Cleanup un-used packages
apt autoremove -y
apt autoclean
# Update GRUB, just in case
update-grub
# Remove update lists
rm /var/lib/apt/lists/* -r
# Free some space (ZFS snapshots)
if is_this_installed libzfs2linux
then
if grep -rq ncdata /etc/mtab
then
run_script DISK prune_zfs_snaphots
fi
fi
# Fix bug in nextcloud.sh
CURRUSR="$(getent group sudo | cut -d: -f4 | cut -d, -f1)"
if grep -q "6.ifcfg.me" $SCRIPTS/nextcloud.sh &>/dev/null
then
rm -f "$SCRIPTS/nextcloud.sh"
download_script STATIC nextcloud
chown "$CURRUSR":"$CURRUSR" "$SCRIPTS/nextcloud.sh"
chmod +x "$SCRIPTS/nextcloud.sh"
elif [ -f $SCRIPTS/techandme.sh ]
then
rm -f "$SCRIPTS/techandme.sh"
download_script STATIC nextcloud
chown "$CURRUSR":"$CURRUSR" "$SCRIPTS/nextcloud.sh"
chmod +x "$SCRIPTS/nextcloud.sh"
if [ -f /home/"$CURRUSR"/.bash_profile ]
then
sed -i "s|techandme|nextcloud|g" /home/"$CURRUSR"/.bash_profile
elif [ -f /home/"$CURRUSR"/.profile ]
then
sed -i "s|techandme|nextcloud|g" /home/"$CURRUSR"/.profile
fi
fi
# Update all Nextcloud apps
if [ "${CURRENTVERSION%%.*}" -ge "15" ]
then
nextcloud_occ maintenance:mode --off
# Check for upgrades
print_text_in_color "$ICyan" "Trying to automatically update all Nextcloud apps..."
UPDATED_APPS="$(nextcloud_occ_no_check app:update --all)"
fi
# Check which apps got updated
if [ -n "$UPDATED_APPS" ]
then
print_text_in_color "$IGreen" "$UPDATED_APPS"
notify_admin_gui \
"You've got app updates!" \
"$UPDATED_APPS"
else
print_text_in_color "$IGreen" "Your apps are already up to date!"
fi
# Nextcloud 13 is required.
lowest_compatible_nc 13
if [ -f /tmp/minor.version ]
then
NCBAD=$(cat /tmp/minor.version)
NCVERSION=$(curl -s -m 900 $NCREPO/ | sed --silent 's/.*href="nextcloud-\([^"]\+\).zip.asc".*/\1/p' | sort --version-sort | grep "${CURRENTVERSION%%.*}" | tail -1)
export NCVERSION
export STABLEVERSION="nextcloud-$NCVERSION"
rm -f /tmp/minor.version
elif [ -f /tmp/nextmajor.version ]
then
NCBAD=$(cat /tmp/nextmajor.version)
NCVERSION=$(curl -s -m 900 $NCREPO/ | sed --silent 's/.*href="nextcloud-\([^"]\+\).zip.asc".*/\1/p' | sort --version-sort | tail -1)
if [ -z "$NCVERSION" ]
then
msg_box "The version that you are trying to upgrade to doesn't exist."
exit 1
fi
export NCVERSION
export STABLEVERSION="nextcloud-$NCVERSION"
rm -f /tmp/nextmajor.version
elif [ -f /tmp/prerelease.version ]
then
PRERELEASE_VERSION=yes
msg_box "WARNING! You are about to update to a Beta/RC version of Nextcloud.\nThere's no turning back, \
as it's not currently possible to downgrade.\n\nPlease only continue if you have made a backup, or took a snapshot."
if ! yesno_box_no "Are you sure you would like to proceed?"
then
rm -f /tmp/prerelease.version
unset PRERELEASE_VERSION
else
if grep -q beta /tmp/prerelease.version
then
NCREPO="https://download.nextcloud.com/server/prereleases"
NCVERSION=$(curl -s -m 900 $NCREPO/ | sed --silent 's/.*href="nextcloud-\([^"]\+\).zip.asc".*/\1/p' | sort --version-sort | tail -1)
STABLEVERSION="nextcloud-$NCVERSION"
rm -f /tmp/prerelease.version
elif grep -q RC /tmp/prerelease.version
then
NCREPO="https://download.nextcloud.com/server/prereleases"
NCVERSION=$(cat /tmp/prerelease.version)
STABLEVERSION="nextcloud-$NCVERSION"
rm -f /tmp/prerelease.version
fi
fi
fi
# Rename snapshot
if [ -n "$SNAPSHOT_EXISTS" ]
then
check_command lvrename /dev/ubuntu-vg/NcVM-snapshot-pending /dev/ubuntu-vg/NcVM-snapshot
fi
# Major versions unsupported
if [[ "${CURRENTVERSION%%.*}" -le "$NCBAD" ]]
then
msg_box "Please note that updates between multiple major versions are unsupported! Your situation is:
Current version: $CURRENTVERSION
Latest release: $NCVERSION
It is best to keep your Nextcloud server upgraded regularly, and to install all point releases
and major releases, as skipping releases increases the risk of errors. Major releases are
16, 17, 18 and 19. Point releases are intermediate releases for each major release.
For example, 18.0.5 and 19.0.2 are point releases.
You can read more about Nextcloud releases here: https://github.com/nextcloud/server/wiki/Maintenance-and-Release-Schedule
Please contact T&M Hansson IT AB to help you with upgrading between major versions.
https://shop.hanssonit.se/product/upgrade-between-major-owncloud-nextcloud-versions/"
exit 1
fi
# Check if new version is larger than current version installed. Skip version check if you want to upgrade to a prerelease.
if [ -z "$PRERELEASE_VERSION" ]
then
print_text_in_color "$ICyan" "Checking for new Nextcloud version..."
if version_gt "$NCVERSION" "$CURRENTVERSION"
then
print_text_in_color "$ICyan" "Latest release is: $NCVERSION. Current version is: $CURRENTVERSION."
print_text_in_color "$IGreen" "New version available, upgrade continues!"
else
print_text_in_color "$IGreen" "You already run the latest version! ($CURRENTVERSION)"
exit 0
fi
fi
############# Don't upgrade to specific version
DONOTUPDATETO='20.0.6'
if [[ "$NCVERSION" == "$DONOTUPDATETO" ]]
then
msg_box "Due to serious bugs with version $DONOTUPDATETO we won't upgrade to that version."
exit
fi
# Check if PHP version is compatible with $NCVERSION
PHP_VER=71
NC_VER=16
if [ "${NCVERSION%%.*}" -ge "$NC_VER" ]
then
if [ "$(php -v | head -n 1 | cut -d " " -f 2 | cut -c 1,3)" -lt "$PHP_VER" ]
then
msg_box "Your PHP version isn't compatible with the new version of Nextcloud. Please upgrade your PHP stack and try again.
If you need support, please visit https://shop.hanssonit.se/product/upgrade-php-version-including-dependencies/"
exit
fi
fi
# Check if PHP version is compatible with $NCVERSION
PHP_VER=72
NC_VER=20
if [ "${NCVERSION%%.*}" -ge "$NC_VER" ]
then
if [ "$(php -v | head -n 1 | cut -d " " -f 2 | cut -c 1,3)" -lt "$PHP_VER" ]
then
msg_box "Your PHP version isn't compatible with the new version of Nextcloud. Please upgrade your PHP stack and try again.
If you need support, please visit https://shop.hanssonit.se/product/upgrade-php-version-including-dependencies/"
exit
fi
fi
# Check if PHP version is compatible with $NCVERSION
PHP_VER=73
NC_VER=21
if [ "${NCVERSION%%.*}" -ge "$NC_VER" ]
then
if [ "$(php -v | head -n 1 | cut -d " " -f 2 | cut -c 1,3)" -lt "$PHP_VER" ]
then
msg_box "Your PHP version isn't compatible with the new version of Nextcloud. Please upgrade your PHP stack and try again.
If you need support, please visit https://shop.hanssonit.se/product/upgrade-php-version-including-dependencies/"
exit
fi
fi
# Upgrade Nextcloud
if ! site_200 $NCREPO
then
msg_box "$NCREPO seems to be down, or temporarily not reachable. Please try again in a few minutes."
exit 1
fi
countdown "Backing up files and upgrading to Nextcloud $NCVERSION in 10 seconds... Press CTRL+C to abort." "10"
# Rename snapshot
if [ -n "$SNAPSHOT_EXISTS" ]
then
check_command lvrename /dev/ubuntu-vg/NcVM-snapshot /dev/ubuntu-vg/NcVM-snapshot-pending
fi
# Backup app status
# Fixing https://github.com/nextcloud/server/issues/4538
print_text_in_color "$ICyan" "Getting and backing up the status of apps for later, this might take a while..."
NC_APPS="$(nextcloud_occ app:list | awk '{print$2}' | tr -d ':' | sed '/^$/d')"
if [ -z "$NC_APPS" ]
then
print_text_in_color "$IRed" "No apps detected, aborting export of app status... Please report this issue to $ISSUES"
APPSTORAGE="no-export-done"
else
declare -Ag APPSTORAGE
for app in $NC_APPS
do
APPSTORAGE[$app]=$(nextcloud_occ_no_check config:app:get "$app" enabled)
done
fi
# Stop Apache2
check_command systemctl stop apache2.service
# Create backup dir (/mnt/NCBACKUP/)
if [ ! -d "$BACKUP" ]
then
BACKUP=/var/NCBACKUP
mkdir -p $BACKUP
fi
# Backup PostgreSQL
#if is_this_installed postgresql-common
#then
# cd /tmp
# if sudo -u postgres psql -c "SELECT 1 AS result FROM pg_database WHERE datname='$NCCONFIGDB'" | grep "1 row" > /dev/null
# then
# print_text_in_color "$ICyan" "Doing pgdump of $NCCONFIGDB..."
# check_command sudo -u postgres pg_dump "$NCCONFIGDB" > "$BACKUP"/nextclouddb.sql
# else
# print_text_in_color "$ICyan" "Doing pgdump of all databases..."
# check_command sudo -u postgres pg_dumpall > "$BACKUP"/alldatabases.sql
# fi
#fi
# Prevent apps from breaking the update due to incompatibility
# Fixes errors like https://github.com/nextcloud/vm/issues/1834
# Needs to be executed before backing up the config directory
if [ "${CURRENTVERSION%%.*}" -lt "${NCVERSION%%.*}" ]
then
print_text_in_color "$ICyan" "Deleting 'app_install_overwrite array' to prevent app breakage..."
nextcloud_occ config:system:delete app_install_overwrite
fi
# Check if backup exists and move to old
print_text_in_color "$ICyan" "Backing up data..."
DATE=$(date +%Y-%m-%d-%H%M%S)
if [ -d "$BACKUP" ]
then
mkdir -p "$BACKUP"-OLD/"$DATE"
install_if_not rsync
rsync -Aaxz "$BACKUP"/ "$BACKUP"-OLD/"$DATE"
rm -R "$BACKUP"
mkdir -p "$BACKUP"
fi
# Do a backup of the ZFS mount
if is_this_installed zfs-auto-snapshot
then
if grep -rq ncdata /etc/mtab
then
check_multiverse
sed -i "s|date --utc|date|g" /usr/sbin/zfs-auto-snapshot
check_command zfs-auto-snapshot -r ncdata
fi
fi
# Backup data
for folders in config apps
do
if [[ "$(rsync -Aaxz $NCPATH/$folders $BACKUP)" -eq 0 ]]
then
BACKUP_OK=1
else
unset BACKUP_OK
fi
done
if [ -z $BACKUP_OK ]
then
msg_box "Backup was not OK. Please check $BACKUP and see if the folders are backed up properly"
exit 1
else
printf "${IGreen}\nBackup OK!${Color_Off}\n"
fi
# Download and validate Nextcloud package
check_command download_verify_nextcloud_stable
if [ -f "$HTML/$STABLEVERSION.tar.bz2" ]
then
print_text_in_color "$ICyan" "$HTML/$STABLEVERSION.tar.bz2 exists"
else
msg_box "Aborting, something went wrong with the download"
exit 1
fi
if [ -d $BACKUP/config/ ]
then
print_text_in_color "$ICyan" "$BACKUP/config/ exists"
else
msg_box "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
print_text_in_color "$ICyan" "$BACKUP/apps/ exists"
echo
print_text_in_color "$IGreen" "All files are backed up."
send_mail \
"Nextcloud update started!" \
"Please don't shutdown or reboot your server during the update! $(date +%T)"
nextcloud_occ maintenance:mode --on
countdown "Removing old Nextcloud instance in 5 seconds..." "5"
rm -rf $NCPATH
print_text_in_color "$IGreen" "Extracting new package...."
check_command tar -xjf "$HTML/$STABLEVERSION.tar.bz2" -C "$HTML"
rm "$HTML/$STABLEVERSION.tar.bz2"
print_text_in_color "$IGreen" "Restoring config to Nextcloud..."
rsync -Aaxz $BACKUP/config "$NCPATH"/
bash $SECURE & spinner_loading
nextcloud_occ maintenance:mode --off
nextcloud_occ upgrade
# Optimize
print_text_in_color "$ICyan" "Optimizing Nextcloud..."
yes | nextcloud_occ db:convert-filecache-bigint
nextcloud_occ db:add-missing-indices
CURRENTVERSION=$(sudo -u www-data php $NCPATH/occ status | grep "versionstring" | awk '{print $3}')
if [ "${CURRENTVERSION%%.*}" -ge "19" ]
then
check_php
nextcloud_occ db:add-missing-columns
install_if_not php"$PHPVER"-bcmath
fi
if [ "${CURRENTVERSION%%.*}" -ge "20" ]
then
nextcloud_occ db:add-missing-primary-keys
fi
if [ "${CURRENTVERSION%%.*}" -ge "21" ]
then
# Set phone region
if [ -n "$KEYBOARD_LAYOUT" ]
then
nextcloud_occ config:system:set default_phone_region --value="$KEYBOARD_LAYOUT"
fi
fi
else
msg_box "Something went wrong with backing up your old Nextcloud instance
Please check in $BACKUP if the folders exist."
exit 1
fi
# Update Bitwarden
if is_docker_running
then
if docker ps -a --format '{{.Names}}' | grep -Eq "bitwarden";
then
if is_this_installed apache2
then
if [ -d /root/bwdata ]
then
curl_to_dir "https://raw.githubusercontent.com/bitwarden/server/master/scripts" "bitwarden.sh" "/root"
chmod +x /root/bitwarden.sh
if [ -f /root/bitwarden.sh ]
then
print_text_in_color "$IGreen" "Upgrading Bitwarden..."
sleep 2
yes no | bash /root/bitwarden.sh updateself
yes no | bash /root/bitwarden.sh update
fi
elif [ -d "$BITWARDEN_HOME"/bwdata ]
then
curl_to_dir "https://raw.githubusercontent.com/bitwarden/server/master/scripts" "bitwarden.sh" "$BITWARDEN_HOME"
chown "$BITWARDEN_USER":"$BITWARDEN_USER" "$BITWARDEN_HOME"/bitwarden.sh
chmod +x "$BITWARDEN_HOME"/bitwarden.sh
if [ -f "$BITWARDEN_HOME"/bitwarden.sh ]
then
print_text_in_color "$IGreen" "Upgrading Bitwarden..."
sleep 2
yes no | sudo -u "$BITWARDEN_USER" bash "$BITWARDEN_HOME"/bitwarden.sh updateself
yes no | sudo -u "$BITWARDEN_USER" bash "$BITWARDEN_HOME"/bitwarden.sh update
fi
fi
fi
fi
fi
# Start Apache2
start_if_stopped apache2
# Just double check if the DB is started as well
if is_this_installed postgresql-common
then
if ! pgrep postgres >/dev/null 2>&1
then
print_text_in_color "$ICyan" "Starting PostgreSQL..."
systemctl start postgresql.service
fi
fi
# Restore app status
# Fixing https://github.com/nextcloud/server/issues/4538
if [ "${APPSTORAGE[0]}" != "no-export-done" ]
then
print_text_in_color "$ICyan" "Restoring the status of apps. This can take a while..."
for app in "${!APPSTORAGE[@]}"
do
if [ -n "${APPSTORAGE[$app]}" ]
then
# Check if the app is in Nextclouds app storage
if ! [ -d "$NC_APPS_PATH/$app" ]
then
# If the app is missing from the apps folder and was installed and enabled before the upgrade was done,
# then reinstall it
if [ "${APPSTORAGE[$app]}" = "yes" ]
then
install_and_enable_app "$app"
# If the app is missing from the apps folder and was installed but not enabled before the upgrade was done,
# then reinstall it but keep it disabled
elif [ "${APPSTORAGE[$app]}" = "no" ]
then
install_and_enable_app "$app"
nextcloud_occ_no_check app:disable "$app"
fi
fi
# If the app still isn't enabled (maybe because it's incompatible), then at least restore from backup,
# and make sure it's disabled
if ! [ -d "$NC_APPS_PATH/$app" ] && [ -d "$BACKUP/apps/$app" ]
then
if yesno_box_no "$app couln't be enabled. Do you want to restore it from backup?\n\nWARNING: It may result in failed integrity checks."
then
print_text_in_color "$ICyan" "Restoring $app from $BACKUP/apps..."
rsync -Aaxz "$BACKUP/apps/$app" "$NC_APPS_PATH/"
bash "$SECURE"
nextcloud_occ_no_check app:disable "$app"
nextcloud_occ upgrade
fi
fi
# Cover the case where the app is enabled for certain groups
if [ "${APPSTORAGE[$app]}" != "yes" ] && [ "${APPSTORAGE[$app]}" != "no" ] && is_app_enabled "$app"
then
# Only restore the group settings, if the app was enabled (and is thus compatible with the new NC version)
nextcloud_occ_no_check config:app:set "$app" enabled --value="${APPSTORAGE[$app]}"
fi
fi
done
fi
# Remove header for Nextcloud 14 (already in .htaccess)
if [ -f /etc/apache2/sites-available/"$(hostname -f)".conf ]
then
if grep -q 'Header always set Referrer-Policy' /etc/apache2/sites-available/"$(hostname -f)".conf
then
sed -i '/Header always set Referrer-Policy/d' /etc/apache2/sites-available/"$(hostname -f)".conf
restart_webserver
fi
fi
# Change owner of $BACKUP folder to root
chown -R root:root "$BACKUP"
# Pretty URLs
print_text_in_color "$ICyan" "Setting RewriteBase to \"/\" in config.php..."
chown -R www-data:www-data "$NCPATH"
nextcloud_occ config:system:set htaccess.RewriteBase --value="/"
nextcloud_occ maintenance:update:htaccess
bash $SECURE & spinner_loading
# Repair
nextcloud_occ maintenance:repair
# Create $VMLOGS dir
if [ ! -d "$VMLOGS" ]
then
mkdir -p "$VMLOGS"
fi
CURRENTVERSION_after=$(nextcloud_occ status | grep "versionstring" | awk '{print $3}')
if [[ "$NCVERSION" == "$CURRENTVERSION_after" ]] || [ -n "$PRERELEASE_VERSION" ]
then
msg_box "Latest version is: $NCVERSION. Current version is: $CURRENTVERSION_after.
||| UPGRADE SUCCESS! |||
If you notice that some apps are disabled, it's because they are not compatible with the new Nextcloud version.
To recover your old apps, please check $BACKUP/apps and copy them to $NCPATH/apps manually.
Thank you for using T&M Hansson IT's updater!"
nextcloud_occ status
nextcloud_occ maintenance:mode --off
print_text_in_color "$ICyan" "Sending notification about the successful update to all admins..."
notify_admin_gui \
"Nextcloud is now updated!" \
"Your Nextcloud is updated to $CURRENTVERSION_after with the update script in the Nextcloud VM."
echo "NEXTCLOUD UPDATE success-$(date +"%Y%m%d")" >> "$VMLOGS"/update.log
if [ -n "$SNAPSHOT_EXISTS" ]
then
check_command lvrename /dev/ubuntu-vg/NcVM-snapshot-pending /dev/ubuntu-vg/NcVM-snapshot
fi
exit 0
else
msg_box "Latest version is: $NCVERSION. Current version is: $CURRENTVERSION_after.
||| UPGRADE FAILED! |||
No worries, your files are still backed up at $BACKUP!
Please report this issue to $ISSUES
Maintenance mode is kept on."
notify_admin_gui \
"Nextcloud update failed!" \
"Your Nextcloud update failed, please check the logs at $VMLOGS/update.log"
nextcloud_occ status
exit 1
fi