blob: ec5d64ad87406d35b6864a17e863b4755882bf50 [file] [log] [blame]
Alberto970ceb8342015-01-02 14:43:15 +01001#!/sbin/sh
2#
3# Backup and restore addon /system files
4#
5
6export C=/tmp/backupdir
7export S=/system
8export V=5
9
10# Preserve /system/addon.d in /tmp/addon.d
11preserve_addon_d() {
12 mkdir -p /tmp/addon.d/
13 cp -a /system/addon.d/* /tmp/addon.d/
14 chmod 755 /tmp/addon.d/*.sh
15}
16
17# Restore /system/addon.d in /tmp/addon.d
18restore_addon_d() {
19 cp -a /tmp/addon.d/* /system/addon.d/
20 rm -rf /tmp/addon.d/
21}
22
23# Proceed only if /system is the expected major and minor version
24check_prereq() {
25if ( ! grep -q "^ro.build.version.release=$V.*" /system/build.prop ); then
26 echo "Not backing up files from incompatible version: $V"
27 return 0
28fi
29return 1
30}
31
32check_blacklist() {
33 if [ -f /system/addon.d/blacklist ];then
34 ## Discard any known bad backup scripts
35 cd /$1/addon.d/
36 for f in *sh; do
37 s=$(md5sum $f | awk {'print $1'})
38 grep -q $s /system/addon.d/blacklist && rm -f $f
39 done
40 fi
41}
42
43check_whitelist() {
44 found=0
45 if [ -f /system/addon.d/whitelist ];then
46 ## forcefully keep any version-independent stuff
47 cd /$1/addon.d/
48 for f in *sh; do
49 s=$(md5sum $f | awk {'print $1'})
50 grep -q $s /system/addon.d/whitelist
51 if [ $? -eq 0 ]; then
52 found=1
53 else
54 rm -f $f
55 fi
56 done
57 fi
58 return $found
59}
60
61# Execute /system/addon.d/*.sh scripts with $1 parameter
62run_stage() {
63for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
64 $script $1
65done
66}
67
68case "$1" in
69 backup)
70 mkdir -p $C
71 if check_prereq; then
72 if check_whitelist system; then
73 exit 127
74 fi
75 fi
76 check_blacklist system
77 preserve_addon_d
78 run_stage pre-backup
79 run_stage backup
80 run_stage post-backup
81 ;;
82 restore)
83 if check_prereq; then
84 if check_whitelist tmp; then
85 exit 127
86 fi
87 fi
88 check_blacklist tmp
89 run_stage pre-restore
90 run_stage restore
91 run_stage post-restore
92 restore_addon_d
93 rm -rf $C
94 sync
95 ;;
96 *)
97 echo "Usage: $0 {backup|restore}"
98 exit 1
99esac
100
101exit 0