With mkb user help, I've build this pre-commit hook:
#!/bin/bash -e
DBHOST=dbhost.yourdomain.com
DBUSER=dbuser
DBPASS=dbpass
DBNAME=dbname
mysqldump -h $DBHOST -u $DBUSER -p $DBPASS -d $DBNAME > sql-version-control/schema.sql
# the -h means host.
# the -u means user.
# the -p means pass.
# the -d means database name (same as "no data").
git add sql-version-control/schema.sql
As far as I can understand:
mysqldump -h $DBHOST -u $DBUSER -p $DBPASS -d $DBNAME > sql-version-control/schema.sql
Will go to my shared (OR local) host, and will do a backup of the atual schema there?
Very often, however, we change the schema locally, and then, we need to apply those changes on the remote server.
Is there anyway I can do this follow this path ? Or there is a better way for doing the intended ?
Thanks in advance
Use this as your pre-commit hook to auto-backup your DB to Git when delivering a new changeset: https://gist.github.com/wilcollins/dfa33ef20caf6dab5826
Since DB schema changes are typically coupled with repo changes to support the DB changes, this system keeps remote & local environments in sync as much as necessary.
#!/bin/bash -e
# -e means exit if any command fails
DBHOST=address.to.your.server
DBUSER=username
DBPASS=password # do this in a more secure fashion
DBNAME=DBNAME
GITREPO=/where/is/your/repo
DUMP=$GITREPO/where/you/store/dumps
NEW=$DUMP/schema.sql
OLD=$NEW.old
DIR=$(pwd)
cd $GITREPO
mv $NEW $OLD
mysqldump -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME --skip-dump-date --single-transaction > $NEW
# NOTE : the new schema.sql file & location need to be added to your GIT repo & ignore .old
if cmp -s $OLD $NEW; then
echo Same
else
echo Differ
git commit $NEW -m "$DBNAME DB update"
echo "schema+data committed"
git push # assuming you have a remote to push to
echo "schema+data pushed"
fi
cd $DIR
then you can import the new schema.sql
when you pull the repo changes