0%

Rsync使用简介

安装配置

安装

直接 yum 安装即可。

1
yum -y install rsync

配置

Rsync 分服务端和客户端,涉及三个配置文件:

  • rsyncd.conf - 具体的配置,放于目标端,也就是服务端提供的同步模块。
  • rsyncd.secrets - auth users用户列表的账号密码。
  • rsync.key - daemon模式时的密码文件,用于校验目标端 secrets。

比如从A(客户端)同步文件到B(服务端),则配置对应如下:
alt

同步命令

在客户端执行,eg.

1
rsync -avzP --delete --exclude "*.log" --password-file=/etc/rsync.key --backup --backup-dir=/server_backup/ /home/mydata/ rsync_user@192.168.0.2::rsync_your_data >> /var/log/rsync_to_02.log

其中:
rsync_your_data 是服务端 rsyncd.conf 中定义的同步模块名称。
192.168.0.2 是目标机器IP,即服务端IP。
--backup-dir 指定的是目标上的备份文件目录,这个目录以 rsyncd.conf 中对应模块配置的 path 为相对路径,比如 path=/data/mydata/,则目标上实际备份目录为 /data/server_backup/。

部分参数说明:

  • -avzP a归档模式(递归传输并保持文件属性),v显示详情,z传输时进行压缩提高效率,P显示传输进度信息。
  • --password-file daemon模式时的密码文件,可以从中读取密码实现非交互式(这不是远程shell认证的密码,是rsync模块认证的密码)。
  • --delete 以源为主,对目标进行同步;多的删除,少的增加(–delete 是在接收端执行的)。
  • --exclude 指定排除规则来排除不需要传输的文件。
  • --backup 对目标上已存在的文件做一个备份,备份的文件名默认使用~做后缀。
  • --backup-dir 指定备份文件的保存路径;不指定时默认和待备份文件保存在同一目录下。

以daemon模式运行

在目标端启动 rsync daemon 服务:

  1. 通过 xinetd 配置 rsync daemon 服务开机自启
    /etc/xinetd.d/rsync 中把 disabled = yes 改成 no
  2. 或者直接 rsync --daemon

常见错误

  • password file must not be other-accessible
    客户端和服务端的密码文件都应该是600的权限才可以。
    即,把 rsyncd.confrsyncd.secretsrsync.key的权限都设置为600:

    1
    2
    3
    4
    cd /etc
    chmod 600 rsyncd.conf
    chmod 600 rsyncd.secrets
    chmod 600 rsync.key
  • failed to set times on “/.” (in rsync_your_data): Operation not permitted
    修改目标服务器上对应同步目录的所属,eg.

    1
    2
    cd /data
    chown -R apache:apache mydata
  • 备份目录的问题
    设置的备份目录生成在了目标路径下面(上面有说明了)。
    经各种尝试和资料检索后,发现,如果目标是Rsync服务端(即运行Rsync守护进程的机器),则 backup-dir 是无法超出模块对应的路径的。

    Note that if you specify a relative path, the backup
    directory will be relative to the destination directory, so
    you probably want to specify either an absolute path or a
    path that starts with “../“. If an rsync daemon is the
    receiver, the backup dir cannot go outside the module’s path
    hierarchy, so take extra care not to delete it or copy into it.

    https://groups.google.com/forum/#!topic/comp.unix.shell/qQVtGqOz8GA

参考资料