Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export and import database on Git push and pull

I need my database to be in sync across two local computers, without using a server in between. After some research I thought I could use Git hooks to do the task for me.

What I basically want is to run mysqldump on git push (and add the SQL file to the commit) and mysqlimport on git pull to and from the remote repository.

I couldn't find any specific hooks for this. I tried the pre-commit hook, but this didn't add the SQL file in the current commit. I then tried prepare-commit, but without luck.

Has anyone got an answer for this one?

like image 502
rebellion Avatar asked Sep 02 '11 09:09

rebellion


2 Answers

This solution by Ben Kulbertis uses the pre-commit and post-merge git hooks to do the job. Works like a charm!

http://ben.kulbertis.org/2011/10/synchronizing-a-mysql-database-with-git-and-git-hooks/

like image 75
sparkyspider Avatar answered Sep 20 '22 01:09

sparkyspider


I'd write a wrapper for your system, maybe "commit.php" or "git-ci.sh" or something. Then you can use bash or perl or python or php or whatever you are comfortable with and run each command one by one, finishing with the commit.

I suggest this after running into similar issues with trying to add to the commit that the pre-commit hook is running. Also, there are no "push" or "pull" hooks on the downstream side (I use a central repository so this might work if you have two machines pushing and pulling with each other as you seem to).

I have a two sided deploy script for one of my web sites where I have a long pre-commit script but on the web server I have a wrapper like this that fetches, then inspects the commit, outputs a report and gives me options about whether to merge and rsync with another server, etc. It replaced a "pull" hook that would probably be a major security hazard.

Hans

like image 28
Hans Avatar answered Sep 20 '22 01:09

Hans