blob: 7ff0c886cce521ebe6a203422655e262bdab0089 [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
Michael Bestaseb83dba2015-10-07 18:38:03 +03008export V=13.0
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() {
Andreas Schneider9cfe9682015-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
Warren Togamib1637c82012-03-03 22:37:42 -100020}
21
Andreas Schneider9cfe9682015-09-03 11:04:58 +020022# Restore /system/addon.d from /tmp/addon.d
Warren Togamib1637c82012-03-03 22:37:42 -100023restore_addon_d() {
Andreas Schneider9cfe9682015-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
Warren Togamib1637c82012-03-03 22:37:42 -100028}
29
Chirayu Desai700fc8f2012-12-13 19:36:37 +053030# Proceed only if /system is the expected major and minor version
kmobse051cf32010-06-24 22:36:11 -050031check_prereq() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020032# If there is no build.prop file the partition is probably empty.
33if [ ! -r /system/build.prop ]; then
34 return 0
35fi
Warren Togamib1637c82012-03-03 22:37:42 -100036if ( ! grep -q "^ro.cm.version=$V.*" /system/build.prop ); then
Chirayu Desai700fc8f2012-12-13 19:36:37 +053037 echo "Not backing up files from incompatible version: $V"
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010038 return 0
Warren Togamib1637c82012-03-03 22:37:42 -100039fi
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010040return 1
kmobse051cf32010-06-24 22:36:11 -050041}
42
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000043check_blacklist() {
44 if [ -f /system/addon.d/blacklist ];then
45 ## Discard any known bad backup scripts
46 cd /$1/addon.d/
47 for f in *sh; do
Tom Marshall517e6912015-11-18 14:45:04 -080048 s=$(md5sum $f | cut -c-32)
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000049 grep -q $s /system/addon.d/blacklist && rm -f $f
50 done
51 fi
52}
53
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010054check_whitelist() {
55 found=0
56 if [ -f /system/addon.d/whitelist ];then
57 ## forcefully keep any version-independent stuff
58 cd /$1/addon.d/
59 for f in *sh; do
Tom Marshall517e6912015-11-18 14:45:04 -080060 s=$(md5sum $f | cut -c-32)
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010061 grep -q $s /system/addon.d/whitelist
62 if [ $? -eq 0 ]; then
63 found=1
64 else
65 rm -f $f
66 fi
67 done
68 fi
69 return $found
70}
71
Warren Togamib1637c82012-03-03 22:37:42 -100072# Execute /system/addon.d/*.sh scripts with $1 parameter
73run_stage() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020074if [ -d /tmp/addon.d/ ]; then
75 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
76 $script $1
77 done
78fi
kmobse051cf32010-06-24 22:36:11 -050079}
80
kmobse051cf32010-06-24 22:36:11 -050081case "$1" in
Warren Togamib1637c82012-03-03 22:37:42 -100082 backup)
83 mkdir -p $C
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010084 if check_prereq; then
85 if check_whitelist system; then
86 exit 127
87 fi
88 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000089 check_blacklist system
Warren Togamib1637c82012-03-03 22:37:42 -100090 preserve_addon_d
91 run_stage pre-backup
92 run_stage backup
93 run_stage post-backup
Warren Togamib1637c82012-03-03 22:37:42 -100094 ;;
95 restore)
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010096 if check_prereq; then
97 if check_whitelist tmp; then
98 exit 127
99 fi
100 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +0000101 check_blacklist tmp
Warren Togamib1637c82012-03-03 22:37:42 -1000102 run_stage pre-restore
103 run_stage restore
104 run_stage post-restore
105 restore_addon_d
Warren Togamib1637c82012-03-03 22:37:42 -1000106 rm -rf $C
107 sync
108 ;;
109 *)
110 echo "Usage: $0 {backup|restore}"
111 exit 1
kmobse051cf32010-06-24 22:36:11 -0500112esac
113
114exit 0