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

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

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

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

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

[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客户端发送同步

[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 = "[email protected]::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 = "[email protected]:/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需要手动输入密码,可以采用密钥认证

#服务器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 [email protected]

至此同步完成配置。

文章作者: AkkunYo
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 AkkunYoの博客
喜欢就支持一下吧