Skip to content

Commit ffeb3f6

Browse files
committed
Merge branch 'release/v1.1.0'
2 parents 3377b86 + e7f03c7 commit ffeb3f6

File tree

4 files changed

+174
-4
lines changed

4 files changed

+174
-4
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [Unreleased]
8+
9+
## [1.1.0] - 2020-07-10
10+
### Added
11+
- Installer
12+
- Example [config file](./example.conf)
13+
- [Changelog](./CHANGELOG.md)
14+
15+
### Changed
16+
- README.md includes more information
17+
18+
19+
## [1.0.0] - 2020-07-09
20+
### Added
21+
- Initial Release

README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
1-
# Magento 2 Production DB Sync v1.0.0
1+
# Magento 2 Production DB Sync v1.1.0
22

3-
A script to copy over a production database to a staging server.
3+
## About
4+
A script to copy over a production database to another server, also has the ability to copy across imagery.
45

5-
You can save a configuration file in the Magento directory to save answering some of the questions.
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`.
7+
8+
## Requirements
9+
- You need curl installed locally.
10+
- The excellent [n98-magerun2](https://github.com/netz98/n98-magerun2) needs to be installed on both the local machine and the production machine. (Needs to be executable as n98-magerun).
11+
- MySQL (or equivalent) needs to be installed on both the local machine and the server (obviously).
12+
- Magento 2 needs to be installed and configured on both the local and the host machine.
13+
- rsync needs to be installed locally.
14+
15+
## Installing and Updating
16+
To install or update the script run the following curl script
17+
```curl -o- https://raw.githubusercontent.com/clivewalkden/bash-magento2-db-sync/v1.1.0/install.sh | bash```
18+
19+
## Usage
20+
To copy over a production database first get a shell on the system you want to copy the data to.
21+
22+
> Notes: If using SSH to connect to the host machine make sure you've connected with the -A flag so that your ssh agent is forwarded to allow additional connections.
23+
24+
Execute the script in your Magento directory
25+
```
26+
$ db-sync.sh
27+
```
28+
29+
You can save a configuration file in the Magento directory to save answering some of the questions. An example is included in this repository [example.conf](./example.conf)
630

731
```bash
832
remote_host=cotswoldcollections.com
@@ -14,4 +38,3 @@ remote_backup_dir=/opt/magento/backups
1438
remote_shared_deployment_dir=/opt/magento/deployment/shared/magento
1539
```
1640

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.

example.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Configuration file for db-sync (https://github.com/clivewalkden/bash-magento2-db-sync)
2+
remote_host=domain.com
3+
remote_domain=www.domain.com
4+
remote_port=22
5+
remote_username=magento
6+
remote_magento_dir=/opt/magento/magento2
7+
remote_backup_dir=/opt/magento/backups
8+
remote_shared_deployment_dir=/opt/magento/deployment/shared/magento

install.sh

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env bash
2+
3+
{ # this ensures the entire script is downloaded #
4+
5+
dbsync_install_dir() {
6+
printf %s "${HOME}/.dbsync"
7+
}
8+
9+
dbsync_latest_version() {
10+
echo "v1.1.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() {
26+
local INSTALL_DIR
27+
INSTALL_DIR="$(dbsync_install_dir)"
28+
29+
# Downloading to $INSTALL_DIR
30+
mkdir -p "$INSTALL_DIR"
31+
if [ -f "$INSTALL_DIR/db-sync.sh" ]; then
32+
echo "=> db-sync is already installed in $INSTALL_DIR, trying to update the script"
33+
else
34+
echo "=> Downloading db-sync as script to '$INSTALL_DIR'"
35+
fi
36+
37+
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 || {
41+
echo >&2 "Failed to update db-sync, run 'git fetch' in $INSTALL_DIR yourself."
42+
exit 1
43+
}
44+
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
50+
command git init "${INSTALL_DIR}" || {
51+
echo >&2 'Failed to initialize db-sync repo. Please report this!'
52+
exit 2
53+
}
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
58+
}
59+
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
62+
}
63+
else
64+
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
67+
}
68+
fi
69+
fi
70+
command git -c advice.detachedHead=false --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" checkout -f --quiet "$(dbsync_latest_version)"
71+
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
73+
command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet -D master >/dev/null 2>&1
74+
else
75+
echo >&2 "Your version of git is out of date. Please update it!"
76+
command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch -D master >/dev/null 2>&1
77+
fi
78+
fi
79+
80+
echo "=> Compressing and cleaning up git repository"
81+
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!"
83+
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!"
86+
fi
87+
88+
local PROFILE_INSTALL_DIR
89+
PROFILE_INSTALL_DIR="$(dbsync_install_dir | command sed "s:^$HOME:\$HOME:")"
90+
SOURCE_STR="\\nexport DBSYNC_DIR=\"${PROFILE_INSTALL_DIR}\"\\nif [ -f \"\$DBSYNC_DIR/db-sync.sh\" ]; then\\n\t export PATH=\"$INSTALL_DIR:\$PATH\"\\nfi # This loads db-sync\\n"
91+
local DBSYNC_PROFILE
92+
DBSYNC_PROFILE="${HOME}/.bashrc"
93+
94+
if ! command grep -qc '/db-sync.sh' "${DBSYNC_PROFILE}"; then
95+
echo "=> Appending db-sync source string to ${DBSYNC_PROFILE}"
96+
command printf "${SOURCE_STR}" >> "${DBSYNC_PROFILE}"
97+
else
98+
echo "=> db-sync source string already in ${DBSYNC_PROFILE}"
99+
fi
100+
101+
\. "${DBSYNC_PROFILE}"
102+
103+
dbsync_reset
104+
105+
echo "=> Close and reopen your terminal to start using db-sync.sh"
106+
}
107+
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+
}
115+
116+
do_install
117+
118+
} # this ensures the entire script is downloaded #

0 commit comments

Comments
 (0)