一、环境描述
操作系统: CentOS release 6.5 (Final)
机器A:10.10.10.102
机器B:10.10.10.101
业务要求:将B数据中的文件非实时同步到A机器中。
二、操作步骤
- 建立B到A的信任关系
a、生成私钥和公钥
[[email protected] .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key(/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
……
b、将B的公钥放到A的目录中
[[email protected] .ssh]# scp ~/.ssh/id_rsa.pub [email protected]:~/.ssh/authorized_keys
c、修改B中的sshd_config配置,将以下注释取消
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
d、重启B中的sshd服务
[[email protected] ~]# service sshd restart
停止 sshd:[确定]
正在启动 sshd:[确定]
e、ssh登陆A机器不需要密码
[[email protected] .ssh]# ssh [email protected]
Last login: Sat Jul 12 07:39:47 2014 from10.10.10.102
三、 crontab建立同步
*/1 * * * * /usr/bin/rsync -a --progress /root/test/ [email protected]:~/test >>~/rsync.log 2>&1
日志如下:
[[email protected]~]# tail -f rsync.log
sendingincremental file list
sent 64bytes received 12 bytes 50.67 bytes/sec
totalsize is 1073741828 speedup is14128181.95
tail:rsync.log:文件已截断
sendingincremental file list
./
tap.D1.tar.gz
6137477 100% 60.64MB/s 0:00:00 (xfer#1, to-check=0/3)
sent6138337 bytes received 34 bytes 12276742.00 bytes/sec
totalsize is 6137481 speedup is 1.00
tail:rsync.log:文件已截断
sending incremental file list