文章

Lsyncd 多服务器实时同步文件目录

lsyncd本地目录同步,有多种工作模式可以选择,本地目录cp,本地目录rsync,远程目录rsyncssh。本文内容为远程目录的同步

需求:本地服务器A有kod网盘,当网盘资料变动时进行同步备份至其他服务器B位置上

服务器A源文件夹/var/www/html/,Ip:192.168.20.6

备份位置服务器B文件夹/data/html,Ip:192.168.20.11

备份服务器B安装rsync 服务端接收同步文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
[root@weekhigh ~]# yum install rsync -y

[root@weekhigh ~]# mkdir /data/html

[root@weekhigh ~]# vi /etc/rsyncd.conf

uid = root                         #用户id

gid = root

use chroot = no                    #安全性,内网一般不考虑,设为no

max connections = 200              #最多有多少个客户端连接我

timeout = 300                      #超时时间,秒

pid file = /var/run/rsyncd.pid     #pid文件

lock file = /var/run/rsync.lock    #传输时会给文件加锁

log file = /var/log/rsyncd.log     #日志文件

 # any name you like

[bkdir]

# destination directory for copy

path = /data/html                  #客户端来同步,就是同步该目录

# hosts you allow to access

hosts allow = 192.168.20.6/24      #允许的IP段

hosts deny = 0.0.0.0/32            #拒绝

ignore errors                      #传输过程中遇到错误,自动忽略

read only = false                  #可读可写

list = true                        #允许列表

auth users = rsync_root            #这是个虚拟用户

secrets file = /etc/rsync.password #虚拟用户对应的密码文件

#直接使用root是否需要授权??

#[root@weekhigh ~]# useradd root -s /sbin/nologin

#授权

#[root@weekhigh ~]# chown -R root.root /data/html

[root@weekhigh ~]# echo "rsync_root:889977" > /etc/rsync.password

[root@weekhigh ~]# chmod 600 /etc/rsync.password

[root@weekhigh ~]# systemctl start rsyncd

[root@weekhigh ~]# systemctl enable rsyncd

服务器A安装lsyncd监控+rsync客户端发送同步

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
[root@akkun ~]# yum -y install lua lua-devel pkgconfig gcc asciidoc

[root@akkun ~]# rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

[root@akkun ~]# yum install lsyncd rsync -y

#rsync客户端设置服务端的账号密码

[root@akkun ~]# echo "889977" > /etc/rsync.password

[root@akkun ~]# chmod 600 /etc/rsync.password

#配置conf

[root@akkun ~]# cat > /etc/lsyncd.conf << EOF

----

-- User configuration file for lsyncd.

--

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

-- create by sh

-- For more examples, see /usr/share/doc/lsyncd*/examples/

-- 本次启用III

-- III. 远程目录同步,rsync模式 + rsyncd daemon

sync {

 default.rsync,

 source = "/var/www/html/",

 target = "rsync_root@192.168.20.11::bkdir",

 delete="running",

 exclude = { ".*", ".tmp" },

 delay = 30,

 init = false,

 rsync = {

  binary = "/usr/bin/rsync",

  archive = true,

  compress = true,

  verbose = true,

  password_file = "/etc/rsyncd.d/rsync.pwd",

  _extra = {"--bwlimit=200"}

 }

}

-- IV. 远程目录同步,rsync模式 + ssh shell

sync {

 default.rsync,

 source = "/var/www/html/",

 target = "192.168.20.11:/data/html",

-- target = "root@192.168.20.11:/data/html",

-- 上面target,注意如果是普通用户,必须拥有写权限

 maxDelays = 5,

 delay = 30,

-- init = true,

rsync = {

 binary = "/usr/bin/rsync",

 archive = true,

 compress = true,

 bwlimit = 2000

-- rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"

-- 如果要指定其它端口,请用上面的rsh

}

}

-- V. 远程目录同步,rsync模式 + rsyncssh,效果与上面相同

sync {

 default.rsyncssh,

 source = "/var/www/html/",

 host = "192.168.20.11",

 targetdir = "/data/html",

 excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",

 -- maxDelays = 5,

 delay = 0,

 -- init = false,

 rsync = {

 binary = "/usr/bin/rsync",

 archive = true,

 compress = true,

 verbose = true,

 _extra = {"--bwlimit=2000"},

 },

 ssh = {

   port = 22

 }

}

EOF

#开启lsyncd服务

[root@akkun ~]# service lsyncd start

#设置lsyncd服务自启动

[root@akkun ~]# chkconfig lsyncd on

其中IV,V需要手动输入密码,可以采用密钥认证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#服务器B生成或者拷贝源密钥

#[root@weekhigh ~]# ssh-keygen -t rsa

[root@weekhigh ~]# ll ~/.ssh/

id_rsa       id_rsa.pub   known_hosts

[root@weekhigh ~]# cat id_rsa.pub >> ~/.ssh/authorized_keys

[root@weekhigh ~]# ll ~/.ssh/

id_rsa       id_rsa.pub   known_hosts  authorized_keys

[root@weekhigh ~]# chmod -Rf 600 ~/.ssh

#服务器A拷贝来自服务器B生成的 .ssh/文件夹

[root@akkun ~]# chmod -Rf 600 ~/.ssh

[root@akkun ~]# ll ~/.ssh/

id_rsa       id_rsa.pub   known_hosts  authorized_keys

#测试能否无密码登录

[root@akkun ~]# ssh root@192.168.20.11

至此同步完成配置。

本文由作者按照 CC BY 4.0 进行授权