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 | |
Tom Marshall | 65aff66 | 2014-12-12 11:56:04 -0800 | [diff] [blame] | 11 | # Scripts in /system/addon.d expect to find backuptool.functions in /tmp |
12 | cp -f /tmp/install/bin/backuptool.functions /tmp | ||||
13 | |||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 14 | # Preserve /system/addon.d in /tmp/addon.d |
15 | preserve_addon_d() { | ||||
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 16 | if [ -d $S/addon.d/ ]; then |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 17 | mkdir -p /tmp/addon.d/ |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 18 | cp -a $S/addon.d/* /tmp/addon.d/ |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 19 | chmod 755 /tmp/addon.d/*.sh |
20 | fi | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 21 | } |
22 | |||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 23 | # Restore /system/addon.d from /tmp/addon.d |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 24 | restore_addon_d() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 25 | if [ -d /tmp/addon.d/ ]; then |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 26 | mkdir -p $S/addon.d/ |
27 | cp -a /tmp/addon.d/* $S/addon.d/ | ||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 28 | rm -rf /tmp/addon.d/ |
29 | fi | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 30 | } |
31 | |||||
Chirayu Desai | 700fc8f | 2012-12-13 19:36:37 +0530 | [diff] [blame] | 32 | # Proceed only if /system is the expected major and minor version |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 33 | check_prereq() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 34 | # If there is no build.prop file the partition is probably empty. |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 35 | if [ ! -r $S/build.prop ]; then |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 36 | return 0 |
37 | fi | ||||
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 38 | if ! grep -q "^ro.lineage.version=$V.*" $S/build.prop; then |
Adrian DC | 29fca64 | 2018-05-17 23:06:24 +0200 | [diff] [blame] | 39 | echo "Not backing up files from incompatible version: $V" |
40 | return 0 | ||||
41 | fi | ||||
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 42 | return 1 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 43 | } |
44 | |||||
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 45 | check_blacklist() { |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 46 | if [ -f $S/addon.d/blacklist -a -d /$1/addon.d/ ]; then |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 47 | ## Discard any known bad backup scripts |
Davide Garberi | 497a6ae | 2020-01-19 19:28:55 +0100 | [diff] [blame] | 48 | for f in /$1/addon.d/*sh; do |
Gabriele M | 4fe3509 | 2017-06-03 18:08:39 +0200 | [diff] [blame] | 49 | [ -f $f ] || continue |
Tom Marshall | 517e691 | 2015-11-18 14:45:04 -0800 | [diff] [blame] | 50 | s=$(md5sum $f | cut -c-32) |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 51 | grep -q $s $S/addon.d/blacklist && rm -f $f |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 52 | done |
53 | fi | ||||
54 | } | ||||
55 | |||||
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 56 | check_whitelist() { |
57 | found=0 | ||||
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 58 | if [ -f $S/addon.d/whitelist ];then |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 59 | ## forcefully keep any version-independent stuff |
60 | cd /$1/addon.d/ | ||||
61 | for f in *sh; do | ||||
Tom Marshall | 517e691 | 2015-11-18 14:45:04 -0800 | [diff] [blame] | 62 | s=$(md5sum $f | cut -c-32) |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 63 | grep -q $s $S/addon.d/whitelist |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 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 Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 74 | # Execute /system/addon.d/*.sh scripts with $1 parameter |
75 | run_stage() { | ||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 76 | if [ -d /tmp/addon.d/ ]; then |
77 | for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do | ||||
78 | $script $1 | ||||
79 | done | ||||
80 | fi | ||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 81 | } |
82 | |||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 83 | determine_system_mount() { |
84 | if grep -q -e"^$SYSDEV" /proc/mounts; then | ||||
85 | umount $(grep -e"^$SYSDEV" /proc/mounts | cut -d" " -f2) | ||||
86 | fi | ||||
87 | |||||
88 | if [ -d /mnt/system ]; then | ||||
89 | SYSMOUNT="/mnt/system" | ||||
90 | elif [ -d /system_root ]; then | ||||
91 | SYSMOUNT="/system_root" | ||||
92 | else | ||||
93 | SYSMOUNT="/system" | ||||
94 | fi | ||||
95 | |||||
96 | export S=$SYSMOUNT/system | ||||
97 | } | ||||
98 | |||||
99 | mount_system() { | ||||
100 | mount -t $SYSFS $SYSDEV $SYSMOUNT -o rw,discard | ||||
101 | } | ||||
102 | |||||
103 | unmount_system() { | ||||
104 | umount $SYSMOUNT | ||||
105 | } | ||||
106 | |||||
107 | determine_system_mount | ||||
108 | |||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 109 | case "$1" in |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 110 | backup) |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 111 | mount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 112 | mkdir -p $C |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 113 | if check_prereq; then |
Davide Garberi | 6b83f94 | 2020-01-19 17:44:11 +0100 | [diff] [blame] | 114 | if check_whitelist $S; then |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 115 | unmount_system |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 116 | exit 127 |
117 | fi | ||||
118 | fi | ||||
Davide Garberi | 6b83f94 | 2020-01-19 17:44:11 +0100 | [diff] [blame] | 119 | check_blacklist $S |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 120 | preserve_addon_d |
121 | run_stage pre-backup | ||||
122 | run_stage backup | ||||
123 | run_stage post-backup | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 124 | unmount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 125 | ;; |
126 | restore) | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 127 | mount_system |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 128 | if check_prereq; then |
129 | if check_whitelist tmp; 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 | ||||
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 134 | check_blacklist tmp |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 135 | run_stage pre-restore |
136 | run_stage restore | ||||
137 | run_stage post-restore | ||||
138 | restore_addon_d | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 139 | rm -rf $C |
140 | sync | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 141 | unmount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 142 | ;; |
143 | *) | ||||
144 | echo "Usage: $0 {backup|restore}" | ||||
145 | exit 1 | ||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 146 | esac |
147 | |||||
148 | exit 0 |