使用expect进行 rsync进行服务器同步备份

来源:互联网 发布:php九九乘法表表格 编辑:程序博客网 时间:2024/06/06 20:56

使用expect去除交付过程,达到能够实现服务器同步备份

第一:安装expect 

检查是否已安装expect

rpm -qa | grep tcl

tclx-8.4.0-5.fc6

tcl-8.4.13-4.el5

第二:安装expect

下载地址:

http://dl.oschina.NET/soft/expect

(或   https://sourceforge.net/projects/expect/)

检查tcl的安装位置

rpm -qa|grep tcl 

需要安装tcl-devel

rpm -ivh   tcl-devel-8.4.13-4.el5.x86_64.rpm

(或  yum install -y tcl-devel tk-devel    )

解压expect源码包

  tar xzvf expect-5.45-1374045102000.tar.gz 

  cd expect5.45/

  编译:需要查找/usr/lib64/tclConfig.sh所在的路径

  ./configure --with-tcl=/usr/lib64/

 make 

make install

运行expect

[root@localhost expect5.45]# expect
expect: error while loading shared libraries: libexpect5.45.so: cannot open shared object file: No such file or directory

find / -name libexpect5.45.so

ln -sf /usr/lib/expect5.45/libexpect5.45.so  /usr/lib64/libexpect5.45.so

再运行expect

expect1.1> [root@localhost /]#  

就好了


加密,解密工具

下会进行expect 脚本的书写:

[ruby] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #!/usr/bin/expect -f  
  2. ##########################################################  
  3. #     1.service ip  
  4. #     2.User  
  5. #     3.userPassword  
  6. #     4.localPath  [本地路径]  
  7. #     5.serverPath [server端路径]  
  8. #返回值:  
  9. #     0  成功  
  10. #     1  参数个数不正确  
  11. ###########################################################  
  12.   
  13.   
  14. proc usage {} {  
  15.     regsub ".*/" $::argv0 "" name  
  16.     send_user "Usage:\n"  
  17.     send_user "$name serviceip  User userPassword localPath serverPath\n"  
  18.     exit 1  
  19. }  
  20.     
  21. ## 判断参数个数  
  22. if {[llength $argv] != 5} {  
  23.     usage  
  24. }  
  25.   
  26.   
  27. #设置变量值  
  28. set severip [lindex $argv 0]  
  29. set User [lindex $argv 1]  
  30. set userPassword [lindex $argv 2]  
  31. set localPath [lindex $argv 3]  
  32. set serverPath [lindex $argv 4]  
  33.   
  34.   
  35.   
  36.   
  37. #定义变量标记rsync连接时是否输入yes确认  
  38. set inputYes 0  
  39.   
  40.   
  41. #rsync -avz /etc/ 192.168.15.234:/home/7_8  
  42. spawn rsync -avz ${localPath} ${User}@${severip}:${serverPath}  
  43. expect {  
  44.     -nocase -re "yes/no" {  
  45.         send -- "yes\r"   
  46.         set inputYes 1     
  47.     }  
  48.     -nocase -re "assword: " {  
  49.         send -- "${userPassword}\r"  
  50.                 interact  
  51.     }  
  52.     -nocase -re "Connection refused" {  
  53.         send_error "Sftp services at ${ftpServerIp} is not active.\n"  
  54.         exit 2  
  55.     }  
  56.     timeout {  
  57.         send_error "Connect to sftp server ${ftpUser}@${ftpServerIp} timeout(10s).\n"  
  58.         exit 8  
  59.     }  
  60. }  
  61.   
  62.   
  63. #如果输入了yes确认,输入密码  
  64. if {$inputYes==1} {  
  65.     expect {  
  66.         -nocase -re "assword: " {  
  67.             send -- "${userPassword}\r"  
  68.             interact  
  69.             }  
  70.     }   
  71. }  

Rsync(remote synchronize) 是一个远程数据同步工具,可以使用“Rsync算法”同步本地和远程主机之间的文件。

rsync的好处是只同步两个文件不同的部分,相同的部分不在传递。类似于增量备份,

这使的在服务器传递备份文件或者同步文件,比起scp工具要省好多时间。

具体的用法:

1.在本地机器上对两个目录同步

$ rsync -zvr filename1 filename2

 

上述代码是将filename1中的文件与filename2中的文件同步,如果将filename2中的文件同步到filename1中,修改代码为:

$ rsync -zvr filename2 filename1

参数说明:

-z 开启压缩

-v 详情输出

-r 表示递归

2.使用rsync –a 同步保留时间按标记

$ rsync -azv filename1 filename2

 

使用上述命令,将filename2中新同步的文件的时间与filename1中的创建的时间相同,

它保留符号链接、权限、时间标记、用户名及组名相同。

3.从本地同步文件到远程服务器

$rsync -avz filename1 root@192.168.0.1:/home/redhat/filename2

上述命令是将本地的filename1同步到远程192.168.0.1的主机上。

注意:如果远程主机的端口不是默认的22端口,假如是3000端口,上述的命令修改为,

$ rsync -avz '-e ssh -p 4000' filename1 root@192.168.0.1:/home/redhat/filename2

 

4.将远程服务器的文件同步到本地

与步骤3类似,只是将filename1与远程服务器的位置对换一下,

$rsync -avz root@192.168.0.1:/home/redhat/filename2 filename1

 

同理如果端口不是22,使用以下命令

$ rsync -avz '-e ssh -p 4000' root@192.168.0.1:/home/redhat/filename2 filename1
0 0