我一直在下面的bash脚本上进行尝试,该脚本试图将我在使用mysql转储/还原时看到的所有好的建议汇总在一起。它针对远程操作。
只需重新配置vars并尝试一下。:)
功能包括:
- 您可以传递要转储的表的列表(选择性转储)
- 系统可能会提示您输入密码(MySQL / SSH)或在变量中设置密码
- 网络传输被压缩
- 您可以选择将压缩的转储保存到远程服务器
- 您可以即时将转储重新导入到远程服务器(本地/远程服务器上没有临时文件)
- 您对正在发生的事情有视觉反馈(感谢echo和pv)
- 您可以在转储过程之前和之后设置mysql变量
需要改进的地方:
- 您需要传递表列表(无法转储所有表)
- 源和目标的MySQL密码相同
- 您需要手动授予特权(看起来像MySQL不允许远程执行)
- 您需要安装sshpass
- 一些innodb巨大的压缩表转储缓慢(可能是mysqldump的错误)
我在这里分享此脚本,希望社区可以对其进行改进。
(最好使用nano或其他为代码着色的编辑器查看)
- - - - - - - - - - - - - - - - - 剪这里 - - - - - - - - -------------------
#!/bin/bash
#set -x
#REQUIRED VARS
SOURCE_USER=root #MySQL user
SOURCE_HOST=localhost
SOURCE_PASSWORD=yourmysqlpass #optional
SOURCE_DBNAME=yourdbname
TARGET_HOST=192.168.1.2
TARGET_DBNAME=yourdbname
TARGET_SSHUSER=root
TARGET_SSHPASSWORD=yoursshpass #optional
TABLES='table1 table2 table3 table4'
TARGET_DIR="/data/dumpfiles"
EXEC_ACTION_TEXT[0]='Reimport TABLES directly into remote MySQL database'
EXEC_ACTION_TEXT[1]='Backup gzipped data to TARGED_DIR on remote TARGET_HOST'
EXEC_ACTION=0
#print config
echo "---------------------------------"
echo " SOURCE_USER: $SOURCE_USER (MySQL)"
if [ "SOURCE_PASSWORD" != "" ]; then
echo " SOURCE_PASSWORD:<present> "; else
echo " SOURCE_PASSWORD:<to be asked> "
fi
echo " SOURCE_HOST: $SOURCE_HOST "
echo " SOURCE_DBNAME: $SOURCE_DBNAME "
echo " TARGET_HOST: $TARGET_HOST "
echo " TARGET_DBNAME: $TARGET_DBNAME "
echo " TARGET_SSHUSER: $TARGET_SSHUSER "
if [ "TARGET_SSHPASSWORD" != "" ]; then
echo " TARGET_SSHPASS: <present> "; else
echo " TARGET_SSHPASS: <to be asked> "
fi
echo " TABLES: $TABLES "
echo " EXEC_ACTION: $EXEC_ACTION - ${EXEC_ACTION_TEXT[$EXEC_ACTION]}"
echo " TARGET_DIR: $TARGET_DIR (only for action 1)"
echo "---------------------------------"
echo "PRESS <ENTER> to continue..."; read; echo
#read the mysql password from command-line (SOURCE and TARGET uses the same password)
if [ "$SOURCE_PASSWORD" == "" ]; then
echo -n "Type $SOURCE_USER password for MySQL servers: "; read -s SOURCE_PASSWORD; echo
fi
echo "Creating database $TARGET_DBNAME on $TARGET_HOST if not exists ... "
mysql \
--user=$SOURCE_USER \
--password=$SOURCE_PASSWORD \
--host=$TARGET_HOST \
--execute "create database if not exists $TARGET_DBNAME;"
echo '--------------------------------------------------------------------------------------'
echo "**** ATTENTION ****: execute this command on mysql server at $TARGET_HOST :"
echo "GRANT ALL PRIVILEGES ON $TARGET_DBNAME.* TO '$SOURCE_USER'@'%' IDENTIFIED BY 'yourpass';"
echo '--------------------------------------------------------------------------------------'
echo "PRESS <ENTER> to continue..."; read; echo
#read the password from command-line
if [ "$TARGET_SSHPASSWORD" == "" ]; then
echo -n "Type the password for remote SSH Server (TARGET) ['$TARGET_SSHUSER'@'$TARGET_HOST']: "; read -s TARGET_SSHPASSWORD; echo
fi
for thistable in $TABLES
do
case "$EXEC_ACTION" in
0)
thisaction="gunzip | mysql --user=$SOURCE_USER --password=$SOURCE_PASSWORD -D $TARGET_DBNAME"
endmessage='remote reimporting has finished'
;;
1)
thisaction="cat > $TARGET_DIR/`date +%Y.%m.%d`-"$thistable".gz"
endmessage="$thisaction has finished"
;;
*) echo "EXEC_ACTION=$EXEC_ACTION not supported" && exit 1
esac
echo "---------------------------------------------------------------------"
echo "-- table $thistable"
echo "---------------------------------------------------------------------"
(
echo -n "-- setting variables... " > /dev/stderr #talk to user via stderr
echo "SET AUTOCOMMIT=0; SET UNIQUE_CHECKS=0; SET FOREIGN_KEY_CHECKS=0;"
echo -n "starting mysqldump... " > /dev/stderr
mysqldump --opt --user=$SOURCE_USER --password=$SOURCE_PASSWORD --host=$SOURCE_HOST $SOURCE_DBNAME $thistable
echo -n "done mysqldump, reseting variables... " > /dev/stderr
echo "SET FOREIGN_KEY_CHECKS=1; SET UNIQUE_CHECKS=1; SET AUTOCOMMIT=1;"
echo -n "commiting... " > /dev/stderr
echo "COMMIT;"
echo "done!" > /dev/stderr
) | \
gzip -c -2 | \
pv | \
sshpass -p $TARGET_SSHPASSWORD ssh $TARGET_SSHUSER'@'$TARGET_HOST $thisaction
echo $endmessage ' with exit status '$?
done
mysqldump | tar cf - | gzip -c | ssh ... 'cat > file.tgz'