Talk is cheap, Show me the code

0%

Ubuntu使用rsync同步文件到Windows

通过Rsync主动同步Ubuntu文件到Windows

Ubuntu服务端配置方法

  • Ubuntu一般都自带rsync,没有的话可以用apt安装一下
  • 接着新建一个文件夹,用来放配置文件等,为了省得总是sudo,可以直接放在home/usr下面
  • 新建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
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    ### rsyncd.conf 文件的配置
    # /etc/rsyncd: configuration file for rsync daemon mode
    # See rsyncd.conf man page for more options.
    # 传输文件使用的用户和用户组,如果是从服务器=>客户端,要保证www用户对文件有读取的权限;如果是从客户端=>服务端,要保证www对文件有写权限。
    uid = www
    gid = www
    # 允许chroot,提升安全性,客户端连接模块,首先chroot到模块path参数指定的目录下,chroot为yes时必须使用root权限,且不能备份path路径外的链接文件
    use chroot = yes
    # 只读
    read only = no
    # 只写
    write only = no
    # 设定白名单,可以指定IP段(172.18.50.1/255.255.255.0),各个Ip段用空格分开
    hosts allow = 172.18.50.110 172.18.50.111
    hosts deny = *
    # 允许的客户端最大连接数
    max connections = 4
    # 欢迎文件的路径,非必须
    # motd file = /etc/rsync/rsyncd.motd
    # pid文件路径
    pid file = /var/run/rsyncd.pid
    # 记录传输文件日志
    transfer logging = yes
    # 日志文件格式
    log format = %t %a %m %f %b
    # 指定日志文件
    log file = /var/log/rsync.log
    # 剔除某些文件或目录,不同步
    exclude = lost+found/
    # 设置超时时间
    timeout = 900
    ignore nonreadable = yes
    # 设置不需要压缩的文件
    dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

    # 模块,可以配置多个,使用如: sate@172.18.50.125::test
    [test]
    # 模块的根目录,同步目录,要注意权限
    path = /tmp/nginx
    # 是否允许列出模块内容
    list = no
    # 忽略错误
    ignore errors
    # 添加注释
    comment = ftp export area
    # 模块验证的用户名称,可使用空格或者逗号隔开多个用户名
    auth users = sate
    # 模块验证密码文件 可放在全局配置里
    secrets file = /etc/rsync/rsyncd.secrets
    # 剔除某些文件或目录,不同步
    exclude = lost+found/ conf/ man/
  • 其中:
    • [test]后面为子模块,可以设置多个,以对应不同的同步目录
    • rsyncd.secrets为密码的文件,内容为:“用户名:密码”
    • 注意,一定要运行chmod 600 rsyncd.secrets修改权限,密码文件必须只有本用户可以修改,否则程序运行会报错

Windows客户端配置方法

配置好后的使用方法

  • 修改rsync.conf,增加子模块,修改”path”为需要同步的目录
  • Ubuntu端运行rsync --daemon --config=rsync.conf重新启动服务
  • Windows端运行:
    rsync.exe -vzrtopgu --progress --port=端口 --delete 用户名@IP地址::模块名称 /cygdrive/E/路径
-------------The End-------------