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 |
Michael Bestas | 89450fb | 2016-10-26 22:35:18 +0300 | [diff] [blame] | 8 | export V=14.1 |
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 |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 37 | if ( ! grep -q "^ro.cm.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() { |
| 45 | if [ -f /system/addon.d/blacklist ];then |
| 46 | ## Discard any known bad backup scripts |
| 47 | cd /$1/addon.d/ |
| 48 | for f in *sh; do |
Tom Marshall | 517e691 | 2015-11-18 14:45:04 -0800 | [diff] [blame] | 49 | s=$(md5sum $f | cut -c-32) |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 50 | grep -q $s /system/addon.d/blacklist && rm -f $f |
| 51 | done |
| 52 | fi |
| 53 | } |
| 54 | |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 55 | check_whitelist() { |
| 56 | found=0 |
| 57 | if [ -f /system/addon.d/whitelist ];then |
| 58 | ## forcefully keep any version-independent stuff |
| 59 | cd /$1/addon.d/ |
| 60 | for f in *sh; do |
Tom Marshall | 517e691 | 2015-11-18 14:45:04 -0800 | [diff] [blame] | 61 | s=$(md5sum $f | cut -c-32) |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 62 | grep -q $s /system/addon.d/whitelist |
| 63 | if [ $? -eq 0 ]; then |
| 64 | found=1 |
| 65 | else |
| 66 | rm -f $f |
| 67 | fi |
| 68 | done |
| 69 | fi |
| 70 | return $found |
| 71 | } |
| 72 | |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 73 | # Execute /system/addon.d/*.sh scripts with $1 parameter |
| 74 | run_stage() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 75 | if [ -d /tmp/addon.d/ ]; then |
| 76 | for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do |
| 77 | $script $1 |
| 78 | done |
| 79 | fi |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 80 | } |
| 81 | |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 82 | case "$1" in |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 83 | backup) |
| 84 | mkdir -p $C |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 85 | if check_prereq; then |
| 86 | if check_whitelist system; then |
| 87 | exit 127 |
| 88 | fi |
| 89 | fi |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 90 | check_blacklist system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 91 | preserve_addon_d |
| 92 | run_stage pre-backup |
| 93 | run_stage backup |
| 94 | run_stage post-backup |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 95 | ;; |
| 96 | restore) |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 97 | if check_prereq; then |
| 98 | if check_whitelist tmp; then |
| 99 | exit 127 |
| 100 | fi |
| 101 | fi |
Ricardo Cerqueira | 129989b | 2012-12-19 01:11:21 +0000 | [diff] [blame] | 102 | check_blacklist tmp |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 103 | run_stage pre-restore |
| 104 | run_stage restore |
| 105 | run_stage post-restore |
| 106 | restore_addon_d |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 107 | rm -rf $C |
| 108 | sync |
| 109 | ;; |
| 110 | *) |
| 111 | echo "Usage: $0 {backup|restore}" |
| 112 | exit 1 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 113 | esac |
| 114 | |
| 115 | exit 0 |