Thesqldump – select table in database only if it contains a specific string

MySQLmysqldumpscripting

(I'm new to Database Administrators, and am not a Database Administrator. However I originally asked this question on Stack Overflow (here) but didn't get any answers. Sorry if this is not the right site to ask this question, but I really need an answer).

I have a pretty big local WordPress Multisite installation, and this is mirrored on a live server, so I can develop a site locally and upload it for a client to see (it's sub-domain based). I have written a custom shell script to upload the database from my local machine to the live server, which uses sed to change all instances of the local domain name to the live domain name. It then commits and pushes it by git and the live server automatically pulls it, takes a backup and applies the new file.

However, this has become problematic in the last month or so. As I've got more client work, and they have been editing their sites live on the server, and I've been pushing new work to the server for other clients to take a look at, I've been overwriting changes on the live database.

What I need is to be able to add a flag or something when I call the shell script file (sh push.sh) that indicates what tables in the database need to be pushed live. Each site in the WordPress Multisite database has a number, e.g network_17_posts. So if I could a table number state when I called my shell script, like sh push.sh --table=17 and it would only upload site 17's data, and not overwrite anything else, that would be awesome. As a bonus, if I could specify multiple numbers, so I could upload multiple sites at a time, that would be amazing!

As a reference, here is my current shell script for pushing the database live (it could probably be 10x better, but I'm primarily a front-end/PHP dev, not shell script!):

rm -rf db_sync.sql
mysqldump -u root -ppassword db_name > db_sync.sql  
sed 's/localdomain.dev/livedomain.com/g' db_sync.sql > new_db_sync.sql
rm -rf db_sync.sql
mv new_db_sync.sql db_sync.sql
git add db_sync.sql
read -p "Enter Commit Message: " commit_message
git commit -m "$commit_message"
git push -u web master

Thanks for any help 🙂

Best Answer

What you need is a tool for controlling migrations and upload them to git a.k.a. a version control for MySQL. While you can deploy your own, this is a very well known problem and there are many tools available, some of them free and open source. I am not going to recommend you anything in particular, as they are different on the goals/features/integration with frameworks, but here it is a list of options:

  • Liquibase
  • Flyway
  • c5-db-migration
  • dbdeploy
  • MyBatis
  • Autopatch
  • Sqitch
  • online-migration

Some recommended readings: http://en.wikipedia.org/wiki/Schema_migration, http://www.liquibase.org/quickstart and http://www.percona.com/live/mysql-conference-2013/sites/default/files/slides/PLMCE13_online-migration.pdf

If you still want to deploy your own tool (not recommended), have a look at least at mysqldbcompare from mysqlutils and some of the percona-toolkit tools.