Created
April 8, 2014 09:45
-
-
Save zhouyl/10105118 to your computer and use it in GitHub Desktop.
同步远程数据库到本地
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| ### 数据库同步到本地 ### | |
| all_databases=(db1 db2) | |
| ssh='ssh root@remote-db-host' | |
| mysqldump='/usr/local/mysql/bin/mysqldump -uroot -proot --single-transaction' | |
| mysql='/usr/local/mysql/bin/mysql -uroot -proot' | |
| create='CREATE DATABASE IF NOT EXISTS' | |
| if [ ! $1 ]; then | |
| echo "Usage: \`$0 all\` or \`$0 db1 db2 ...\`" | |
| exit | |
| elif [ $1 = 'all' ]; then | |
| databases=$all_databases | |
| else | |
| databases=$* | |
| fi | |
| for dbname in ${databases[@]}; do | |
| echo -n "dumping $dbname ..." | |
| $mysql -e "$create \`$dbname\`;" | |
| $ssh $mysqldump $dbname | $mysql $dbname | |
| echo -e " \033[32;49;2mdone!\033[39;49;0m" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment