Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Commit fc5a2d3

Browse files
committed
Merge branch 'master' of https://github.com/wplib/box-scripts
2 parents 0771484 + 3d92b89 commit fc5a2d3

File tree

6 files changed

+173
-13
lines changed

6 files changed

+173
-13
lines changed

guest/cli/box

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fi
2222

2323
if [ ! -f "${command_file}" ]; then
2424
echo
25-
echo "Command not found: $1"
25+
echo -e "\tCommand not found: $1"
2626
echo
2727
exit
2828
fi

guest/cli/commands/backup-db

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,27 @@
22
#
33
# Backup the database to /vagrant/sql/current.sql
44
#
5+
#
6+
# Usage: box import-db {dump_file} [--echo-filename] [--quiet]
7+
#
8+
# Result ==> Imports from /vagrant/sql/{dump_file}.sql
9+
#
10+
# --echo-filename - Echos the backed-up filename
11+
# --quiet - Omits status messages
12+
# --dry-run - Do everything except actually run it
13+
#
514
# Save the prior backup (if exists) to /vagrant/sql/previous.sql
615
# If /vagrant/sql/previous.sql save to /vagrant/sql/previous.2.sql, and so on.
716
#
817

918
bakfile="/vagrant/sql/current.sql"
1019

20+
if [ "true" == $(is_dry_run "$*") ]; then
21+
dryrun=1
22+
else
23+
dryrun=0
24+
fi
25+
1126
#
1227
# Check to see if we need to save a prior backup
1328
#
@@ -22,15 +37,36 @@ if [[ -f "${bakfile}" ]] ; then
2237
# Okay, we can now save the prior backup.
2338
#
2439
echo_if_not_quiet "$*", "=Saving Prior Backup as ${priorfile}..."
25-
cp "${bakfile}" "${priorfile}"
40+
41+
if [ $dryrun -eq 0 ]; then
42+
cp "${bakfile}" "${priorfile}"
43+
fi
2644

2745
fi
2846

2947
echo_if_not_quiet "$*", "=Backing up database to ${bakfile}..."
3048

31-
mkdir -p /vagrant/sql
49+
if [ $dryrun -eq 0 ]; then
50+
51+
mkdir -p /vagrant/sql
52+
53+
mysqldump -u wordpress -pwordpress wordpress > ${bakfile}
3254

33-
mysqldump -u wordpress -pwordpress wordpress > ${bakfile}
55+
result="$?"
56+
57+
if [ "${result}" != "0" ]; then
58+
59+
echo_if_not_quiet "$*", "=mysqldump error (${result})"
60+
return
61+
fi
62+
63+
fi
64+
65+
if [[ "$*" == *"--echo-filename"* ]]; then
66+
67+
echo "${bakfile}"
68+
69+
fi
3470

3571

3672

guest/cli/commands/import-db

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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: Cannot backup '$1' because the"
41+
echo_if_not_quiet "$*", "=\t default backup filename is '$1'"
42+
echo_if_not_quiet "$*", "^\t thus cannot backup before import."
43+
echo_if_not_quiet "$*", "=\t Rename ${importfile} to "
44+
echo_if_not_quiet "$*", "=\t bypass this check and allow import."
45+
return
46+
47+
fi
48+
49+
echo_if_not_quiet "$*", "^Backing up database..."
50+
51+
bakfile=$(box backup-db --quiet --echo-filename)
52+
53+
if [ "" == "${bakfile}" ]; then
54+
55+
echo_if_not_quiet "$*", "^Unknown error when generating the backup."
56+
return
57+
58+
fi
59+
60+
if [[ "$*" == *" error "* ]]; then
61+
62+
echo_if_not_quiet "$*", "^Error when generating the backup: ${bakfile}"
63+
return
64+
65+
fi
66+
67+
echo_if_not_quiet "$*", "^Database backed-up to ${bakfile}..."
68+
69+
echo_if_not_quiet "$*", "=Importing database from ${importfile}..."
70+
71+
mysql -u wordpress -pwordpress wordpress < ${importfile}
72+
73+
74+
75+
76+

guest/cli/commands/reassociate-git-repo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ mv "${WPLIB_BOX_ORIGINAL_GIT_DIR}" "${WPLIB_BOX_GIT_DIR}" 2> /dev/null
3333
grep -q -F "/watchers" "${WPLIB_BOX_DIR}/.gitignore" || echo "/watchers" >> ${WPLIB_BOX_DIR}/.gitignore
3434

