您的位置:首页 > 运维架构 > Linux

Centos 7.6 安装expect,使用expect远程登录,并异地多机房备份数据库

2020-05-13 17:42 92 查看

检查是否安装expect

[root@VM_32_11_centos ~]# rpm -qa|grep expect
[root@VM_32_11_centos ~]#

没有任何返回,就说明没有安装expect

安装expect

[root@VM_32_11_centos ~]# yum install expect
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
epel                                                                                                                                            | 4.7 kB  00:00:00
extras                                                                                                                                          | 2.9 kB  00:00:00
os                                                                                                                                              | 3.6 kB  00:00:00
updates                                                                                                                                         | 2.9 kB  00:00:00
(1/7): epel/7/x86_64/group_gz                                                                                                                   |  95 kB  00:00:00
(2/7): epel/7/x86_64/updateinfo                                                                                                                 | 1.0 MB  00:00:00
(3/7): os/7/x86_64/group_gz                                                                                                                     | 153 kB  00:00:00
(4/7): updates/7/x86_64/primary_db                                                                                                              | 176 kB  00:00:00
(5/7): extras/7/x86_64/primary_db                                                                                                               | 190 kB  00:00:00
(6/7): os/7/x86_64/primary_db                                                                                                                   | 6.1 MB  00:00:01
(7/7): epel/7/x86_64/primary_db                                                                                                                 | 6.8 MB  00:00:01
Determining fastest mirrors
Resolving Dependencies
--> Running transaction check
---> Package expect.x86_64 0:5.45-14.el7_1 will be installed
--> Processing Dependency: libtcl8.5.so()(64bit) for package: expect-5.45-14.el7_1.x86_64
--> Running transaction check
---> Package tcl.x86_64 1:8.5.13-8.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================
Package                                Arch                                   Version                                        Repository                          Size
=======================================================================================================================================================================
Installing:
expect                                 x86_64                                 5.45-14.el7_1                                  os                                 262 k
Installing for dependencies:
tcl                                    x86_64                                 1:8.5.13-8.el7                                 os                                 1.9 M

Transaction Summary
=======================================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 2.1 M
Installed size: 4.9 M
Is this ok [y/d/N]: y
Downloading packages:
(1/2): expect-5.45-14.el7_1.x86_64.rpm                                                                                                          | 262 kB  00:00:00
(2/2): tcl-8.5.13-8.el7.x86_64.rpm                                                                                                              | 1.9 MB  00:00:00
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                  2.8 MB/s | 2.1 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:tcl-8.5.13-8.el7.x86_64                                                                                                                           1/2
Installing : expect-5.45-14.el7_1.x86_64                                                                                                                         2/2
Verifying  : 1:tcl-8.5.13-8.el7.x86_64                                                                                                                           1/2
Verifying  : expect-5.45-14.el7_1.x86_64                                                                                                                         2/2

Installed:
expect.x86_64 0:5.45-14.el7_1

Dependency Installed:
tcl.x86_64 1:8.5.13-8.el7

Complete!
[root@VM_32_11_centos ~]#

中途输入一下y 回车。
最后看到Complete! 就说明已经安装完成。
可以使用命令检查一下:

[root@VM_32_11_centos ~]# rpm -qa|grep expect
expect-5.45-14.el7_1.x86_64
[root@VM_32_11_centos ~]#

expect 示例程序

程序用途:远程登录数据库服务器备份数据库,并将备份文件二次备份到其他服务器。

准备三台机器:
192.168.1.100 安装了expect
192.168.1.101 安装了mysql 数据库
192.168.1.102 用于存放备份数据文件

远程登录192.168.1.100 服务器,安装expect 工具(上面的操作步骤即可)。
创建脚本文件:

[root@VM_32_11_centos ~]# touch expect_test.sh
[root@VM_32_11_centos ~]#
[root@VM_32_11_centos ~]# vim expect_test.sh

输入如下代码:

#!/usr/bin/expect

set ip 192.168.1.101
set user mysql
set pass password
set timeout 600

set time [exec date "+%Y%m%d_%H%M%S"]
set path [lrange $argv 0 0]

spawn ssh $user@$ip
expect {
"(yes/no)" {send "yes\r"; exp_continue}
"password:" {send "$pass\r"}
}

set db_username username
set db_password password
set db_database test_db
expect "mysql@*" {
send "/home/mysql/bin/mysqldump -h127.0.0.1 -P3306 -u$db_username -p$db_password $db_database -S /home/mysql/mysql.sock | gzip  > $path/db_back_$db_database_$time.sql.gz\r"
}

set remote_ip 192.168.1.102
set remote_user user
set remote_password password
expect "mysql@*" {
send "scp $path/db_back_$db_database_$time.sql.gz $remote_user@$remote_ip:/home/$remote_user/db/back/db_back_$db_database_$time.sql.gz\r"
}

expect {
"(yes/no)" {send "yes\r"; exp_continue}
"password:" {send "$remote_password\r"}
}

expect "mysql@*" {send "exit\r"}
exit

最后保存退出,然后就可以执行命令:
expect expect_test.sh /local/path
说明:这里的/local/path 根据实际情况指定,程序中表示备份文件的存储路径。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: