blob: bf7114973bedd0c598abc044b06219357aeeb965 [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 Cerqueira49a30e72014-11-05 18:22:50 +00008export V=12
kmobse051cf32010-06-24 22:36:11 -05009
Tom Marshall65aff662014-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
Warren Togamib1637c82012-03-03 22:37:42 -100013# 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
Chirayu Desai700fc8f2012-12-13 19:36:37 +053026# Proceed only if /system is the expected major and minor version
kmobse051cf32010-06-24 22:36:11 -050027check_prereq() {
Warren Togamib1637c82012-03-03 22:37:42 -100028if ( ! grep -q "^ro.cm.version=$V.*" /system/build.prop ); then
Chirayu Desai700fc8f2012-12-13 19:36:37 +053029 echo "Not backing up files from incompatible version: $V"
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010030 return 0
Warren Togamib1637c82012-03-03 22:37:42 -100031fi
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010032return 1
kmobse051cf32010-06-24 22:36:11 -050033}
34
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000035check_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
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010046check_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
Warren Togamib1637c82012-03-03 22:37:42 -100064# 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
kmobse051cf32010-06-24 22:36:11 -050069}
70
kmobse051cf32010-06-24 22:36:11 -050071case "$1" in
Warren Togamib1637c82012-03-03 22:37:42 -100072 backup)
73 mkdir -p $C
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010074 if check_prereq; then
75 if check_whitelist system; then
76 exit 127
77 fi
78 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000079 check_blacklist system
Warren Togamib1637c82012-03-03 22:37:42 -100080 preserve_addon_d
81 run_stage pre-backup
82 run_stage backup
83 run_stage post-backup
Warren Togamib1637c82012-03-03 22:37:42 -100084 ;;
85 restore)
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010086 if check_prereq; then
87 if check_whitelist tmp; then
88 exit 127
89 fi
90 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000091 check_blacklist tmp
Warren Togamib1637c82012-03-03 22:37:42 -100092 run_stage pre-restore
93 run_stage restore
94 run_stage post-restore
95 restore_addon_d
Warren Togamib1637c82012-03-03 22:37:42 -100096 rm -rf $C
97 sync
98 ;;
99 *)
100 echo "Usage: $0 {backup|restore}"
101 exit 1
kmobse051cf32010-06-24 22:36:11 -0500102esac
103
104exit 0