3535

36-
echo_if_not_quiet "$*" "=The WPLib Box Git repository has been reassociated and/nthe prior .git directory has been renamed to ${WPLIB_BOX_SAVE_GIT_DIR}/n(assuming there was one.)"
36+
echo_if_not_quiet "$*" "=The WPLib Box Git repository has been reassociated and\nthe prior .git directory has been renamed to ${WPLIB_BOX_SAVE_GIT_DIR}\n(assuming there was one.)"

guest/cli/includes/constants

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export WPLIB_BOX_FLAG_FILE="WPLIB_BOX_GIT_REPO"
2323
export WPLIB_BOX_WP_CONTENT_DIR=$(find_wp_content_dir)
2424
export WPLIB_BOX_PLUGINS_DIR=$(find_wp_plugins_dir)
2525
export WPLIB_BOX_MU_PLUGINS_DIR=$(find_wp_mu_plugins_dir)
26+
27+
export WPLIB_BOX_SQL_DIR="/vagrant/sql"

guest/cli/includes/functions

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
#!/usr/bin/env bash
22

3+
#
4+
# Outputs the file extension given a file name
5+
#
6+
# $1 - String
7+
#
8+
# Usage: string=$(to_lowercase "FOO.BAR")
9+
# Result: $string ==> "foo.bar"
10+
#
11+
#
12+
function to_lowercase() {
13+
echo "${1,,}"
14+
}
15+
16+
#
17+
# Outputs the file extension given a file name
18+
#
19+
# $1 - Filename
20+
#
21+
# Usage: extension=$(file_extension "foo.bar")
22+
# Result: $extension ==> "bar"
23+
#
24+
#
25+
function file_extension() {
26+
echo "${1##*.}"
27+
}
28+
29+
#
30+
# Outputs the base filename given a full filename
31+
# Base filename means no path and no extension
32+
#
33+
# $1 - Filename
34+
#
35+
# Usage: basename=$(file_basename "foo/bar.bar")
36+
# Result: $extension ==> "bar"
37+
#
38+
function file_basename() {
39+
filename=$(basename "$1")
40+
echo "${filename%.*}"
41+
}
342

443
#
544
# Returns a filename that does not exist given an existing filename.
@@ -21,8 +60,8 @@ function next_backup_file() {
2160
# Get the file extension if there is one
2261
# See: http://stackoverflow.com/a/965072/102699
2362
#
24-
ext=".${bakfile##*.}"
25-
if [[ "." == "${ext}" || "${ext}" == ".${bakfile}" ]]; then
63+
ext=$(file_extension "${bakfile}")
64+
if [[ "" == "${ext}" || "${ext}" == "${bakfile}" ]]; then
2665
ext=""
2766
fi
2867

@@ -37,15 +76,22 @@ function next_backup_file() {
3776
# Now check to see if the default prior backup exists
3877
# Continue checking until we find a workable filename
3978
#
40-
test="${bakfile}${ext}"
79+
test="${bakfile}.${ext}"
4180
until [ ! -e "${test}" ]; do
4281
counter=$((counter))
4382
let counter++
44-
test="${bakfile}${counter}${ext}"
83+
test="${bakfile}${counter}.${ext}"
4584
done
4685
echo "${test}"
4786
}
4887

88+
function is_dry_run() {
89+
if [[ "$1" == *"--dry-run"* ]]; then
90+
echo "true"
91+
return
92+
fi
93+
echo "false"
94+
}
4995

5096
function is_quiet() {
5197
if [[ "$1" == *"--quiet"* ]]; then
@@ -76,20 +122,20 @@ function echo_if_not_quiet() {
76122
message=${2:1}
77123
case "${control}" in
78124
"^")
79-
echo "${message}" | sed "${regex}"
125+
echo -e "${message}" | sed "${regex}"
80126
echo
81127
;;
82128
"*")
83129
echo
84-
echo "${message}" | sed "${regex}"
130+
echo -e "${message}" | sed "${regex}"
85131
echo
86132
;;
87133
"=")
88-
echo "${message}" | sed "${regex}"
134+
echo -e "${message}" | sed "${regex}"
89135
;;
90136
"&")
91137
echo
92-
echo "${message}" | sed "${regex}"
138+
echo -e "${message}" | sed "${regex}"
93139
;;
94140
*)
95141
echo

0 commit comments

Comments
 (0)