lsyncd自动同步配置

因生产环境需要,需要将2.60上的数据目录,备份到2.61上;计划利用lsyncd的实时同步功能来实现备份。

源:172.16.2.60  Ubuntu系统

备份:172.16.2.61  Ubuntu系统

主要的几个步骤是:

1.配置2.602.61上,实现无密码登录。

2.源机上安装lsyncd服务

3.源机上开启rsync服务

4.备份机上开启rsync服务

注意事项:ubuntu系统的root用户默认没有开启,需要先开启root用户的ssh登录权限。

具体如下:

1.配置源与备份主机之间的无密码登录

 root@ubuntu-yangben01:~# ssh-keygen -t rsa

 root@ubuntu-yangben01:~# ssh-copy-id root@172.16.2.61

2.源机上安装lsyncd服务

 root@ubuntu-yangben01:~# apt install lsyncd

安装成功后,你可以在:/usr/share/doc/lsyncd/examples,看到Lsyncd配置使用示例,你可以参考这些示例自己再编写配置

手动创建日志文件和状态文件的目录:

root@ubuntu-yangben01:~# mkdir -p /var/log/lsyncd

root@ubuntu-yangben01:~# touch /var/log/lsyncd/lsyncd.log

root@ubuntu-yangben01:~# touch /var/log/lsyncd/lsyncd.status

配置文件详情:

root@ubuntu-yangben01:~# cat /etc/lsyncd/lsyncd.conf.lua 

----

-- User configuration file for lsyncd.

--

-- Simple example for default rsync, but executing moves through on the target.

--

--sync{default.rsyncssh, source="src", host="localhost", targetdir="dst/"}

settings {

    logfile ="/var/log/lsyncd/lsyncd.log",

    statusFile ="/var/log/lsyncd/lsyncd.status",

    inotifyMode = "CloseWrite",

    maxProcesses = 8,

    }

-- rsync模式 + ssh shell

-- 第一个同步目录

sync {

    default.rsync,

    source    = "/data",

    target    = "root@172.16.2.61:/data",

    maxDelays = 5,

    delay = 30,

    rsync     = {

        binary = "/usr/bin/rsync",

        archive = true,

        compress = true,

        bwlimit   = 2000

        }

    }

--第二个同步目录

sync {

    default.rsync,

    source    = "/yangben",

    target    = "root@172.16.2.61:/yangben",

    maxDelays = 5,

    delay = 30,

    rsync     = {

        binary = "/usr/bin/rsync",

        archive = true,

        compress = true,

        bwlimit   = 2000

        }

}

配置文件编辑完后,启动lsyncd,查看状态,并设置开机自启。

root@ubuntu-yangben01:~# systemctl start lsyncd

root@ubuntu-yangben01:~# systemctl status lsyncd

root@ubuntu-yangben01:~# systemctl enable lsyncd

3.ubuntu默认安装了rsync,但是默认是没有启动的,需要手动启用。

  编辑vim /etc/default/rsync,RSYNC_ENABLE=false改为true,保存;

  然后重启lsyncd服务,systemctl restart lsyncd

  (这个地方卡了我很久,服务起来了,但就是不产生日志,后面排查了好久,才发现是rsync没有启用。当然,这一步也可以放到前面操作,就省去重启lsyncd服务这一步。)

4.备份机上开启rsync服务

5.验证同步效果

 

posted @ 2020-01-15 20:19  巴州夜雨  阅读(1274)  评论(0编辑  收藏  举报