linux-rsync同步文件

linux-rsync同步文件


安装 rsync

先略过,服务器和客户端都默认安装了rsync


启动rsync服务

  1. 编辑 /etc/xinetd.d/rsync 文件,将其中的 disable=yes 改为 disable=no

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@localhost ~]# vi /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 = no
    flags = IPv6
    socket_type = stream
    wait = no
    user = root
    server = /usr/bin/rsync
    server_args = --daemon
    log_on_failure += USERID
    }
  2. 重启 xinetd 服务

    1
    2
    3
    [root@localhost ~]# /etc/init.d/xinetd restart
    Stopping xinetd: [ OK ]
    Starting xinetd: [ OK ]

编辑 rsync 配置文件

  1. 创建配置文件,默认安装好 rsync 程序后,并不会自动创建rsync的主配置文件,需要手工来创建,其主配置文件为**/etc/rsyncd.conf**,创建该文件并插入如下内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [root@localhost ~]# vi /etc/rsyncd.conf                                                
    uid=root
    gid=root
    max connections=4
    log file=/var/log/rsyncd.log
    pid file=/var/run/rsyncd.pid
    lock file=/var/run/rsyncd.lock
    secrets file=/etc/rsyncd.passwd
    hosts deny=172.16.78.0/22

    [www]
    comment= backup web
    path=/home/rsync_test
    read only = no
    exclude=test
    auth users=root # 认证的用户
    • www : 同步模块,客户端同步命令中需要制定的模块
    • secrets file : 密码文件,同步命令后需要输入对应用户的密码
    • path : 该模块的同步路径
  2. 创建 密码文件,采用这种方式不能使用系统用户对客户端进行认证,所以需要创建一个密码文件,其格式为“username:password”,用户名可以和密码可以随便定义,最好不要和系统帐户一致,同时要把创建的密码文件权限设置为600

    1
    2
    3
    4
    [root@localhost ~]# vi /etc/rsyncd.passwd 
    root:abc123

    [root@localhost ~]# chmod 600 /etc/rsyncd.passwd
  3. 重启 xinetd 服务

  4. 客户端使用命令同步,然后输入密码 abc123 ,同步成功后

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    MacBook-Pro:~ wilker$ rsync -avz -e "ssh -p 23333" --progress --delete root@114.114.114.114::www ~/Desktop/rsync_test
    Password:
    receiving file list ...
    2 files to consider
    ./
    test.lua
    13 100% 12.70kB/s 0:00:00 (xfer#1, to-check=0/2)

    sent 88 bytes received 199 bytes 11.71 bytes/sec
    total size is 13 speedup is 0.05

    直接指定密码文件的命名

1
2
3
4
5
6
   MacBook-Pro:~ wilker$ rsync -avz -e "ssh -p 23333" --progress --timeout=3000 --password-file=/Users/wilker/Desktop/rsync_pwd.lua --delete root@114.114.114.114::www ~/Desktop/rsync_test
receiving file list ...
2 files to consider

sent 60 bytes received 137 bytes 56.29 bytes/sec
total size is 13 speedup is 0.07

rsync_pwd.lua 文件内容是密码

1
abc123

踩到的小坑

  1. 模块配置不正确,报错: @ERROR: Unknown module ‘tee_nonexists'

    • 需要修改为正确的模块,如配置文件 /etc/rsyncd.conf 中的 [www] 模块,同步命令中则为 root@114.114.114.114::www
  2. 服务端目录不存在,报错: @ERROR: chroot failed

    • 创建同步目录 及 分配权限
  3. 输入 rsync 的密码错误:报错: @ERROR: auth failed on module www

    • 输入正确的密码,在配置文件 **/etc/rsyncd.passwd ** 中的 abc123
  4. 服务端的ssh公钥认证报错: Permission denied (publickey,gssapi-keyex,gssapi-with-mic)

    • 编辑ssh的配置文件 /etc/ssh/sshd_config,开启下面几项

      1
      2
      3
      4
      5
      [root@localhost ~]# vi /etc/ssh/sshd_config
      ...
      RSAAuthentication yes
      PubkeyAuthentication yes
      AuthorizedKeysFile .ssh/authorized_keys # 这个为ssh公钥
    • 重启ssh服务

      1
      2
      3
      [root@localhost ~]# /etc/init.d/sshd restart
      Stopping sshd: [ OK ]
      Starting sshd: [ OK ]
  5. 客户端使用命令指定了密码文件,报错: password file must not be other-accessible

    • 客户度的密码文件 /Users/wilker/Desktop/rsync_pwd.lua 需要 600 权限,不能被其他人访问

      1
      MacBook-Pro:Desktop wilker$ chmod 600 rsync_pwd.lua

参考资料


推送、拉取

推送和拉取的区别就是调换一下 远端 本地 的位置, 和 scp 命令差不多

  • ssh方式

    1
    2
    rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST # 执行“推”操作
    rsync [OPTION]... [USER@]HOST:SRC [DEST] # 执行“拉”操作
  • rsync C/S 方式

    1
    2
    3
    4
    rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST                    # 执行“推”操作
    rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST # 执行“推”操作
    rsync [OPTION]... [USER@]HOST::SRC [DEST] # 执行“拉”操作
    ersync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST] # 执行“拉”操作

