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