blob: 61d447ac57f52a6f57274f60cadad3bd0e33e8a1 [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/
Alessandro Astone3b6b6b82020-12-29 14:16:20 +010014mountpoint /postinstall/tmp >/dev/null 2>&1 || mount -t tmpfs tmpfs /postinstall/tmp
Dan Pasanen894b3522017-01-16 19:26:50 -060015cp -f /postinstall/system/bin/backuptool_ab.functions /postinstall/tmp/backuptool.functions
16
17# Preserve /system/addon.d in /tmp/addon.d
18preserve_addon_d() {
19 if [ -d /system/addon.d/ ]; then
20 mkdir -p /postinstall/tmp/addon.d/
21 cp -a /system/addon.d/* /postinstall/tmp/addon.d/
Marko Man4e254982018-10-17 16:08:23 +020022 rm -f /postinstall/tmp/addon.d/70-gapps.sh
Dan Pasanen8320eb12018-01-26 10:26:47 -060023
24 # Discard any scripts that aren't at least our version level
25 for f in /postinstall/tmp/addon.d/*sh; do
26 SCRIPT_VERSION=$(grep "^# ADDOND_VERSION=" $f | cut -d= -f2)
27 if [ -z "$SCRIPT_VERSION" ]; then
28 SCRIPT_VERSION=1
29 fi
30 if [ $SCRIPT_VERSION -lt $ADDOND_VERSION ]; then
31 rm $f
32 fi
33 done
Dan Pasanen894b3522017-01-16 19:26:50 -060034 chmod 755 /postinstall/tmp/addon.d/*.sh
35 fi
36}
37
38# Restore /postinstall/system/addon.d from /postinstall/tmp/addon.d
39restore_addon_d() {
40 if [ -d /postinstall/tmp/addon.d/ ]; then
41 mkdir -p /postinstall/system/addon.d/
42 cp -a /postinstall/tmp/addon.d/* /postinstall/system/addon.d/
43 rm -rf /postinstall/tmp/addon.d/
44 fi
45}
46
47# Proceed only if /system is the expected major and minor version
48check_prereq() {
49# If there is no build.prop file the partition is probably empty.
50if [ ! -r /system/build.prop ]; then
z3DD3r31285e52020-03-04 18:07:40 +030051 echo "Backup/restore is not possible. Partition is probably empty"
52 return 1
Dan Pasanen894b3522017-01-16 19:26:50 -060053fi
54
z3DD3r8ca31e62020-03-04 12:42:10 +030055if ! grep -q "^ro.omni.version=$V.*" /product/build.prop; then
z3DD3r31285e52020-03-04 18:07:40 +030056 echo "Backup/restore is not possible. Incompatible ROM version: $V"
57 return 2
z3DD3r8ca31e62020-03-04 12:42:10 +030058fi
z3DD3r31285e52020-03-04 18:07:40 +030059return 0
Dan Pasanen894b3522017-01-16 19:26:50 -060060}
61
Dan Pasanen894b3522017-01-16 19:26:50 -060062# Execute /system/addon.d/*.sh scripts with $1 parameter
63run_stage() {
maxwen1e534e82020-03-29 16:52:37 +020064log -t "update_engine" $1
65
Dan Pasanen894b3522017-01-16 19:26:50 -060066if [ -d /postinstall/tmp/addon.d/ ]; then
67 for script in $(find /postinstall/tmp/addon.d/ -name '*.sh' |sort -n); do
maxwen1e534e82020-03-29 16:52:37 +020068 log -t "update_engine" $script
69
Dan Pasanen894b3522017-01-16 19:26:50 -060070 # we have no /sbin/sh in android, only recovery
71 # use /system/bin/sh here instead
72 sed -i '0,/#!\/sbin\/sh/{s|#!/sbin/sh|#!/system/bin/sh|}' $script
Alessandro Astone3b6b6b82020-12-29 14:16:20 +010073 # we can't count on /tmp existing on an A/B device, so utilize /postinstall/tmp as tmpfs
Dan Pasanen894b3522017-01-16 19:26:50 -060074 sed -i 's|. /tmp/backuptool.functions|. /postinstall/tmp/backuptool.functions|g' $script
75 $script $1
76 done
77fi
78}
79
80case "$1" in
81 backup)
z3DD3r31285e52020-03-04 18:07:40 +030082 if check_prereq; then
83 mkdir -p $C
84 preserve_addon_d
85 run_stage pre-backup
86 run_stage backup
87 run_stage post-backup
Dan Pasanen894b3522017-01-16 19:26:50 -060088 fi
maxwen1e534e82020-03-29 16:52:37 +020089 log -t "update_engine" "backuptool_ab.sh backup"
Dan Pasanen894b3522017-01-16 19:26:50 -060090 ;;
91 restore)
z3DD3r31285e52020-03-04 18:07:40 +030092 if check_prereq; then
93 run_stage pre-restore
94 run_stage restore
95 run_stage post-restore
96 restore_addon_d
97 rm -rf $C
Alessandro Astone3b6b6b82020-12-29 14:16:20 +010098 umount /postinstall/tmp
z3DD3r31285e52020-03-04 18:07:40 +030099 rm -rf /postinstall/tmp
100 sync
maxwen1e534e82020-03-29 16:52:37 +0200101 log -t "update_engine" "backuptool_ab.sh restore"
Dan Pasanen894b3522017-01-16 19:26:50 -0600102 ;;
103 *)
104 echo "Usage: $0 {backup|restore}"
105 exit 1
106esac
107
108exit 0