lsyncd+rsync 实时同步
作者:互联网
一、lsyncd简介
lsyncd功能和sersync相同,属于实时同步工具,但是Lsyncd功能更加强大,Lysncd 实际上是lua语言封装了 inotify 和 rsync 工具,采用了 Linux 内核里的 inotify 触发机制,然后通过rsync去差异同步,达到实时的效果。Lsyncd最强大之处在于简单高效传输海量数据并且lsyncd支持多种工作模式。
二、环境准备
服务器 | 主机名 | ip地址 | 服务软件 |
---|---|---|---|
服务端 | rsync | 172.16.1.41 | rysncd |
客户端 | nfs | 172.16.1.31 | lsyncd |
三、服务端机器安装及配置
下载rsyncd
[root@rsync ~]# yum install -y rsyncd
配置conf文件
[root@rsync ~]# cat /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
#hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = "backup dir by oldboy"
path = /backup
创建rsync虚拟用户
[root@rsync ~]# useradd rsync -M -s /sbin/nologin
创建备份目录
[root@rsync ~]# mkdir backup
修改目录属主和属组权限
[root@rsync ~]# chown rysnc.rsync /backup
创建密码文件
[root@rsync ~]# touch /etc/rsync.password
[root@rsync ~]# echo "rsync_backup:1" >/etc/rsync.password
修改权限为root才能访问
[root@rsync ~]# chmod 600 /etc/rsync.password
启动服务/加入开机自启
[root@rsync ~]# systemctl restart rsyncd
[root@rsync ~]# systemctl enable rsyncd
四、客户端机器及配置
下载服务
[root@nfs ~]# yum install -y lsyncd
配置文件
[root@nfs ~]# cat /etc/lsyncd.conf
settings {
logfile ="/var/log/lsyncd.log",
statusFile ="/usr/local/lsyncd-2.1.5/var/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 1, #延迟1秒
-- nodaemon =true,
}
sync {
default.rsync,
source = "/backup", #客户端目录
target = "rsync_backup@172.16.1.41::backup", #服务端模块
-- excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
rsync = {
binary = "/usr/bin/rsync",
password_file = "/etc/rsync.password", #免交互密码文件
archive = true,
compress = true,
verbose = true
}
}
创建密码文件
[root@nfs ~]# touch /etc/rsync.password
[root@nfs ~]# echo "1" > /etc/rsync.password
修改权限为root才能访问
[root@nfs ~]# chmod 600 /etc/rsync.password
创建客户端服务器存储目录
[root@nfs ~]# mkdir /backup
启动服务/开机自启
[root@nfs ~]# systemctl restart lsyncd
[root@nfs ~]# systemctl enable lsyncd
五、检查服务开启状态并测试实时同步效果
服务端
[root@rsync ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2020-01-27 14:00:48 CST; 18min ago
Main PID: 7942 (rsync)
CGroup: /system.slice/rsyncd.service
└─7942 /usr/bin/rsync --daemon --no-detach
Jan 27 14:00:48 rsync systemd[1]: Stopped fast remote file copy program daemon.
Jan 27 14:00:48 rsync systemd[1]: Started fast remote file copy program daemon.
客户端
[root@nfs ~]# systemctl status lsyncd
● lsyncd.service - Live Syncing (Mirror) Daemon
Loaded: loaded (/usr/lib/systemd/system/lsyncd.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2020-01-27 14:15:49 CST; 4min 53s ago
Main PID: 8016 (lsyncd)
CGroup: /system.slice/lsyncd.service
└─8016 /usr/bin/lsyncd -nodaemon /etc/lsyncd.conf
Jan 27 14:15:49 nfs systemd[1]: Unit lsyncd.service entered failed state.
Jan 27 14:15:49 nfs systemd[1]: lsyncd.service failed.
Jan 27 14:15:49 nfs systemd[1]: Started Live Syncing (Mirror) Daemon.
Jan 27 14:15:49 nfs lsyncd[8016]: 14:15:49 Error: Cannot open status file "/usr/local/lsyncd-2.1.5/...ctory
Jan 27 14:15:49 nfs lsyncd[8016]: sending incremental file list
Jan 27 14:15:49 nfs lsyncd[8016]: deleting backup/
Jan 27 14:15:49 nfs lsyncd[8016]: ./
Jan 27 14:15:49 nfs lsyncd[8016]: sent 331 bytes received 40 bytes 742.00 bytes/sec
Jan 27 14:15:49 nfs lsyncd[8016]: total size is 1,890,490 speedup is 5,095.66
Jan 27 14:15:59 nfs lsyncd[8016]: 14:15:59 Error: Cannot open status file "/usr/local/lsyncd-2.1.5/...ctory
Hint: Some lines were ellipsized, use -l to show in full.
测试:
客户端创建文件
[root@nfs ~]# cd /backup/
[root@nfs backup]# touch 123.txt
[root@nfs backup]# ll
total 0
-rw-r--r-- 1 root root 0 Jan 27 14:22 123.txt
服务端检查结果
[root@rsync ~]# cd /backup
[root@rsync backup]# ll
total 0
-rw-r--r-- 1 rsync rsync 0 Jan 27 14:22 123.txt
octtom
发布了11 篇原创文章 · 获赞 1 · 访问量 218
私信
关注
标签:rsync,lsyncd,backup,实时,nfs,root,14 来源: https://blog.csdn.net/octtom/article/details/104092311