blob: bfc8f835e94ea0845cfac3316f7c34bde2939f9f [file] [log] [blame]
Meninblack00776bd9aa2016-04-03 07:14:30 -04001#!/sbin/sh
2#
3# Backup and restore addon /system files
4#
5
6export C=/tmp/backupdir
7export S=/system
Dan Pasanen5a8e34c2018-07-15 13:02:00 +02008export V=11.4-Stable
9
10export ADDOND_VERSION=1
Meninblack00776bd9aa2016-04-03 07:14:30 -040011
12# Scripts in /system/addon.d expect to find backuptool.functions in /tmp
13cp -f /tmp/install/bin/backuptool.functions /tmp
14
15# Preserve /system/addon.d in /tmp/addon.d
16preserve_addon_d() {
17 if [ -d /system/addon.d/ ]; then
18 mkdir -p /tmp/addon.d/
19 cp -a /system/addon.d/* /tmp/addon.d/
Dan Pasanen5a8e34c2018-07-15 13:02:00 +020020
21 # Discard any scripts that aren't at least our version level
22 for f in /postinstall/tmp/addon.d/*sh; do
23 SCRIPT_VERSION=$(grep "^# ADDOND_VERSION=" $f | cut -d= -f2)
24 if [ -z "$SCRIPT_VERSION" ]; then
25 SCRIPT_VERSION=1
26 fi
27 if [ $SCRIPT_VERSION -lt $ADDOND_VERSION ]; then
28 rm $f
29 fi
30 done
31
Meninblack00776bd9aa2016-04-03 07:14:30 -040032 chmod 755 /tmp/addon.d/*.sh
33 fi
34}
35
martinusbe10679692018-02-15 10:05:58 +010036# Proceed only if /system is the expected Bliss version
Meninblack00776bd9aa2016-04-03 07:14:30 -040037check_prereq() {
38# If there is no build.prop file the partition is probably empty.
39if [ ! -r /system/build.prop ]; then
40 return 0
41fi
Dan Pasanen5a8e34c2018-07-15 13:02:00 +020042if ( ! grep -q "^ro.bliss.version=$V.*" /system/build.prop ); then
Meninblack00776bd9aa2016-04-03 07:14:30 -040043 echo "Not backing up files from incompatible version: $V"
44 return 0
45fi
46return 1
47}
48
martinusbe10679692018-02-15 10:05:58 +010049# Restore /system/addon.d from /tmp/addon.d
50restore_addon_d() {
51 if [ -d /tmp/addon.d/ ]; then
52 mkdir -p /system/addon.d/
53 cp -a /tmp/addon.d/* /system/addon.d/
54 rm -rf /tmp/addon.d/
55 fi
56}
57
Meninblack00776bd9aa2016-04-03 07:14:30 -040058check_blacklist() {
martinusbe10679692018-02-15 10:05:58 +010059 if [ -f /system/addon.d/blacklist -a -d /$1/addon.d/ ]; then
Meninblack00776bd9aa2016-04-03 07:14:30 -040060 ## Discard any known bad backup scripts
61 cd /$1/addon.d/
62 for f in *sh; do
martinusbe10679692018-02-15 10:05:58 +010063 [ -f $f ] || continue
Meninblack00776bd9aa2016-04-03 07:14:30 -040064 s=$(md5sum $f | cut -c-32)
65 grep -q $s /system/addon.d/blacklist && rm -f $f
66 done
67 fi
68}
69
70check_whitelist() {
71 found=0
72 if [ -f /system/addon.d/whitelist ];then
73 ## forcefully keep any version-independent stuff
74 cd /$1/addon.d/
75 for f in *sh; do
76 s=$(md5sum $f | cut -c-32)
77 grep -q $s /system/addon.d/whitelist
78 if [ $? -eq 0 ]; then
79 found=1
80 else
81 rm -f $f
82 fi
83 done
84 fi
85 return $found
86}
87
88# Execute /system/addon.d/*.sh scripts with $1 parameter
89run_stage() {
90if [ -d /tmp/addon.d/ ]; then
91 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
92 $script $1
93 done
94fi
95}
96
97case "$1" in
98 backup)
99 mkdir -p $C
100 if check_prereq; then
101 if check_whitelist system; then
102 exit 127
103 fi
104 fi
105 check_blacklist system
106 preserve_addon_d
107 run_stage pre-backup
108 run_stage backup
109 run_stage post-backup
110 ;;
111 restore)
112 if check_prereq; then
113 if check_whitelist tmp; then
114 exit 127
115 fi
116 fi
117 check_blacklist tmp
118 run_stage pre-restore
119 run_stage restore
120 run_stage post-restore
121 restore_addon_d
122 rm -rf $C
123 sync
124 ;;
125 *)
126 echo "Usage: $0 {backup|restore}"
127 exit 1
128esac
129
130exit 0