Dan Pasanen | 894b352 | 2017-01-16 19:26:50 -0600 | [diff] [blame] | 1 | #!/system/bin/sh |
| 2 | # |
| 3 | # Backup and restore addon /system files |
| 4 | # |
| 5 | |
| 6 | export S=/system |
| 7 | export C=/postinstall/tmp/backupdir |
Marko Man | 97ff586 | 2018-08-09 14:29:04 +0200 | [diff] [blame^] | 8 | export V=9.0 |
Dan Pasanen | 894b352 | 2017-01-16 19:26:50 -0600 | [diff] [blame] | 9 | |
Dan Pasanen | 8320eb1 | 2018-01-26 10:26:47 -0600 | [diff] [blame] | 10 | export ADDOND_VERSION=2 |
| 11 | |
Dan Pasanen | 894b352 | 2017-01-16 19:26:50 -0600 | [diff] [blame] | 12 | # Scripts in /system/addon.d expect to find backuptool.functions in /tmp |
| 13 | mkdir -p /postinstall/tmp/ |
| 14 | cp -f /postinstall/system/bin/backuptool_ab.functions /postinstall/tmp/backuptool.functions |
| 15 | |
| 16 | # Preserve /system/addon.d in /tmp/addon.d |
| 17 | preserve_addon_d() { |
| 18 | if [ -d /system/addon.d/ ]; then |
| 19 | mkdir -p /postinstall/tmp/addon.d/ |
| 20 | cp -a /system/addon.d/* /postinstall/tmp/addon.d/ |
Dan Pasanen | 8320eb1 | 2018-01-26 10:26:47 -0600 | [diff] [blame] | 21 | |
| 22 | # Discard any scripts that aren't at least our version level |
| 23 | for f in /postinstall/tmp/addon.d/*sh; do |
| 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 | |
Dan Pasanen | 894b352 | 2017-01-16 19:26:50 -0600 | [diff] [blame] | 33 | chmod 755 /postinstall/tmp/addon.d/*.sh |
| 34 | fi |
| 35 | } |
| 36 | |
| 37 | # Restore /postinstall/system/addon.d from /postinstall/tmp/addon.d |
| 38 | restore_addon_d() { |
| 39 | if [ -d /postinstall/tmp/addon.d/ ]; then |
| 40 | mkdir -p /postinstall/system/addon.d/ |
| 41 | cp -a /postinstall/tmp/addon.d/* /postinstall/system/addon.d/ |
| 42 | rm -rf /postinstall/tmp/addon.d/ |
| 43 | fi |
| 44 | } |
| 45 | |
| 46 | # Proceed only if /system is the expected major and minor version |
| 47 | check_prereq() { |
| 48 | # If there is no build.prop file the partition is probably empty. |
| 49 | if [ ! -r /system/build.prop ]; then |
| 50 | return 0 |
| 51 | fi |
| 52 | |
| 53 | grep -q "^ro.omni.version=$V.*" /system/etc/prop.default /system/build.prop && return 1 |
| 54 | |
| 55 | echo "Not backing up files from incompatible version: $V" |
| 56 | return 0 |
| 57 | } |
| 58 | |
| 59 | check_blacklist() { |
| 60 | if [ -f /system/addon.d/blacklist -a -d /$1/addon.d/ ]; then |
| 61 | ## Discard any known bad backup scripts |
| 62 | cd /$1/addon.d/ |
| 63 | for f in *sh; do |
| 64 | [ -f $f ] || continue |
| 65 | s=$(md5sum $f | cut -c-32) |
| 66 | grep -q $s /system/addon.d/blacklist && rm -f $f |
| 67 | done |
| 68 | fi |
| 69 | } |
| 70 | |
| 71 | check_whitelist() { |
| 72 | found=0 |
| 73 | if [ -f /system/addon.d/whitelist ];then |
| 74 | ## forcefully keep any version-independent stuff |
| 75 | cd /$1/addon.d/ |
| 76 | for f in *sh; do |
| 77 | s=$(md5sum $f | cut -c-32) |
| 78 | grep -q $s /system/addon.d/whitelist |
| 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 | |
| 89 | # Execute /system/addon.d/*.sh scripts with $1 parameter |
| 90 | run_stage() { |
| 91 | if [ -d /postinstall/tmp/addon.d/ ]; then |
| 92 | for script in $(find /postinstall/tmp/addon.d/ -name '*.sh' |sort -n); do |
| 93 | # we have no /sbin/sh in android, only recovery |
| 94 | # use /system/bin/sh here instead |
| 95 | sed -i '0,/#!\/sbin\/sh/{s|#!/sbin/sh|#!/system/bin/sh|}' $script |
| 96 | # we can't count on /tmp existing on an A/B device, so utilize /postinstall/tmp |
| 97 | # as a pseudo-/tmp dir |
| 98 | sed -i 's|. /tmp/backuptool.functions|. /postinstall/tmp/backuptool.functions|g' $script |
| 99 | $script $1 |
| 100 | done |
| 101 | fi |
| 102 | } |
| 103 | |
| 104 | case "$1" in |
| 105 | backup) |
| 106 | mkdir -p $C |
| 107 | if check_prereq; then |
| 108 | if check_whitelist postinstall/system; then |
| 109 | exit 127 |
| 110 | fi |
| 111 | fi |
| 112 | check_blacklist postinstall/system |
| 113 | preserve_addon_d |
| 114 | run_stage pre-backup |
| 115 | run_stage backup |
| 116 | run_stage post-backup |
| 117 | ;; |
| 118 | restore) |
| 119 | if check_prereq; then |
| 120 | if check_whitelist postinstall/tmp; then |
| 121 | exit 127 |
| 122 | fi |
| 123 | fi |
| 124 | check_blacklist postinstall/tmp |
| 125 | run_stage pre-restore |
| 126 | run_stage restore |
| 127 | run_stage post-restore |
| 128 | restore_addon_d |
| 129 | rm -rf $C |
| 130 | rm -rf /postinstall/tmp |
| 131 | sync |
| 132 | ;; |
| 133 | *) |
| 134 | echo "Usage: $0 {backup|restore}" |
| 135 | exit 1 |
| 136 | esac |
| 137 | |
| 138 | exit 0 |