blob: eba625d2d71d0bd464a7dd51987e3827cf9f1ca0 [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
Adrian DC93256e42017-12-07 01:34:33 +01008export V=15.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
Luca Stefani5c60e4f2017-08-17 19:28:48 +020037if ( ! grep -q "^ro.lineage.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() {
Gabriele M4fe35092017-06-03 18:08:39 +020045 if [ -f /system/addon.d/blacklist -a -d /$1/addon.d/ ]; then
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000046 ## Discard any known bad backup scripts
47 cd /$1/addon.d/
48 for f in *sh; do
Gabriele M4fe35092017-06-03 18:08:39 +020049 [ -f $f ] || continue
Tom Marshall517e6912015-11-18 14:45:04 -080050 s=$(md5sum $f | cut -c-32)
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000051 grep -q $s /system/addon.d/blacklist && rm -f $f
52 done
53 fi
54}
55
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010056check_whitelist() {
57 found=0
58 if [ -f /system/addon.d/whitelist ];then
59 ## forcefully keep any version-independent stuff
60 cd /$1/addon.d/
61 for f in *sh; do
Tom Marshall517e6912015-11-18 14:45:04 -080062 s=$(md5sum $f | cut -c-32)
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010063 grep -q $s /system/addon.d/whitelist
64 if [ $? -eq 0 ]; then
65 found=1
66 else
67 rm -f $f
68 fi
69 done
70 fi
71 return $found
72}
73
Warren Togamib1637c82012-03-03 22:37:42 -100074# Execute /system/addon.d/*.sh scripts with $1 parameter
75run_stage() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020076if [ -d /tmp/addon.d/ ]; then
77 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
78 $script $1
79 done
80fi
kmobse051cf32010-06-24 22:36:11 -050081}
82
kmobse051cf32010-06-24 22:36:11 -050083case "$1" in
Warren Togamib1637c82012-03-03 22:37:42 -100084 backup)
85 mkdir -p $C
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010086 if check_prereq; then
87 if check_whitelist system; then
88 exit 127
89 fi
90 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000091 check_blacklist system
Warren Togamib1637c82012-03-03 22:37:42 -100092 preserve_addon_d
93 run_stage pre-backup
94 run_stage backup
95 run_stage post-backup
Warren Togamib1637c82012-03-03 22:37:42 -100096 ;;
97 restore)
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010098 if check_prereq; then
99 if check_whitelist tmp; then
100 exit 127
101 fi
102 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +0000103 check_blacklist tmp
Warren Togamib1637c82012-03-03 22:37:42 -1000104 run_stage pre-restore
105 run_stage restore
106 run_stage post-restore
107 restore_addon_d
Warren Togamib1637c82012-03-03 22:37:42 -1000108 rm -rf $C
109 sync
110 ;;
111 *)
112 echo "Usage: $0 {backup|restore}"
113 exit 1
kmobse051cf32010-06-24 22:36:11 -0500114esac
115
116exit 0