常用选项

  • -v : Verbose (try -vv for more detailed information) # 详细模式显示

  • -e “ssh options” : specify the ssh as remote shell # 指定ssh作为远程shell

  • -a : archive mode # 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD

  • -r(–recursive) : 目录递归

  • -l(–links) :保留软链接

  • -p(–perms) :保留文件权限

  • -t(–times) :保留文件时间信息

  • -g(–group) :保留属组信息

  • -o(–owner) :保留文件属主信息

  • -D(–devices) :保留设备文件信息

  • -z : 压缩文件

  • -h : 以可读方式输出

  • -H : 复制硬链接

  • -X : 保留扩展属性

  • -A : 保留ACL属性

  • -n : 只测试输出而不正真执行命令,推荐使用,特别防止--delete误删除!

  • --stats : 输出文件传输的状态

  • --progress : 输出文件传输的进度

  • ––exclude=PATTERN : 指定排除一个不需要传输的文件匹配模式

  • ––exclude-from=FILE : 从 FILE 中读取排除规则

  • ––include=PATTERN : 指定需要传输的文件匹配模式

  • ––include-from=FILE : 从 FILE 中读取包含规则

  • --numeric-ids : 不映射 uid/gid 到 user/group 的名字

  • -S, --sparse : 对稀疏文件进行特殊处理以节省DST的空间

  • --delete : 删除DST中SRC没有的文件,也就是所谓的镜像[mirror]备份


Ubuntu16.04 的启动方式


Ubuntu 启动 rsync 服务

