kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 1 | #!/sbin/sh |
2 | # | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 3 | # Backup and restore addon /system files |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 4 | # |
5 | |||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 6 | export C=/tmp/backupdir |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 7 | export S=$2 |
Rashed Abdel-Tawab | b6b3421 | 2019-09-04 15:08:51 -0700 | [diff] [blame] | 8 | export V=17.0 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 9 | |
Dan Pasanen | 524fe03 | 2018-01-26 10:26:47 -0600 | [diff] [blame] | 10 | export ADDOND_VERSION=1 |
11 | |||||
Tom Marshall | 65aff66 | 2014-12-12 11:56:04 -0800 | [diff] [blame] | 12 | # Scripts in /system/addon.d expect to find backuptool.functions in /tmp |
13 | cp -f /tmp/install/bin/backuptool.functions /tmp | ||||
14 | |||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 15 | # Preserve /system/addon.d in /tmp/addon.d |
16 | preserve_addon_d() { | ||||
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 17 | if [ -d $S/addon.d/ ]; then |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 18 | mkdir -p /tmp/addon.d/ |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 19 | cp -a $S/addon.d/* /tmp/addon.d/ |
Dan Pasanen | 524fe03 | 2018-01-26 10:26:47 -0600 | [diff] [blame] | 20 | |
21 | # Discard any scripts that aren't at least our version level | ||||
22 | for f in /postinstall/tmp/addon.d/*sh; do | ||||
23 | SCRIPT_VERSION=$(grep "^# ADDOND_VERSION=" $f | cut -d= -f2) | ||||
24 | if [ -z "$SCRIPT_VERSION" ]; then | ||||
25 | SCRIPT_VERSION=1 | ||||
26 | fi | ||||
27 | if [ $SCRIPT_VERSION -lt $ADDOND_VERSION ]; then | ||||
28 | rm $f | ||||
29 | fi | ||||
30 | done | ||||
31 | |||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 32 | chmod 755 /tmp/addon.d/*.sh |
33 | fi | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 34 | } |
35 | |||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 36 | # Restore /system/addon.d from /tmp/addon.d |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 37 | restore_addon_d() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 38 | if [ -d /tmp/addon.d/ ]; then |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 39 | mkdir -p $S/addon.d/ |
40 | cp -a /tmp/addon.d/* $S/addon.d/ | ||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 41 | rm -rf /tmp/addon.d/ |
42 | fi | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 43 | } |
44 | |||||
Chirayu Desai | 700fc8f | 2012-12-13 19:36:37 +0530 | [diff] [blame] | 45 | # Proceed only if /system is the expected major and minor version |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 46 | check_prereq() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 47 | # If there is no build.prop file the partition is probably empty. |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 48 | if [ ! -r $S/build.prop ]; then |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 49 | return 0 |
50 | fi | ||||
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 51 | if ! grep -q "^ro.lineage.version=$V.*" $S/build.prop; then |
Adrian DC | 29fca64 | 2018-05-17 23:06:24 +0200 | [diff] [blame] | 52 | echo "Not backing up files from incompatible version: $V" |
53 | return 0 | ||||
54 | fi | ||||
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 55 | return 1 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 56 | } |
57 | |||||
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 58 | check_blacklist() { |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 59 | if [ -f $S/addon.d/blacklist -a -d /$1/addon.d/ ]; then |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 60 | ## Discard any known bad backup scripts |
61 | cd /$1/addon.d/ | ||||
62 | for f in *sh; do | ||||
Gabriele M | 4fe3509 | 2017-06-03 18:08:39 +0200 | [diff] [blame] | 63 | [ -f $f ] || continue |
Tom Marshall | 517e691 | 2015-11-18 14:45:04 -0800 | [diff] [blame] | 64 | s=$(md5sum $f | cut -c-32) |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 65 | grep -q $s $S/addon.d/blacklist && rm -f $f |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 66 | done |
67 | fi | ||||
68 | } | ||||
69 | |||||
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 70 | check_whitelist() { |
71 | found=0 | ||||
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 72 | if [ -f $S/addon.d/whitelist ];then |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 73 | ## forcefully keep any version-independent stuff |
74 | cd /$1/addon.d/ | ||||
75 | for f in *sh; do | ||||
Tom Marshall | 517e691 | 2015-11-18 14:45:04 -0800 | [diff] [blame] | 76 | s=$(md5sum $f | cut -c-32) |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 77 | grep -q $s $S/addon.d/whitelist |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 78 | if [ $? -eq 0 ]; then |
79 | found=1 | ||||
80 | else | ||||
81 | rm -f $f | ||||
82 | fi | ||||
83 | done | ||||
84 | fi | ||||
85 | return $found | ||||
86 | } | ||||
87 | |||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 88 | # Execute /system/addon.d/*.sh scripts with $1 parameter |
89 | run_stage() { | ||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 90 | if [ -d /tmp/addon.d/ ]; then |
91 | for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do | ||||
92 | $script $1 | ||||
93 | done | ||||
94 | fi | ||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 95 | } |
96 | |||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 97 | case "$1" in |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 98 | backup) |
99 | mkdir -p $C | ||||
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 100 | if check_prereq; then |
101 | if check_whitelist system; then | ||||
102 | exit 127 | ||||
103 | fi | ||||
104 | fi | ||||
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 105 | check_blacklist system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 106 | preserve_addon_d |
107 | run_stage pre-backup | ||||
108 | run_stage backup | ||||
109 | run_stage post-backup | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 110 | ;; |
111 | restore) | ||||
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 112 | if check_prereq; then |
113 | if check_whitelist tmp; then | ||||
114 | exit 127 | ||||
115 | fi | ||||
116 | fi | ||||
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 117 | check_blacklist tmp |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 118 | run_stage pre-restore |
119 | run_stage restore | ||||
120 | run_stage post-restore | ||||
121 | restore_addon_d | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 122 | rm -rf $C |
123 | sync | ||||
124 | ;; | ||||
125 | *) | ||||
126 | echo "Usage: $0 {backup|restore}" | ||||
127 | exit 1 | ||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 128 | esac |
129 | |||||
130 | exit 0 |