使用mkb用户帮助,我构建了这个预提交钩子:
#!/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
据我所知:
mysqldump -h $DBHOST -u $DBUSER -p $DBPASS -d $DBNAME > sql-version-control/schema.sql
将转到我的共享(OR本地)主机,并将在那里备份原始架构吗?
但是,我们经常在本地更改架构,然后,我们需要在远程服务器上应用这些更改。
无论如何我可以按照这条路走吗? 或者有更好的方法来实现目标?
提前致谢
答案 0 :(得分:0)
在提供新的变更集时,将此作为预先提交的挂钩,将您的数据库自动备份到Git:https://gist.github.com/wilcollins/dfa33ef20caf6dab5826
由于数据库模式更改通常与repo更改相关联以支持数据库更改,因此该系统保持远程&本地环境尽可能同步。
#!/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
然后您可以在提取回购更改时导入新的schema.sql