• linux把文件从A服务器备份到B服务器的两种办法


    1.建立ssh的信任关系

      1.1 进入A服务器的root文件夹里面的.ssh目录

    cd /root/.ssh
    

      1.2 生成公钥和私钥,命令行如下,并且一直回车即可

    ssh-keygen -t rsa
    

      1.3 把公钥内容添加到临时文件authorized_keys_server1中

    cat id_rsa.pub >authorized_keys_server1
    

      1.4 把临时文件发送到服务器B下的root下的.ssh文件夹下

    scp /root/.ssh/authorized_keys_server1 root@服务器B的ip:/root/.ssh/
    

      1.5 打开服务器B,进入到.ssh目录下,把临时文件添加到该目录下的authorized_keys文件中

    cat authorized_keys_server1 >>authorized_keys
    

      之后服务器A通过scp传输文件到服务器B,就不需要密码了。

    2.通过TCL, expect结合scp传输文件

      2.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 test.txt root@测试服务器的ip:/home/test.txt

    出现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 "password"                       #这里的内容主要是备注,在正式文件中要记得删掉,这                                                                      个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
    "
    
    exp_continue
    
    }
    
    "*es/no)?*" {
    
    send "yes
    "
    
    exp_continue
    
    }
    
    timeout {
    
    puts "connect is timeout"
    
    exit 3
    
    }
    
    }
    
    #文件的格式要是unix
    

     最后再编写一个.sh脚本,结合cron即可实现从A服务器传文件到B服务器

  • 相关阅读:
    java的语法基础(二)
    MyBatis 的好处是什么?
    python中字符串的编码和解码
    Spring的简介和优点?
    相对于Statement,PreparedStatement的优点是什么?
    MyBatis 的好处是什么?
    .final finally finalize区别
    final类有什么用
    web广泛用到的技术:
    JDK,JRE,JVM三者的关系
  • 原文地址:https://www.cnblogs.com/Yirson/p/13130954.html
Copyright © 2020-2023  润新知