Skip to content

Commit fb52242

Browse files
committed
Initial Commit
0 parents  commit fb52242

12 files changed

+405
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Magento 2 Production DB Sync
2+
3+
A script to copy over a production database to a staging server.
4+
5+
You can save a configuration file in the Magento directory to save answering some of the questions.
6+
7+
```bash
8+
remote_host=cotswoldcollections.com
9+
remote_domain=www.cotswoldcollections.com
10+
remote_port=22
11+
remote_username=magento
12+
remote_magento_dir=/opt/magento/magento2
13+
remote_backup_dir=/opt/magento/backups
14+
remote_shared_deployment_dir=/opt/magento/deployment/shared/magento
15+
```
16+
17+
| Notes: Make sure you've connected to the host machine with the -A flag so that your ssh agent is forwarded to allow additional connections.

db-sync.sh

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env bash
2+
3+
## Assumptions
4+
# A ssh key is available to connect to the box
5+
#
6+
7+
# set path to allow calling util subdir files
8+
source "set_util"
9+
10+
# Make sure we are in a Magento directory
11+
if [ ! -f "bin/magento" ]; then
12+
echo -e "${bg_red} ${txt_end}"
13+
echo -e "${bg_red}${txt_white} You are not currently in a Magento directory ${txt_end}"
14+
echo -e "${bg_red} ${txt_end}"
15+
exit
16+
fi
17+
18+
# Set global paths and functions
19+
source "${util}set_constants.sh"
20+
21+
echo -e "${bg_black}${txt_white} ${txt_end}"
22+
echo -e "${bg_black}${txt_white} Copy over a production database to staging ${txt_end}"
23+
echo -e "${bg_black}${txt_white} ${txt_end}"
24+
25+
# Check for a local config file
26+
echo -e "\n${txt_white}${bg_black}Checking for local configuration file (${conf_file}).${txt_end}\n"
27+
if [[ -f "$conf_file" ]]; then
28+
. "$conf_file"
29+
fi
30+
31+
set -e
32+
33+
# Set ${remote_host}
34+
source "${util}set_remote_host.sh"
35+
36+
# Test Connection
37+
while true; do
38+
echo -e "\n"
39+
read -p "Would you like to test the connection? " yn
40+
41+
case $yn in
42+
[Yy]* ) source "${util}ssh_connection_test.sh"; break;;
43+
[Nn]* ) break;;
44+
* ) echo "Please answer yes or no.";;
45+
esac
46+
done
47+
48+
# Run the remote backup
49+
source "${util}remote_backup.sh"
50+
source "${util}remote_retrieve.sh"
51+
source "${util}local_backup.sh"
52+
source "${util}url_update.sh"
53+
54+
# Import the database(s)
55+
while true; do
56+
echo -e "\n"
57+
read -p "Are you ready to import databases to local system? " yn
58+
59+
case $yn in
60+
[Yy]* ) source "${util}local_import.sh"; break;;
61+
[Nn]* ) break;;
62+
* ) echo "Please answer yes or no.";;
63+
esac
64+
done
65+
66+
# Import latest imagery
67+
while true; do
68+
echo -e "\n"
69+
read -p "Do you want to download the latest imagery? " yn
70+
71+
case $yn in
72+
[Yy]* ) source "${util}local_imagery_import.sh"; break;;
73+
[Nn]* ) break;;
74+
* ) echo "Please answer yes or no.";;
75+
esac
76+
done
77+
78+
echo -e "\n${txt_white}${bg_black}Run the Magento configuration.${txt_end}\n"
79+
n98-magerun setup:upgrade
80+
echo
81+
n98-magerun deploy:mode:set developer
82+
echo
83+
n98-magerun setup:di:compile
84+
echo
85+
n98-magerun indexer:reindex
86+
echo
87+
n98-magerun cache:flush
88+
89+
echo -e "\n${txt_green}Database migrated: ${domain}${txt_end}\n"

set_util

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
util="$( dirname ${BASH_SOURCE[0]} )/utils/"

