blob: d542700234fdd929e109acb2b3709f4dae0a24cb [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
maxwen5b158882022-08-21 00:02:47 +02008export V=13
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
z3DD3r8ca31e62020-03-04 12:42:10 +030053if ! grep -q "^ro.omni.version=$V.*" /product/build.prop; then
54 echo "Not backing up files from incompatible version: $V"
55 return 0
56fi
57return 1
Dan Pasanen894b3522017-01-16 19:26:50 -060058}
59
Dan Pasanen894b3522017-01-16 19:26:50 -060060# Execute /system/addon.d/*.sh scripts with $1 parameter
61run_stage() {
maxwen1e534e82020-03-29 16:52:37 +020062log -t "update_engine" $1
63
Dan Pasanen894b3522017-01-16 19:26:50 -060064if [ -d /postinstall/tmp/addon.d/ ]; then
65 for script in $(find /postinstall/tmp/addon.d/ -name '*.sh' |sort -n); do
maxwen1e534e82020-03-29 16:52:37 +020066 log -t "update_engine" $script
67
Dan Pasanen894b3522017-01-16 19:26:50 -060068 # we have no /sbin/sh in android, only recovery
69 # use /system/bin/sh here instead
70 sed -i '0,/#!\/sbin\/sh/{s|#!/sbin/sh|#!/system/bin/sh|}' $script
71 # we can't count on /tmp existing on an A/B device, so utilize /postinstall/tmp
72 # as a pseudo-/tmp dir
73 sed -i 's|. /tmp/backuptool.functions|. /postinstall/tmp/backuptool.functions|g' $script
74 $script $1
75 done
76fi
77}
78
79case "$1" in
80 backup)
81 mkdir -p $C
z3DD3r8ca31e62020-03-04 12:42:10 +030082 if ! check_prereq; then
83 exit 127
Dan Pasanen894b3522017-01-16 19:26:50 -060084 fi
maxwen1e534e82020-03-29 16:52:37 +020085 log -t "update_engine" "backuptool_ab.sh backup"
86
Dan Pasanen894b3522017-01-16 19:26:50 -060087 preserve_addon_d
88 run_stage pre-backup
89 run_stage backup
90 run_stage post-backup
91 ;;
92 restore)
z3DD3r8ca31e62020-03-04 12:42:10 +030093 if ! check_prereq; then
94 exit 127
Dan Pasanen894b3522017-01-16 19:26:50 -060095 fi
maxwen1e534e82020-03-29 16:52:37 +020096 log -t "update_engine" "backuptool_ab.sh restore"
97
Dan Pasanen894b3522017-01-16 19:26:50 -060098 run_stage pre-restore
99 run_stage restore
100 run_stage post-restore
101 restore_addon_d
102 rm -rf $C
103 rm -rf /postinstall/tmp
104 sync
105 ;;
106 *)
107 echo "Usage: $0 {backup|restore}"
108 exit 1
109esac
110
111exit 0