Last Updated: February 25, 2016
·
1.026K
· choyer

MySQL Database Dump via Git Post-Commit Hook with Appended Commit SHA ID

Many of my web development projects are built on top of content management systems. In order to maintain some sense of database versioning, without actually putting the db under source control, I perform a mysql database dump while appending a timestamp and the git commit SHA id to the file name for reference.

Here is my git post-commit file to do that:

#!/bin/sh
# ./.git/hooks/post-commit
# dump local database and store

# Time and commit id
current_time=$(date +%F-%H_%M_%S)
git_commit_id=$(git rev-parse --short HEAD)

# Go go database dump
printf "==> Dumping DB ..."
mysqldump -u root -p'your password here' database-name | cat > ~/db/database-name-$current_time-$git_commit_id.sql
printf " Done. [database-name-$current_time-$git_commit_id.sql]\n\n"

Replace mysql login details, path and database-name with your environments specific settings.