Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Db export task, use env vars #232

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@
"type": "shell",
"command": "chmod -R 777 wordpress/wp-content/themes/headless/acf-json"
},
{
"label": "Wordpress DB Export",
"type": "shell",
"command": "cd wordpress && bash _build/db.sh export"
},
{
"label": "Wordpress DB Import",
"type": "shell",
"command": "cd wordpress && bash _build/db.sh"
"command": "cd wordpress && bash _build/db.sh import"
},
{
"label": "Wordpress Dev",
Expand Down
3 changes: 3 additions & 0 deletions wordpress/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
# on this template. We also use this variable to set the wordpress database
# name in docker-compose.yml and _build/db.sh
COMPOSE_PROJECT_NAME=bubsnext
COMPOSE_WPE_PRODUCTION=bubsnext
COMPOSE_WPE_STAGING=bubsnexts
COMPOSE_WPE_DEVELOPMENT=bubsnextd
74 changes: 48 additions & 26 deletions wordpress/_build/db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,60 @@ if [ -f ".env" ]; then
export $(grep -v '^#' .env | xargs)
fi

WORDPRESS_DB_HOST="127.0.0.1"
WORDPRESS_DB_PORT=3307
WORDPRESS_DB_USER="root"
WORDPRESS_DB_PASSWORD="somewordpress"
WORDPRESS_DB_NAME=${COMPOSE_PROJECT_NAME:-bubsnext}

## Ideally these would work without having to hardcode above
## instead coming from docker ENV
sql=`ls -Art _data/* | tail -n 1`
echo $sql
ext=${sql##*.}

if [ $ext = "zip" ]; then
unzip -p $sql | mysql -u $WORDPRESS_DB_USER -p$WORDPRESS_DB_PASSWORD -h $WORDPRESS_DB_HOST -P $WORDPRESS_DB_PORT $WORDPRESS_DB_NAME
elif [ $ext = "gz" ]; then
gunzip < $sql | mysql -u $WORDPRESS_DB_USER -p$WORDPRESS_DB_PASSWORD -h $WORDPRESS_DB_HOST -P $WORDPRESS_DB_PORT $WORDPRESS_DB_NAME
else
mysql -u mysql -u $WORDPRESS_DB_USER -p$WORDPRESS_DB_PASSWORD -h $WORDPRESS_DB_HOST -P $WORDPRESS_DB_PORT $WORDPRESS_DB_NAME < $sql
fi
WORDPRESS_DB_NAME=${COMPOSE_PROJECT_NAME:-wordpress}
DB_CONTAINER="${COMPOSE_PROJECT_NAME:-wordpress}_db_1"
PRODUCTION_SSH="${COMPOSE_WPE_PRODUCTION}@${COMPOSE_WPE_PRODUCTION}.ssh.wpengine.net"

function db_import() {
sql=`ls -Art _data/* | tail -n 1`
echo $sql
ext=${sql##*.}

if [ $ext = "zip" ]; then
unzip -p $sql | docker exec -i $DB_CONTAINER mysql -u $WORDPRESS_DB_USER -p$WORDPRESS_DB_PASSWORD -D $WORDPRESS_DB_NAME
elif [ $ext = "gz" ]; then
gunzip < $sql | docker exec -i $DB_CONTAINER mysql -u $WORDPRESS_DB_USER -p$WORDPRESS_DB_PASSWORD -D $WORDPRESS_DB_NAME
else
docker exec -i $DB_CONTAINER mysql -u $WORDPRESS_DB_USER -p$WORDPRESS_DB_PASSWORD -D $WORDPRESS_DB_NAME < $sql
fi

# run local mods if present
file="_data/local.sql"
if [ -f "$file" ]
then
docker exec -i $DB_CONTAINER mysql -u $WORDPRESS_DB_USER -p$WORDPRESS_DB_PASSWORD -D $WORDPRESS_DB_NAME < $file
echo "$file imported."
else
echo "$file not found."
fi

# run local mods if present
file="_data/local.sql"
if [ -f "$file" ]
then
mysql -u $WORDPRESS_DB_USER -p$WORDPRESS_DB_PASSWORD -h $WORDPRESS_DB_HOST -P $WORDPRESS_DB_PORT $WORDPRESS_DB_NAME < $file
echo "$file imported."
if [ -f ".env" ]; then
unset $(grep -v '^#' .env | sed -E 's/(.*)=.*/\1/' | xargs)
fi

echo 'import complete'
}

function db_export() {
echo "$PRODUCTION_SSH";
wp db export --add-drop-table --ssh=$PRODUCTION_SSH - | gzip > _data/$(date +'%Y-%m-%d-%H-%M-%S').sql.gz
echo "export complete";
}

CALLED_FUNCTION=${1}

if [ "$CALLED_FUNCTION" = "export" ]; then
echo "running DB export script"
db_export
elif [ "$CALLED_FUNCTION" = "import" ]; then
echo "running DB import script"
db_import
else
echo "$file not found."
error_exit "Specify a DB task (export or import)"
fi

if [ -f ".env" ]; then
unset $(grep -v '^#' .env | sed -E 's/(.*)=.*/\1/' | xargs)
fi

echo 'import complete'
21 changes: 15 additions & 6 deletions wordpress/_build/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#!/bin/bash
#!/bin/sh

## Per Project Variables -- CUSTOMIZE THESE FIRST
PRODUCTION_REMOTE="[email protected]:production/bubsnext.git"
STAGING_REMOTE="[email protected]:production/bubsnexts.git"
DEVELOPMENT_REMOTE="[email protected]:production/bubsnextd.git"

## env export, with unset at end of script
if [ -f ".env" ]; then
export $(grep -v '^#' .env | xargs)
fi
PRODUCTION_REMOTE="[email protected]:production/${COMPOSE_WPE_PRODUCTION}.git"
STAGING_REMOTE="[email protected]:production/${COMPOSE_WPE_STAGING}.git"
DEVELOPMENT_REMOTE="[email protected]:production/${COMPOSE_WPE_DEVELOPMENT}.git"
GIT_EMAIL="[email protected]"
GIT_NAME="Bubs Deploy"

Expand Down Expand Up @@ -93,8 +98,8 @@ else
fi

if [ `git branch --list deploy` ]; then
echo "Branch deploy already exists, deleting then continuing"
git branch -D deploy
echo "Branch deploy already exists, deleting then continuing"
git branch -D deploy
fi

# save current branch to a variable
Expand Down Expand Up @@ -159,3 +164,7 @@ else
error_exit "Something went wrong with the deploy."
fi
fi

if [ -f ".env" ]; then
unset $(grep -v '^#' .env | sed -E 's/(.*)=.*/\1/' | xargs)
fi