-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
syno_app_mover.sh
3357 lines (3047 loc) · 129 KB
/
syno_app_mover.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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# shellcheck disable=SC2076,SC2207,SC2238,SC2129
#------------------------------------------------------------------------------
# Easily move Synology packages from one volume to another volume.
# Also can backup and restore packages.
#
# Github: https://github.com/007revad/Synology_app_mover
# Script verified at https://www.shellcheck.net/
#
# To run in a shell (replace /volume1/scripts/ with path to script):
# sudo -s /volume1/scripts/syno_app_mover.sh
#------------------------------------------------------------------------------
# TODO
# Instead of moving large extra folders copy them to the target volume.
# Then rename the source volume's @downloads to @downloads_backup.
#
# Add ability to move all apps
# https://www.reddit.com/r/synology/comments/1eybzc1/comment/ljcj8re/
#
# Maybe add backing up "/volume#/@iSCSI/VDISK_BLUN" (VMM VMs)
# https://www.synology-forum.de/threads/backup-der-vms.135462/post-1194705
# https://www.synology-forum.de/threads/virtual-machine-manager-vms-sichern.91952/post-944113
#
#------------------------------------------------------------------------------
# DONE Add `@database` as an app that can be moved.
# DONE Added logging
# DONE Added USB Copy to show how to move USB Copy database (move mode only)
#------------------------------------------------------------------------------
scriptver="v4.2.83"
script=Synology_app_mover
repo="007revad/Synology_app_mover"
scriptname=syno_app_mover
logpath="$(dirname "$(realpath "$0")")"
logfile="$logpath/${scriptname}_$(date +%Y-%m-%d_%H-%M).log"
# Prevent Entware or user edited PATH causing issues
# shellcheck disable=SC2155 # Declare and assign separately to avoid masking return values
export PATH=$(echo "$PATH" | sed -e 's/\/opt\/bin:\/opt\/sbin://')
ding(){
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}"
printf \\a
}
# Save options used
args=("$@")
if [[ $1 == "--debug" ]] || [[ $1 == "-d" ]]; then
set -x
export PS4='`[[ $? == 0 ]] || echo "\e[1;31;40m($?)\e[m\n "`LINE $LINENO '
fi
if [[ $1 == "--trace" ]] || [[ $1 == "-t" ]]; then
trace="yes"
fi
if [[ ${1,,} == "--fix" ]]; then
# Bypass exit if dependent package failed to stop
# For restoring broken package to original volume
fix="yes"
fi
# Check script is running as root
if [[ $( whoami ) != "root" ]]; then
ding
echo -e "${Error}ERROR${Off} This script must be run as sudo or root!"
exit 1 # Not running as root
fi
# Check script is running on a Synology NAS
if ! /usr/bin/uname -a | grep -i synology >/dev/null; then
echo "This script is NOT running on a Synology NAS!"
echo "Copy the script to a folder on the Synology"
echo "and run it from there."
exit 1 # Not a Synology NAS
fi
# Get NAS model
model=$(cat /proc/sys/kernel/syno_hw_version)
#modelname="$model"
# Check for dodgy characters after model number
if [[ $model =~ 'pv10-j'$ ]]; then # GitHub syno_hdd_db issue #10
model=${model%??????}+ # replace last 6 chars with +
elif [[ $model =~ '-j'$ ]]; then # GitHub syno_hdd_db issue #2
model=${model%??} # remove last 2 chars
fi
# Show script version
#echo -e "$script $scriptver\ngithub.com/$repo\n"
echo "$script $scriptver"
# Get DSM full version
productversion=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION productversion)
buildphase=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION buildphase)
buildnumber=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION buildnumber)
smallfixnumber=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION smallfixnumber)
majorversion=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION majorversion)
#minorversion=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION minorversion)
# Show DSM full version and model
if [[ $buildphase == GM ]]; then buildphase=""; fi
if [[ $smallfixnumber -gt "0" ]]; then smallfix="-$smallfixnumber"; fi
echo -e "$model DSM $productversion-$buildnumber$smallfix $buildphase\n"
usage(){
cat <<EOF
Usage: $(basename "$0") [options]
Options:
-h, --help Show this help message
-v, --version Show the script version
--autoupdate=AGE Auto update script (useful when script is scheduled)
AGE is how many days old a release must be before
auto-updating. AGE must be a number: 0 or greater
--auto=APP Automatically backup APP (for scheduling backups)
APP can be a single app or a comma separated list
APP can also be 'all' to backup all apps (except
any you excluded in the syno_app_mover.conf)
Examples:
--auto=radarr
--auto=Calender,ContainerManager,radarr
--auto=all
APP names need to be the app's system name
View the system names with the --list option
--list Display installed apps' system names
EOF
}
list_names(){
# List app system names
if ! cd /var/packages; then
echo "Failed to cd to /var/packages!"
exit 1
fi
# Print header
echo -e "Use app system name for --auto option or exclude in conf file\n"
printf -- '-%.0s' {1..62}; echo # print 62 -
echo "APP SYSTEM NAME APP DISPLAY NAME"
printf -- '-%.0s' {1..62}; echo # print 62 -
for p in *; do
if [[ -d "$p" ]]; then
if [[ ! -a "$p/target" ]] ; then
echo -e "\e[41mBroken symlink\e[0m $p"
else
if [[ -f "/var/packages/${p}/INFO" ]]; then
long_name="$(/usr/syno/bin/synogetkeyvalue "/var/packages/${p}/INFO" displayname)"
if [[ -z "$long_name" ]]; then
long_name="$(/usr/syno/bin/synogetkeyvalue "/var/packages/${p}/INFO" package)"
fi
else
# Package with no INFO file
long_name="!!! MISSING INFO FILE !!!"
fi
# Pad with spaces to 29 chars
pad=$(printf -- ' %.0s' {1..29})
printf '%.*s' 29 "$p${pad}"
echo "$long_name"
fi
fi
done < <(find . -maxdepth 1 -type d)
echo ""
exit 0
}
scriptversion(){
cat <<EOF
$script $scriptver - by 007revad
See https://github.com/$repo
EOF
exit 0
}
# Save options used for getopt
args=("$@")
autoupdate=""
# Check for flags with getopt
if options="$(getopt -o abcdefghijklmnopqrstuvwxyz0123456789 -l \
auto:,list,help,version,autoupdate:,log,debug -- "${args[@]}")"; then
eval set -- "$options"
while true; do
case "${1,,}" in
-h|--help) # Show usage options
usage
exit
;;
-v|--version) # Show script version
scriptversion
;;
-l|--log) # Log
log=yes
;;
-d|--debug) # Show and log debug info
debug=yes
;;
--list) # List installed app's system names
list_names
;;
--auto) # Specify pkgs for scheduled backup
auto="yes"
color=no # Disable colour text in task scheduler emails
mode="Backup"
action="Backing up"
if [[ ${2,,} == "all" ]]; then
all="yes"
elif [[ $2 ]]; then
IFS=',' read -r -a autos <<< "$2"; unset IFS
if [[ ${#autos[@]} -gt "0" ]]; then
for i in "${autos[@]}"; do
# Trim leading and trailing spaces
j=$(echo -n "$i" | xargs)
# Check pkg name exists
if [[ ! -d "/var/packages/$j" ]]; then
echo -e "Invalid auto argument '$j'\n"
else
if readlink -f "/var/packages/$j/target" | grep -q -E '^/volume'; then
autolist+=("$j")
else
skipped+=("$j")
fi
fi
done
else
ding
echo -e "Missing argument to auto!\n"
usage
exit 2 # Missing argument
fi
else
ding
echo -e "Missing argument to auto!\n"
usage
exit 2 # Missing argument
fi
shift
;;
--autoupdate) # Auto update script
autoupdate=yes
if [[ $2 =~ ^[0-9]+$ ]]; then
delay="$2"
shift
else
delay="0"
fi
;;
--)
shift
break
;;
*) # Show usage options
ding
echo -e "Invalid option '$1'\n"
usage
exit 2 # Invalid argument
;;
esac
shift
done
else
echo
usage
exit
fi
# Abort if autolist is empty
if [[ $auto == "yes" ]] && [[ ! ${#autolist[@]} -gt "0" ]]; then
ding
echo -e "No apps to backup!\n"
exit 2 # autolist empty
fi
# Show apps to auto backup
#if [[ ${#autolist[@]} -gt "0" ]]; then # debug
# echo -e "Backing up ${autolist[*]}\n" # debug
#fi # debug
if [[ $debug == "yes" ]]; then
set -x
export PS4='`[[ $? == 0 ]] || echo "\e[1;31;40m($?)\e[m\n "`:.$LINENO:'
fi
# Shell Colors
if [[ $color != "no" ]]; then
#Black='\e[0;30m' # ${Black}
Red='\e[0;31m' # ${Red}
#Green='\e[0;32m' # ${Green}
Yellow='\e[0;33m' # ${Yellow}
#Blue='\e[0;34m' # ${Blue}
#Purple='\e[0;35m' # ${Purple}
Cyan='\e[0;36m' # ${Cyan}
#White='\e[0;37m' # ${White}
Error='\e[41m' # ${Error}
#Warn='\e[47;31m' # ${Warn}
Off='\e[0m' # ${Off}
fi
#------------------------------------------------------------------------------
# Check latest release with GitHub API
# Get latest release info
# Curl timeout options:
# https://unix.stackexchange.com/questions/94604/does-curl-have-a-timeout
release=$(curl --silent -m 10 --connect-timeout 5 \
"https://api.github.com/repos/$repo/releases/latest")
# Release version
tag=$(echo "$release" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
shorttag="${tag:1}"
# Release published date
published=$(echo "$release" | grep '"published_at":' | sed -E 's/.*"([^"]+)".*/\1/')
published="${published:0:10}"
published=$(date -d "$published" '+%s')
# Today's date
now=$(date '+%s')
# Days since release published
age=$(((now - published)/(60*60*24)))
# Get script location
# https://stackoverflow.com/questions/59895/
source=${BASH_SOURCE[0]}
while [ -L "$source" ]; do # Resolve $source until the file is no longer a symlink
scriptpath=$( cd -P "$( dirname "$source" )" >/dev/null 2>&1 && pwd )
source=$(readlink "$source")
# If $source was a relative symlink, we need to resolve it
# relative to the path where the symlink file was located
[[ $source != /* ]] && source=$scriptpath/$source
done
scriptpath=$( cd -P "$( dirname "$source" )" >/dev/null 2>&1 && pwd )
scriptfile=$( basename -- "$source" )
echo "Running from: ${scriptpath}/$scriptfile"
#echo "Script location: $scriptpath" # debug
#echo "Source: $source" # debug
#echo "Script filename: $scriptfile" # debug
#echo "tag: $tag" # debug
#echo "scriptver: $scriptver" # debug
cleanup_tmp(){
# Delete downloaded .tar.gz file
if [[ -f "/tmp/$script-$shorttag.tar.gz" ]]; then
if ! rm "/tmp/$script-$shorttag.tar.gz"; then
echo -e "${Error}ERROR${Off} Failed to delete"\
"downloaded /tmp/$script-$shorttag.tar.gz!" >&2
fi
fi
# Delete extracted tmp files
if [[ -d "/tmp/$script-$shorttag" ]]; then
if ! rm -r "/tmp/$script-$shorttag"; then
echo -e "${Error}ERROR${Off} Failed to delete"\
"downloaded /tmp/$script-$shorttag!" >&2
fi
fi
}
if ! printf "%s\n%s\n" "$tag" "$scriptver" |
sort --check=quiet --version-sort >/dev/null ; then
echo -e "\n${Cyan}There is a newer version of this script available.${Off}"
echo -e "Current version: ${scriptver}\nLatest version: $tag"
scriptdl="$scriptpath/$script-$shorttag"
if [[ -f ${scriptdl}.tar.gz ]] || [[ -f ${scriptdl}.zip ]]; then
# They have the latest version tar.gz downloaded but are using older version
echo "You have the latest version downloaded but are using an older version"
sleep 10
elif [[ -d $scriptdl ]]; then
# They have the latest version extracted but are using older version
echo "You have the latest version extracted but are using an older version"
sleep 10
else
if [[ $autoupdate == "yes" ]]; then
if [[ $age -gt "$delay" ]] || [[ $age -eq "$delay" ]]; then
echo "Downloading $tag"
reply=y
else
echo "Skipping as $tag is less than $delay days old."
fi
else
echo -e "${Cyan}Do you want to download $tag now?${Off} [y/n]"
read -r -t 30 reply
fi
if [[ ${reply,,} == "y" ]]; then
# Delete previously downloaded .tar.gz file and extracted tmp files
cleanup_tmp
if cd /tmp; then
url="https://github.com/$repo/archive/refs/tags/$tag.tar.gz"
if ! curl -JLO -m 30 --connect-timeout 5 "$url"; then
echo -e "${Error}ERROR${Off} Failed to download"\
"$script-$shorttag.tar.gz!"
else
if [[ -f /tmp/$script-$shorttag.tar.gz ]]; then
# Extract tar file to /tmp/<script-name>
if ! tar -xf "/tmp/$script-$shorttag.tar.gz" -C "/tmp"; then
echo -e "${Error}ERROR${Off} Failed to"\
"extract $script-$shorttag.tar.gz!"
else
# Set script sh files as executable
if ! chmod a+x "/tmp/$script-$shorttag/"*.sh ; then
permerr=1
echo -e "${Error}ERROR${Off} Failed to set executable permissions"
fi
# Copy new script sh file to script location
if ! cp -p "/tmp/$script-$shorttag/${scriptname}.sh" "${scriptpath}/${scriptfile}";
then
copyerr=1
echo -e "${Error}ERROR${Off} Failed to copy"\
"$script-$shorttag sh file(s) to:\n $scriptpath/${scriptfile}"
fi
# Copy script's conf file to script location if missing
if [[ ! -f "$scriptpath/${scriptname}.conf" ]]; then
# Set permission on config file
if ! chmod 664 "/tmp/$script-$shorttag/${scriptname}.conf"; then
permerr=1
echo -e "${Error}ERROR${Off} Failed to set read/write permissions on:"
echo "$scriptpath/${scriptname}.conf"
fi
# Copy existing conf file settings to new conf file
while read -r LINE; do
if [[ ${LINE:0:1} != "#" ]]; then
if [[ $LINE =~ ^[a-z_]+=.* ]]; then
oldfile="${scriptpath}/${scriptname}.conf"
newfile="/tmp/$script-$shorttag/${scriptname}.conf"
key="${LINE%=*}"
oldvalue="$(synogetkeyvalue "$oldfile" "$key")"
newvalue="$(synogetkeyvalue "$newfile" "$key")"
if [[ $oldvalue != "$newvalue" ]]; then
synosetkeyvalue "$newfile" "$key" "$oldvalue"
fi
fi
fi
done < "${scriptpath}/${scriptname}.conf"
# Copy conf file to script location
if ! cp -p "/tmp/$script-$shorttag/${scriptname}.conf"\
"${scriptpath}/${scriptname}.conf"; then
copyerr=1
echo -e "${Error}ERROR${Off} Failed to copy"\
"$script-$shorttag conf file to:\n $scriptpath/${scriptname}.conf"
else
conftxt=", ${scriptname}.conf"
fi
fi
# Copy new CHANGES.txt file to script location (if script on a volume)
if [[ $scriptpath =~ /volume* ]]; then
# Set permissions on CHANGES.txt
if ! chmod 664 "/tmp/$script-$shorttag/CHANGES.txt"; then
permerr=1
echo -e "${Error}ERROR${Off} Failed to set read/write permissions on:"
echo "$scriptpath/CHANGES.txt"
fi
# Copy new CHANGES.txt file to script location
if ! cp -p "/tmp/$script-$shorttag/CHANGES.txt"\
"${scriptpath}/${scriptname}_CHANGES.txt"; then
echo -e "${Error}ERROR${Off} Failed to copy"\
"$script-$shorttag/CHANGES.txt to:\n $scriptpath"
else
changestxt=" and changes.txt"
fi
fi
# Delete downloaded tmp files
cleanup_tmp
# Notify of success (if there were no errors)
if [[ $copyerr != 1 ]] && [[ $permerr != 1 ]]; then
echo -e "\n$tag ${scriptfile}$conftxt$changestxt downloaded to: ${scriptpath}\n"
# Reload script
printf -- '-%.0s' {1..79}; echo # print 79 -
exec "${scriptpath}/$scriptfile" "${args[@]}"
fi
fi
else
echo -e "${Error}ERROR${Off}"\
"/tmp/$script-$shorttag.tar.gz not found!"
#ls /tmp | grep "$script" # debug
fi
fi
cd "$scriptpath" || echo -e "${Error}ERROR${Off} Failed to cd to script location!"
else
echo -e "${Error}ERROR${Off} Failed to cd to /tmp!"
fi
fi
fi
fi
conffile="${scriptpath}/${scriptname}.conf"
# Fix line endings
# grep can't detect Windows or Mac line endings
# but can detect if there's no Linux endings.
if grep -rIl -m 1 $'\r' "$conffile" >/dev/null; then
# Does not contain Linux line endings
sed -i 's/\r\n/\n/g' "$conffile" # Fix Windows line endings
sed -i 's/\r/\n/g' "$conffile" # Fix Mac line endings
fi
# Add log header
echo "$script $scriptver" > "$logfile"
echo -e "$model DSM $productversion-$buildnumber$smallfix $buildphase\n" >> "$logfile"
echo "Running from: ${scriptpath}/$scriptfile" >> "$logfile"
#------------------------------------------------------------------------------
# Functions
# shellcheck disable=SC2317,SC2329 # Don't warn about unreachable commands in this function
pause(){
# When debugging insert pause command where needed
read -s -r -n 1 -p "Press any key to continue..."
read -r -t 0.1 -s -e -- # Silently consume all input
stty echo echok # Ensure read didn't disable echoing user input
echo -e "\n" |& tee -a "$logfile"
}
# shellcheck disable=SC2317,SC2329 # Don't warn about unreachable commands in this function
debug(){
if [[ $1 == "on" ]]; then
set -x
export PS4='`[[ $? == 0 ]] || echo "\e[1;31;40m($?)\e[m\n "`LINE $LINENO '
elif [[ $1 == "off" ]]; then
set +x
fi
}
progbar(){
# $1 is pid of process
# $2 is string to echo
string="$2"
local dots
local progress
dots=""
while [[ -d /proc/$1 ]]; do
dots="${dots}."
progress="$dots"
if [[ ${#dots} -gt "10" ]]; then
dots=""
progress=" "
fi
echo -ne " ${2}$progress\r"; /usr/bin/sleep 0.3
done
}
progstatus(){
# $1 is return status of process
# $2 is string to echo
# $3 line number function was called from
local tracestring
local pad
tracestring="${FUNCNAME[0]} called from ${FUNCNAME[1]} $3"
pad=$(printf -- ' %.0s' {1..80})
[ "$trace" == "yes" ] && printf '%.*s' 80 "${tracestring}${pad}" && echo ""
if [[ $1 == "0" ]]; then
echo -e "$2 "
else
ding
echo -e "Line ${LINENO}: ${Error}ERROR${Off} $2 failed!" |& tee -a "$logfile"
echo "$tracestring ($scriptver)" |& tee -a "$logfile"
if [[ $exitonerror != "no" ]]; then
exit 1 # Skip exit if exitonerror != no
fi
fi
exitonerror=""
#echo "return: $1" # debug
}
package_status(){
# $1 is package name
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
# local code
/usr/syno/bin/synopkg status "${1}" >/dev/null
code="$?"
# DSM 7.2 0 = started, 17 = stopped, 255 = not_installed, 150 = broken
# DSM 6 to 7.1 0 = started, 3 = stopped, 4 = not_installed, 150 = broken
if [[ $code == "0" ]]; then
#echo "$1 is started" # debug
return 0
elif [[ $code == "17" ]] || [[ $code == "3" ]]; then
#echo "$1 is stopped" # debug
return 1
elif [[ $code == "255" ]] || [[ $code == "4" ]]; then
#echo "$1 is not installed" # debug
return 255
elif [[ $code == "150" ]]; then
#echo "$1 is broken" # debug
return 150
else
return "$code"
fi
}
package_is_running(){
# $1 is package name
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
/usr/syno/bin/synopkg is_onoff "${1}" >/dev/null
code="$?"
return "$code"
}
wait_status(){
# Wait for package to finish stopping or starting
# $1 is package
# $2 is start or stop
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
local num
if [[ $2 == "start" ]]; then
state="0"
elif [[ $2 == "stop" ]]; then
state="1"
fi
if [[ $state == "0" ]] || [[ $state == "1" ]]; then
num="0"
package_status "$1"
while [[ $? != "$state" ]]; do
sleep 1
num=$((num +1))
if [[ $num -gt "20" ]]; then
break
fi
package_status "$1"
done
fi
}
package_stop(){
# $1 is package name
# $2 is package display name
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
if [[ ${#pkgs_sorted[@]} -gt "1" ]]; then
/usr/syno/bin/synopkg stop "$1" >/dev/null &
else
# Only timeout if there are other packages to process
#timeout 5.0m /usr/syno/bin/synopkg stop "$1" >/dev/null &
# Docker can take 12 minutes to stop 70 containers
timeout 30.0m /usr/syno/bin/synopkg stop "$1" >/dev/null &
fi
pid=$!
string="Stopping ${Cyan}${2}${Off}"
echo "Stopping $2" >> "$logfile"
progbar "$pid" "$string"
wait "$pid"
progstatus "$?" "$string" "line ${LINENO}"
# Allow package processes to finish stopping
#wait_status "$1" stop
wait_status "$1" stop &
pid=$!
string="Waiting for ${Cyan}${2}${Off} to stop"
echo "Waiting for $2 to stop" >> "$logfile"
progbar "$pid" "$string"
wait "$pid"
progstatus "$?" "$string" "line ${LINENO}"
}
package_start(){
# $1 is package name
# $2 is package display name
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
if [[ ${#pkgs_sorted[@]} -gt "1" ]]; then
/usr/syno/bin/synopkg start "$1" >/dev/null &
else
# Only timeout if there are other packages to process
#timeout 5.0m /usr/syno/bin/synopkg start "$1" >/dev/null &
# Docker can take 15 minutes to start 70 containers
timeout 30.0m /usr/syno/bin/synopkg start "$1" >/dev/null &
fi
pid=$!
string="Starting ${Cyan}${2}${Off}"
echo "Starting $2" >> "$logfile"
progbar "$pid" "$string"
wait "$pid"
progstatus "$?" "$string" "line ${LINENO}"
# Allow package processes to finish starting
#wait_status "$1" start
wait_status "$1" start &
pid=$!
string="Waiting for ${Cyan}${2}${Off} to start"
echo "Waiting for $2 to start" >> "$logfile"
progbar "$pid" "$string"
wait "$pid"
progstatus "$?" "$string" "line ${LINENO}"
}
# shellcheck disable=SC2317 # Don't warn about unreachable commands in this function
package_uninstall(){
# $1 is package name
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
/usr/syno/bin/synopkg uninstall "$1" >/dev/null &
pid=$!
string="Uninstalling ${Cyan}${1}${Off}"
echo "Ininstalling $1" >> "$logfile"
progbar "$pid" "$string"
wait "$pid"
progstatus "$?" "$string" "line ${LINENO}"
}
# shellcheck disable=SC2317 # Don't warn about unreachable commands in this function
package_install(){
# $1 is package name
# $2 is /volume2 etc
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
/usr/syno/bin/synopkg install_from_server "$1" "$2" >/dev/null &
pid=$!
string="Installing ${Cyan}${1}${Off} on ${Cyan}$2${Off}"
echo "Installing $1 on $2" >> "$logfile"
progbar "$pid" "$string"
wait "$pid"
progstatus "$?" "$string" "line ${LINENO}"
}
is_empty(){
# $1 is /path/folder
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
if [[ -d $1 ]]; then
local contents
contents=$(find "$1" -maxdepth 1 -printf '.')
if [[ ${#contents} -gt 1 ]]; then
return 1 # Not empty
fi
fi
}
backup_dir(){
# $1 is folder to backup (@docker etc)
# $2 is volume (/volume1 etc)
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
local perms
if [[ -d "$2/$1" ]]; then
# Make backup folder on $2
if [[ ! -d "${2}/${1}_backup" ]]; then
# Set same permissions as original folder
perms=$(stat -c %a "${2:?}/${1:?}")
if ! mkdir -m "$perms" "${2:?}/${1:?}_backup"; then
ding
echo -e "Line ${LINENO}: ${Error}ERROR${Off} Failed to create directory!"
echo -e "Line ${LINENO}: ERROR Failed to create directory!" >> "$logfile"
process_error="yes"
if [[ $all != "yes" ]] || [[ $fix != "yes" ]]; then
exit 1 # Skip exit if mode != all and fix != yes
fi
return 1
fi
fi
# Backup $1
if ! is_empty "${2:?}/${1:?}_backup"; then
# @docker_backup folder exists and is not empty
echo -e "There is already a backup of $1" |& tee -a "$logfile"
echo -e "Do you want to overwrite it? [y/n]" |& tee -a "$logfile"
read -r answer
echo "$answer" >> "$logfile"
echo "" |& tee -a "$logfile"
if [[ ${answer,,} != "y" ]]; then
return
fi
fi
cp -prf "${2:?}/${1:?}/." "${2:?}/${1:?}_backup" |& tee -a "$logfile" &
pid=$!
# If string is too long progbar repeats string for each dot
string="Backing up $1 to ${Cyan}${1}_backup${Off}"
echo "Backing up $1 to ${1}_backup" >> "$logfile"
progbar "$pid" "$string"
wait "$pid"
progstatus "$?" "$string" "line ${LINENO}"
fi
}
cdir(){
# $1 is path to cd to
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
if ! cd "$1"; then
ding
echo -e "Line ${LINENO}: ${Error}ERROR${Off} cd to $1 failed!"
echo -e "Line ${LINENO}: ERROR cd to $1 failed!" >> "$logfile"
process_error="yes"
if [[ $all != "yes" ]] || [[ $fix != "yes" ]]; then
exit 1 # Skip exit if mode != all and fix != yes
fi
return 1
fi
}
create_dir(){
# $1 is source /path/folder
# $2 is target /path/folder
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
# Create target folder with source folder's permissions
if [[ ! -d "$2" ]]; then
# Set same permissions as original folder
perms=$(stat -c %a "${1:?}")
if ! mkdir -m "$perms" "${2:?}"; then
ding
echo -e "Line ${LINENO}: ${Error}ERROR${Off} Failed to create directory!"
echo -e "Line ${LINENO}: ERROR Failed to create directory!" >> "$logfile"
process_error="yes"
if [[ $all != "yes" ]] || [[ $fix != "yes" ]]; then
exit 1 # Skip exit if mode != all and fix != yes
fi
return 1
fi
fi
}
move_pkg_do(){
# $1 is package name
# $2 is destination volume or path
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
# Move package's @app directories
if [[ ${mode,,} == "move" ]]; then
#mv -f "${source:?}" "${2:?}/${appdir:?}" |& tee -a "$logfile" &
#pid=$!
#string="${action} $source to ${Cyan}$2${Off}"
#echo "${action} $source to $2" >> "$logfile"
#progbar "$pid" "$string"
#wait "$pid"
#progstatus "$?" "$string"
if [[ ! -d "${2:?}/${appdir:?}/${1:?}" ]] ||\
is_empty "${2:?}/${appdir:?}/${1:?}"; then
# Move source folder to target folder
if [[ -w "/$sourcevol" ]]; then
mv -f "${source:?}" "${2:?}/${appdir:?}" |& tee -a "$logfile" &
else
# Source volume is read only
cp -prf "${source:?}" "${2:?}/${appdir:?}" |& tee -a "$logfile" &
fi
pid=$!
string="${action} $source to ${Cyan}$2${Off}"
echo "${action} $source to $2" >> "$logfile"
progbar "$pid" "$string"
wait "$pid"
progstatus "$?" "$string" "line ${LINENO}"
else
# Copy source contents if target folder exists
cp -prf "${source:?}" "${2:?}/${appdir:?}" |& tee -a "$logfile" &
pid=$!
string="Copying $source to ${Cyan}$2${Off}"
echo "Copying $source to $2" >> "$logfile"
progbar "$pid" "$string"
wait "$pid"
progstatus "$?" "$string" "line ${LINENO}"
#rm -rf "${source:?}" |& tee -a "$logfile" &
rm -r --preserve-root "${source:?}" |& tee -a "$logfile" &
pid=$!
exitonerror="no"
string="Removing $source"
echo "$string" >> "$logfile"
progbar "$pid" "$string"
wait "$pid"
progstatus "$?" "$string" "line ${LINENO}"
fi
else
# if ! is_empty "${destination:?}/${appdir:?}/${1:?}"; then
# echo "Skipping ${action,,} ${appdir}/$1 as target is not empty:" |& tee -a "$logfile"
# echo " ${destination}/${appdir}/$1" |& tee -a "$logfile"
# else
#mv -f "${source:?}" "${2:?}/${appdir:?}" |& tee -a "$logfile" &
#pid=$!
#string="${action} $source to ${Cyan}$2${Off}"
#echo "${action} $source to $2" >> "$logfile"
#progbar "$pid" "$string"
#wait "$pid"
#progstatus "$?" "$string"
exitonerror="no" && move_dir "$appdir"
# fi
fi
}
edit_symlinks(){
# $1 is package name
# $2 is destination volume
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
# Edit /var/packages symlinks
case "$appdir" in
@appconf) # etc --> @appconf
rm "/var/packages/${1:?}/etc" |& tee -a "$logfile"
ln -s "${2:?}/@appconf/${1:?}" "/var/packages/${1:?}/etc" |& tee -a "$logfile"
# /usr/syno/etc/packages/$1
# /volume1/@appconf/$1
if [[ -L "/usr/syno/etc/packages/${1:?}" ]]; then
rm "/usr/syno/etc/packages/${1:?}" |& tee -a "$logfile"
ln -s "${2:?}/@appconf/${1:?}" "/usr/syno/etc/packages/${1:?}" |& tee -a "$logfile"
fi
;;
@apphome) # home --> @apphome
rm "/var/packages/${1:?}/home" |& tee -a "$logfile"
ln -s "${2:?}/@apphome/${1:?}" "/var/packages/${1:?}/home" |& tee -a "$logfile"
;;
@appshare) # share --> @appshare
rm "/var/packages/${1:?}/share" |& tee -a "$logfile"
ln -s "${2:?}/@appshare/${1:?}" "/var/packages/${1:?}/share" |& tee -a "$logfile"
;;
@appstore) # target --> @appstore
rm "/var/packages/${1:?}/target" |& tee -a "$logfile"
ln -s "${2:?}/@appstore/${1:?}" "/var/packages/${1:?}/target" |& tee -a "$logfile"
# DSM 6 - Some packages have var symlink
if [[ $majorversion -lt 7 ]]; then
if [[ -L "/var/packages/${1:?}/var" ]]; then
rm "/var/packages/${1:?}/var" |& tee -a "$logfile"
ln -s "${2:?}/@appstore/${1:?}/var" "/var/packages/${1:?}/var" |& tee -a "$logfile"
fi
fi
;;
@apptemp) # tmp --> @apptemp
rm "/var/packages/${1:?}/tmp" |& tee -a "$logfile"
ln -s "${2:?}/@apptemp/${1:?}" "/var/packages/${1:?}/tmp" |& tee -a "$logfile"
;;
@appdata) # var --> @appdata
rm "/var/packages/${1:?}/var" |& tee -a "$logfile"
ln -s "${2:?}/@appdata/${1:?}" "/var/packages/${1:?}/var" |& tee -a "$logfile"
;;
*)
echo -e "${Red}Oops!${Off} appdir: ${appdir}\n"
echo -e "Oops! appdir: ${appdir}\n" >> "$logfile"
return
;;
esac
}
move_pkg(){
# $1 is package name
# $2 is destination volume
[ "$trace" == "yes" ] && echo "${FUNCNAME[0]} called from ${FUNCNAME[1]}" |& tee -a "$logfile"
local appdir
local perms
local destination
local appdirs_tmp
local app_paths_tmp
if [[ ${mode,,} == "backup" ]]; then
destination="$bkpath"
elif [[ ${mode,,} == "restore" ]]; then
destination="$2"
else
destination="$2"
fi
if [[ $majorversion -gt 6 ]]; then
applist=( "@appconf" "@appdata" "@apphome" "@appshare" "@appstore" "@apptemp" )
else
applist=( "@appstore" )
fi
if [[ ${mode,,} == "restore" ]]; then
if ! cdir "$bkpath"; then
process_error="yes"
return 1
fi
sourcevol=$(echo "$bkpath" | cut -d "/" -f2) # var is used later in script
# shellcheck disable=SC1083
while IFS= read -r appdir; do
if [[ "${applist[*]}" =~ "$appdir" ]]; then
appdirs_tmp+=("$appdir")
fi
done < <(find . -name "@app*" -exec basename \{} \;)
# Sort array
IFS=$'\n' appdirs=($(sort <<<"${appdirs_tmp[*]}")); unset IFS
if [[ ${#appdirs[@]} -gt 0 ]]; then
for appdir in "${appdirs[@]}"; do
create_dir "/${sourcevol:?}/${appdir:?}" "${destination:?}/${appdir:?}"
move_pkg_do "$1" "$destination"
done
fi