blob: 56924f999a6e2746219ba7411bbec77c0ffa9250 [file] [log] [blame]
Dan Pasanen894b3522017-01-16 19:26:50 -06001#!/system/bin/sh
2#
3# Backup and restore addon /system files
4#
5
6export S=/system
7export C=/postinstall/tmp/backupdir
micky387f250ac02021-10-17 14:18:16 +02008export V=12
Dan Pasanen894b3522017-01-16 19:26:50 -06009
Dan Pasanen8320eb12018-01-26 10:26:47 -060010export ADDOND_VERSION=2
11
Dan Pasanen894b3522017-01-16 19:26:50 -060012# Scripts in /system/addon.d expect to find backuptool.functions in /tmp
13mkdir -p /postinstall/tmp/
14cp -f /postinstall/system/bin/backuptool_ab.functions /postinstall/tmp/backuptool.functions
15
16# Preserve /system/addon.d in /tmp/addon.d
17preserve_addon_d() {
18 if [ -d /system/addon.d/ ]; then
19 mkdir -p /postinstall/tmp/addon.d/
20 cp -a /system/addon.d/* /postinstall/tmp/addon.d/
Marko Man4e254982018-10-17 16:08:23 +020021 rm -f /postinstall/tmp/addon.d/70-gapps.sh
Dan Pasanen8320eb12018-01-26 10:26:47 -060022
23 # Discard any scripts that aren't at least our version level
24 for f in /postinstall/tmp/addon.d/*sh; do
25 SCRIPT_VERSION=$(grep "^# ADDOND_VERSION=" $f | cut -d= -f2)
26 if [ -z "$SCRIPT_VERSION" ]; then
27 SCRIPT_VERSION=1
28 fi
29 if [ $SCRIPT_VERSION -lt $ADDOND_VERSION ]; then
30 rm $f
31 fi
32 done
Dan Pasanen894b3522017-01-16 19:26:50 -060033 chmod 755 /postinstall/tmp/addon.d/*.sh
34 fi
35}
36
37# Restore /postinstall/system/addon.d from /postinstall/tmp/addon.d
38restore_addon_d() {
39 if [ -d /postinstall/tmp/addon.d/ ]; then
40 mkdir -p /postinstall/system/addon.d/
41 cp -a /postinstall/tmp/addon.d/* /postinstall/system/addon.d/
42 rm -rf /postinstall/tmp/addon.d/
43 fi
44}
45
46# Proceed only if /system is the expected major and minor version
47check_prereq() {
48# If there is no build.prop file the partition is probably empty.
49if [ ! -r /system/build.prop ]; then
50 return 0
51fi
52
micky3876886cd02019-09-25 07:33:45 +020053grep -q "^ro.omni.version=$V.*" /product/build.prop && return 1
Dan Pasanen894b3522017-01-16 19:26:50 -060054
55echo "Not backing up files from incompatible version: $V"
56return 0
57}
58
59check_blacklist() {
60 if [ -f /system/addon.d/blacklist -a -d /$1/addon.d/ ]; then
61 ## Discard any known bad backup scripts
62 cd /$1/addon.d/
63 for f in *sh; do
64 [ -f $f ] || continue
65 s=$(md5sum $f | cut -c-32)
66 grep -q $s /system/addon.d/blacklist && rm -f $f
67 done
68 fi
69}
70
71check_whitelist() {
72 found=0
73 if [ -f /system/addon.d/whitelist ];then
74 ## forcefully keep any version-independent stuff
75 cd /$1/addon.d/
76 for f in *sh; do
77 s=$(md5sum $f | cut -c-32)
78 grep -q $s /system/addon.d/whitelist
79 if [ $? -eq 0 ]; then
80 found=1
81 else
82 rm -f $f
83 fi
84 done
85 fi
86 return $found
87}
88
89# Execute /system/addon.d/*.sh scripts with $1 parameter
90run_stage() {
maxwen1e534e82020-03-29 16:52:37 +020091log -t "update_engine" $1
92
Dan Pasanen894b3522017-01-16 19:26:50 -060093if [ -d /postinstall/tmp/addon.d/ ]; then
94 for script in $(find /postinstall/tmp/addon.d/ -name '*.sh' |sort -n); do
maxwen1e534e82020-03-29 16:52:37 +020095 log -t "update_engine" $script
96
Dan Pasanen894b3522017-01-16 19:26:50 -060097 # we have no /sbin/sh in android, only recovery
98 # use /system/bin/sh here instead
99 sed -i '0,/#!\/sbin\/sh/{s|#!/sbin/sh|#!/system/bin/sh|}' $script
100 # we can't count on /tmp existing on an A/B device, so utilize /postinstall/tmp
101 # as a pseudo-/tmp dir
102 sed -i 's|. /tmp/backuptool.functions|. /postinstall/tmp/backuptool.functions|g' $script
103 $script $1
104 done
105fi
106}
107
108case "$1" in
109 backup)
110 mkdir -p $C
111 if check_prereq; then
112 if check_whitelist postinstall/system; then
113 exit 127
114 fi
115 fi
maxwen1e534e82020-03-29 16:52:37 +0200116 log -t "update_engine" "backuptool_ab.sh backup"
117
Dan Pasanen894b3522017-01-16 19:26:50 -0600118 check_blacklist postinstall/system
119 preserve_addon_d
120 run_stage pre-backup
121 run_stage backup
122 run_stage post-backup
123 ;;
124 restore)
125 if check_prereq; then
126 if check_whitelist postinstall/tmp; then
127 exit 127
128 fi
129 fi
maxwen1e534e82020-03-29 16:52:37 +0200130 log -t "update_engine" "backuptool_ab.sh restore"
131
Dan Pasanen894b3522017-01-16 19:26:50 -0600132 check_blacklist postinstall/tmp
133 run_stage pre-restore
134 run_stage restore
135 run_stage post-restore
136 restore_addon_d
137 rm -rf $C
138 rm -rf /postinstall/tmp
139 sync
140 ;;
141 *)
142 echo "Usage: $0 {backup|restore}"
143 exit 1
144esac
145
146exit 0