您的位置:首页 > 运维架构 > Linux

Linux rsync+crontab实现数据实时同步

2013-11-06 15:29 579 查看
一、服务器端配置:
1.安装:
[root@localhost ~]#yum install rsync
[root@localhost ~]# rpm -qa | grep rsync
rsync-3.0.6-4.el5_7.1
[root@localhost ~]#
2.手动创建配置文件:
[root@localhost ~]# vim /etc/rsyncd.conf
uid = root                #运行rsync进程的uid(可定义在模块中)
gid = root               #运行rsync进程的gid(可定义在模块中)
use chroot = no         #不使用chroot
max connections = 2    #最大连接数
strict modes = yes     #是否检查口令文件的权限
port = 873             #指定端口号,默认是873
[test]             #认证的模块名,在client端需要指定
path = /root/abc/     #要备份的目录路径
comment = This is a test   #模块注释信息
ignore errors            #可以忽略一些无关的IO错误
#read only = no      #no客户端可上传文件,yes只读
#write only = no     #no客户端可下载文件,yes不能下载
auth users = zdx       #认证的用户名,如果没有这行,则表明是匿名
secrets file = /etc/rsync.pas     #指定认证口令文件路径
hosts allow = *     #允许任何主机连接
#hosts deny = 1.1.1.1         #禁止指定的主机连接
#motd file = /etc/rsyncd.motd  #欢迎信息文件名称和存放位置(需手动添加,可有可无)
3.创建口令认证文件:
[root@localhost ~]# vim /etc/rsync.pas
zdx:123456    #用户名:密码
[root@localhost ~]# ll /etc/rsync.pas
-rw------- 1 root root 11 Sep 17 14:26 /etc/rsync.pas
[root@localhost ~]#
注:要将rsync.pas设置为root拥有,且权限为600。
3.启动rsync服务:
[root@localhost ~]# vim /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
disable = yes
socket_type     = stream
wait            = no
user            = root
server          = /usr/bin/rsync
server_args     = --daemon
log_on_failure  += USERID
}
将disable参数值yes修改为no。
以下2种选一即可:
①独立启动:
[root@localhost ~]# /usr/bin/rsync --daemon
[root@localhost ~]#vim /etc/rc.local       ----在文件末尾加入一行/usr/bin/rsync --daemon使其开机自启动
②有xinetd超级进程启动:
[root@localhost ~]# /etc/rc.d/init.d/xinetd start
4.查看服务是否启动:

[root@localhost ~]# netstat -nlutp | grep rsync
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      2893/rsync
tcp        0      0 :::873                      :::*                        LISTEN      2893/rsync

二、客户端配置
1.安装:

[root@localhost ~]#yum install rsync
[root@localhost ~]# rpm -qa | grep rsync
rsync-3.0.6-4.el5_7.1
[root@localhost ~]#

2.创建密码文件(文件名自定):

[root@localhost ~]# vim passwd
123456     #要求只写密码即可

3.设置crontab定时任务:

[root@localhost ~]# crontab -e
*/1 * * * *     /usr/bin/rsync -avu --password-file=/root/passwd zdx@192.168.11.85::test /root/abc/

到此为止,配置完毕,剩下的时间就是自己手动在服务器端添加文件,坐等客户端自动同步吧。
糟糕,都5分钟了还没有文件同步过来,看下客户端邮件有报错信息:

password file must not be other-accessible
continuing without password file
Password: @ERROR: auth failed on module test
rsync error: error starting client-server protocol (code 5) at main.c(1530) [rec
eiver=3.0.6]
&

得知密码文件必须不能有其他的用户访问,所以需要将密码文件权限修改为600:

[root@localhost ~]# chmod 600 passwd
[root@localhost ~]# ll passwd
-rw------- 1 root root 7 Sep 17 14:56 passwd

一分钟后成功同步:




同样也可以通过客户端邮件查看同步信息!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息