Ubuntu 已经安装了 rsync, 只是默认不启动

  1. 修改配置文件 vi /etc/default/rsync

    1
    2
    3
    4
    5
    # vi /etc/default/rsync

    RSYNC_ENABLE=false
    # 改成
    RSYNC_ENABLE=true
  2. 拷贝一个示例配置文件到 /etc

    1
    # cp /usr/share/doc/rsync/examples/rsyncd.conf /etc
    • 不是一定要到这个目录下, 只是启动命令默认会找 rsyncd.conf, 启动命令也可以指定 --config 指定配置文件路径

    • 然后修改配置文件 /etc/rsyncd.conf, 我的配置

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      # vi /etc/rsyncd.conf

      log file = /var/log/rsyncd.log
      pid file=/var/run/rsyncd.pid
      use chroot = no
      max connections=1200
      uid = root
      gid = root
      strict modes = yes

      ignore errors = no
      ignore nonreadable = yes
      transfer logging = no
      timeout = 600
      refuse options = checksum dry-run
      dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz

      [Family]
      comment = wolegequ
      path = /nas/Family
      read only = no
      list = yes
      lock file = /var/lock/rsyncd_Family
      auth users = backup # (不建议) 授权帐号,认证的用户名,如果没有这行则表明是匿名,多个用户用,分隔。
      secrets file = /etc/rsyncd.passwd # 认证文件名,用来存放密码

      [Threejs]
      comment = wolegequ
      path = /nas/home_xuan/Threejs
      read only = no
      list = yes
      lock file = /var/lock/rsyncd_Threejs

      [Public]
      comment = wolegequ
      path = /nas/Public
      read only = no
      list = yes
      lock file = /var/lock/rsyncd_Public
  3. 启动 rsync 服务

    1
    2
    # /etc/init.d/rsync start # 启动
    # /etc/init.d/rsync stop # 停止
    • 查看一下是否运行中

      1
      2
      # ps auxw | grep rsync
      root 834 0.0 0.0 15800 944 ? S 15:30 0:00 /usr/bin/rsync --no-detach --daemon --config /etc/rsyncd.conf
    • 也可以这样启动

      1
      # /usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
  4. done

  5. 客户端实测一下

    1
    2
    3
    4
    5
    6
    # rsync -avzhe "ssh -p 32888" --delete --progress -O root@192.168.2.222::Public /mnt/j/Public
    receiving incremental file list
    ./

    sent 37 bytes received 131 bytes 48.00 bytes/sec
    total size is 4.63M speedup is 27,578.10
    • 这里的 Public 就是 /etc/rsyncd.conf 中的 [Public], 这种是指定远端 别名 的情况, 需要用 ip::[别名]
      • 也可以直接指定远端地址, ip:[远端地址]

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
                    # rsync -avzhe "ssh -p 32888" --delete --progress -O root@192.168.2.222:/nas/Public /mnt/j/Public
        receiving incremental file list
        ./

        sent 37 bytes received 131 bytes 48.00 bytes/sec
        total size is 4.63M speedup is 27,578.10

        - `-avzhe` 中的 `e` 是指使用的是 ssh 的方式.



        相关命令

        都是在 win10 子系统 Ubuntu 中操作. ( 以 ssh 的方式 )

        - 将 远端 `Public` 同步到本地 `/mnt/j/Public`

        ```bash
        $ rsync -avzhe "ssh -p 32888" --delete --progress -O root@192.168.2.222::Public /mnt/j/Public
  • 将 本地 /mnt/j/Public 同步到 远端 Public

    1
    $ rsync -avzhe "ssh -p 32888" --delete --progress -O /mnt/j/Public root@192.168.2.222::Public

踩坑

  • rsync: mkstemp “xxx” failed: Operation not permitted (1)

    同步时本地会新建一个临时目录, 由于权限不够才报这个错. 使用命令时加上 sudo 或者直接切到超级用户 roo 下操作.
    切到 root 操作会方便很多.

  • rsync: failed to set times on “xxx”: Operation not permitted (1)

    参考: https://codeday.me/bug/20170520/16939.html

    如果/ foo / bar在NFS(或可能是某些FUSE文件系统)上,则可能是问题。加上 -O 参数, 如:

    1
    rsync -avzhe "ssh -p 32888" --delete --progress -O root@192.168.2.222::Public /mnt/j/Public
  • 启动报错: rsync daemon failed to start

    看日志最快了, 根据日志文件 log file = /var/log/rsyncd.log 直接看错误

    1
    2
    3
    # vi /var/log/rsyncd.log

    2019/07/02 14:11:34 [150] rsync: failed to create pid file /var/run/rsyncd.pid: File exists (17)

    上面看出已经存在了一个进程文件, 直接删掉就可以重启成功了

    1
    2
    # rm -f /var/run/rsyncd.pid
    # service rsync restart

包含 排除 文件

排除

参考:

包含

参考:

  • 详解RSYNC 好文 - https://blog.csdn.net/lianzg/article/details/24817087

  • rsync –include-from –exclude-from的理解 - http://copyfuture.com/blogs-details/ce8a054d8c895980cde91d448a0458d7

  • 远端 Threejs 的目录树

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    ├── aaa
    │   ├── 15.txt
    │   ├── aaa.lua
    │   └── bbb
    │   ├── 15.txt
    │   ├── aaa.lua
    │   ├── ccc
    │   │   ├── 15.txt
    │   │   ├── aaa.lua
    │   │   └── lwrp.cs
    │   └── lwrp.cs
    └── threejs源码阅读.vsd
  • 要包含的 include.txt

    1
    2
    3
    4
    5
    6
    7
    8
    + Ar/ # 要同步的目录一定要加上这样一行, 代表先同步出文件夹
    + Ar/** # 然后在同步该文件夹下的所有文件

    - Threejs/**/bbb/** #要排除的一定要写在对应目录 + 之前
    + Threejs/
    + Threejs/**

    - * # 排除所有
    • 执行顺序是从上到下执行. 所以要排除的目录一定要卸载 + 之前
    • 这里的意思是: Threejs 目录下的 Ar 目录所有都同步, Threejs 目录 除了 bbb 目录 外 全部同步, 除了以上然后所有的都不同步
示例1
  • include.txt

    1
    2
    3
    4
    5
    6
    + Threejs/
    + Threejs/aaa/
    + Threejs/aaa/bbb/
    + Threejs/aaa/bbb/** # 这个只包含了目录下的文件,如果没有上面每一级目录这个也无效

    - *
    • 只包含 bbb 目录下的文件. bbb之前的每一级的目录都要包含进去
示例2
  • include.txt

    1
    2
    3
    4
    + Threejs/
    + Threejs/**/

    - *
    • 只会同步 Threejs 目录下的所有目录, 但不会同步任何文件
示例3
  • include.txt

    1
    2
    3
    4
    5
    - Threejs/aaa/**
    + Threejs/
    + Threejs/**

    - *
  • 除了 aaa 目录以外所有的都同步


常见错误


同步命令

不管是远端还是本地, 目录后面有没有 / 符号都是有区别的

别名访问 和 直接远端目录访问也是有区别的, 别名访问隐藏远端目录, 增加一些自定义的配置, 如: www

1
2
3
4
5
6
7
8
[root@localhost ~]# vi /etc/rsyncd.conf 

[www]
comment= backup web
path=/home/rsync_test
read only = no
exclude=test
auth users=root # 认证的用户

同步文件

指定远端文件夹

1
$ E:/cwRsync_5.5.0_x86_Free/bin/rsync.exe --progress -Pav -e "E:/cwRsync_5.5.0_x86_Free/bin/ssh.exe -i E:/id_rsa_cdn" /cygdrive/C/Users/wilker/Desktop/asd.zip root@aaa.bbb.cn:/webapps/aaa/

会将 asd.zip 推送到远端 /webapps/aaa/ 目录下


同步目录

  • 推送

    1
    $ E:/cwRsync_5.5.0_x86_Free/bin/rsync.exe --chmod=D755,F644 --progress -Pav -e "E:/cwRsync_5.5.0_x86_Free/bin/ssh.exe -i E:/id_rsa_cdn" /cygdrive/C/Users/wilker/Desktop/asd/ root@aaa.bbb.cn:/webapps/aaa/

    会将 asd 目录里面的东西推送到远端 /webapps/aaa/ 目录下

    • 如果也要包含 asd 目录本身, 也就是 /webapps/aaa/asd/**, 去掉 asd 后面的 / 即可

      1
      $ E:/cwRsync_5.5.0_x86_Free/bin/rsync.exe --chmod=D755,F644 --progress -Pav -e "E:/cwRsync_5.5.0_x86_Free/bin/ssh.exe -i E:/id_rsa_cdn" /cygdrive/C/Users/wilker/Desktop/asd root@aaa.bbb.cn:/webapps/aaa/
  • 拉取

    ```json
    $ E:/cwRsync_5.5.0_x86_Free/bin/rsync.exe -Pav -e “E:/cwRsync_5.5.0_x86_Free/bin/ssh.exe -i E:/id_rsa_cdn” root@ali.wilker.cn:/webapps/aaa/ /cygdrive/C/Users/wilker/Desktop/asd/


踩坑

windows 上传到远端的 权限太高

windows 使用 cwRsync 上传上去的权限文件都是 777

解决办法是增加权限参数 --chmod=D755,F644 即可, D 是 目录, F 是文件

参考: https://qastack.cn/server/796330/how-do-i-set-destination-permissions-with-rsync-chown-chmod