Skip to content

Commit 21b7582

Browse files
committed
Merge branch 'release/v1.4.0'
2 parents 86ca6b6 + c1ffdb5 commit 21b7582

12 files changed

+117
-97
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.4.0] - 2020-09-02
10+
### Added
11+
- wordpress flag (disabled by default) to backup wordpress database and imagery.
12+
13+
### Changed
14+
- Now using n98 to backup WordPress database to improve speed of system.
15+
16+
917
## [1.3.0] - 2020-07-29
1018
### Added
1119
- local-backup option (enabled by default)

MENU.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Use the below to generate the menu.sh file from the site [https://argbash.io/gen
1010
#
1111
# ARG_OPTIONAL_BOOLEAN([local-backup], [b], [perform a backup before importing remote], [off])
1212
# ARG_OPTIONAL_BOOLEAN([full], [f], [full database dump])
13+
# ARG_OPTIONAL_BOOLEAN([wordpress], [w], [include wordpress content], [off])
1314
# ARG_VERSION([echo test v$version])
1415
# ARG_HELP([The general script's help msg])
1516
# ARGBASH_GO

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Magento 2 Production DB Sync v1.3.0
1+
# Magento 2 Production DB Sync v1.4.0
22

33
## About
44
A script to copy over a production database to another server, also has the ability to copy across imagery.
55

6-
Currently the script is configured to attempt Magento 2 and WordPress database migrations and assumes you have your WordPress database details configured in the `magento/app/etc/env.php` file as a second `connection`.
6+
Currently the script is configured to attempt Magento 2 and WordPress database migrations and assumes you have your WordPress database details configured in the `magento/app/etc/env.php` file as a second `connection` named `wordpress`.
77

88
## Requirements
99
- You need curl installed locally.
@@ -15,7 +15,7 @@ Currently the script is configured to attempt Magento 2 and WordPress database m
1515
## Installing and Updating
1616
To install or update the script run the following curl script
1717

18-
```curl -o- https://raw.githubusercontent.com/clivewalkden/bash-magento2-db-sync/v1.3.0/install.sh | bash```
18+
```curl -o- https://raw.githubusercontent.com/clivewalkden/bash-magento2-db-sync/v1.4.0/install.sh | bash```
1919

2020
## Usage
2121
To copy over a production database first get a shell on the system you want to copy the data to.

db-sync.sh

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ source "${dbsyncutil}menu.sh"
1313

1414
# Make sure we are in a Magento directory
1515
if [ ! -f "bin/magento" ]; then
16-
echo -e "${bg_red} ${txt_end}"
16+
echo -e "${bg_red}${txt_white} ${txt_end}"
1717
echo -e "${bg_red}${txt_white} You are not currently in a Magento directory ${txt_end}"
18-
echo -e "${bg_red} ${txt_end}"
18+
echo -e "${bg_red}${txt_white} ${txt_end}"
1919
exit
2020
fi
2121

@@ -27,7 +27,7 @@ echo -e "${bg_black}${txt_white} Copy over a production database to staging ${
2727
echo -e "${bg_black}${txt_white} ${txt_end}"
2828

2929
# Check for a local config file
30-
echo -e "\n${txt_white}${bg_black}Checking for local configuration file (${conf_file}).${txt_end}\n"
30+
echo -e "${txt_blue} Checking for local configuration file (${conf_file}). ${txt_end}"
3131
if [[ -f "$conf_file" ]]; then
3232
. "$conf_file"
3333
fi
@@ -79,7 +79,10 @@ while true; do
7979
esac
8080
done
8181

82-
echo -e "\n${txt_white}${bg_black}Run the Magento configuration.${txt_end}\n"
82+
echo -e "${bg_black}${txt_white} ${txt_end}"
83+
echo -e "${bg_black}${txt_white} Run the Magento configuration. ${txt_end}"
84+
echo -e "${bg_black}${txt_white} ${txt_end}"
85+
8386
n98-magerun setup:upgrade
8487
echo
8588
n98-magerun deploy:mode:set developer
@@ -90,4 +93,6 @@ n98-magerun indexer:reindex
9093
echo
9194
n98-magerun cache:flush
9295

93-
echo -e "\n${txt_green}Database migrated: ${domain}${txt_end}\n"
96+
echo -e "${bg_green}${txt_white}${txt_bold} ${txt_end}"
97+
echo -e "${bg_green}${txt_white}${txt_bold} Database migrated: ${txt_yellow}${domain} ${txt_end}"
98+
echo -e "${bg_green}${txt_white}${txt_bold} ${txt_end}"

install.sh

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,87 +2,87 @@
22

33
{ # this ensures the entire script is downloaded #
44

5-
dbsync_install_dir() {
5+
dbsync_install_dir() {
66
printf %s "${HOME}/.dbsync"
7-
}
8-
9-
dbsync_latest_version() {
10-
echo "v1.3.0"
11-
}
12-
13-
#
14-
# Outputs the location to DB Sync depending on:
15-
# * The availability of $GIT_SOURCE
16-
#
17-
dbsync_source() {
18-
local DBSYNC_METHOD
19-
DBSYNC_METHOD="$1"
20-
local DBSYNC_SOURCE_URL
21-
DBSYNC_SOURCE_URL="https://github.com/clivewalkden/bash-magento2-db-sync.git"
22-
echo "$DBSYNC_SOURCE_URL"
23-
}
24-
25-
do_install() {
7+
}
8+
9+
dbsync_latest_version() {
10+
echo "v1.4.0"
11+
}
12+
13+
#
14+
# Outputs the location to DB Sync depending on:
15+
# * The availability of $GIT_SOURCE
16+
#
17+
dbsync_source() {
18+
local DBSYNC_METHOD
19+
DBSYNC_METHOD="$1"
20+
local DBSYNC_SOURCE_URL
21+
DBSYNC_SOURCE_URL="https://github.com/clivewalkden/bash-magento2-db-sync.git"
22+
echo "$DBSYNC_SOURCE_URL"
23+
}
24+
25+
do_install() {
2626
local INSTALL_DIR
2727
INSTALL_DIR="$(dbsync_install_dir)"
2828

2929
# Downloading to $INSTALL_DIR
3030
mkdir -p "$INSTALL_DIR"
3131
if [ -f "$INSTALL_DIR/db-sync.sh" ]; then
32-
echo "=> db-sync is already installed in $INSTALL_DIR, trying to update the script"
32+
echo "=> db-sync is already installed in $INSTALL_DIR, trying to update the script"
3333
else
34-
echo "=> Downloading db-sync as script to '$INSTALL_DIR'"
34+
echo "=> Downloading db-sync as script to '$INSTALL_DIR'"
3535
fi
3636

3737
if [ -d "$INSTALL_DIR/.git" ]; then
38-
echo "=> db-sync is already installed in $INSTALL_DIR, trying to update using git"
39-
command printf '\r=> '
40-
command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin tag "$(dbsync_latest_version)" --depth=1 2> /dev/null || {
38+
echo "=> db-sync is already installed in $INSTALL_DIR, trying to update using git"
39+
command printf '\r=> '
40+
command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin tag "$(dbsync_latest_version)" --depth=1 2>/dev/null || {
4141
echo >&2 "Failed to update db-sync, run 'git fetch' in $INSTALL_DIR yourself."
4242
exit 1
43-
}
43+
}
4444
else
45-
# Cloning to $INSTALL_DIR
46-
echo "=> Downloading db-sync from git to '$INSTALL_DIR'"
47-
command printf '\r=> '
48-
mkdir -p "${INSTALL_DIR}"
49-
if [ "$(ls -A "${INSTALL_DIR}")" ]; then
45+
# Cloning to $INSTALL_DIR
46+
echo "=> Downloading db-sync from git to '$INSTALL_DIR'"
47+
command printf '\r=> '
48+
mkdir -p "${INSTALL_DIR}"
49+
if [ "$(ls -A "${INSTALL_DIR}")" ]; then
5050
command git init "${INSTALL_DIR}" || {
51-
echo >&2 'Failed to initialize db-sync repo. Please report this!'
52-
exit 2
51+
echo >&2 'Failed to initialize db-sync repo. Please report this!'
52+
exit 2
5353
}
54-
command git --git-dir="${INSTALL_DIR}/.git" remote add origin "$(dbsync_source)" 2> /dev/null \
55-
|| command git --git-dir="${INSTALL_DIR}/.git" remote set-url origin "$(dbsync_source)" || {
56-
echo >&2 'Failed to add remote "origin" (or set the URL). Please report this!'
57-
exit 2
54+
command git --git-dir="${INSTALL_DIR}/.git" remote add origin "$(dbsync_source)" 2>/dev/null ||
55+
command git --git-dir="${INSTALL_DIR}/.git" remote set-url origin "$(dbsync_source)" || {
56+
echo >&2 'Failed to add remote "origin" (or set the URL). Please report this!'
57+
exit 2
5858
}
5959
command git --git-dir="${INSTALL_DIR}/.git" fetch origin tag "$(dbsync_latest_version)" --depth=1 || {
60-
echo >&2 'Failed to fetch origin with tags. Please report this!'
61-
exit 2
60+
echo >&2 'Failed to fetch origin with tags. Please report this!'
61+
exit 2
6262
}
63-
else
63+
else
6464
command git -c advice.detachedHead=false clone "$(dbsync_source)" -b "$(dbsync_latest_version)" --depth=1 "${INSTALL_DIR}" || {
65-
echo >&2 'Failed to clone dbsync repo. Please report this!'
66-
exit 2
65+
echo >&2 'Failed to clone dbsync repo. Please report this!'
66+
exit 2
6767
}
68-
fi
68+
fi
6969
fi
7070
command git -c advice.detachedHead=false --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" checkout -f --quiet "$(dbsync_latest_version)"
7171
if [ -n "$(command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" show-ref refs/heads/master)" ]; then
72-
if command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet 2>/dev/null; then
72+
if command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet 2>/dev/null; then
7373
command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet -D master >/dev/null 2>&1
74-
else
74+
else
7575
echo >&2 "Your version of git is out of date. Please update it!"
7676
command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch -D master >/dev/null 2>&1
77-
fi
77+
fi
7878
fi
7979

8080
echo "=> Compressing and cleaning up git repository"
8181
if ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" reflog expire --expire=now --all; then
82-
echo >&2 "Your version of git is out of date. Please update it!"
82+
echo >&2 "Your version of git is out of date. Please update it!"
8383
fi
84-
if ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" gc --auto --aggressive --prune=now ; then
85-
echo >&2 "Your version of git is out of date. Please update it!"
84+
if ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" gc --auto --aggressive --prune=now; then
85+
echo >&2 "Your version of git is out of date. Please update it!"
8686
fi
8787

8888
local PROFILE_INSTALL_DIR
@@ -93,7 +93,7 @@ do_install() {
9393

9494
if ! command grep -qc '/db-sync.sh' "${DBSYNC_PROFILE}"; then
9595
echo "=> Appending db-sync source string to ${DBSYNC_PROFILE}"
96-
command printf "${SOURCE_STR}" >> "${DBSYNC_PROFILE}"
96+
command printf "${SOURCE_STR}" >>"${DBSYNC_PROFILE}"
9797
else
9898
echo "=> db-sync source string already in ${DBSYNC_PROFILE}"
9999
fi
@@ -103,16 +103,16 @@ do_install() {
103103
dbsync_reset
104104

105105
echo "=> Close and reopen your terminal to start using db-sync.sh"
106-
}
106+
}
107107

108-
#
109-
# Unsets the various functions defined
110-
# during the execution of the install script
111-
#
112-
dbsync_reset() {
113-
unset -f dbsync_install_dir dbsync_latest_version dbsync_source do_install
114-
}
108+
#
109+
# Unsets the various functions defined
110+
# during the execution of the install script
111+
#
112+
dbsync_reset() {
113+
unset -f dbsync_install_dir dbsync_latest_version dbsync_source do_install
114+
}
115115

116-
do_install
116+
do_install
117117

118-
} # this ensures the entire script is downloaded #
118+
} # this ensures the entire script is downloaded #

utils/local_backup.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ if [ $_arg_local_backup == 'on' ]; then
66

77
n98-magerun --root-dir="${PWD}" db:dump --compression="gzip" --force "../backups/$DATESTAMP-m2.sql.gz"
88

9-
if [ ! -z $DB_WP_HOST ]; then
10-
mysqldump -u$DB_WP_USER -p$DB_WP_PASS $DB_WP_DBASE>"../backups/$DATESTAMP-wp.sql"
11-
gzip "../backups/$DATESTAMP-wp.sql"
9+
if [ $_arg_wordpress == 'on' ]; then
10+
n98-magerun --root-dir="${PWD}" db:dump --compression="gzip" --connection="wordpress" --force "../backups/$DATESTAMP-wp.sql.gz"
1211
fi
1312
else
1413
# No local backup

utils/local_imagery_import.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
echo -e "\n${txt_white}${bg_black}Importing imagery from live.${txt_end}\n"
55

66
# Set the default directories to transfer
7-
source=("/pub/media" "/wp/wp-content/uploads")
8-
dest=("./pub/" "./wp/wp-content/")
7+
source=("/pub/media/" "/wp/wp-content/uploads/")
8+
dest=("./pub/media/" "./wp/wp-content/uploads/")
99

1010
for i in "${!source[@]}"; do
1111
rsync -rlDhP --exclude 'cache*' -e "ssh -p${remote_port}" "$remote_username@$remote_host:$remote_shared_deployment_dir${source[$i]}" "${dest[$i]}"

utils/local_import.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ echo -e "\n${txt_white}${bg_black}Importing into local database(s).${txt_end}\n"
55

66
mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_DBASE<../backups/latest-m2.sql
77

8-
if existRemoteFile "$remote_wp_db_file"; then
8+
if [ $_arg_wordpress == 'on' ]; then
99
mysql -h$DB_WP_HOST -u$DB_WP_USER -p$DB_WP_PASS $DB_WP_DBASE<../backups/latest-wp.sql
1010
fi

utils/menu.sh

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
version="1.3.0"
2+
version="1.4.0"
33

44
die()
55
{
@@ -24,6 +24,7 @@ begins_with_short_option()
2424
# THE DEFAULTS INITIALIZATION - OPTIONALS
2525
_arg_full="off"
2626
_arg_local_backup="off"
27+
_arg_wordpress="off"
2728

2829
dbsync_print_help() {
2930
cat <<HEREDOC
@@ -34,10 +35,11 @@ dbsync_print_help() {
3435
3536
Make sure you are in a Magento 2 directory before trying to run any scripts.
3637
37-
Usage: db-sync.sh [-b|--(no-)local-backup] [-f|--(no-)full] [-v|--version] [-h|--help]
38+
Usage: db-sync.sh [-b|--local-backup] [-f|--full] [-w|--wordpress] [-v|--version] [-h|--help]
3839
Options:
39-
-b, --local-backup, --no-local-backup: perform a backup before importing remote (off by default)
40-
-f, --full, --no-full: full database dump (off by default)
40+
-b, --local-backup: perform a backup before importing remote (off by default)
41+
-f, --full: full database dump (off by default)
42+
-w, --wordpress: include wordpress content (off by default)"
4143
-v, --version: Prints version
4244
-h, --help: Prints help
4345
@@ -75,6 +77,18 @@ parse_commandline()
7577
{ begins_with_short_option "$_next" && shift && set -- "-f" "-${_next}" "$@"; } || die "The short option '$_key' can't be decomposed to ${_key:0:2} and -${_key:2}, because ${_key:0:2} doesn't accept value and '-${_key:2:1}' doesn't correspond to a short option."
7678
fi
7779
;;
80+
-w|--no-wordpress|--wordpress)
81+
_arg_wordpress="on"
82+
test "${1:0:5}" = "--no-" && _arg_wordpress="off"
83+
;;
84+
-w*)
85+
_arg_wordpress="on"
86+
_next="${_key##-w}"
87+
if test -n "$_next" -a "$_next" != "$_key"
88+
then
89+
{ begins_with_short_option "$_next" && shift && set -- "-w" "-${_next}" "$@"; } || die "The short option '$_key' can't be decomposed to ${_key:0:2} and -${_key:2}, because ${_key:0:2} doesn't accept value and '-${_key:2:1}' doesn't correspond to a short option."
90+
fi
91+
;;
7892
-v|--version)
7993
echo db-sync.sh - v$version
8094
exit 0

utils/remote_backup.sh

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ ssh -p "${remote_port}" "${remote_username}@${remote_host}" <<ENDSSH
1010
mkdir -p $remote_backup_dir
1111
cd $remote_magento_dir
1212
13-
REMOTE_DB_HOST=$(n98-magerun config:env:show db.connection.default.host)
14-
REMOTE_DB_USER=$(n98-magerun config:env:show db.connection.default.username)
15-
REMOTE_DB_PASS=$(n98-magerun config:env:show db.connection.default.password)
16-
REMOTE_DB_DBASE=$(n98-magerun config:env:show db.connection.default.dbname)
17-
1813
REMOTE_DB_WP_HOST=$(n98-magerun config:env:show db.connection.wordpress.host)
1914
REMOTE_DB_WP_USER=$(n98-magerun config:env:show db.connection.wordpress.username)
2015
REMOTE_DB_WP_PASS=$(n98-magerun config:env:show db.connection.wordpress.password)
@@ -26,8 +21,7 @@ else
2621
n98-magerun db:dump --compression="gzip" --strip="@log @sessions @trade @sales" --force $remote_backup_dir/latest-m2.sql.gz
2722
fi
2823
29-
if [ ! -z $REMOTE_DB_WP_HOST ]; then
30-
mysqldump -h$REMOTE_DB_WP_HOST -u$REMOTE_DB_WP_USER -p$REMOTE_DB_WP_PASS $REMOTE_DB_WP_DBASE>$remote_backup_dir/latest-wp.sql
31-
gzip -f $remote_backup_dir/latest-wp.sql
24+
if [ $_arg_wordpress == 'on' ]; then
25+
n98-magerun db:dump --compression="gzip" --conneciton="wordpress" --force $remote_backup_dir/latest-wp.sql.gz
3226
fi
3327
ENDSSH

0 commit comments

Comments
 (0)