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