blob: 8b26032e018b3ff707b609c82b5c774b4b031b3b [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
7export S=/system
Marko Man97ff5862018-08-09 14:29:04 +02008export V=9.0
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/
18 cp -a /system/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() {
34 cp -a /tmp/addon.d/* /system/addon.d/
35 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() {
52 if [ -f /system/addon.d/blacklist ];then
maxwen1c2e98d2017-09-15 17:13:48 +020053 ## Discard any known bad backup scripts
54 cd /$1/addon.d/
55 for f in *sh; do
56 s=$(md5sum $f | awk {'print $1'})
57 grep -q $s /system/addon.d/blacklist && rm -f $f
58 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
Marko Man2d218732017-09-12 02:24:39 +020076 cp /system/bin/backuptool.functions /tmp
maxwen1c2e98d2017-09-15 17:13:48 +020077 cp /system/build.prop /tmp
Andrew Dodd0c209012013-09-28 15:34:25 -040078 mkdir -p $C
maxwen1c2e98d2017-09-15 17:13:48 +020079 #check_prereq
Andrew Dodd0c209012013-09-28 15:34:25 -040080 check_blacklist system
81 preserve_addon_d
82 run_stage pre-backup
83 run_stage backup
84 run_stage post-backup
85 ;;
86 restore)
Marko Man2d218732017-09-12 02:24:39 +020087 cp /system/bin/backuptool.functions /tmp
Andrew Dodd0c209012013-09-28 15:34:25 -040088 check_prereq
89 check_blacklist tmp
90 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