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 |
Jackeagle | e7ed277 | 2021-10-10 05:27:28 -0400 | [diff] [blame] | 7 | export V=12.0 |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 8 | export SYSDEV="$(readlink -nf "$2")" |
| 9 | export SYSFS="$3" |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 10 | |
Alessandro Astone | 42aa1fa | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 11 | export ADDOND_VERSION=3 |
| 12 | |
| 13 | # Partitions to mount for backup/restore in V3 |
| 14 | export all_V3_partitions="vendor product system_ext" |
| 15 | |
Tom Marshall | 65aff66 | 2014-12-12 11:56:04 -0800 | [diff] [blame] | 16 | # Scripts in /system/addon.d expect to find backuptool.functions in /tmp |
| 17 | cp -f /tmp/install/bin/backuptool.functions /tmp |
| 18 | |
Alessandro Astone | 42aa1fa | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 19 | get_script_version() { |
| 20 | version=$(grep "^# ADDOND_VERSION=" $1 | cut -d= -f2) |
| 21 | [ -z "$version" ] && version=1 |
| 22 | echo $version |
| 23 | } |
| 24 | |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 25 | # Preserve /system/addon.d in /tmp/addon.d |
| 26 | preserve_addon_d() { |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 27 | if [ -d $S/addon.d/ ]; then |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 28 | mkdir -p /tmp/addon.d/ |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 29 | cp -a $S/addon.d/* /tmp/addon.d/ |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 30 | chmod 755 /tmp/addon.d/*.sh |
| 31 | fi |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 32 | } |
| 33 | |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 34 | # Restore /system/addon.d from /tmp/addon.d |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 35 | restore_addon_d() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 36 | if [ -d /tmp/addon.d/ ]; then |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 37 | mkdir -p $S/addon.d/ |
| 38 | cp -a /tmp/addon.d/* $S/addon.d/ |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 39 | rm -rf /tmp/addon.d/ |
| 40 | fi |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 41 | } |
| 42 | |
Chirayu Desai | 700fc8f | 2012-12-13 19:36:37 +0530 | [diff] [blame] | 43 | # Proceed only if /system is the expected major and minor version |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 44 | check_prereq() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 45 | # If there is no build.prop file the partition is probably empty. |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 46 | if [ ! -r $S/build.prop ]; then |
z3DD3r | 284ec83 | 2020-03-04 18:07:40 +0300 | [diff] [blame] | 47 | echo "Backup/restore is not possible. Partition is probably empty" |
| 48 | return 1 |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 49 | fi |
Jackeagle | ee73e6c | 2020-04-22 17:32:53 +0200 | [diff] [blame] | 50 | if ! grep -q "^ro.bliss.static.version=$V.*" $S/build.prop; then |
z3DD3r | 284ec83 | 2020-03-04 18:07:40 +0300 | [diff] [blame] | 51 | echo "Backup/restore is not possible. Incompatible ROM version: $V" |
| 52 | return 2 |
Adrian DC | 29fca64 | 2018-05-17 23:06:24 +0200 | [diff] [blame] | 53 | fi |
z3DD3r | 284ec83 | 2020-03-04 18:07:40 +0300 | [diff] [blame] | 54 | return 0 |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Alessandro Astone | 42aa1fa | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 57 | # Execute /system/addon.d/*.sh scripts with each $@ parameter |
| 58 | run_stages() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 59 | if [ -d /tmp/addon.d/ ]; then |
| 60 | for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do |
Alessandro Astone | 42aa1fa | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 61 | v=$(get_script_version $script) |
| 62 | if [ $v -ge 3 ]; then |
| 63 | mount_extra $all_V3_partitions |
| 64 | else |
| 65 | umount_extra $all_V3_partitions |
| 66 | fi |
| 67 | |
| 68 | for stage in $@; do |
| 69 | if [ $v -ge 3 ]; then |
| 70 | $script $stage |
| 71 | else |
Alessandro Astone | 145be42 | 2021-03-26 12:52:38 +0100 | [diff] [blame] | 72 | ADDOND_VERSION= $script $stage |
Alessandro Astone | 42aa1fa | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 73 | fi |
| 74 | done |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 75 | done |
| 76 | fi |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Alessandro Astone | 42aa1fa | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 79 | ##################### |
| 80 | ### Mount helpers ### |
| 81 | ##################### |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 82 | determine_system_mount() { |
| 83 | if grep -q -e"^$SYSDEV" /proc/mounts; then |
| 84 | umount $(grep -e"^$SYSDEV" /proc/mounts | cut -d" " -f2) |
| 85 | fi |
| 86 | |
| 87 | if [ -d /mnt/system ]; then |
| 88 | SYSMOUNT="/mnt/system" |
| 89 | elif [ -d /system_root ]; then |
| 90 | SYSMOUNT="/system_root" |
| 91 | else |
| 92 | SYSMOUNT="/system" |
| 93 | fi |
| 94 | |
| 95 | export S=$SYSMOUNT/system |
| 96 | } |
| 97 | |
| 98 | mount_system() { |
| 99 | mount -t $SYSFS $SYSDEV $SYSMOUNT -o rw,discard |
| 100 | } |
| 101 | |
| 102 | unmount_system() { |
| 103 | umount $SYSMOUNT |
| 104 | } |
| 105 | |
Alessandro Astone | 42aa1fa | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 106 | get_block_for_mount_point() { |
Erik Jensen | 08d29c4 | 2021-04-30 05:42:41 +0000 | [diff] [blame] | 107 | grep -v "^#" /etc/recovery.fstab | grep "[[:blank:]]$1[[:blank:]]" | tail -n1 | tr -s [:blank:] ' ' | cut -d' ' -f1 |
Alessandro Astone | 42aa1fa | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | find_block() { |
| 111 | local name="$1" |
| 112 | local fstab_entry=$(get_block_for_mount_point "/$name") |
| 113 | # P-SAR hacks |
| 114 | [ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/") |
| 115 | [ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/system_root") |
| 116 | |
| 117 | local dev |
| 118 | if [ "$DYNAMIC_PARTITIONS" = "true" ]; then |
| 119 | if [ -n "$fstab_entry" ]; then |
| 120 | dev="${BLK_PATH}/${fstab_entry}" |
| 121 | else |
| 122 | dev="${BLK_PATH}/${name}" |
| 123 | fi |
| 124 | else |
| 125 | if [ -n "$fstab_entry" ]; then |
| 126 | dev="$fstab_entry" |
| 127 | else |
| 128 | dev="${BLK_PATH}/${name}" |
| 129 | fi |
| 130 | fi |
| 131 | |
| 132 | if [ -b "$dev" ]; then |
| 133 | echo "$dev" |
| 134 | fi |
| 135 | } |
| 136 | |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 137 | determine_system_mount |
| 138 | |
Alessandro Astone | 42aa1fa | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 139 | DYNAMIC_PARTITIONS=$(getprop ro.boot.dynamic_partitions) |
Tim Zimmermann | c8a5259 | 2021-05-08 16:17:18 +0200 | [diff] [blame] | 140 | if [ "$DYNAMIC_PARTITIONS" = "true" ]; then |
| 141 | BLK_PATH="/dev/block/mapper" |
| 142 | else |
| 143 | BLK_PATH=$(dirname "$SYSDEV") |
| 144 | fi |
Alessandro Astone | 42aa1fa | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 145 | |
| 146 | mount_extra() { |
| 147 | for partition in $@; do |
| 148 | mnt_point="/$partition" |
Alessandro Astone | b008630 | 2021-04-02 00:18:16 +0200 | [diff] [blame] | 149 | mountpoint "$mnt_point" >/dev/null 2>&1 && continue |
| 150 | [ -L "$SYSMOUNT/$partition" ] && continue |
Alessandro Astone | 42aa1fa | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 151 | |
| 152 | blk_dev=$(find_block "$partition") |
| 153 | if [ -e "$blk_dev" ]; then |
| 154 | [ "$DYNAMIC_PARTITIONS" = "true" ] && blockdev --setrw "$blk_dev" |
| 155 | mkdir -p "$mnt_point" |
| 156 | mount -o rw "$blk_dev" "$mnt_point" |
| 157 | fi |
| 158 | done |
| 159 | } |
| 160 | |
| 161 | umount_extra() { |
| 162 | for partition in $@; do |
| 163 | umount -l "/$partition" 2>/dev/null |
| 164 | done |
| 165 | } |
| 166 | |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 167 | case "$1" in |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 168 | backup) |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 169 | mount_system |
z3DD3r | 284ec83 | 2020-03-04 18:07:40 +0300 | [diff] [blame] | 170 | if check_prereq; then |
| 171 | mkdir -p $C |
| 172 | preserve_addon_d |
Alessandro Astone | 42aa1fa | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 173 | run_stages pre-backup backup post-backup |
z3DD3r | 284ec83 | 2020-03-04 18:07:40 +0300 | [diff] [blame] | 174 | fi |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 175 | unmount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 176 | ;; |
| 177 | restore) |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 178 | mount_system |
z3DD3r | 284ec83 | 2020-03-04 18:07:40 +0300 | [diff] [blame] | 179 | if check_prereq; then |
Alessandro Astone | 42aa1fa | 2020-12-29 18:38:28 +0100 | [diff] [blame] | 180 | run_stages pre-restore restore post-restore |
| 181 | umount_extra $all_V3_partitions |
z3DD3r | 284ec83 | 2020-03-04 18:07:40 +0300 | [diff] [blame] | 182 | restore_addon_d |
| 183 | rm -rf $C |
| 184 | sync |
| 185 | fi |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 186 | unmount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 187 | ;; |
| 188 | *) |
| 189 | echo "Usage: $0 {backup|restore}" |
| 190 | exit 1 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 191 | esac |
| 192 | |
| 193 | exit 0 |