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 |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 7 | export SYSDEV="$(readlink -nf "$2")" |
8 | export SYSFS="$3" | ||||
Michael Bestas | 42020e2 | 2020-01-03 02:03:00 +0200 | [diff] [blame] | 9 | export V=17.1 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 10 | |
Dan Pasanen | 524fe03 | 2018-01-26 10:26:47 -0600 | [diff] [blame] | 11 | export ADDOND_VERSION=1 |
12 | |||||
Tom Marshall | 65aff66 | 2014-12-12 11:56:04 -0800 | [diff] [blame] | 13 | # Scripts in /system/addon.d expect to find backuptool.functions in /tmp |
14 | cp -f /tmp/install/bin/backuptool.functions /tmp | ||||
15 | |||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 16 | # Preserve /system/addon.d in /tmp/addon.d |
17 | preserve_addon_d() { | ||||
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 18 | if [ -d $S/addon.d/ ]; then |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 19 | mkdir -p /tmp/addon.d/ |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 20 | cp -a $S/addon.d/* /tmp/addon.d/ |
Dan Pasanen | 524fe03 | 2018-01-26 10:26:47 -0600 | [diff] [blame] | 21 | |
22 | # Discard any scripts that aren't at least our version level | ||||
Davide Garberi | 23d677b | 2020-01-19 12:21:00 +0100 | [diff] [blame] | 23 | for f in /tmp/addon.d/*sh; do |
Dan Pasanen | 524fe03 | 2018-01-26 10:26:47 -0600 | [diff] [blame] | 24 | SCRIPT_VERSION=$(grep "^# ADDOND_VERSION=" $f | cut -d= -f2) |
25 | if [ -z "$SCRIPT_VERSION" ]; then | ||||
26 | SCRIPT_VERSION=1 | ||||
27 | fi | ||||
28 | if [ $SCRIPT_VERSION -lt $ADDOND_VERSION ]; then | ||||
29 | rm $f | ||||
30 | fi | ||||
31 | done | ||||
32 | |||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 33 | chmod 755 /tmp/addon.d/*.sh |
34 | fi | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 35 | } |
36 | |||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 37 | # Restore /system/addon.d from /tmp/addon.d |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 38 | restore_addon_d() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 39 | if [ -d /tmp/addon.d/ ]; then |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 40 | mkdir -p $S/addon.d/ |
41 | cp -a /tmp/addon.d/* $S/addon.d/ | ||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 42 | rm -rf /tmp/addon.d/ |
43 | fi | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 44 | } |
45 | |||||
Chirayu Desai | 700fc8f | 2012-12-13 19:36:37 +0530 | [diff] [blame] | 46 | # Proceed only if /system is the expected major and minor version |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 47 | check_prereq() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 48 | # If there is no build.prop file the partition is probably empty. |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 49 | if [ ! -r $S/build.prop ]; then |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 50 | return 0 |
51 | fi | ||||
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 52 | if ! grep -q "^ro.lineage.version=$V.*" $S/build.prop; then |
Adrian DC | 29fca64 | 2018-05-17 23:06:24 +0200 | [diff] [blame] | 53 | echo "Not backing up files from incompatible version: $V" |
54 | return 0 | ||||
55 | fi | ||||
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 56 | return 1 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 57 | } |
58 | |||||
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 59 | check_blacklist() { |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 60 | if [ -f $S/addon.d/blacklist -a -d /$1/addon.d/ ]; then |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 61 | ## Discard any known bad backup scripts |
Davide Garberi | 497a6ae | 2020-01-19 19:28:55 +0100 | [diff] [blame^] | 62 | for f in /$1/addon.d/*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 | |||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 97 | determine_system_mount() { |
98 | if grep -q -e"^$SYSDEV" /proc/mounts; then | ||||
99 | umount $(grep -e"^$SYSDEV" /proc/mounts | cut -d" " -f2) | ||||
100 | fi | ||||
101 | |||||
102 | if [ -d /mnt/system ]; then | ||||
103 | SYSMOUNT="/mnt/system" | ||||
104 | elif [ -d /system_root ]; then | ||||
105 | SYSMOUNT="/system_root" | ||||
106 | else | ||||
107 | SYSMOUNT="/system" | ||||
108 | fi | ||||
109 | |||||
110 | export S=$SYSMOUNT/system | ||||
111 | } | ||||
112 | |||||
113 | mount_system() { | ||||
114 | mount -t $SYSFS $SYSDEV $SYSMOUNT -o rw,discard | ||||
115 | } | ||||
116 | |||||
117 | unmount_system() { | ||||
118 | umount $SYSMOUNT | ||||
119 | } | ||||
120 | |||||
121 | determine_system_mount | ||||
122 | |||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 123 | case "$1" in |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 124 | backup) |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 125 | mount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 126 | mkdir -p $C |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 127 | if check_prereq; then |
Davide Garberi | 6b83f94 | 2020-01-19 17:44:11 +0100 | [diff] [blame] | 128 | if check_whitelist $S; then |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 129 | unmount_system |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 130 | exit 127 |
131 | fi | ||||
132 | fi | ||||
Davide Garberi | 6b83f94 | 2020-01-19 17:44:11 +0100 | [diff] [blame] | 133 | check_blacklist $S |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 134 | preserve_addon_d |
135 | run_stage pre-backup | ||||
136 | run_stage backup | ||||
137 | run_stage post-backup | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 138 | unmount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 139 | ;; |
140 | restore) | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 141 | mount_system |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 142 | if check_prereq; then |
143 | if check_whitelist tmp; then | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 144 | unmount_system |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 145 | exit 127 |
146 | fi | ||||
147 | fi | ||||
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 148 | check_blacklist tmp |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 149 | run_stage pre-restore |
150 | run_stage restore | ||||
151 | run_stage post-restore | ||||
152 | restore_addon_d | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 153 | rm -rf $C |
154 | sync | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 155 | unmount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 156 | ;; |
157 | *) | ||||
158 | echo "Usage: $0 {backup|restore}" | ||||
159 | exit 1 | ||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 160 | esac |
161 | |||||
162 | exit 0 |