用shell脚本同步两个mysql数据库

2013-06-05

如何同步mysql数据库和网站。

1、配置ssh无密码访问
https://www.wallcopper.com/linux/441.html

2、crontab计划任务添加backup.sh脚本并执行远程备份服务器restore.sh脚本
0 1 * * * /data/backup/backup.sh;

3、backup.sh脚本
#!/bin/bash
. ~/.bash_profile
cd /data/backup
#backup testdb
mysqldump -utest -ptest123456 testdb table01 > wallcopper.sql
#archive
tar zcf wallcopper.sql.tar.gz wallcopper.sql
#scp backup file to remote
scp -P322 wallcopper.sql.tar.gz root@192.168.100.1:/data/backup
#rsync web
rsync -av '-e ssh -p 322' /data/test root@192.168.100.1:/data
#restore
ssh -p322 root@192.168.100.1 "/data/backup/restore.sh"

---------------------------------------------------------------------------
4、restore.sh
#!/bin/bash
. ~/.bash_profile
tar zxf wallcopper.sql.tar.gz -C /data/backup

mysql -utest -ptest123456 -Dtestdb < wallcopper.sql mysql -utest -ptest123456 -Dtesetdb -e "select option_id,option_value from wp_options where option_id in (1,36)" mysql -utest -ptest123456 -Dtestdb -e "update wp_options set option_value='http://wallcopper.oicp.net' where option_id in (1,36)" mysql -utest -ptest123456 -Dtestdb -e "commit" 5、也可以通过冷备份来进行 检查datadir show variables like 'datadir'; 将datadir数据文件直接传输到远程。 表文件:.frm InnoDB引擎数据文件:.ibd文件 MyISAM引擎数据文件:.MYD文件 MyISAM引擎索引文件:.MYI文件

分类:数据库 | 标签: |

相关日志

评论被关闭!