|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Import a database from /vagrant/sql/$1.sql |
| 4 | +# |
| 5 | +# Usage: box import-db {dump_file} [--echo] [--quiet] |
| 6 | +# |
| 7 | +# Result ==> Imports from /vagrant/sql/{dump_file}.sql |
| 8 | +# |
| 9 | +# --echo - Echos the backed-up filename |
| 10 | +# --quiet - Omits |
| 11 | + |
| 12 | +importfile="$1" |
| 13 | + |
| 14 | +if [ 'sql' != $(to_lowercase $(file_extension "${importfile}")) ]; then |
| 15 | + # |
| 16 | + # If no .sql extension, add one. |
| 17 | + # |
| 18 | + importfile="${importfile}.sql" |
| 19 | +fi |
| 20 | + |
| 21 | +# |
| 22 | +# Force import from /vagrant/sql/ |
| 23 | +# |
| 24 | +importfile="${WPLIB_BOX_SQL_DIR}/${importfile}" |
| 25 | + |
| 26 | +# |
| 27 | +# Check to see if we need to import from exists |
| 28 | +# |
| 29 | +if [ ! -f "${importfile}" ] ; then |
| 30 | + |
| 31 | + echo_if_not_quiet "$*", "=No ${importfile} exists.." |
| 32 | + return |
| 33 | + |
| 34 | +fi |
| 35 | + |
| 36 | +bakfile=$(box backup-db --quiet --echo-filename --dry-run) |
| 37 | + |
| 38 | +if [ "${bakfile}" == "${importfile}" ]; then |
| 39 | + |
| 40 | + echo_if_not_quiet "$*", "^\tERROR: The default backup filename is '$1'." |
| 41 | + echo_if_not_quiet "$*", "=\tCannot backup before import thus cannot continue." |
| 42 | + return |
| 43 | + |
| 44 | +fi |
| 45 | + |
| 46 | +echo_if_not_quiet "$*", "^Backing up database..." |
| 47 | + |
| 48 | +bakfile=$(box backup-db --quiet --echo-filename) |
| 49 | + |
| 50 | +if [ "" == "${bakfile}" ]; then |
| 51 | + |
| 52 | + echo_if_not_quiet "$*", "^Unknown error when generating the backup." |
| 53 | + return |
| 54 | + |
| 55 | +fi |
| 56 | + |
| 57 | +if [[ "$*" == *" error "* ]]; then |
| 58 | + |
| 59 | + echo_if_not_quiet "$*", "^Error when generating the backup: ${bakfile}" |
| 60 | + return |
| 61 | + |
| 62 | +fi |
| 63 | + |
| 64 | +echo_if_not_quiet "$*", "^Database backed-up to ${bakfile}..." |
| 65 | + |
| 66 | +echo_if_not_quiet "$*", "=Importing database from ${importfile}..." |
| 67 | + |
| 68 | +mysql -u wordpress -pwordpress wordpress < ${importfile} |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
0 commit comments