mariadb数据库自动备份和expect的使用

案例1:自动备份Mariadb脚本

 

实战:

1、自动备份mariadb脚本

需要做准备:登录到服务器上

安装mariadb-server

                      开启mariadb-server

[root@localhost ~]# yum-y install mariadb mariadb-server

软件包mariadb   #mariadb数据库Linux下的客户端

软件包mariadb-server#mariadb数据库

[root@localhost ~]#systemctl start mariadb

 

[root@localhost ~]# rpm-qf `which mysql`

mariadb-5.5.41-2.el7_0.x86_64

 

[root@localhost ~]# mysql

Welcome to the MariaDBmonitor.  Commands end with ; or \g.

Your MariaDB connectionid is 2

Server version:5.5.41-MariaDB MariaDB Server

 

Copyright (c) 2000, 2014,Oracle, MariaDB Corporation Ab and others.

 

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

 

MariaDB [(none)]> showdatabases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+

4 rows in set (0.01 sec)

 

MariaDB [(none)]>exit;

Bye

 

备份脚本:

说明:

find  . -type f -name *.sql |xargs rm -rf

xargs 构造参数列表并运行命令

 

 

sh  -x  调试脚本

 

自动解压ZIP数据包 脚本

如下脚本为自动解压zip包脚本,仅供参考,可以根据实际情况修改,可以举一反三,应用到其他的应用中。

 

awk -F. '{print $2}'     #awk 列操作

-F.  以.作为分隔符   

print 输入

$2  表示每2列

 

实战:expect实现无交互登录

expect是在tcl基础上创建起来的,它还提供了一些tcl所没有的命令,它可以用来做一些linux下无法做到交互的一些命令操作

 

安装expect

[root@xuegod163 ~]# yum -yinstall expect

也可以通过源码包的方式进行安装

源码下载链接

http://jaist.dl.sourceforge.net/project/tcl/Tcl/8.6.4/tcl8.6.4-src.tar.gz

http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz/download

 

使用expect创建脚本的方法

1)定义脚本执行的shell

#!/usr/bin/expect

这里定义的是expect可执行文件的链接路径(或真实路径),功能类似于bash等shell功能

 

2)set timeout 30

设置超时时间,单位是秒,如果设为timeout -1 意为永不超时

 

3)spawn

spawn 是进入expect环境后才能执行的内部命令,不能直接在默认的shell环境中进行执行

主要功能:传递交互指令

 

4)expect

这里的expect同样是expect的内部命令

主要功能:判断输出结果是否包含某项字符串,没有则立即返回,否则就等待一段时间后返回,等待时间通过timeout进行设置

 

5)send

执行交互动作,将交互要执行的动作进行输入给交互指令

命令字符串结尾要加上"r",如果出现异常等待的状态可以进行核查

 

6)interact

执行完后保持交互状态,把控制权交给控制台

如果不加这一项,交互完成会自动退出

 

7)exp_continue

继续执行接下来的交互操作

8)$argv

expect 脚本可以接受从bash传递过来的参数,可以使用 [lindex $argv n]获得,n从0开始,分别表示第一个,第二个,第三个……参数

 

实战:通过expect实现无交互式ssh远程登录

[root@localhost ~]# vimssh.exp

#!/usr/bin/expect

set ipaddress"192.168.1.69"

set passwd"123456"

set timeout 30

spawn ssh rm@$ipaddress

expect {

"yes/no" { send"yes\r";exp_continue }

"password:" {send "$passwd\r" }

}

interact

 

执行结果:

[root@localhost ~]#./ssh.exp

spawn ssh [email protected]

[email protected]'s password:

 

2)通过调用bash的位置参数实现ssh远程登录

[root@localhost ~]# vimssh2.exp

#!/usr/bin/expect

set ipaddress [ lindex$argv 0 ]

set passwd [ lindex $argv1 ]

set user [ lindex $argv 2]

set timeout 30

spawn ssh$user@$ipaddress

expect {

"yes/no" { send"yes\r";exp_continue }

"password:" {send "$passwd\r" }

}

interact

执行结果

[root@localhost ~]#./ssh2.exp 192.168.1.69 123456 root

spawn [email protected]

[email protected]'spassword:

Last login: Wed Dec 3021:47:25 2015 from 192.168.1.68

 

 学习过程中如果问题,请留言。更多内容请加:
学神IT-linux讲师-RM老师QQ:2805537762 
学神IT-戚老师QQ:3341251313 
学神IT-旭斌QQ:372469347
学神IT教育RHEL7交流群:468845589

 

 



你可能感兴趣的:(mariadb数据库自动备份和expect的使用)