使用rsync实现文件同步

第一步,先来下载这个免费的软件。

  use chroot = no # 不使用chroot

  max connections = 10 # 最大连接数为10

  #motd file = /etc/rsyncd.motd #欢迎信息文件名称和存放位置(此文件没有,可以自行添加)

  pid file = /var/run/rsyncd.pid #指定rsync的pid文件

  lock file = /var/run/rsync.lock #指定支持max connections参数的锁文件,默认值是/var/run/rsyncd.lock

  log file = /var/log/rsyncd.log #指定rsync的日志文件,而不将日志发送给syslog

  #下面就是每个模块的配置了

  [rsync_module_name1] # 这里是认证的模块名,在client端需要指定

  path = /rsync_path/dir1 # 需要做镜像的目录

  comment = rsync files

  ignore errors # 可以忽略一些无关的IO错误

  read only = yes # 只读

  list = no # 不允许列文件

  auth users = rsync_user # 认证的用户名,如果没有这行,则表明是匿名,多个用户用,分隔

  secrets file = /etc/rsyncd.pas # 认证文件名

  #pid file = /var/run/rsyncd.pid

  #log file = /var/log/rsyncd.log

  #lock file = /var/run/rsync.lock

  其中,个人认为,没项配置都可以既在全局配置,也可以在模块内部配置,其中如果模块内部如果有与全局相同的配置,

  则模块内部的配置覆盖全局的配置。

  ok,现在启动下试试看吧。

  如果要在启动时把服务起来,有几种不同的方法,比如:

  加入inetd.conf

  编辑/etc/services,加入rsync 873/tcp,指定rsync的服务端口是873

  编辑/etc/inetd.conf,加入rsync stream tcp nowait root /bin/rsync rsync –daemon

  加入rc.local

  在各种操作系统中,rc文件存放位置不尽相同,可以修改使系统启动时rsync –daemon加载进去。

  记得配置防火墙,允许你rsync端口的tcp和udp协议。

  873:tcp 873:udp

  下面这个命令行中-vzrtopg里的v是verbose,

  z是压缩传输,

  r是recursive,

  topg都是保持文件原有属性如属主、时间的参数。

  u是只同步已经更新的文件,避免没有更新的文件被重复更新一次,不过要注意两者机器的时钟的同步。

  –progress是指显示出详细的进度情况,

  –delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致。后面的rsync_user@192.168.0.2::rsync_module_name1中,之后的rsync_module_name1是模块名,也就是在/etc/rsyncd.conf中自定义的名称,rsync_user是指定模块中指定的可以同步的用户名。最后的/tmp是备份到本地的目录名。在这里面,还可以用-e ssh的参数建立起加密的连接。

  可以用–password-file=/password/path/file来指定密码文件,这样就可以在脚本中使用而无需交互式地输入验证密码了,这里需要注意的是这份密码文件权限属性要设得只有属主可读。

  #/usr/local/rsync/bin/rsync -vzrtopg –progress –delete rsync_user@192.168.0.2::rsync_module_name1 /tmp/

  Password:

  或者你也可以制定password文件

  由于需要系统crontab执行,所以这里采用读入密码文件的方式,

  这个时候就成功了。

  #vi /etc/rsyncd.pas

  加入

  注意,客户端的密码文件只需要密码,而不需要用户名!

  hosts allow = 192.168.1.38,192.168.1.3,192.168.1.138

  #hosts deny = 0.0.0.0/32

  use chroot = no

  max connections = 4

  pid file = /var/run/rsyncd.pid

  lock file = /var/run/rsync.lock

  log file = /var/log/rsyncd.log

  [www]

  path = /www

  ignore errors

  read only = yes

  list = no

  auth users = backup

  secrets file = /etc/backserver.pas

  [snmp]

  path = /usr/local/net-snmp

  ignore errors

  read only = yes

  list = no

  auth users = backup

  secrets file = /etc/backserver.pas

  建立backserver.pas

  #echo “backup:backserver” > /etc/backserver.pas

  服务器启动服务

  /usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd.conf

  客户端运行的命令

  /usr/local/rsync/bin/rsync -vzrtopgu --progress --delete --password-file=/etc/backserver.pas backup@192.168.1.238::snmp /tmp

  /usr/local/rsync/bin/rsync -vzrtopgu --progress --delete --password-file=/etc/backserver.pas backup@192.168.1.238::www /tmp

  问题排除:

  1、

  [root@bo bin]# /usr/local/rsync/bin/rsync -vazu -progress --delete backup@192.168.1.238::www /tmp --password-file=/etc/backserver.pas

  rsync: Failed to exec ss: No such file or directory (2)

  rsync error: error in IPC code (code 14) at pipe.c(86) [receiver=2.6.9]

  rsync: writefd_unbuffered failed to write 12 bytes [receiver]: Broken pipe (32)

  rsync error: error in rsync protocol data stream (code 12) at io.c(1122) [receiver=2.6.9]

  [root@ht-store bin]#

  以上错误可能是上面使用的参数前面的符号不正确。换成下面的就没有上面的错误了

  [root@bo /]# usr/local/rsync/bin/rsync -vzrtopgu --progress --delete --password-file=/etc/backserver.pas backup@192.168.1.238:www /tmp

  2、

  [root@ht-store /]# usr/local/rsync/bin/rsync -vzrtopgu --progress --delete backup@192.168.1.238:www /tmp/ --password-file=/etc/backserver.pas backup@192.168.1.238's password:

  Permission denied, please try again.

  backup@192.168.1.238's password:

  Permission denied, please try again.

  backup@192.168.1.238's password:

  Permission denied (publickey,gssapi-with-mic,password).

  rsync: connection unexpectedly closed (0 bytes received so far) [receiver]

  rsync error: unexplained error (code 255) at io.c(453) [receiver=2.6.9]

  [root@ht-store /]#

  更改成

  [root@ht-store /]# usr/local/rsync/bin/rsync -vzrtopgu --progress --delete --password-file=/etc/backserver.pas

  backup@192.168.1.238::www /tmp

  @ERROR: auth failed on module www

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

  [root@ht-store /]#

  3、

  [root@ht-store /]# /usr/local/rsync/bin/rsync -vazu --progress --delete --password-file=/etc/backserver.pas

  backserver@192.168.1.238::www /tmp

  @ERROR: auth failed on module www

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

  检查一下用户名是否错误backserver 应该是backup

  4、

  [root@ht-store /]# /usr/local/rsync/bin/rsync -vzar --progress --delete --password-file=/etc/backserver.pas backup@192.168.1.238:www /tmp

  backup@192.168.1.238's password:

  Permission denied, please try again.

  backup@192.168.1.238's password:

  rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(244) [receiver=2.6.9]

  是双引号“::”backup@192.168.1.238::www 尔不是单引号“:”backup@192.168.1.238:www

  5、

  [root@ht-store /]# /usr/local/rsync/bin/rsync -vzrtopgu --progress --delete --password-file=/etc/backserver.pas backup@192.168.1.238::www /tmp

  @ERROR: Unknown module 'www'

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

  这是因为服务器端hosts allow 没有允许该主机访问

  6、

  # chmod 600 rsyncd.conf rsyncd.secrets

  -rw------- 1 root root 795 Aug 23 06:21 rsyncd.conf

  -rw------- 1 root root 37 Aug 23 06:23 rsyncd.secrets

  注意:这个安全配置很重要,假如不这样做客户机连接服务器时会提示:

  @ERROR: auth failed on module smb_recover_files

  rsync: connection unexpectedly closed (102 bytes read so far)

  rsync error: error in rsync protocol data stream (code 12) at io.c(165)

  服务器端日志将出现错误提示:

  换一个备份目录就可以了。

  在Windows下采取从文件读取密码方式出现的错误信息

  必需条件密码文件存放的位置一定要是ntfs分区,另一定要注意是下面的书写格式,--password-file=F:pw一定要在后面指定

  D:LinuxBakbbsweb>rsync -vzrtopgu --progress --delete rsync://rsync_user@192.168.6.143/bbsweb ./ --password-file=F:pw

  当执行以下命令,出现错误信息时

  D:LinuxBakmysqldb>rsync -vzrtopgu --progress --delete rsync://rsync_user@192.168.6.143/mysqldb ./ --password-file=F:pw

  错误信息:

  rsync: mkstemp "/cygdrive/d/LinuxBak/mysqldb/mysql/.dbvpopmail081 213-14:01.tar.g

  z.3favI3" failed: No such file or directory (2)

  101974 100% 529.70kB/s 0:00:00 (xfer#11, to-check=1/15)

  rsync: mkstemp "/cygdrive/d/LinuxBak/mysqldb/mysql/.dbvpopmail081 213-15:01.tar.g

  z.xI3P0F" failed: No such file or directory (2)

  101975 100% 529.71kB/s 0:00:00 (xfer#12, to-check=0/15)

  这是因为在服务器端的备份文件dbvpopmail081213-15:01.tar.gz中有非法符号,可能造成Windows同步数据时无法识别文件名,尔导致问题。将服务器端文件改名为dbvpopmail0812131501.tar.gz 。这样在执行同样的命令就OK了! :) :) :)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值