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 |
Luca Stefani | 5c60e4f | 2017-08-17 19:28:48 +0200 | [diff] [blame] | 8 | export V=15.0 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 9 | |
Tom Marshall | 65aff66 | 2014-12-12 11:56:04 -0800 | [diff] [blame] | 10 | # Scripts in /system/addon.d expect to find backuptool.functions in /tmp |
| 11 | cp -f /tmp/install/bin/backuptool.functions /tmp |
| 12 | |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 13 | # Preserve /system/addon.d in /tmp/addon.d |
| 14 | preserve_addon_d() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 15 | if [ -d /system/addon.d/ ]; then |
| 16 | mkdir -p /tmp/addon.d/ |
| 17 | cp -a /system/addon.d/* /tmp/addon.d/ |
| 18 | chmod 755 /tmp/addon.d/*.sh |
| 19 | fi |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 20 | } |
| 21 | |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 22 | # Restore /system/addon.d from /tmp/addon.d |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 23 | restore_addon_d() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 24 | if [ -d /tmp/addon.d/ ]; then |
Adrian DC | 13512fb | 2016-10-23 18:37:13 +0200 | [diff] [blame] | 25 | mkdir -p /system/addon.d/ |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 26 | cp -a /tmp/addon.d/* /system/addon.d/ |
| 27 | rm -rf /tmp/addon.d/ |
| 28 | fi |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 29 | } |
| 30 | |
Chirayu Desai | 700fc8f | 2012-12-13 19:36:37 +0530 | [diff] [blame] | 31 | # Proceed only if /system is the expected major and minor version |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 32 | check_prereq() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 33 | # If there is no build.prop file the partition is probably empty. |
| 34 | if [ ! -r /system/build.prop ]; then |
| 35 | return 0 |
| 36 | fi |
Luca Stefani | 5c60e4f | 2017-08-17 19:28:48 +0200 | [diff] [blame] | 37 | if ( ! grep -q "^ro.lineage.version=$V.*" /system/build.prop ); then |
Chirayu Desai | 700fc8f | 2012-12-13 19:36:37 +0530 | [diff] [blame] | 38 | echo "Not backing up files from incompatible version: $V" |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 39 | return 0 |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 40 | fi |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 41 | return 1 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 44 | check_blacklist() { |
Gabriele M | 4fe3509 | 2017-06-03 18:08:39 +0200 | [diff] [blame] | 45 | if [ -f /system/addon.d/blacklist -a -d /$1/addon.d/ ]; then |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 46 | ## Discard any known bad backup scripts |
| 47 | cd /$1/addon.d/ |
| 48 | for f in *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) |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 51 | grep -q $s /system/addon.d/blacklist && rm -f $f |
| 52 | done |
| 53 | fi |
| 54 | } |
| 55 | |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 56 | check_whitelist() { |
| 57 | found=0 |
| 58 | if [ -f /system/addon.d/whitelist ];then |
| 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) |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 63 | grep -q $s /system/addon.d/whitelist |
| 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 | |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 83 | case "$1" in |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 84 | backup) |
| 85 | mkdir -p $C |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 86 | if check_prereq; then |
| 87 | if check_whitelist system; then |
| 88 | exit 127 |
| 89 | fi |
| 90 | fi |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 91 | check_blacklist system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 92 | preserve_addon_d |
| 93 | run_stage pre-backup |
| 94 | run_stage backup |
| 95 | run_stage post-backup |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 96 | ;; |
| 97 | restore) |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 98 | if check_prereq; then |
| 99 | if check_whitelist tmp; then |
| 100 | exit 127 |
| 101 | fi |
| 102 | fi |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 103 | check_blacklist tmp |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 104 | run_stage pre-restore |
| 105 | run_stage restore |
| 106 | run_stage post-restore |
| 107 | restore_addon_d |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 108 | rm -rf $C |
| 109 | sync |
| 110 | ;; |
| 111 | *) |
| 112 | echo "Usage: $0 {backup|restore}" |
| 113 | exit 1 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 114 | esac |
| 115 | |
| 116 | exit 0 |