实时同步lsyncd
实时同步lsyncd
1 lsyncd
1.1 lsyncd 简介
Lsyncd使用文件系统事件接口(inotify或fsevents)来监视对本地文件和目录的更改。Lsyncd将这些事件整理几秒钟,然后生成一个或多个进程以将更改同步到远程文件系统。
默认同步方法是rsync Lsyncd是一种轻量级的实时镜像解决方案。Lsyncd相对容易安装,不需要新的文件系统或块设备。Lysncd不会妨碍本地文件系统性能,可以通过配置文件实现细粒度的自定义。自定义操作配置甚至可以从头开始编写,从shell脚本到用Lua语言编写的代码。
1.2 环境准备
rsync服务端:默认就有rsync无需安装
lsyncd客户端:安装lsyncd软件
在这里,备份服务器为服务端,存储服务器为客户端
[root@nfs ~]#yum install -y lsyncd
1.3 rsync配置文件
backup配置文件
[root@backup ~]#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
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = "backup dir by oldboy"
path = /backup
1.4 创建虚拟用户rsync
id rsync #查看以下是否有rsync这个用户
useradd rsync -M -s /sbin/nologin #创建rsync虚拟用户
1.5 创建密码文件并修改访问权限为600
echo "rsync_backup:123456" >/etc/rsync.password
chmod 600 /etc/rsync.password #只能root用户才能查看密码
1.6 创建备份目录/修改目录属主和属组信息
mkdir /backup
chown rsync.rsync /backup
1.7 启动服务程序/重启服务程序
systemctl start rsyncd
systemctl restart rsyncd
systemctl enable rsyncd
1.8 lsyncd配置文件
lsyncd配置文件
root@nfs ~]#cat /etc/lsyncd.conf
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 8,
}
sync {
default.rsync,
source = "/data",
target = "rsync_backup@172.16.1.41::backup",
delete= true,
exclude = { ".*" },
delay = 1,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file = "/etc/rsync.password",
_extra = {"--bwlimit=200"}
}
}
1.9 存储服务器创建备份的目录
mkdir /data
1.10 创建密码文件,并修改权限
echo "123456" >/etc/rsync.password #创建密码文件
chmod 600 /etc/rsync.password
1.11免交互式传输密码文件
rsync -avz /etc/passwd rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
1.12 存储服务器进行测试
[root@nfs data]#touch test.txt
[root@nfs data]#ls
test.txt
++++++++++++++++++++++++++++++
[root@backup backup]#ls
test.txt