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 |
| 7 | export S=/system |
Rashed Abdel-Tawab | 6326caa | 2018-08-07 10:49:49 -0700 | [diff] [blame] | 8 | export V=16.0 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 9 | |
Dan Pasanen | 524fe03 | 2018-01-26 10:26:47 -0600 | [diff] [blame] | 10 | export ADDOND_VERSION=1 |
| 11 | |
Tom Marshall | 65aff66 | 2014-12-12 11:56:04 -0800 | [diff] [blame] | 12 | # Scripts in /system/addon.d expect to find backuptool.functions in /tmp |
| 13 | cp -f /tmp/install/bin/backuptool.functions /tmp |
| 14 | |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 15 | # Preserve /system/addon.d in /tmp/addon.d |
| 16 | preserve_addon_d() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 17 | if [ -d /system/addon.d/ ]; then |
| 18 | mkdir -p /tmp/addon.d/ |
| 19 | cp -a /system/addon.d/* /tmp/addon.d/ |
Dan Pasanen | 524fe03 | 2018-01-26 10:26:47 -0600 | [diff] [blame] | 20 | |
| 21 | # Discard any scripts that aren't at least our version level |
| 22 | for f in /postinstall/tmp/addon.d/*sh; do |
| 23 | SCRIPT_VERSION=$(grep "^# ADDOND_VERSION=" $f | cut -d= -f2) |
| 24 | if [ -z "$SCRIPT_VERSION" ]; then |
| 25 | SCRIPT_VERSION=1 |
| 26 | fi |
| 27 | if [ $SCRIPT_VERSION -lt $ADDOND_VERSION ]; then |
| 28 | rm $f |
| 29 | fi |
| 30 | done |
| 31 | |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 32 | chmod 755 /tmp/addon.d/*.sh |
| 33 | fi |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 34 | } |
| 35 | |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 36 | # Restore /system/addon.d from /tmp/addon.d |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 37 | restore_addon_d() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 38 | if [ -d /tmp/addon.d/ ]; then |
Adrian DC | 13512fb | 2016-10-23 18:37:13 +0200 | [diff] [blame] | 39 | mkdir -p /system/addon.d/ |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 40 | cp -a /tmp/addon.d/* /system/addon.d/ |
| 41 | rm -rf /tmp/addon.d/ |
| 42 | fi |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 43 | } |
| 44 | |
Chirayu Desai | 700fc8f | 2012-12-13 19:36:37 +0530 | [diff] [blame] | 45 | # Proceed only if /system is the expected major and minor version |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 46 | check_prereq() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 47 | # If there is no build.prop file the partition is probably empty. |
| 48 | if [ ! -r /system/build.prop ]; then |
| 49 | return 0 |
| 50 | fi |
Adrian DC | 05ab72a | 2018-05-08 15:12:16 +0200 | [diff] [blame] | 51 | if ! grep -q "^ro.lineage.version=$V.*" /system/build.prop; then |
Adrian DC | 29fca64 | 2018-05-17 23:06:24 +0200 | [diff] [blame] | 52 | echo "Not backing up files from incompatible version: $V" |
| 53 | return 0 |
| 54 | fi |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 55 | return 1 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 58 | check_blacklist() { |
Gabriele M | 4fe3509 | 2017-06-03 18:08:39 +0200 | [diff] [blame] | 59 | if [ -f /system/addon.d/blacklist -a -d /$1/addon.d/ ]; then |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 60 | ## Discard any known bad backup scripts |
| 61 | cd /$1/addon.d/ |
| 62 | for f in *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) |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 65 | grep -q $s /system/addon.d/blacklist && rm -f $f |
| 66 | done |
| 67 | fi |
| 68 | } |
| 69 | |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 70 | check_whitelist() { |
| 71 | found=0 |
| 72 | if [ -f /system/addon.d/whitelist ];then |
| 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) |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 77 | grep -q $s /system/addon.d/whitelist |
| 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 | |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 97 | case "$1" in |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 98 | backup) |
| 99 | mkdir -p $C |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 100 | if check_prereq; then |
| 101 | if check_whitelist system; then |
| 102 | exit 127 |
| 103 | fi |
| 104 | fi |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 105 | check_blacklist system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 106 | preserve_addon_d |
| 107 | run_stage pre-backup |
| 108 | run_stage backup |
| 109 | run_stage post-backup |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 110 | ;; |
| 111 | restore) |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 112 | if check_prereq; then |
| 113 | if check_whitelist tmp; then |
| 114 | exit 127 |
| 115 | fi |
| 116 | fi |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 117 | check_blacklist tmp |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 118 | run_stage pre-restore |
| 119 | run_stage restore |
| 120 | run_stage post-restore |
| 121 | restore_addon_d |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 122 | rm -rf $C |
| 123 | sync |
| 124 | ;; |
| 125 | *) |
| 126 | echo "Usage: $0 {backup|restore}" |
| 127 | exit 1 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 128 | esac |
| 129 | |
| 130 | exit 0 |