CentOS 无密码将文件备份到另一台服务器

来源:互联网 发布:xalharnet软件下载 编辑:程序博客网 时间:2024/05/17 01:10

假设现在有以下两台服务器

服务器A:文件服务器

服务器B:备份服务器


现在要将服务器A上/usr/local/backup/下的文件备份到服务器B上/usr/local/backup/目录下

1.以root用户登录服务器A,执行ssh-keygen -b 1024 -t rsa

Generatingpublic/private rsa key pair.

Enter file inwhich to save the key (/root/.ssh/id_rsa):

Enter passphrase(empty for no passphrase):          <-- 直接输入回车

Enter samepassphrase again:                          <-- 直接输入回车

Youridentification has been saved in /root/.ssh/id_rsa.

Your public keyhas been saved in /root/.ssh/id_rsa.pub.

The key fingerprintis:

49:9c:8a:8f:bc:19:5e:8c:c0:10:d3:15:60:a3:32:1croot@Client

注意:在程序提示输入passphrase时直接输入回车,表示无证书密码。


2.在服务器A执行 scp -P 服务器B端口 /root/.ssh/id_rsa.pub root@服务器B的IP:/root/.ssh/authorized_key,输入服务器B的密码,然后输入yes回车,会显示公钥上传进度。  #将公钥从服务器A传到服务器B,建立安全信任关系


3.在服务器A新建脚本,  vi /usr/local/backup.sh  #新建脚本backup.sh,输入 

#!/bin/sh

scp -P服务器B端口 /usr/local/backup/文件名 root@服务器B的IP:/usr/local/backup/文件名


4.将脚本/usr/local/backup.sh加入到crontab定时任务中

vi crontab #编辑定时任务,加入下面一句话

12 12 * * * root/usr/local/backup.sh  #每天12点12分执行backup.sh脚本


5.在服务器A上编辑sshd_config, vi /etc/ssh/sshd_config, 开启以下内容

HostKey /etc/ssh/ssh_host_rsa_key

RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile      .ssh/authorized_keys


6.注意backup文件夹,backup.sh,以及公私钥的权限,

这样就能实现无密码将服务器A上的文件备份到服务器B上。

0 0