Skip to content

Commit

Permalink
feat: Remove dbmigration and add dbimport/dbexport
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Ris committed May 27, 2021
1 parent 6346189 commit 17204b8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 31 deletions.
34 changes: 34 additions & 0 deletions shells/dbexport.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
if [ "$1" = "local" ]
then
# Locally
docker exec -it $(docker ps -aqf "name=gulpwordpress_db_1") mysqldump -uroot -ptoor --opt --add-drop-table wordpress > ./sql/local.sql
sed -i '1d' ./sql/local.sql
elif [ "$1" = "prod" ] || [ "$1" = "stage" ]
then
# Remotely
SSH_USER=$(grep SSH_USER .env | xargs)
IFS='=' read -ra SSH_USER <<< "$SSH_USER"
SSH_USER=${SSH_USER[1]}

# Get ssh host
SSH_HOST=$(grep SSH_HOST .env | xargs)
IFS='=' read -ra SSH_HOST <<< "$SSH_HOST"
SSH_HOST=${SSH_HOST[1]}

# Get ssh key
SSH_KEY=$SSH_USER
IFS='.' read -ra SSH_KEY <<< "$SSH_KEY"
SSH_KEY=${SSH_KEY[0]}

# Set public directory
PUBLICPATH="public_html"

if [[ "$1" == "stage" ]]
then
# Set public directory
PUBLICPATH+="/stage"
fi

ssh -i ~/.ssh/$SSH_KEY $SSH_USER@$SSH_HOST "cd ~/$PUBLICPATH && wp db export sql/$1.sql"
fi
45 changes: 41 additions & 4 deletions shells/dbimport.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
#!/bin/bash
cat ./sql/local.sql | docker exec -i gulpwordpress_db_1 mysql -uwordpress -pwordpress wordpress
rm -f ./sql/local.sql
rm -f ./sql/prod.sql
rm -f ./sql/stage.sql
if [ "$1" = "local" ]
then
# Clear local wordpress database
docker exec -it $(docker ps -aqf "name=gulpwordpress_db_1") mysql -uroot -ptoor -e "DROP DATABASE wordpress; CREATE DATABASE wordpress;"
# Import local.sql
cat ./sql/local.sql | docker exec -i gulpwordpress_db_1 mysql -uwordpress -pwordpress wordpress
rm -f ./sql/local.sql
elif [ "$1" = "prod" ] || [ "$1" = "stage" ]
then
# Remotely
SSH_USER=$(grep SSH_USER .env | xargs)
IFS='=' read -ra SSH_USER <<< "$SSH_USER"
SSH_USER=${SSH_USER[1]}

# Get ssh host
SSH_HOST=$(grep SSH_HOST .env | xargs)
IFS='=' read -ra SSH_HOST <<< "$SSH_HOST"
SSH_HOST=${SSH_HOST[1]}

# Get ssh key
SSH_KEY=$SSH_USER
IFS='.' read -ra SSH_KEY <<< "$SSH_KEY"
SSH_KEY=${SSH_KEY[0]}

# Set public directory
PUBLICPATH="public_html"

if [[ "$1" == "stage" ]]
then
# Set public directory
PUBLICPATH+="/stage"
fi

# Connect to host, clear remote wordpress database, import sql dump, remove sql dump file
ssh -i ~/.ssh/$SSH_KEY $SSH_USER@$SSH_HOST "cd ~/$PUBLICPATH && wp db clean --yes && wp db import sql/$1.sql; rm -f sql/$1.sql"
else
echo "Wrong parameter!"
echo "npm run dbimport local"
echo "npm run dbimport stage"
echo "npm run dbimport prod"
fi
27 changes: 0 additions & 27 deletions shells/dbmigration.sh

This file was deleted.

0 comments on commit 17204b8

Please sign in to comment.