blob: fe8ca43a4fd4e8834a642f898438dcc59221d4e3 [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 Bestas89450fb2016-10-26 22:35:18 +03008export V=14.1
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
Adrian DC13512fb2016-10-23 18:37:13 +020025 mkdir -p /system/addon.d/
Andreas Schneider9cfe9682015-09-03 11:04:58 +020026 cp -a /tmp/addon.d/* /system/addon.d/
27 rm -rf /tmp/addon.d/
28 fi
Warren Togamib1637c82012-03-03 22:37:42 -100029}
30
Chirayu Desai700fc8f2012-12-13 19:36:37 +053031# Proceed only if /system is the expected major and minor version
kmobse051cf32010-06-24 22:36:11 -050032check_prereq() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020033# If there is no build.prop file the partition is probably empty.
34if [ ! -r /system/build.prop ]; then
35 return 0
36fi
Warren Togamib1637c82012-03-03 22:37:42 -100037if ( ! grep -q "^ro.cm.version=$V.*" /system/build.prop ); then
Chirayu Desai700fc8f2012-12-13 19:36:37 +053038 echo "Not backing up files from incompatible version: $V"
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010039 return 0
Warren Togamib1637c82012-03-03 22:37:42 -100040fi
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010041return 1
kmobse051cf32010-06-24 22:36:11 -050042}
43
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000044check_blacklist() {
45 if [ -f /system/addon.d/blacklist ];then
46 ## Discard any known bad backup scripts
47 cd /$1/addon.d/
48 for f in *sh; do
Tom Marshall517e6912015-11-18 14:45:04 -080049 s=$(md5sum $f | cut -c-32)
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000050 grep -q $s /system/addon.d/blacklist && rm -f $f
51 done
52 fi
53}
54
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010055check_whitelist() {
56 found=0
57 if [ -f /system/addon.d/whitelist ];then
58 ## forcefully keep any version-independent stuff
59 cd /$1/addon.d/
60 for f in *sh; do
Tom Marshall517e6912015-11-18 14:45:04 -080061 s=$(md5sum $f | cut -c-32)
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010062 grep -q $s /system/addon.d/whitelist
63 if [ $? -eq 0 ]; then
64 found=1
65 else
66 rm -f $f
67 fi
68 done
69 fi
70 return $found
71}
72
Warren Togamib1637c82012-03-03 22:37:42 -100073# Execute /system/addon.d/*.sh scripts with $1 parameter
74run_stage() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020075if [ -d /tmp/addon.d/ ]; then
76 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
77 $script $1
78 done
79fi
kmobse051cf32010-06-24 22:36:11 -050080}
81
kmobse051cf32010-06-24 22:36:11 -050082case "$1" in
Warren Togamib1637c82012-03-03 22:37:42 -100083 backup)
84 mkdir -p $C
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010085 if check_prereq; then
86 if check_whitelist system; then
87 exit 127
88 fi
89 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000090 check_blacklist system
Warren Togamib1637c82012-03-03 22:37:42 -100091 preserve_addon_d
92 run_stage pre-backup
93 run_stage backup
94 run_stage post-backup
Warren Togamib1637c82012-03-03 22:37:42 -100095 ;;
96 restore)
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010097 if check_prereq; then
98 if check_whitelist tmp; then
99 exit 127
100 fi
101 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +0000102 check_blacklist tmp
Warren Togamib1637c82012-03-03 22:37:42 -1000103 run_stage pre-restore
104 run_stage restore
105 run_stage post-restore
106 restore_addon_d
Warren Togamib1637c82012-03-03 22:37:42 -1000107 rm -rf $C
108 sync
109 ;;
110 *)
111 echo "Usage: $0 {backup|restore}"
112 exit 1
kmobse051cf32010-06-24 22:36:11 -0500113esac
114
115exit 0