blob: a3bfc1cd7922dbc85b61061aac41e79e61a4c0e6 [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() {
Andreas Schneiderb1fc7b42015-09-03 11:04:58 +020015 if [ -d /system/addon.d/ ]; then
16 mkdir -p /tmp/addon.d/
17 cp -a /system/addon.d/* /tmp/addon.d/
18 chmod 755 /tmp/addon.d/*.sh
19 fi
Alberto970ceb8342015-01-02 14:43:15 +010020}
21
Andreas Schneiderb1fc7b42015-09-03 11:04:58 +020022# Restore /system/addon.d from /tmp/addon.d
Alberto970ceb8342015-01-02 14:43:15 +010023restore_addon_d() {
Andreas Schneiderb1fc7b42015-09-03 11:04:58 +020024 if [ -d /tmp/addon.d/ ]; then
25 cp -a /tmp/addon.d/* /system/addon.d/
26 rm -rf /tmp/addon.d/
27 fi
Alberto970ceb8342015-01-02 14:43:15 +010028}
29
30# Proceed only if /system is the expected major and minor version
31check_prereq() {
32if ( ! grep -q "^ro.build.version.release=$V.*" /system/build.prop ); then
33 echo "Not backing up files from incompatible version: $V"
34 return 0
35fi
36return 1
37}
38
39check_blacklist() {
40 if [ -f /system/addon.d/blacklist ];then
41 ## Discard any known bad backup scripts
42 cd /$1/addon.d/
43 for f in *sh; do
44 s=$(md5sum $f | awk {'print $1'})
45 grep -q $s /system/addon.d/blacklist && rm -f $f
46 done
47 fi
48}
49
50check_whitelist() {
51 found=0
52 if [ -f /system/addon.d/whitelist ];then
53 ## forcefully keep any version-independent stuff
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/whitelist
58 if [ $? -eq 0 ]; then
59 found=1
60 else
61 rm -f $f
62 fi
63 done
64 fi
65 return $found
66}
67
68# Execute /system/addon.d/*.sh scripts with $1 parameter
69run_stage() {
Andreas Schneiderb1fc7b42015-09-03 11:04:58 +020070if [ -d /tmp/addon.d/ ]; then
71 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
72 $script $1
73 done
74fi
Alberto970ceb8342015-01-02 14:43:15 +010075}
76
77case "$1" in
78 backup)
79 mkdir -p $C
80 if check_prereq; then
81 if check_whitelist system; then
82 exit 127
83 fi
84 fi
85 check_blacklist system
86 preserve_addon_d
87 run_stage pre-backup
88 run_stage backup
89 run_stage post-backup
90 ;;
91 restore)
92 if check_prereq; then
93 if check_whitelist tmp; then
94 exit 127
95 fi
96 fi
97 check_blacklist tmp
98 run_stage pre-restore
99 run_stage restore
100 run_stage post-restore
101 restore_addon_d
102 rm -rf $C
103 sync
104 ;;
105 *)
106 echo "Usage: $0 {backup|restore}"
107 exit 1
108esac
109
110exit 0