-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
migrator.sh
1249 lines (1015 loc) · 35.8 KB
/
migrator.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
#!/system/bin/sh
# Migrator
# A Backup Solution and Data Migration Utility for Android
# Copyright 2018-present, VR25
# License: GPLv3+
echo
set -u
umask 0077
ssaid=false
log=/dev/migrator.log
tmp=/dev/migrator.tmp
bkp_dir=/data/migrator/local
data_dir=/sdcard/Documents/vr25/migrator
packages=/data/system/packages.
version="v2021.7.16-beta (202107160)"
ssaid_xml_tmp=/dev/.settings_ssaid.xml.tmp
settings=/data/system/users/0/settings_
ssaid_xml=${settings}ssaid.xml
ssaid_boot_script=${bkp_dir%/*}/enable-ssaid-apps.sh
sysdata="/data/system_?e/0/accounts_?e.db*
/data/system/sync/accounts.xml
/data/misc/bluedroid/bt_config.*
/data/misc/apexdata/com.android.wifi/WifiConfigStoreSoftAp.xml
/data/misc/apexdata/com.android.wifi/WifiConfigStore.xml
/data/misc/wifi/WifiConfigStore.xml
/data/misc/wifi/softap.conf
/data/system/xlua/xlua.db*
/data/system/users/0/photo.png
/data/system/users/0/appwidgets.xml
/data/system/users/0/roles.xml
/data/system/users/0/wallpaper*
/data/user_de/0/com.android.*provider*/databases/*.db*
/data/data/com.android.*provider*/databases/*.db*
/data/system/deviceidle.xml"
# excluded system data
#/data/misc/adb/adb*_keys*
get_apk_dir() {
grep " name=\"$1\" codePath=" ${packages}xml \
| awk '{print $3}' \
| sed -e 's/codePath=\"//' -e s/\"//
}
mv_bkps() {
t -d /data/media/0/.migrator || {
mv ${bkp_dir%/*} /data/media/0/.migrator 2>/dev/null \
|| mv ${bkp_dir%/*} /data/media/.migrator
}
}
parse_params() {
regex=..
compressor=-
dir=$data_dir/exported
while t -n "${1-}"; do
case "$1" in
-d)
dir="$2"
shift 2
;;
-c)
compressor="$2"
shift 2
;;
-v)
regex="$1 \"$(echo "$2" | sed 's/\,/\|/g')\""
shift 2
;;
*)
regex="$(echo "$1" | sed 's/\,/\|/g')"
shift
;;
esac
done
}
# test
t() { test "$@"; }
# extended test
tt() {
eval "case \"$1\" in
$2) return 0;;
esac"
return 1
}
# verbose
tt "${1-}" "-B|-L" || {
touch $log
t $(du -m $log | cut -f1) -ge 2 && date > $log || date >> $log
echo "$version" >> $log
set -x >> $log 2>&1
}
# prepare busybox and extra executables
bin_dir=/data/adb/bin
busybox_dir=/dev/.vr25/busybox
magisk_busybox=/data/adb/magisk/busybox
[ -x $busybox_dir/ls ] || {
mkdir -p $busybox_dir
chmod 0700 $busybox_dir
if [ -f $bin_dir/busybox ]; then
[ -x $bin_dir/busybox ] || chmod -R 0700 $bin_dir
$bin_dir/busybox --install -s $busybox_dir
elif [ -f $magisk_busybox ]; then
[ -x $magisk_busybox ] || chmod 0700 $magisk_busybox
$magisk_busybox --install -s $busybox_dir
elif which busybox > /dev/null; then
eval "$(which busybox) --install -s $busybox_dir"
else
echo "(!) Install busybox or simply place it in $bin_dir/"
exit 3
fi
}
export PATH="$bin_dir:$busybox_dir:/data/adb/modules_update/migrator/bin:/data/adb/modules/migrator/bin:$PATH"
unset bin_dir busybox_dir magisk_busybox
# "rsync -t --inplace --no-W" wrapper
which rsync > /dev/null || \
rsync() {
shift 3
tt "$2" "*/" && mkdir -p "$2"
if tt "$1" "*/"; then
cp -Rduf "$1"* "$1".* "$2" 2>/dev/null
else
cp -Rduf "$1" "$2"
fi
}
param1=${1-}
t -n "$param1" && shift
# set up recovery environment
recovery_mode=false
pgrep -f zygote > /dev/null || {
set +x
clear
recovery_mode=true
trap 'exit_code=$?; set +x; rm ${tmp}* 2>/dev/null; echo; exit $exit_code' EXIT
tt "$param1" "-[Brs]*" && {
echo "This option is not meant for recovery environments"
exit 1
}
}
$recovery_mode || {
# temporarily disable SELinux
sestatus=$(getenforce || exit)
setenforce 0 || exit
exxit() {
local exit_code=$?
t .$sestatus = .Enforcing && setenforce 1
set +x
$ssaid && {
printf "Reboot $(t -f /data/adb/modules/migrator/service.sh \
|| echo "& run \"${0##*/} -s\" ")"
printf "to enable apps with Settings.Secure.ANDROID_ID (SSAID)\n"
}
rm /data/.__hltest /dev/._split ${tmp}* 2>/dev/null
echo
exit $exit_code
}
trap exxit EXIT
}
tt "$param1" "-[br]|-[br]*[bMn]*" && both=true || both=false
if t -n "${p-}"; then
p_="$p"
encryption=true
which ccrypt > /dev/null || {
echo "ccrypt not found"
exit 1
}
else
encryption=false
fi
: > $tmp
# move the backup directory back to where it belongs
! tt "$param1" "-m" && ! t -d /data/migrator && {
mv /data/media/0/.migrator /data/migrator \
|| mv /data/media/.migrator /data/migrator
} 2>/dev/null
case "$param1" in
-b*) # backup
echo "Backing up"
extras="$(echo "$*" | grep -o '\+.*' | sed 's/\+ //')"
set -- $(echo "$@" | sed 's/\+.*//')
tt "$*" "*-v*" && v=v || v=
regex="$(echo "$*" | sed -E 's/ |-v//g' | sed 's/\,/\|/g')"
no_list=false
if tt "$regex" "/*"; then
set -e
grep -Ev '^#|^$' $regex | dos2unix > $tmp
set +e
elif tt "$regex" "--"; then
set -e
grep -Ev '^#|^$' $data_dir/packages.list | dos2unix > $tmp
set +e
else
no_list=true
if $recovery_mode; then
grep 'name=.*codePath="/data/app/' ${packages}xml \
| grep -v com.offsec.nhterm \
| grep -E$v "${regex:-..}" \
| awk '{print $2}' | tr -d \" \
| sed 's/^name=//' > $tmp
else
pm list packages -3 \
| sed 's/^package://' \
| grep -v com.offsec.nhterm \
| grep -E$v "${regex:-..}" > $tmp
fi
fi
$no_list || {
# parse regex and generate full package names
(set +x
cut -d ' ' -f 1 ${packages}list > ${tmp}1
: > ${tmp}2
while IFS= read -r l; do
grep -E "$l" ${tmp}1 >> ${tmp}2
done < $tmp
mv -f ${tmp}2 $tmp)
}
unset regex
mkdir -p $bkp_dir
t -f /data/system/users/0/runtime-permissions.xml \
&& bkp_runtime_perms=true \
|| bkp_runtime_perms=false
t -n "$extras" && {
if tt "$extras" "*/*"; then
grep -Ev '^#|^$' $extras | dos2unix >> $tmp
echo >> $tmp
else
echo "$extras" | sed 's/ /\n/g' >> $tmp
fi
}
sort -u $tmp | sed -e 's/ //g' -e '/^$/d' > ${tmp}1 \
&& mv -f ${tmp}1 $tmp
t -n "$extras" && {
# filter out unistalled packages
(set +x
while IFS= read -r l; do
grep -q "^$l " ${packages}list || sed -i "/^$l$/d" $tmp
done < $tmp)
}
tt "$param1" "-b*n*" && {
# exclude already backed up
while IFS= read -r l; do
tt "$l" "*[a-z]*" || continue
t -d $bkp_dir/$l && sed -i "/^$l$/d" $tmp
done < $tmp
}
checked=false
while IFS= read -r pkg; do
tt "$pkg" "*[a-z]*" || continue
$checked || {
checked=true
{ $both || tt "$param1" "-b*[ade]*"; } || break
}
mkdir -p $bkp_dir/$pkg
# backup app
app=false
if $both || tt "$param1" "-b*[ae]*"; then
if t -f $(get_apk_dir $pkg)/base.apk; then
app=true
printf " $pkg\n App\n"
rsync -t --inplace --no-W $(get_apk_dir $pkg)/*.apk $bkp_dir/$pkg/ 2>/dev/null
fi
for f in $bkp_dir/$pkg/*.apk; do
t -f $(get_apk_dir $pkg)/${f##*/} || rm $f
done
# legacy
# APK hard links do not play nice with SELinux
# rm $bkp_dir/$pkg/*.apk
# ln $(get_apk_dir $pkg)/*.apk $bkp_dir/$pkg/ && {
# app=true
# printf " $pkg\n App\n"
# }
# fi 2>/dev/null
fi
# backup data
if $both || tt "$param1" "-b*[de]*"; then
$app && echo " Data" || printf " $pkg\n Data\n"
rm -rf $bkp_dir/$pkg/$pkg $bkp_dir/$pkg/${pkg}_de $bkp_dir/$pkg/${pkg}_media 2>/dev/null
#mkdir $bkp_dir/$pkg/$pkg $bkp_dir/$pkg/${pkg}_de $bkp_dir/$pkg/${pkg}_media # rsync [wrapper] will handle this
: > $bkp_dir/$pkg/modes.txt
for e in /data/data/${pkg}::$pkg /data/user_de/0/${pkg}::${pkg}_de /data/media/0/Android/data/${pkg}::${pkg}_media; do
ls -1d ${e%::*}/* ${e%::*}/.* 2>/dev/null \
| grep -Eiv '/\.$|/\.\.$|/app_optimized|/app_tmp|cache|/dex$|/lib$|oat$|thumbnail' | \
while IFS= read -r i; do
t -z "$i" && continue
#cp -dlR "$i" $bkp_dir/$pkg/${e#*::}/
rsync -rtl --inplace --no-W "$i" $bkp_dir/$pkg/${e#*::}/
find "$i" -print0 2>/dev/null | xargs -0 -n 10 stat -c "%a %n" \
>> $bkp_dir/$pkg/modes.txt
done
done
# backup SSAID
grep -q $pkg $ssaid_xml 2>/dev/null \
&& grep $pkg $ssaid_xml > $bkp_dir/$pkg/ssaid.txt
# ensure restored $pkg can use Google Clould Messaging
rm -f $bkp_dir/$pkg/${pkg}*/shared_prefs/com.google.android.gms.appid.xml 2>/dev/null
fi
# backup runtime permissions
$bkp_runtime_perms && {
sed -n "/$pkg/,/\<\//p" /data/system/users/0/runtime-permissions.xml \
| grep 'granted=\"true\"' \
| grep -o 'android.permission.*[A-Z]' > $bkp_dir/$pkg/runtime-perms.txt
}
killall -CONT $pkg > /dev/null 2>&1
done < $tmp
# backup migrator itself
cp $(readlink -f "$0") $bkp_dir/migrator.sh
# backup Android settings
if tt "$param1" "-b*[es]*"; then
echo " Generic Android settings"
rm -rf $bkp_dir/_settings 2>/dev/null
mkdir $bkp_dir/_settings
if $recovery_mode; then
for i in global secure system; do
awk '{print $3,$4}' ${settings}$i.xml | tr -d \" \
| sed -e s/name=// -e 's/ value//' -e '/^$/d' -e '/ standalone=/d' -e '/^ /d' > $bkp_dir/_settings/$i.txt
done
cp -l /data/user_de/0/org.cyanogenmod.cmsettings/databases/cmsettings.db \
/data/user_de/0/org.lineageos.lineagesettings/databases/lineagesettings.db \
$bkp_dir/_settings/ 2>/dev/null
else
for i in global secure system; do
settings list $i > $bkp_dir/_settings/$i.txt
done
# CM/Lineage-specific settings
if t -f /data/user_de/0/org.cyanogenmod.cmsettings/databases/cmsettings.db \
|| t -f /data/user_de/0/org.lineageos.lineagesettings/databases/lineagesettings.db
then
t -f /data/user_de/0/org.lineageos.lineagesettings/databases/lineagesettings.db \
&& flag="-lineage" \
|| flag="-cm"
rm -rf $bkp_dir/_settings-* 2>/dev/null
mkdir $bkp_dir/_settings$flag
for i in global secure system; do
settings -$flag list $i > $bkp_dir/_settings$flag/$i.txt 2>/dev/null || {
rm -rf $bkp_dir/_settings$flag
break
}
done
fi
fi
# exclude input method settings to prevent "no keyboard" issue
sed -i /inputmethod/d $bkp_dir/_settings/secure.txt
fi
# backup system data
if tt "$param1" "-b*[De]*"; then
echo " System data"
rm -rf $bkp_dir/_sysdata 2>/dev/null
mkdir $bkp_dir/_sysdata
echo "$sysdata" > $tmp
while IFS= read -r l; do
for i in $l; do
cp $i $bkp_dir/_sysdata/ 2>/dev/null || continue
echo "[ -f $i ] && cat $bkp_dir/_sysdata/${i##*/} > $i" >> $bkp_dir/_sysdata/restore.sh
done
done < $tmp
fi
# backup magisk data
if tt "$param1" "-b*[em]*"; then
echo " Magisk data"
rm -rf $bkp_dir/_magisk; 2>/dev/null
mkdir $bkp_dir/_magisk
ls -1d /data/adb/* /data/adb/.* 2>/dev/null | grep -Ev '/\.$|/\.\.$|/magisk$' | \
while IFS= read -r i; do
cp -dlR "$i" $bkp_dir/_magisk/
find "$i" -print0 2>/dev/null | xargs -0 -n 10 stat -c "chown %U:%G %n; chmod %a %n" \
>> $bkp_dir/_magisk/restore-attributes.sh
find "$i" -print0 2>/dev/null | xargs -0 -n 10 ls -1dZ 2>/dev/null | sed 's/^/chcon /' \
>> $bkp_dir/_magisk/restore-attributes.sh
done
fi
# remove backups of uninstalled packages
set +x
ls -1 $bkp_dir 2>/dev/null \
| grep -Ev '^_magisk$|^migrator.sh$|^_settings|^_sysdata$' | \
while IFS= read -r pkg; do
grep -q "^$pkg " ${packages}list || rm -rf $bkp_dir/$pkg
done
# make hard link backups immune to factory resets
tt "$param1" "*M*" && mv_bkps
;;
# enable apps with Settings.Secure.ANDROID_ID (SSAID) and start automatic backups (if enabled)
-B|-s)
t $param1 = -B && ssaid_only=false || ssaid_only=true
until t -d /sdcard/Documents \
&& t .$(getprop sys.boot_completed 2>/dev/null) = .1 \
&& pm list packages -s > /dev/null 2>&1
do
sleep 15
done
t -f $ssaid_boot_script && {
. $ssaid_boot_script
rm $ssaid_boot_script
}
$ssaid_only && exit 0
cmd="${0##*/} -be && ${0##*/} -e" # Commands to run
freq=24 # Every 24 hours
delay=60 # Starting 60 minutes after boot
config=/data/migrator.conf
t -f $config && {
dos2unix $config
. $config
sleep $(( $delay * 60 ))
while :; do
eval "$cmd"
sleep $(( $freq * 60 * 60 ))
dos2unix $config && . $config || exit
set +x
done
}
;;
-d) # delete
tt "$*" "*/*" || {
l="$(cd $bkp_dir && { ls -1dp $* | sed 's/^/ /' | sort -u | grep -v '\./'; })"
t -n "$l" && printf "Removing\n$l\n" || echo "No matches"
cd $bkp_dir && eval rm -rf "$@"
} 2>/dev/null
;;
-e*) # export
cd $bkp_dir || exit
case $param1 in
-*e*i*) # interactive mode
clear
echo
ls -1 | grep -v '^migrator.sh$'
echo
echo '[regex (default: "..") | -v regex] [-d <base directory, default (full): $data_dir/exported>] [-c <"compression method" | "-" (none, default)>]'
echo
printf ": "
read params
eval parse_params $params
;;
*)
parse_params "$@"
;;
esac
case "$compressor" in
bzip2*) extension=.tar.bz2;;
gzip*|pigz*) extension=.tar.gz;;
lzop*) extension=.tar.lzo;;
xz*) extension=.tar.xz;;
zip*) extension=.tar.zip;;
zstd*) extension=.tar.zst;;
*) extension=.tar.${compressor%% *};;
esac
t $dir = $data_dir/exported || dir=$dir/migrator_exported
mkdir -p $dir
echo "Exporting"
# workaround for Termux backup size issue
rm -rf com.termux/com.termux/files/home/storage/ 2>/dev/null
ls -1 | grep -v '^migrator.sh$' | grep -E "$regex" | \
while IFS= read -r bkp; do
echo " $bkp"
rm $dir/${bkp}.tar* 2>/dev/null
if t "$compressor" != -; then
if $encryption; then
tar -c $bkp | $compressor | ccrypt -E p > $dir/${bkp}${extension}.cpt
export p="$p_"
else
tar -c $bkp | $compressor > $dir/${bkp}$extension
fi
else
if $encryption; then
tar -c $bkp | ccrypt -E p > $dir/${bkp}.tar.cpt
export p="$p_"
else
tar -cf $dir/${bkp}.tar $bkp
fi
fi
done
cp $(readlink -f "$0") $dir/migrator.sh
# remove backups of uninstalled packages
set +x
ls -1 $dir 2>/dev/null \
| grep -Ev '^_magisk\.|^migrator.sh$|^_settings.*\.|^_sysdata\.' | \
while IFS= read -r bkp; do
t -d $bkp_dir/${bkp%.tar*} || rm $dir/$bkp
done
;;
-i*) # import
mkdir -p $bkp_dir
parse_params "$@"
cd $dir || exit
tt "$param1" "-*i*i*" && { # interactive mode
clear
echo
ls -1 | grep -v '^migrator.sh$'
echo
echo '[regex (default: "..") | -v regex] [-c <"compression method" | "-" (none, default)>]'
echo
printf ": "
read params
eval parse_params $params
}
decrypt() {
if $encryption; then
ccrypt -dcE p $bkp | $@
export p="$p_"
else
$@ $bkp
fi
}
echo "Importing"
ls -1 | grep -v '^migrator.sh$' | grep -E "$regex" | \
while IFS= read -r bkp; do
tt "$bkp" "*[a-z]*" || continue
echo " ${bkp%.tar*}"
rm -f $bkp_dir /${bkp%.tar*} 2>/dev/null
case ${bkp##*.tar.} in
bz2*|bzip2*) decrypt bzip2 -cd | tar -xf - -C $bkp_dir ;;
gz*|pigz*) decrypt gzip -cd | tar -xf - -C $bkp_dir ;;
lzo*) decrypt lzop -cd $bkp | tar -xf - -C $bkp_dir ;;
xz*|lzma*) decrypt xz -cd | tar -xf - -C $bkp_dir ;;
zip*) decrypt unzip -p | tar -xf - -C $bkp_dir ;;
zst*) decrypt zstd -cd | tar -xf - -C $bkp_dir ;;
*.tar|cpt)
if $encryption; then
ccrypt -dcE p $bkp | tar -xf - -C $bkp_dir
export p="$p_"
else
tar -xf $bkp -C $bkp_dir
fi
;;
*) decrypt $compressor | tar -xf - -C $bkp_dir;;
esac
done
cp -f migrator.sh $bkp_dir/
;;
-l) # list
regex="$(echo "$*" | sed 's/\,/\|/g')"
list_bkps() {
echo $1/
ls -1 $1 2>/dev/null | sed -En -e '/^migrator.sh/d' -e "/${regex:-..}/p" | \
while IFS= read -r i; do
t -n "$i" || continue
printf " $i"
tt "$i" "_*" && echo || {
grep -q "$i " ${packages}list && echo " (installed)" || echo
}
if t -f $1/$i/base.apk; then
if ls -d $1/$i/$i/* > /dev/null 2>&1; then
echo " App and data"
else
echo " App only"
fi
else
echo " Data only"
fi
done
}
list_bkps $bkp_dir
;;
-L) # export (L)ogs
mkdir -p $data_dir
bzip2 -9 < $log > $data_dir/${log##*/}.bz2 \
&& echo "$data_dir/${log##*/}.bz2"
;;
-M) # make hard link backups immune to factory resets
mv_bkps
;;
-n) # fix push notifications
rm /data/data/*/shared_prefs/com.google.android.gms.appid.xml 2>/dev/null
;;
-r*) # restore
echo "Restoring"
tt "$*" "*-v*" && v=v || v=
regex="$(echo "$*" | sed -E 's/ |-v//g' | sed 's/\,/\|/g')"
ls -d $bkp_dir/* > /dev/null && cd $bkp_dir || exit
params="$@"
if $both || tt "$param1" "-r*[ade]*"; then
# set the stage for SSAIDs restore
if grep -q '^com.google.android.gms ' ${packages}list \
&& t -f $ssaid_xml && ls */ssaid.txt > /dev/null 2>&1
then
no_ssaid=false
id=$(grep -Eo 'id=.*name=' $ssaid_xml | grep -Eo '[0-9]+' | sort -n | tail -n 1)
grep -v '</settings>' $ssaid_xml > $ssaid_xml_tmp
else
no_ssaid=true
fi
# enable "unknown sources" and disable package verification
if $both || tt "$param1" "-r*[ae]*"; then
settings put secure install_non_market_apps 1
settings put global verifier_verify_adb_installs 0
settings put global package_verifier_enable 0
fi
ls -1 $bkp_dir 2>/dev/null \
| grep -Ev '^_magisk$|^migrator.sh$|^_settings|^_sysdata$' \
| grep -E$v "${regex:-..}" > $tmp
tt "$param1" "-r*n*" && {
# exclude installed apps
while IFS= read -r i; do
tt "$i" "*[a-z]*" || continue
grep -q "$i " ${packages}list && sed -i "/^$i$/d" $tmp
done < $tmp
}
touch /data/.__hltest
while IFS= read -r pkg; do
tt "$pkg" "*[a-z]*" || continue
# restore app
app=false
if t -f $pkg/base.apk && \
{ $both || tt "$param1" "-r*[ae]*"; }
then
app=true
printf " $pkg\n App\n"
# base APK
pm install -r -i com.android.vending $pkg/base.apk > /dev/null && {
rm $pkg/base.apk
ln $(get_apk_dir $pkg)/base.apk $pkg/
# split APKs
ls $pkg/split_*.apk > /dev/null 2>&1 && {
for pkg_ in $pkg/split_*.apk; do
pm install -r -i com.android.vending -p $pkg $pkg_ > /dev/null && {
rm $pkg_
ln $(get_apk_dir $pkg)/${pkg_##*/} $pkg/
}
done
}
}
fi
{ t -d /data/data/$pkg && \
{ ls -d $pkg/$pkg/* > /dev/null 2>&1 \
|| ls -d $pkg/${pkg}_de/* > /dev/null 2>&1; }
} || continue
if $both || tt "$param1" "-r*[de]*"; then
$app && echo " Data" || printf " $pkg\n Data\n"
killall $pkg > /dev/null 2>&1
# restore Settings.Secure.ANDROID_ID (SSAID)
! $no_ssaid && t -f $ssaid_xml && t -f $pkg/ssaid.txt && {
ssaid=true
killall $pkg > /dev/null 2>&1
pm suspend $pkg > /dev/null 2>&1 || pm disable $pkg > /dev/null
grep -q " $pkg " $ssaid_boot_script 2>/dev/null \
|| echo "pm unsuspend $pkg 2>/dev/null || pm enable $pkg" >> $ssaid_boot_script
set -- $(cat $pkg/ssaid.txt)
id=$(( id + 1 ))
f2="id=\"$id\""
f3="name=\"$(stat -c %u /data/data/$pkg)\""
sed -i "/\"$pkg\"/d" $ssaid_xml_tmp
echo "${@}" | sed 's/^/ /' | sed -e "s/$2/$f2/" -e "s/$3/$f3/" >> $ssaid_xml_tmp
}
# restore data
ls -d $pkg/${pkg}_de/* > /dev/null 2>&1 \
&& de=/data/user_de/0/${pkg}::${pkg}_de || de=
ls -d $pkg/${pkg}_media/* > /dev/null 2>&1 \
&& media=/data/media/0/Android/data/${pkg}::${pkg}_media || media=
for i in /data/data/${pkg}::$pkg $de $media; do
lib_dir=$(readlink ${i%::*}/lib)
if ! tt "$i" "*media"; then
set -- $(stat -c "%u:%g %a" ${i%::*})
fi
rm -rf ${i%::*} 2>/dev/null
if ln /data/.__hltest ${i%::*} 2>/dev/null; then
rm ${i%::*}
cp -dlR $pkg/${i#*::} ${i%::*}
else
cp -dR $pkg/${i#*::} ${i%::*} && {
rm -rf $pkg/${i#*::}
cp -dlR ${i%::*} $pkg/${i#*::}
}
fi
t -n "$lib_dir" && ln -sf $lib_dir ${i%::*}/lib
# restore attributes
if tt "$i" "*media"; then
user=$(stat -c "%U" /data/data/$pkg)
chown -R media_rw:${user}_ext ${i%::*}
else
chown -R $1 ${i%::*}
chmod $2 ${i%::*}
fi
done
rm -rf /dev/._split 2>/dev/null
mkdir /dev/._split
sed 's/^/chmod /' $pkg/modes.txt | split -l 1000 - /dev/._split/
ls -1 /dev/._split | while IFS= read -r f; do
t -f /dev/._split/$f && . /dev/._split/$f 2>/dev/null
done
/system/bin/restorecon -R /data/data/$pkg > /dev/null 2>&1
/system/bin/restorecon -R /data/user*/0/$pkg > /dev/null 2>&1
fi
# restore runtime permissions
for perm in $(cat $pkg/runtime-perms.txt 2>/dev/null); do
pm grant $pkg $perm 2>/dev/null
done
done < $tmp
fi
# commit changes to $ssaid_xml
$ssaid && {
echo "</settings>" >> $ssaid_xml_tmp
cat $ssaid_xml_tmp > $ssaid_xml
}
# restore Android settings
if t -d $bkp_dir/_settings \
&& { tt "$param1" "-r*[es]*"; }
then
echo " Generic Android Settings"
for path in $bkp_dir/_settings*; do
t -d $path || break
flag=
if t $path = $bkp_dir/_settings-cm \
&& t -f /data/user_de/0/org.cyanogenmod.cmsettings/databases/cmsettings.db
then
flag="--cm"
elif t $path = $bkp_dir/_settings-lineage \
&& t -f /data/user_de/0/org.cyanogenmod.cmsettings/databases/cmsettings.db
then
flag="--lineage"
fi
for namespace in global secure system; do
t -f $path/${namespace}.txt || continue
settings $flag list $namespace > $tmp 2>/dev/null || continue
grep -q '\$' $path/${namespace}.txt \
&& sed -i 's/\$/\\$/g' $path/${namespace}.txt
while IFS= read -r setting; do
tt "$setting" "*[a-z]*" || continue
grep -q "^${setting%%=*}" $tmp && {
echo " ${setting%%=*}=${setting#*=}"
settings $flag put ${namespace} "${setting%%=*}" "${setting#*=}"
}
done < $path/${namespace}.txt
done
done
fi
# restore system data
if t -f $bkp_dir/_sysdata/restore.sh && tt "$param1" "-r*D*"; then
echo " System Data"
grep -q '^eu.faircode.xlua ' ${packages}list \
&& mkdir /data/system/xlua 2>/dev/null && {
chown 1000:1000 /data/system/xlua
chmod 0770 /data/system/xlua
}
#. $bkp_dir/_sysdata/restore.sh > /dev/null 2>&1
for i in $(cat $bkp_dir/_sysdata/restore.sh | awk '{ print $3 "::" $7 }'); do
#echo $i
target_file=${i%::*}
bkp_file=${i#*::}
#echo $bkp_file
if [ -f $target_file ]; then
echo " Installing: $target_file"
cat $bkp_file > $target_file
else
if tt "$target_file" "/data/system/users*"; then
echo " Installing (plus fixing ownership & permissions): $target_file"
cp $bkp_file $target_file
chown system:system $target_file
chmod 0600 $target_file
elif tt "$target_file" "/data/*/apexdata/com.android.wifi/WifiConfigStoreSoftAp.xml"; then
echo " Installing (plus fixing ownership & permissions): $target_file"
cp $bkp_file $target_file
chown system:system $target_file
chmod 0600 $target_file
else
echo " WARNING: File does not exist, do not know how to handle ownership & permissions, skipping install: $target_file"
fi
fi
#ls -al $(dirname $target_file)
done
fi
# restore magisk data
if t -d $bkp_dir/_magisk \
&& { tt "$param1" "-r*[em]*"; }
then
echo " Magisk Data"
ls -1d $bkp_dir/_magisk/* $bkp_dir/_magisk/.* | grep -Ev '/\.$|/\.\.$|/restore-attributes.sh$' | \
while IFS= read -r i; do
rm -rf "/data/adb/${i##*/}"
cp -dlR "$i" /data/adb/
done 2>/dev/null
rm -rf /dev/._split 2>/dev/null
mkdir /dev/._split
split -l 1000 $bkp_dir/_magisk/restore-attributes.sh /dev/._split/
for f in /dev/._split/*; do
. $f 2>/dev/null
done
fi 2>/dev/null
;;
wip) ### wizard
exit 0
select_() {
local item=""
local list=""
local n=""
local _var_="$1"
shift
[ $# -gt 9 ] || n="-n 1"
for item in "$@"; do
list="$(printf "$list\n$item")"
done
list="$(echo "$list" | grep -v '^$' | nl -s ") " -w 1)"
printf "$list\n\n${PS3:-#? }"
read $n item
list="$(echo "$list" | sed -n "s|^${item}. ||p")"
list="$_var_=\"$list\""
eval "$list"
}
options
backup
User apps
all
specify comma-separated regex
Packages from a list
default
specify
new
delete
all
specify
export
all
defaults
dir
compress
encrypt
specify
import
default
dir
log
restore
all
specify
not intalled
help
;;
*) # help text
TMPDIR=/dev
cd $TMPDIR
cat <<EOF | more
Migrator $version
A Backup Solution and Data Migration Utility for Android
Copyright 2018-present, VR25
License: GPLv3+
ZERO warranties, use at your own risk!
This is still in beta. Backup your data before using.
USAGE
${0##*/} (wizard)
${0##*/} [option...] [arg...]
[p=<"password for encryption/decryption">] ${0##*/} [option...] [arg...]
M is a migrator alias.
OPTIONS[flags]
Backup
-b[abdDemMns] [regex|-v regex] [[+ file or full pkg names] | [/path/to/list] | [-- for $data_dir/packages.list]]
Delete local backups
-d <"bkp name (wildcards supported)" ...>
Export backups
-e[i] [regex|-v regex] [-d <destination directory>] [-c <"compression method" or "-" (none, default)>]
Import backups
-i [regex|-v regex] [-d <source directory>] [-c <"decompression method" or "-" (none)>]