美文网首页
ubuntu16.04基于rsync的lsyncd自动同步配置

ubuntu16.04基于rsync的lsyncd自动同步配置

作者: 亻火子 | 来源:发表于2019-07-15 14:28 被阅读0次

环境部署

源机:192.168.0.11
目标机:192.168.0.12

目标机配置(192.168.0.12)

Ubuntu16.04默认安装rsync,但是没有默认启动

  • 启动rsync

修改vim /etc/default/rsync将RSYNC_ENABLE=false改为true
复制cp /usr/share/doc/rsync/examples/rsyncd.conf /etc
修改vim /etc/rsyncd.conf


# sample rsyncd.conf configuration file

# GLOBAL OPTIONS

#motd file=/etc/motd
log file=/var/log/rsyncd
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
# The init.d script does its own pid file handling,
# so omit the "pid file" line completely in that case.
pid file=/var/run/rsyncd.pid
syslog facility=daemon
#socket options=

# MODULE OPTIONS

[website]

        comment = public archive
        path = /wwwroot/website #同步目录
        use chroot = no
#       max connections=10
        lock file = /var/lock/rsyncd
# the default for read only is yes...
        read only = no #允许写入文件
        list = yes
        uid = root #写入文件时要ROOT权限
        gid = root
#       exclude =
#       exclude from =
#       include =
#       include from =
        auth users = webs
        secrets file = /etc/rsyncd.secrets
        strict modes = yes
        hosts allow = 192.168.0.11
#       hosts deny =
        ignore errors = yes
        ignore nonreadable = yes
        transfer logging = yes
        log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
        timeout = 600
        refuse options = checksum dry-run
        dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
  • 配置认证用户和密码

vim  /etc/rsyncd.secrets 
webs:webs$sync
sync:654321sync
#设置权限
chmod 600 /etc/rsyncd.secrets
  • 基本命令:

启动:/etc/init.d/rsync start
重启:/etc/init.d/rsync restart
关闭:/etc/init.d/rsync stop
查看状态:/etc/init.d/rsync status
上传下载:rsync [选项] 原始位置 目标位置
参数:
-a 归档模式,递归并保留对象属性,等同于 -rlptgoD
-v 显示同步过程的详细(verbose)信息
-z 在传输文件时进行压缩(compress)
-H 保留硬链接文件
-A 保留ACL属性
--delete 删除目标位置有而原始位置没有的文件
-r 递归模式,包含目录及子目录中所有文件
-l 对于软链接文件仍然复制为软链接文件
-p 保留文件的权限标记
-t 保留文件的时间标记
-g 保留文件的属组标记(仅超级用户使用)
-o 保留文件的属主标记(仅超级用户使用)
-D 保留设备文件及其他特殊文件

方式同步:
下行同步:rsync -avz 用户名@服务器地址::website /本地目录
rsync -avz --progress --delete webs@192.168.0.12::website ./
上行同步:rsync -avz /本地目录 用户名@服务器地址::website
rsync -avz --progress --delete ./a.txt webs@192.168.0.12::website
使用上行同步,须要将配置文件rsyncd.conf中,设置read only = no。

源机配置(192.168.0.11)

  1. 安装lsyncd
    apt install lsyncd
    安装成功后,你可以在/usr/share/doc/lsyncd/examples/看到lsyncd配置使用示例。
  2. 配置lsyncd
    配置文件是/etc/lsyncd/lsyncd.conf.lua
    须要手动创建配置文件夹mkdir /etc/lsyncd
    log文件夹mkdir /var/log/lsyncd
    密码文件vim /etc/lsyncd/backwebsite.pas
    密码文件权限chmod 600 /etc/lsyncd/backwebsite.pas
#同步站点源码
settings {
        logfile = "/var/log/lsyncd/lsyncd.log",
        statusFile = "/var/log/lsyncd/lsyncd.status",
        maxDelays = 100, #累计到多少所监控的事件激活一次同步,即使后面的delay延迟时间还未到
        delay = 5,
        exitcodes = {[0] = "ok", [1] = "again", [2] = "die"},
        maxProcesses = 5, #同步进程的最大个数
        statusInterval = 5 #将lsyncd的状态写入上面的statusFile的间隔,默认10秒
}
sync{
        default.rsync,
        source          = "/wwwroot/website",
        target          = "webs@192.168.0.12::website",
        delete          = true,
        exclude         = {  ".*", "*.log"},
        delay           = 1,
        rsync   = {
                binary = "/usr/bin/rsync",
                archive = true,
                compress = true, #压缩
                verbose = true, #增量
                password_file = "/etc/lsyncd/backwebsite.pas"
        }
}

启动:/etc/init.d/lsyncd start

相关文章

网友评论

      本文标题:ubuntu16.04基于rsync的lsyncd自动同步配置

      本文链接:https://www.haomeiwen.com/subject/zpbgkctx.html