数据库异地备份---服务器配置流程

1.需要借助expect这个软件,expect是在tcl的基础上建立的,所以在安装expect之前需要安装tcl

为了防止装的东西过于散乱,在root下新建一个文件夹tools

安装TCL

下载地址:http://www.tcl.tk/software/tcltk/download.html

[root@test ~]# cd /tools/

[root@test tools]# wget http://prdownloads.sourceforge.net/tcl/tcl8.5.19-src.tar.gz

[root@test tools]# tar xf tcl8.5.19-src.tar.gz

[root@test tools]# cd tcl8.5.19/unix/

[root@test unix]#./configure

这一步可能会报错,需要安装gcc库,所以先:yum install -y gcc

安装完了再:./configure

[root@test unix]# make

[root@test unix]# makeinstall

[root@test unix]# cd

安装expect

官网:http://expect.sourceforge.net/

[root@test ~]# cd /tools/

[root@test tools]# wget http://nchc.dl.sourceforge.net/project/expect/Expect/5.45/expect5.45.tar.gz 

[root@test tools]# tar xf expect5.45.tar.gz

[root@test tools]# cd expect5.45

[root@test expect5.45]#./configure --with-tcl=/usr/local/lib/ --with-tclinclude=/root/

tools/tcl8.5.19/generic/

[root@test expect5.45]# make

[root@test expect5.45]# make install

[root@test expect5.45]# cd

[root@mysql-master ~]# which expect

/usr/local/bin/expect

到这里,expect则安装完毕。

 

安装完了之后,先测试自动备份命令,目的是先把一对一公钥保存下来,以免在定时任务出现异常:

我是进入到home下,执行的以下命令:

scp xxxxxxxxxxx.gz root@测试服务器的ip:/home/from_other_server.gz

出现yes/no,选择yes,输入密码,回车。这样顺利的话就是已经把数据库备份到别的服务器上面了。

 

在/usr/sbin下新建一个scp.exp文件,里面加上代码:

#! /usr/local/bin/expect

# FileName:scp.exp

 

set timeout 60

 

 

if { [llength $argv] < 2} {

puts "Usage:"

puts "$argv0 local_file remote_path"

exit 1

}

 

 

set local_file [lindex $argv 0]

set remote_path [lindex $argv 1]

set passwd "xxxxxx"                       #这里的内容主要是备注,在正式文件中要记得删掉,这                                                                      个password是备份服务器的服务器密码

 

 

set passwderror 0

 

 

spawn scp $local_file $remote_path

 

 

expect {

"*assword:*" {

if { $passwderror == 1 } {

puts "passwd is error"

exit 2

}

set timeout 1000

set passwderror 1

send "$passwd\r"

exp_continue

}

"*es/no)?*" {

send "yes\r"

exp_continue

}

timeout {

puts "connect is timeout"

exit 3

}

}

文件的格式要是unix

然后在新建一个sh脚本文件,里面的内容为:

 

#! /bin/bash

# FileName:bakdb.sh

# this is a ShellScript for auto db backup

#

DB_NAME="test_server"   #要备份的数据库名

DB_USER="root"        #生产服务器的数据库用户名

DB_PASS="xxxxxxx"     #生产服务器的数据库密码

# Others vars

BIN_DIR="~"

BCK_DIR="/home/"

DATE=`date +%F`

TARGETPATH="/home/"      #备份服务器要放的数据库备份文件的地址

TARGETSERVERIP="xxxxx"     #备份服务器的ip

TARGETSERVERUSER="xxxxx"   #备份服务器的用户名

# TODO

#$BIN_DIR/

mysqldump --opt -u$DB_USER -p$DB_PASS $DB_NAME | gzip >$BCK_DIR/db_$DATE.gz

/usr/sbin/scp.exp $BCK_DIR/db_$DATE.gz $TARGETSERVERUSER@$TARGETSERVERIP:$TARGETPATH/$DB_NAME$DATE.gz

转载于:https://www.cnblogs.com/Yirson/p/9476105.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值