Skip to content

Instantly share code, notes, and snippets.

@zhouyl
Created April 8, 2014 09:45
Show Gist options
  • Select an option

  • Save zhouyl/10105118 to your computer and use it in GitHub Desktop.

Select an option

Save zhouyl/10105118 to your computer and use it in GitHub Desktop.
同步远程数据库到本地
#!/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