utils/local_backup.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
# Backup the current local database
4+
echo -e "\n${txt_white}${bg_black}Backing up local database(s).${txt_end}\n"
5+
6+
n98-magerun --root-dir="${PWD}" db:dump --compression="gzip" --force "../backups/$DATESTAMP-m2.sql.gz"
7+
8+
if [ ! -z $DB_WP_HOST ]; then
9+
mysqldump -u$DB_WP_USER -p$DB_WP_PASS $DB_WP_DBASE>"../backups/$DATESTAMP-wp.sql"
10+
gzip "../backups/$DATESTAMP-wp.sql"
11+
fi

utils/local_imagery_import.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
# Backup the current local database
4+
echo -e "\n${txt_white}${bg_black}Importing imagery from live.${txt_end}\n"
5+
6+
# Set the default directories to transfer
7+
source=("/pub/media" "/wp/wp-content/uploads")
8+
dest=("./pub/" "./wp/wp-content/")
9+
10+
for i in "${!source[@]}"; do
11+
rsync -rlDhP --exclude 'cache*' -e "ssh -p${remote_port}" "$remote_username@$remote_host:$remote_shared_deployment_dir${source[$i]}" "${dest[$i]}"
12+
done

utils/local_import.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
# Backup the current local database
4+
echo -e "\n${txt_white}${bg_black}Importing into local database(s).${txt_end}\n"
5+
6+
mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_DBASE<../backups/latest-m2.sql
7+
8+
if existRemoteFile "$remote_wp_db_file"; then
9+
mysql -h$DB_WP_HOST -u$DB_WP_USER -p$DB_WP_PASS $DB_WP_DBASE<../backups/latest-wp.sql
10+
fi

utils/remote_backup.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
# Use the settings to check your connection
4+
echo -e "\n${txt_white}${bg_black}Backing up remote database(s).${txt_end}\n"
5+
6+
ssh -p "${remote_port}" "${remote_username}@${remote_host}" <<ENDSSH
7+
mkdir -p $remote_backup_dir
8+
cd $remote_magento_dir
9+
10+
REMOTE_DB_HOST=$(n98-magerun config:env:show db.connection.default.host)
11+
REMOTE_DB_USER=$(n98-magerun config:env:show db.connection.default.username)
12+
REMOTE_DB_PASS=$(n98-magerun config:env:show db.connection.default.password)
13+
REMOTE_DB_DBASE=$(n98-magerun config:env:show db.connection.default.dbname)
14+
15+
REMOTE_DB_WP_HOST=$(n98-magerun config:env:show db.connection.wordpress.host)
16+
REMOTE_DB_WP_USER=$(n98-magerun config:env:show db.connection.wordpress.username)
17+
REMOTE_DB_WP_PASS=$(n98-magerun config:env:show db.connection.wordpress.password)
18+
REMOTE_DB_WP_DBASE=$(n98-magerun config:env:show db.connection.wordpress.dbname)
19+
20+
n98-magerun db:dump --compression="gzip" --strip="@log @sessions @trade @sales" --force $remote_backup_dir/latest-m2.sql.gz
21+
if [ ! -z $REMOTE_DB_WP_HOST ]; then
22+
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
23+
gzip -f $remote_backup_dir/latest-wp.sql
24+
fi
25+
ENDSSH

utils/remote_retrieve.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
# Use the settings to check your connection
4+
echo -e "\n${txt_white}${bg_black}Retrieving database(s).${txt_end}\n"
5+
6+
remote_m2_db_file="$remote_backup_dir/latest-m2.sql.gz"
7+
remote_wp_db_file="$remote_backup_dir/latest-wp.sql.gz"
8+
9+
mkdir -p ../backups
10+
11+
scp -C -P "${remote_port}" "${remote_username}@${remote_host}:$remote_m2_db_file" ../backups/latest-m2.sql.gz
12+
gunzip -f ../backups/latest-m2.sql.gz
13+
14+
if existRemoteFile "$remote_wp_db_file"; then
15+
scp -C -P "${remote_port}" "${remote_username}@${remote_host}:$remote_backup_dir/latest-wp.sql.gz" ../backups/latest-wp.sql.gz
16+
gunzip -f ../backups/latest-wp.sql.gz
17+
fi

utils/set_constants.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env bash
2+
3+
# to highlight errors eg. echo -e "${txt_red}Required${txt_end}"
4+
txt_end="\e[0;0m"
5+
txt_bold="\e[1;1m"
6+
txt_muted="\e[1;2m"
7+
txt_italic="\e[1;3m"
8+
txt_underline="\e[1;4m"
9+
txt_black="\e[1;30m"
10+
txt_red="\e[1;31m"
11+
txt_green="\e[1;32m"
12+
txt_yellow="\e[1;33m"
13+
txt_blue="\e[1;34m"
14+
txt_magenta="\e[1;35m"
15+
txt_cyan="\e[1;36m"
16+
txt_white="\e[1;37m"
17+
bg_black="\e[1;40m"
18+
bg_red="\e[1;41m"
19+
bg_green="\e[1;42m"
20+
bg_yellow="\e[1;43m"
21+
bg_blue="\e[1;44m"
22+
bg_magenta="\e[1;45m"
23+
bg_cyan="\e[1;46m"
24+
bg_white="\e[1;47m"
25+
bg_black_light="\e[1;100m"
26+
bg_red_light="\e[1;101m"
27+
bg_green_light="\e[1;102m"
28+
bg_yellow_light="\e[1;103m"
29+
bg_blue_light="\e[1;104m"
30+
bg_magenta_light="\e[1;105m"
31+
bg_cyan_light="\e[1;106m"
32+
bg_white_light="\e[1;107m"
33+
34+
DATESTAMP=$(date +%Y%m%d%H%M%S)
35+
36+
# Generate 32 char string based on md5 of now in epoch format
37+
gen_string ()
38+
{
39+
# date %s # seconds since 1970-01-01 00:00:00 UTC
40+
# date %N # nanoseconds
41+
date +%s.%N |
42+
# md5sum -t # read in text mode
43+
md5sum -t |
44+
# awk '{print $1}' # prints the first returned variable
45+
awk '{print $1}'
46+
}
47+
48+
function existRemoteFile ()
49+
{
50+
FILE=$1
51+
RESULT=$(ssh -p"$remote_port" "${remote_username}@${remote_host}" "test -e $FILE" && echo \"0\" || echo \"1\")
52+
if [ $RESULT == 0 ]; then
53+
return 0
54+
else
55+
return 1
56+
fi
57+
}
58+
59+
conf_file=../backup.conf
60+
61+
DB_HOST=$(n98-magerun config:env:show db.connection.default.host)
62+
DB_USER=$(n98-magerun config:env:show db.connection.default.username)
63+
DB_PASS=$(n98-magerun config:env:show db.connection.default.password)
64+
DB_DBASE=$(n98-magerun config:env:show db.connection.default.dbname)
65+
66+
DB_WP_HOST=$(n98-magerun config:env:show db.connection.wordpress.host)
67+
DB_WP_USER=$(n98-magerun config:env:show db.connection.wordpress.username)
68+
DB_WP_PASS=$(n98-magerun config:env:show db.connection.wordpress.password)
69+
DB_WP_DBASE=$(n98-magerun config:env:show db.connection.wordpress.dbname)

utils/set_remote_host.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env bash
2+
3+
# Set data
4+
5+
echo -e "\n${txt_white}${bg_black}Enter the remote server info.${txt_end}\n"
6+
7+
# loop until validation
8+
while [[ -z ${remote_host} ]]; do
9+
10+
# prompt and set ${remote_host}
11+
read -p $'\e[1mRemote Host (domain or ip)\e[0m: ' -r -e remote_host
12+
13+
if [[ -z ${remote_host} ]]; then
14+
15+
echo -e "${txt_red}Remote Host is required${txt_end}"
16+
17+
fi
18+
done
19+
20+
while [[ -z ${remote_domain} ]]; do
21+
22+
# prompt and set ${remote_domain}
23+
read -p $'\e[1mRemote Domain (no need to https://)\e[0m: ' -r -e remote_domain
24+
25+
if [[ -z ${remote_domain} ]]; then
26+
27+
echo -e "${txt_red}Remote Domain is required${txt_end}"
28+
29+
fi
30+
done
31+
32+
while [[ -z ${remote_port} ]]; do
33+
34+
# prompt and set ${remote_port}
35+
read -p $'\e[1mRemote Port\e[0m: ' -r -e remote_port
36+
37+
if [[ -z ${remote_port} ]]; then
38+
39+
echo -e "${txt_red}Remote Port is required${txt_end}"
40+
41+
fi
42+
done
43+
44+
while [[ -z ${remote_username} ]]; do
45+
46+
# prompt and set ${remote_username}
47+
read -p $'\e[1mRemote Username\e[0m: ' -r -e remote_username
48+
49+
if [[ -z ${remote_username} ]]; then
50+
51+
echo -e "${txt_red}Remote Username is required${txt_end}"
52+
53+
fi
54+
done
55+
56+
while [[ -z ${remote_magento_dir} ]]; do
57+
58+
# prompt and set ${remote_magento_dir}
59+
read -p $'\e[1mRemote hosts Magento directory\e[0m: ' -r -e remote_magento_dir
60+
61+
if [[ -z ${remote_magento_dir} ]]; then
62+
63+
echo -e "${txt_red}Remote hosts Magento directory is required${txt_end}"
64+
65+
fi
66+
done
67+
68+
while [[ -z ${remote_backup_dir} ]]; do
69+
70+
# prompt and set ${remote_backup_dir}
71+
read -p $'\e[1mRemote hosts backup directory\e[0m: ' -r -e remote_backup_dir
72+
73+
if [[ -z ${remote_backup_dir} ]]; then
74+
75+
echo -e "${txt_red}Remote hosts backup directory is required${txt_end}"
76+
77+
fi
78+
done
79+
80+
while [[ -z ${remote_shared_deployment_dir} ]]; do
81+
82+
# prompt and set ${remote_shared_deployment_dir}
83+
read -p $'\e[1mRemote hosts shared deployment directory\e[0m: ' -r -e remote_shared_deployment_dir
84+
85+
if [[ -z ${remote_shared_deployment_dir} ]]; then
86+
87+
echo -e "${txt_red}Remote hosts shared deployment directory is required${txt_end}"
88+
89+
fi
90+
done
91+
92+
echo -e "\n"
93+
echo -e "${bg_blue} ${txt_end}"
94+
echo -e "${bg_blue} ${txt_white}Using the following for remote host ${txt_end}"
95+
echo -e "${bg_blue} ${txt_white} Remote Host: ${txt_green}${remote_host} ${txt_end}"
96+
echo -e "${bg_blue} ${txt_white} Remote Domain: ${txt_green}${remote_domain} ${txt_end}"
97+
echo -e "${bg_blue} ${txt_white} Remote Port: ${txt_green}${remote_port} ${txt_end}"
98+
echo -e "${bg_blue} ${txt_white} Remote Username: ${txt_green}${remote_username} ${txt_end}"
99+
echo -e "${bg_blue} ${txt_white} Remote Host Magento Dir: ${txt_green}${remote_magento_dir} ${txt_end}"
100+
echo -e "${bg_blue} ${txt_white} Remote Host Backup Dir: ${txt_green}${remote_backup_dir} ${txt_end}"
101+
echo -e "${bg_blue} ${txt_white} Remote Host Shared Deployment Backup Dir: ${txt_green}${remote_shared_deployment_dir} ${txt_end}"
102+
echo -e "${bg_blue} ${txt_end}"

0 commit comments

Comments
 (0)