Rsync

安装

1
2
3
4
5
6
# Debian
$ sudo apt-get install rsync
# Red Hat
$ sudo yum install rsync
# Arch Linux
$ sudo pacman -S rsync

基本用法

1
rsync -a source1 source2 destination
  • -r,表示递归,即包含子目录,-r是必须的,否则rsync运行不会成功。
  • -a--archive,表示存档模式,保存所有的元数据,比如修改时间(modification time)、权限、所有者等,并且软链接也会同步过去。由于 rsync 默认使用文件大小和修改时间决定文件是否需要更新,所以-a-r更有用。
  • -n
  • --delete
  • --exclude
  • --include
  • --link-dest

使用 SSH 协议

1
rsync -av -e 'ssh -p 2234' source/ user@remote_host:/destination

使用 rsync 协议

命令格式:

1
2
rsync -av source/ remote_host::module/destination
rsync -av source/ rsync://remote_host/module/destination

匿名用户:

  1. 服务端配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    # /etc/rsyncd.conf
    uid = nobody
    gid = nobody
    use chroot = no
    max connections = 4
    syslog facility = local5
    pid file = /var/run/rsyncd.pid

    [tmp]
    uid = mqq
    gid = mqq
    path = /home/mqq
    comment = tmp sync
    read only = false
  2. 服务端执行rsync --daemon启动服务

  3. 客户端执行

    1
    rsync -av file server::tmp

指定用户:

  1. 服务端配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    # /etc/rsyncd.conf
    uid = nobody
    gid = nobody
    use chroot = no
    max connections = 4
    syslog facility = local5
    pid file = /var/run/rsyncd.pid
    secrets file = /etc/rsyncd.secrets

    [tmp]
    uid = mqq
    gid = mqq
    path = /home/mqq
    comment = tmp sync
    read only = false
    auth users = qq

    # /etc/rsyncd.secrets
    echo "qq:123456" > /etc/rsyncd.secrets
    chmod 600 /etc/rsyncd.secrets
  2. 服务端执行rsync --daemon启动服务

  3. 客户端配置

    1
    2
    echo "123456" > rsyncd.secrets
    chmod 600 rsyncd.secrets
  4. 客户端执行

    1
    rsync -av --password-file=rsyncd.secrets file qq@server::tmp

参考文献


Rsync
https://laplac2.github.io/tools/rsync/
作者
Laplace
发布于
2022年4月19日
许可协议