blob: a45721eceada118beb47e76973c111cf1626d963 [file] [log] [blame]
kmobse051cf32010-06-24 22:36:11 -05001#!/sbin/sh
2#
Warren Togamib1637c82012-03-03 22:37:42 -10003# Backup and restore addon /system files
kmobse051cf32010-06-24 22:36:11 -05004#
5
Warren Togamib1637c82012-03-03 22:37:42 -10006export C=/tmp/backupdir
7export S=/system
Ricardo Cerqueiraa6900132013-07-27 01:54:17 +01008export V=10.2
kmobse051cf32010-06-24 22:36:11 -05009
Warren Togamib1637c82012-03-03 22:37:42 -100010# 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
Chirayu Desai700fc8f2012-12-13 19:36:37 +053023# Proceed only if /system is the expected major and minor version
kmobse051cf32010-06-24 22:36:11 -050024check_prereq() {
Warren Togamib1637c82012-03-03 22:37:42 -100025if ( ! grep -q "^ro.cm.version=$V.*" /system/build.prop ); then
Chirayu Desai700fc8f2012-12-13 19:36:37 +053026 echo "Not backing up files from incompatible version: $V"
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010027 return 0
Warren Togamib1637c82012-03-03 22:37:42 -100028fi
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010029return 1
kmobse051cf32010-06-24 22:36:11 -050030}
31
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000032check_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
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010043check_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
Warren Togamib1637c82012-03-03 22:37:42 -100061# 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
kmobse051cf32010-06-24 22:36:11 -050066}
67
kmobse051cf32010-06-24 22:36:11 -050068case "$1" in
Warren Togamib1637c82012-03-03 22:37:42 -100069 backup)
70 mkdir -p $C
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010071 if check_prereq; then
72 if check_whitelist system; then
73 exit 127
74 fi
75 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000076 check_blacklist system
Warren Togamib1637c82012-03-03 22:37:42 -100077 preserve_addon_d
78 run_stage pre-backup
79 run_stage backup
80 run_stage post-backup
Warren Togamib1637c82012-03-03 22:37:42 -100081 ;;
82 restore)
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010083 if check_prereq; then
84 if check_whitelist tmp; then
85 exit 127
86 fi
87 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000088 check_blacklist tmp
Warren Togamib1637c82012-03-03 22:37:42 -100089 run_stage pre-restore
90 run_stage restore
91 run_stage post-restore
92 restore_addon_d
Warren Togamib1637c82012-03-03 22:37:42 -100093 rm -rf $C
94 sync
95 ;;
96 *)
97 echo "Usage: $0 {backup|restore}"
98 exit 1
kmobse051cf32010-06-24 22:36:11 -050099esac
100
101exit 0