#!/bin/bash src_database="192.168.10.10" src_database_user="root" src_database_passwd="xxxx" dst_database="192.168.10.12" dst_database_user="root" dst_database_passwd="xxxxx" cd ~ mkdir ./datadump #存放sql的临时目录 echo "start export databases from ${src_database}!" databasename_list=`echo "show databases" | mysql -h ${src_database} -u${src_database_user} -p${src_database_passwd} | sed -n '/^100msh/'p` for databasename in ${databasename_list} do mysqldump -h ${src_database} -u${src_database_user} -p${src_database_passwd} -R ${databasename} > ./datadump/${databasename}.sql if [ $? == 0 ];then echo "export databases ${databasename} success!" else echo "export databases ${databasename} fail!" exit 1 fi done echo "export databases from ${src_database} end!" chmod -R 777 ./datadump cd ./datadump echo "start import databases to ${dst_database}!" for databasename in ${databasename_list} do echo "create database ${databasename}" | mysql -h ${dst_database} -u${dst_database_user} -p${dst_database_passwd} if [ $? == 0 ];then echo "create databases ${databasename} success!" else echo "create databases ${databasename} fail!" exit 1 fi mysql -h ${dst_database} -u${dst_database_user} -p${dst_database_passwd} ${databasename} < ${databasename}.sql if [ $? == 0 ];then echo "import databases ${databasename} success!" else echo "import databases ${databasename} fail!" exit 1 fi done echo "import databases to ${dst_database} end!"