Push mysql database script to server using git

4.1k views Asked by At

I need to take backup of MySQL database script(without data) periodically. And also I want to push the script to server along with source code using git. I already done push and pull the source code using git. But I want to push the DB script too. How to achieve this using git? Any ideas?

1

There are 1 answers

4
Hazok On

I'm not sure if Git is the best place to store a backup of a DB depending upon how the DB is expected grow. However if the desire is to store the SQL backup in Git, this can be done using mysqldump and git commands if the following is executed from a git repo:

mysqldump -h <host> -u <user> -p <db_name> > <sql_file>
git add <sql_file>
git commit -m "Latest mysqldump"
git push

For example:

mysqldump -h 127.0.0.1 -u mydbuser -p mydbschema > 'db/currentBackup.sql'
git add 'db/currentBackup.sql'
git commit -m "Latest mysqldump"
git push

More options are available in the MySQL documentation: http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html