blob: 98e1c64b8e558c6227f9bb18611d47f65992734a [file] [log] [blame]
Andrew Dodd0c209012013-09-28 15:34:25 -04001#!/sbin/sh
2#
3# Backup and restore addon /system files
4#
5
6export C=/tmp/backupdir
LuK1337ae517382020-01-20 16:09:50 +00007export S=$2
micky3871a865e22020-10-01 03:31:52 +02008export V=11
Andrew Dodd0c209012013-09-28 15:34:25 -04009
Dan Pasanen8320eb12018-01-26 10:26:47 -060010export ADDOND_VERSION=1
11
Marko Man2d218732017-09-12 02:24:39 +020012DEBUG=0
13
Andrew Dodd0c209012013-09-28 15:34:25 -040014# Preserve /system/addon.d in /tmp/addon.d
15preserve_addon_d() {
maxwen1c2e98d2017-09-15 17:13:48 +020016 rm -rf /tmp/addon.d/
Andrew Dodd0c209012013-09-28 15:34:25 -040017 mkdir -p /tmp/addon.d/
LuK1337ae517382020-01-20 16:09:50 +000018 cp -a $S/addon.d/* /tmp/addon.d/
Dan Pasanen8320eb12018-01-26 10:26:47 -060019 # Discard any scripts that aren't at least our version level
20 for f in /postinstall/tmp/addon.d/*sh; do
21 SCRIPT_VERSION=$(grep "^# ADDOND_VERSION=" $f | cut -d= -f2)
22 if [ -z "$SCRIPT_VERSION" ]; then
23 SCRIPT_VERSION=1
24 fi
25 if [ $SCRIPT_VERSION -lt $ADDOND_VERSION ]; then
26 rm $f
27 fi
28 done
Andrew Dodd0c209012013-09-28 15:34:25 -040029 chmod 755 /tmp/addon.d/*.sh
30}
31
32# Restore /system/addon.d in /tmp/addon.d
33restore_addon_d() {
LuK1337ae517382020-01-20 16:09:50 +000034 cp -a /tmp/addon.d/* $S/addon.d/
Andrew Dodd0c209012013-09-28 15:34:25 -040035 rm -rf /tmp/addon.d/
36}
37
maxwen1c2e98d2017-09-15 17:13:48 +020038# Restore only if backup has the expected major and minor version
Andrew Dodd0c209012013-09-28 15:34:25 -040039check_prereq() {
maxwen1c2e98d2017-09-15 17:13:48 +020040 if [ ! -f /tmp/build.prop ]; then
41 # this will block any backups made before 8 cause file was not copied before
42 echo "Not restoring files from incompatible version: $V"
43 exit 127
44 fi
45 if ( ! grep -q "^ro.build.version.release=$V.*" /tmp/build.prop ); then
46 echo "Not restoring files from incompatible version: $V"
47 exit 127
48 fi
Andrew Dodd0c209012013-09-28 15:34:25 -040049}
50
51check_blacklist() {
LuK1337ae517382020-01-20 16:09:50 +000052 if [ -f $S/addon.d/blacklist ];then
maxwen1c2e98d2017-09-15 17:13:48 +020053 ## Discard any known bad backup scripts
LuK1337ae517382020-01-20 16:09:50 +000054 cd $1/addon.d/
maxwen1c2e98d2017-09-15 17:13:48 +020055 for f in *sh; do
56 s=$(md5sum $f | awk {'print $1'})
LuK1337ae517382020-01-20 16:09:50 +000057 grep -q $s $S/addon.d/blacklist && rm -f $f
maxwen1c2e98d2017-09-15 17:13:48 +020058 done
Andrew Dodd0c209012013-09-28 15:34:25 -040059 fi
60}
61
62# Execute /system/addon.d/*.sh scripts with $1 parameter
63run_stage() {
maxwen1c2e98d2017-09-15 17:13:48 +020064 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
65 if [ $DEBUG -eq 1 ]; then
66 echo run_stage $script $1
67 fi
68 $script $1
69 done
Andrew Dodd0c209012013-09-28 15:34:25 -040070}
71
72case "$1" in
73 backup)
maxwen1c2e98d2017-09-15 17:13:48 +020074 # make sure we dont start with any leftovers
75 rm -rf $C
LuK1337ae517382020-01-20 16:09:50 +000076 cp $S/bin/backuptool.functions /tmp
77 cp $S/build.prop /tmp
Andrew Dodd0c209012013-09-28 15:34:25 -040078 mkdir -p $C
maxwen1c2e98d2017-09-15 17:13:48 +020079 #check_prereq
LuK1337ae517382020-01-20 16:09:50 +000080 check_blacklist $S
Andrew Dodd0c209012013-09-28 15:34:25 -040081 preserve_addon_d
82 run_stage pre-backup
83 run_stage backup
84 run_stage post-backup
85 ;;
86 restore)
LuK1337ae517382020-01-20 16:09:50 +000087 cp $S/bin/backuptool.functions /tmp
Andrew Dodd0c209012013-09-28 15:34:25 -040088 check_prereq
LuK1337ae517382020-01-20 16:09:50 +000089 check_blacklist /tmp
Andrew Dodd0c209012013-09-28 15:34:25 -040090 run_stage pre-restore
91 run_stage restore
92 run_stage post-restore
93 restore_addon_d
94 rm -rf $C
95 sync
96 ;;
97 *)
98 echo "Usage: $0 {backup|restore}"
99 exit 1
100esac
101
102exit 0