rsync同步两台服务器

需求:两台服务器(A、B),在服务器A上存放着大量的图片,需要在每天夜里三点同步到服务器B,源目录和目的目录都是/image

启用程序:rsync

服务器A上的配置:

1、/etc/rsyncd.conf

uid = nobody

gid = nobody

use chroot = no

max connectionts = 6

read only = yes

hosts allow = 192.168.95.0/24

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsyncd.lock

log file = /var/log/rsyncd.log

[image]

comment = image

path = /image/

ignore errors

list = no

auth users = root

secrets file = /etc/rsyncd.passwd

2、/etc/rsyncd.passwd

root:test

完成上面的配置后运行

# rsync --daemon

如果提示没有找到rsyncd.passwd文件则说明这个文件没有在默认的/etc/路径下或者没有创建,只要把这个文件移动过去就可以了

服务器B上的配置:

/etc/rsync.passwd

dongpi2014

这里要注意,服务器A和服务器B上对于密码文件的写法是不一样的,服务器A的密码文件需要记录用户名,而服务器B的密码文件不需要记录用户名。

之后执行命令

rsync -arv  --password-file=/etc/rsyncd.passwd  rsync://root@192.168.95.11/test /image/

这样就开始同步了

注意

如果有以下的报错:

@ERROR: Unknown module 'xxx'

rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]

这个报错表明密码文件的权限有问题,正常的密码文件权限应为600


The --password-file option may only be used when accessing an rsync daemon.

这个报错表明服务器端没有执行rsync --daemon命令(没有启动服务)

@ERROR: auth failed on module xxxxx

这个报错表明服务器A和服务器B里面的用户名密码有问题,有可能是服务器B里面用户名密码格式问题。

除了以上方法外,还可以用expect的自动交互脚本完成

#!/usr/bin/expect

set timeout 3000

spawn rsync -avcz --delete root@192.168.95.11:/image/  /image

expect "password:"

send "dongpi2014\r"

expect "]*"


exit

interact

利用inotifywait监控主机文件和目录

利用inotify+rsync实现Linux文件批量更新

inotify-tools+rsync实时同步文件安装和配置

rsync同步完整配置

CentOS 6.5下Rsync远程同步

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/9203ac12fcb1f366ea05be50feae4b53.html