Skip to content

Commit f749a68

Browse files
authored
Merge pull request #20 from sourcetoad/db-migrate
DB Migrator
2 parents 3525527 + 4692b4c commit f749a68

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ popular frameworks like Laravel and Yii2.
5454
The examples make the assumption that any domain used for local development is
5555
already added to the `/etc/hosts` file. You can edit to add `127.0.0.1 domain.docker`.
5656

57+
## Scripts
58+
Inside the `scripts` folder you will find one-off scripts to help with tasks.
59+
60+
* `db_migrate.sh` - Helps migrate databases between versions of mysql.
61+
5762
## Docs
5863
* [Setting up Nginx-Proxy](docs/nginx-proxy/README.md)
5964
* [Setting up PHP Testing in PHPStorm](docs/phpstorm-docker/README.md)

scripts/db_migrate.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
3+
# Variable Setup
4+
NEWLINE=$'\n'
5+
6+
DB_BINARY_NAME=''
7+
DB_PORT=''
8+
DB_HOST='127.0.0.1'
9+
DB_USER='root'
10+
DB_PASS='root'
11+
DB_EXPORT_FLAGS='--routines --column-statistics=0 --quick --hex-blob --single-transaction'
12+
DB_IMPORT_FLAGS=''
13+
14+
# Determine DB
15+
echo "Welcome to the SDIT DB Migrator (mysql/postgres) $NEWLINE"
16+
read -rp "Select (mysql|postgres):" DB_MODE
17+
read -rp "Enter (FROM) port number:" DB_PORT
18+
read -rp "Enter (FROM) database name: " DB_NAME
19+
read -rp "Enter (TO) port number:" DB_TO_PORT
20+
read -rp "Enter (TO) database name: " DB_TO_NAME
21+
22+
case $DB_MODE in
23+
mysql|maria|mariadb)
24+
DB_BINARY_NAME='mysql'
25+
26+
# Attempt to connect to DB (FROM and TO).
27+
if ! $DB_BINARY_NAME --host="${DB_HOST}" --user="${DB_USER}" --password="${DB_PASS}" --port="${DB_PORT}" -e "use ${DB_NAME}"; then
28+
echo "Database (FROM) could not be connected ($DB_NAME, $DB_PORT). Aborting...$NEWLINE"
29+
exit 1
30+
fi
31+
32+
if ! $DB_BINARY_NAME --host="${DB_HOST}" --user="${DB_USER}" --password="${DB_PASS}" --port="${DB_TO_PORT}" -e "use ${DB_TO_NAME}"; then
33+
echo "Database (TO) could not be connected ($DB_TO_NAME, $DB_TO_PORT). Aborting...$NEWLINE"
34+
exit 1
35+
fi
36+
37+
echo -n "Copying...! $NEWLINE"
38+
mysqldump $DB_EXPORT_FLAGS --user="${DB_USER}" --password="${DB_PASS}" --host="${DB_HOST}" --port="${DB_PORT}" "${DB_NAME}" | mysql $DB_IMPORT_FLAGS --user="${DB_USER}" --password="${DB_PASS}" --host="${DB_HOST}" --port="${DB_TO_PORT}" "${DB_TO_NAME}"
39+
echo -n "Copied...! $NEWLINE"
40+
;;
41+
42+
pg | postgres)
43+
echo -n "Postgres support is still in progress. Aborting...$NEWLINE"
44+
;;
45+
46+
*)
47+
echo -n "This selection is unknown. Aborting...$NEWLINE"
48+
exit 1
49+
;;
50+
esac
51+
52+
exit 0

0 commit comments

Comments
 (0)