前言:Lysncd 实际上是lua语言封装了 inotify 和 rsync 工具
项目需求
web端通过lsyn01cd软件进行完成向备份服务器实时同步备份数据。
主机 | IP地址 | 安装的软件 |
---|---|---|
nfs服务器 | 172.16.1.6 | rsync,lsyncd |
backup | 172.16.1.41 | rsync |
部署过程
nfs服务器(客户端)
第一个里程:
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm # 安装 epel 源
yum -y install lsyncd rsync # 安装
vim /etc/lsyncd.conf # 配置
第二个里程
vim /etc/lsyncd.conf # 配置
[root@nfs01 tmp]# vi /etc/lsyncd.conf
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/ #开始将此项变为注释,否则启动服务会出现问题。--代表注释
--
-- sync{default.rsyncssh, source="/var/www/html", host="localhost", targetdir="/tmp/htmlcopy/"}
settings {
logfile = "/var/log/lsyncd/lsyncd.log", #指定lsyncd工具本身运行所产生的日志文件存放位置
statusFile = "/var/log/lsyncd/lsyncd.status",#定义lsyncd监控目录状态文件的存放位置
inotifyMode = "CloseWrite", #事件的状态
maxProcesses = 1000, #指定同步时进程的最大个数
maxDelays = 1, #当事件被命中累计多少次后才进行一次同步
}
sync { #主要用来定义同步时的一些设置,可以同时同步多个目录,只需要在该代码块中事先定义好多个sync即可
default.rsync, #指定lsyncd运行模式,另外,还有>default.direct,default.rsyncssh模式
source = "/tmp/", #需要同步数据的目录
target = "rsync_backup@172.16.1.41::backup", #rsync服务器的相关命令
delay = 1, #当命中的事件累计到多少时再触发同步
delete = true, #删除目录中的数据会与rsync服务器同步
rsync = {
binary = "/usr/bin/rsync", #rsync的命令
password_file = "/etc/rsync.password", #本地rsync调用的密码文件
archive = true,
compress = false,
verbose = true
}
}
便于以后用到方便拷贝
[root@nfs01 tmp]# vi /etc/lsyncd.conf
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
--
-- sync{default.rsyncssh, source="/var/www/html", host="localhost", targetdir="/tmp/htmlcopy/"}
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 1000,
maxDelays = 10,
}
sync {
default.rsync,
source = "/tmp/",
target = "rsync_backup@172.16.1.41::backup",
delay = 1,
delete = true,
rsync = {
binary = "/usr/bin/rsync",
password_file = "/etc/rsync.password",
archive = true,
compress = false,
verbose = true
}
}
第三个里程
启动lsyncd服务
systemctl start lsyncd
查看服务运行状态
[root@nfs01 tmp]# systemctl status lsyncd
● lsyncd.service - Live Syncing (Mirror) Daemon
Loaded: loaded (/usr/lib/systemd/system/lsyncd.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2019-07-21 19:10:01 CST; 1min 18s ago
Main PID: 9291 (lsyncd)
CGroup: /system.slice/lsyncd.service
└─9291 /usr/bin/lsyncd -nodaemon /etc/lsyncd.conf
Jul 21 19:10:01 nfs01 lsyncd[9163]: 18:45:00 Normal: Finished a list after exitcode: 0
Jul 21 19:10:01 nfs01 lsyncd[9163]: 18:45:00 Normal: Finished a list after exitcode: 0
Jul 21 19:10:01 nfs01 lsyncd[9163]: 19:10:01 Normal: --- TERM signal, fading ---
Jul 21 19:10:01 nfs01 systemd[1]: Stopped Live Syncing (Mirror) Daemon.
Jul 21 19:10:01 nfs01 systemd[1]: Unit lsyncd.service entered failed state.
Jul 21 19:10:01 nfs01 systemd[1]: lsyncd.service failed.
Jul 21 19:10:01 nfs01 systemd[1]: Started Live Syncing (Mirror) Daemon.
Jul 21 19:10:01 nfs01 lsyncd[9291]: sending incremental file list
Jul 21 19:10:01 nfs01 lsyncd[9291]: sent 178 bytes received 25 bytes 406.00 bytes/sec
Jul 21 19:10:01 nfs01 lsyncd[9291]: total size is 0 speedup is 0.00
backup服务器端
- 1.确保rsync服务运行正常
- 2.确保从客户端可正常备份数据
3.进行测试
nfs服务器的/tmp目录下创建文件
[root@nfs01 tmp]# touch oldboy{01..10}.txt
[root@nfs01 tmp]# ls
oldboy{01.10}.txt oldboy02.txt oldboy04.txt oldboy06.txt oldboy08.txt oldboy10.txt
oldboy01.txt oldboy03.txt oldboy05.txt oldboy07.txt oldboy09.txt
backup端进行查看
[root@backup backup]# ls
oldboy01.txt oldboy03.txt oldboy05.txt oldboy07.txt oldboy09.txt
oldboy02.txt oldboy04.txt oldboy06.txt oldboy08.txt oldboy10.txt
nfs服务器的/tmp 删除文件
[root@nfs01 tmp]# rm -fr oldboy*
[root@nfs01 tmp]# ls
[root@nfs01 tmp]#
backup服务器/backup的目录中查看
[root@backup backup]# ls
[root@backup backup]#
网友评论