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 |
62 | cd /$1/addon.d/ | ||||
63 | for f in *sh; do | ||||
Gabriele M | 4fe3509 | 2017-06-03 18:08:39 +0200 | [diff] [blame] | 64 | [ -f $f ] || continue |
Tom Marshall | 517e691 | 2015-11-18 14:45:04 -0800 | [diff] [blame] | 65 | s=$(md5sum $f | cut -c-32) |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 66 | grep -q $s $S/addon.d/blacklist && rm -f $f |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 67 | done |
68 | fi | ||||
69 | } | ||||
70 | |||||
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 71 | check_whitelist() { |
72 | found=0 | ||||
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 73 | if [ -f $S/addon.d/whitelist ];then |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 74 | ## forcefully keep any version-independent stuff |
75 | cd /$1/addon.d/ | ||||
76 | for f in *sh; do | ||||
Tom Marshall | 517e691 | 2015-11-18 14:45:04 -0800 | [diff] [blame] | 77 | s=$(md5sum $f | cut -c-32) |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 78 | grep -q $s $S/addon.d/whitelist |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 79 | if [ $? -eq 0 ]; then |
80 | found=1 | ||||
81 | else | ||||
82 | rm -f $f | ||||
83 | fi | ||||
84 | done | ||||
85 | fi | ||||
86 | return $found | ||||
87 | } | ||||
88 | |||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 89 | # Execute /system/addon.d/*.sh scripts with $1 parameter |
90 | run_stage() { | ||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 91 | if [ -d /tmp/addon.d/ ]; then |
92 | for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do | ||||
93 | $script $1 | ||||
94 | done | ||||
95 | fi | ||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 96 | } |
97 | |||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 98 | determine_system_mount() { |
99 | if grep -q -e"^$SYSDEV" /proc/mounts; then | ||||
100 | umount $(grep -e"^$SYSDEV" /proc/mounts | cut -d" " -f2) | ||||
101 | fi | ||||
102 | |||||
103 | if [ -d /mnt/system ]; then | ||||
104 | SYSMOUNT="/mnt/system" | ||||
105 | elif [ -d /system_root ]; then | ||||
106 | SYSMOUNT="/system_root" | ||||
107 | else | ||||
108 | SYSMOUNT="/system" | ||||
109 | fi | ||||
110 | |||||
111 | export S=$SYSMOUNT/system | ||||
112 | } | ||||
113 | |||||
114 | mount_system() { | ||||
115 | mount -t $SYSFS $SYSDEV $SYSMOUNT -o rw,discard | ||||
116 | } | ||||
117 | |||||
118 | unmount_system() { | ||||
119 | umount $SYSMOUNT | ||||
120 | } | ||||
121 | |||||
122 | determine_system_mount | ||||
123 | |||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 124 | case "$1" in |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 125 | backup) |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 126 | mount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 127 | mkdir -p $C |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 128 | if check_prereq; then |
Davide Garberi | 6b83f94 | 2020-01-19 17:44:11 +0100 | [diff] [blame^] | 129 | if check_whitelist $S; then |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 130 | unmount_system |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 131 | exit 127 |
132 | fi | ||||
133 | fi | ||||
Davide Garberi | 6b83f94 | 2020-01-19 17:44:11 +0100 | [diff] [blame^] | 134 | check_blacklist $S |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 135 | preserve_addon_d |
136 | run_stage pre-backup | ||||
137 | run_stage backup | ||||
138 | run_stage post-backup | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 139 | unmount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 140 | ;; |
141 | restore) | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 142 | mount_system |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 143 | if check_prereq; then |
144 | if check_whitelist tmp; then | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 145 | unmount_system |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 146 | exit 127 |
147 | fi | ||||
148 | fi | ||||
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 149 | check_blacklist tmp |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 150 | run_stage pre-restore |
151 | run_stage restore | ||||
152 | run_stage post-restore | ||||
153 | restore_addon_d | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 154 | rm -rf $C |
155 | sync | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 156 | unmount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 157 | ;; |
158 | *) | ||||
159 | echo "Usage: $0 {backup|restore}" | ||||
160 | exit 1 | ||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 161 | esac |
162 | |||||
163 | exit 0 |