Andrew Dodd | 0c20901 | 2013-09-28 15:34:25 -0400 | [diff] [blame^] | 1 | #!/sbin/sh |
| 2 | # |
| 3 | # Backup and restore addon /system files |
| 4 | # |
| 5 | |
| 6 | export C=/tmp/backupdir |
| 7 | export S=/system |
| 8 | export V=4.3 |
| 9 | |
| 10 | # Preserve /system/addon.d in /tmp/addon.d |
| 11 | preserve_addon_d() { |
| 12 | mkdir -p /tmp/addon.d/ |
| 13 | cp -a /system/addon.d/* /tmp/addon.d/ |
| 14 | chmod 755 /tmp/addon.d/*.sh |
| 15 | } |
| 16 | |
| 17 | # Restore /system/addon.d in /tmp/addon.d |
| 18 | restore_addon_d() { |
| 19 | cp -a /tmp/addon.d/* /system/addon.d/ |
| 20 | rm -rf /tmp/addon.d/ |
| 21 | } |
| 22 | |
| 23 | # Proceed only if /system is the expected major and minor version |
| 24 | check_prereq() { |
| 25 | if ( ! grep -q "^ro.build.version.release=$V.*" /system/build.prop ); then |
| 26 | echo "Not backing up files from incompatible version: $V" |
| 27 | exit 127 |
| 28 | fi |
| 29 | } |
| 30 | |
| 31 | check_blacklist() { |
| 32 | if [ -f /system/addon.d/blacklist ];then |
| 33 | ## Discard any known bad backup scripts |
| 34 | cd /$1/addon.d/ |
| 35 | for f in *sh; do |
| 36 | s=$(md5sum $f | awk {'print $1'}) |
| 37 | grep -q $s /system/addon.d/blacklist && rm -f $f |
| 38 | done |
| 39 | fi |
| 40 | } |
| 41 | |
| 42 | # Execute /system/addon.d/*.sh scripts with $1 parameter |
| 43 | run_stage() { |
| 44 | for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do |
| 45 | $script $1 |
| 46 | done |
| 47 | } |
| 48 | |
| 49 | case "$1" in |
| 50 | backup) |
| 51 | mkdir -p $C |
| 52 | check_prereq |
| 53 | check_blacklist system |
| 54 | preserve_addon_d |
| 55 | run_stage pre-backup |
| 56 | run_stage backup |
| 57 | run_stage post-backup |
| 58 | ;; |
| 59 | restore) |
| 60 | check_prereq |
| 61 | check_blacklist tmp |
| 62 | run_stage pre-restore |
| 63 | run_stage restore |
| 64 | run_stage post-restore |
| 65 | restore_addon_d |
| 66 | rm -rf $C |
| 67 | sync |
| 68 | ;; |
| 69 | *) |
| 70 | echo "Usage: $0 {backup|restore}" |
| 71 | exit 1 |
| 72 | esac |
| 73 | |
| 74 | exit 0 |