Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zilahir
Created January 3, 2019 21:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zilahir/c2b4187d9a1f1391c3545934cda8fe66 to your computer and use it in GitHub Desktop.
Save zilahir/c2b4187d9a1f1391c3545934cda8fe66 to your computer and use it in GitHub Desktop.
change-db
#!/bin/bash
#This script changes the database with the latest file in repo's !sql folder. This script is triggered by a manual step in bitbucket pipeline
cd !sqldump;
latest_sql_file=`ls -Art | tail -n 1`;
cd ..
#connect to devel database and
#1. drop the current database there
TABLES=$(mysql --host <host> -u <user> -p<pass> <db-name> -e 'show tables' | awk '{ print $1}' | grep -v '^Tables' );
for t in $TABLES
do
echo "Deleting $t table from <db-name> database..."
mysql --host <host> -u <user> -p<pass> <db-name> -e "drop table $t"
done
#2 inject the new database
mysql --host "<host>" -u "user" -p<pass> "<db-name>" < pre_db.sql
echo "pre db script executed"
mysql --host "<host>" -u "user" -p<pass> "<db-name>" < "!sqldump/$latest_sql_file/$latest_sql_file.sql"
echo "new sql file inserted"
mysql --host "<host>" -u "user" -p<pass> "<db-name>" < post_db.sql
echo "post db escript executed"
mysql --host <host> -u <user> -p<pass> <db-name> -e update table wp_options set siteurl="<site-url>"
echo "changed wp_options table siteurl"
mysql --host <host> -u <user> -p<pass> <db-name> -e update table wp_options set home="<site-url>"
echo "changed wp_options table home url"
echo "database successfully changed on devel"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment