Skip to content

Instantly share code, notes, and snippets.

@X4
Last active December 19, 2015 06:49
Show Gist options
  • Select an option

  • Save X4/5913903 to your computer and use it in GitHub Desktop.

Select an option

Save X4/5913903 to your computer and use it in GitHub Desktop.
Git Dude and Git MySQL HowTo https://github.com/sickill/git-dude

Hi!

I have lots of repositories locally and on my servers, but I follow even more repositories and I contribute to some here and there. That's why I've the need to stay notified when there is a change. But I don't want to actively follow through RSS or E-Mail. A commit notification isn't worth bloating my inbox even more.

For those who don't know, there is a cool way to keep notified of commits etc. and to exchange messages. You can use IRC in combination with a git hook through cia.vc/ or github.com/ itself, even bitbucket.org supports it now. Otherwise just setup an eggdrop, supybot or the phergie IRC Bot and write a script for it. You can share it here if you want.

It's obvious that a snapshot of your application only makes sense when you also add a database snapshot to it. That's why I'll explain howto create a git hook to take database dumps on each commit or tag.

GIT based Database version control.

Go to the hidden .git folder in your project, then a sub folder in there called hooks Create a file called pre-commit and open it in your editor Paste the above hook script into that file and save it, your dump will be saved in the db directory, that directory will be created if it doesn't exist. (note that you can add the -A flag to dump all databases and --no-data=true will dump your datasets if you need them) Without any further effort, you will update the schema file on every commit. In case you need to dump the data too, you can speed up the import of very huge dumps by adding these lines into your [mysqld] section of your my.cnf. You need to reload or restart MySQL after that. (sudo /etc/init.d/mysqld reload or restart). It's not recommended to keep these settings in your my.cnf, so it's better to remove them after a restore. It will delay index generation and assign 512MB buffers.

Speedup Huge Imports

[mysqld] delay_key_write=ALL bulk_insert_buffer_size=512M

Next time your run git commit your MySQL schema dump will be included. You can of course customize yourself when to dump the db, ie if you prefer it to be included only on tags in example you would have to to replace git tag with an alias or you treat tags differently in your post-receive hook. Here's an example

Git Dude

Now you can easily create symlinks of your favorite repositories in your .git-dude folder or simply git clone --mirror github.com/user/… inside of it. dude.interval 30 means it checks every 30 sec for a commit. You can adjust that to your likings.

EDIT: Here's the code. I don't know why it disappeared from my post..weird..I luckily do backups :)

GIT in Color

git config --global color.ui always

Add the Hook

cd .git/hooks
nano pre-commit

The Hook-Script

#!/bin/sh
if [ ! -d "db" ]; then
    mkdir db
fi
mysqldump -uDBUSER -pDBPASSWORD DATABASE -q --no-data=true | bzip2 -c > db/data-from-$(date --rfc-3339=seconds).sql.bz2
git add db/*
exit 0

Restoring Data (in mysqldump you can add --add-drop-table to make it easier to restore)

bzip2 -d FILE.bz2
mysql -o -uDBUSER -pDBPASSWORD DATABASE < FILE.sql

Install Git Dude

sudo -s
curl -skL https://github.com/sickill/git-dude/raw/master/git-dude > /usr/local/bin/git-dude
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment