CentOS8.2 配置部署Rsync+sersync和rsync+inotify文件实时同步

  • A+
所属分类:linux技术
摘要

【1】安装rsync【2】创建rsync配置文件【3】启动rsync和xinetd服务【4】配置rsync开机自启动

CentOS8.2 配置rsync+sersync文件实时同步

Rsyncd 服务器(备份端) 数据源端测试rsync同步
主机名 rsyncserver.lianglab.cn sersyncserver.lianglab.cn
IP信息 192.168.6.200 192.168.6.65

第一步:Rsyncd 服务器(备份端)配置

192.168.6.200----rsyncd Rsync服务器(备份端) hostnamectl set-hostname rsyncserver.lianglab.cn 查看rsync是否安装  [root@centos8 ~]# rpm -qa | grep rsync rsync-3.1.3-7.el8.x86_64 [root@centos8 ~]#  

【1】安装rsync

[root@rsyncserver ~]# dnf -y install xinetd rsync Last metadata expiration check: 1:22:21 ago on Mon 21 Dec 2020 09:09:48 PM CST. Package rsync-3.1.3-7.el8.x86_64 is already installed. Dependencies resolved. ====================================================================================================================  Package                 Architecture            Version                           Repository                  Size ==================================================================================================================== Installing:  xinetd                  x86_64                  2:2.3.15-24.el8                   AppStream                  135 k  Transaction Summary ==================================================================================================================== Install  1 Package  Total download size: 135 k Installed size: 379 k Downloading Packages: xinetd-2.3.15-24.el8.x86_64.rpm                                                     7.5 MB/s | 135 kB     00:00     -------------------------------------------------------------------------------------------------------------------- Total                                                                               6.9 MB/s | 135 kB     00:00      Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction   Preparing        :                                                                                            1/1    Installing       : xinetd-2:2.3.15-24.el8.x86_64                                                              1/1    Running scriptlet: xinetd-2:2.3.15-24.el8.x86_64                                                              1/1    Verifying        : xinetd-2:2.3.15-24.el8.x86_64                                                              1/1  Installed products updated.  Installed:   xinetd-2:2.3.15-24.el8.x86_64                                                                                       Complete! [root@rsyncserver ~]#   [root@rsyncserver ~]# rsync --daemon [root@rsyncserver ~]# netstat -antup | grep 873 tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      51170/rsync          tcp6       0      0 :::873                  :::*                    LISTEN      51170/rsync          [root@rsyncserver ~]#   

【2】创建rsync配置文件

[root@rsyncserver ~]# cat /etc/rsyncd.conf   #Rsync server uid = root                         #运行进程的身份 gid = root                         #运行进程的组 use chroot = yes                   #是否锁定家目录 max connections = 100              #最大连接数 timeout = 600                      #超时时间 log file = /var/log/rsyncd.log     #日志文件 ignore errors                      #忽略错误 read only = false                  #设置服务端文件读写权限 list = false                       #不显示服务端资源列表 hosts allow = 192.168.6.0/24       #*代表所有 hosts deny = 0.0.0.0/32 auth users = backup secrets file = /etc/rsync.password  [data] comment = data path = /data/www   

【3】启动rsync和xinetd服务

[root@rsyncserver ~]# systemctl start xinetd  [root@rsyncserver ~]# systemctl enable xinetd [root@rsyncserver ~]# rsync --daemon --config=/etc/rsyncd.conf [root@rsyncserver ~]# netstat -antup | grep 873 tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      51170/rsync          tcp6       0      0 :::873                  :::*                    LISTEN      51170/rsync          [root@rsyncserver ~]# ps aux | grep rsync root       51170  0.0  0.0  11508   192 ?        Ss   22:32   0:00 rsync --daemon root       51322  0.0  0.0  12108  1048 pts/1    S+   22:34   0:00 grep --color=auto rsync [root@rsyncserver ~]#  

【4】配置rsync开机自启动

方法一: echo "rsync --daemon --config=/etc/rsyncd.conf"  >> /etc/rc.d/rc.local  方法二:  

【5】创建同步目录

[root@rsyncserver ~]# mkdir -p /data/www 

【6】创建密码文件

[root@rsyncserver ~]# vi  /etc/rsync.password [root@rsyncserver ~]#  [root@rsyncserver ~]# cat /etc/rsync.password backup:lianglab123 [root@rsyncserver ~]# chmod 600 /etc/rsync.password 

第二步:数据源端测试rsync同步

【1】新建一个文件保存好密码,然后在rsync命令中使用–password-file指定此文件即可

[root@sersyncserver ~]# hostnamectl set-hostname sersyncserver.lianglab.cn  [root@sersyncserver ~]# echo "lianglab123" > /etc/rsync.password  [root@sersyncserver ~]# chmod 600 /etc/rsync.password  

【2】创建测试文件

[root@sersyncserver ~]# mkdir -p /data/www [root@sersyncserver ~]# cp /etc/passwd /data/www/ [root@sersyncserver www]# md5sum /data/www/passwd  b42f457347877832fc6da97e6cc3481e  /data/www/passwd [root@sersyncserver ~]# rsync -avzP  /data/www/ [email protected]::data  --password-file=/etc/rsync.password sending incremental file list ./  sent 74 bytes  received 27 bytes  67.33 bytes/sec total size is 2,923  speedup is 28.94 [root@sersyncserver ~]#   

【3】登陆到rsyncserver 查看文件是否同步

[root@rsyncserver www]# cd /data/www/ [root@rsyncserver www]# ls passwd [root@rsyncserver www]# pwd /data/www [root@rsyncserver www]# md5sum passwd  b42f457347877832fc6da97e6cc3481e  passwd [root@rsyncserver www]#   

CentOS8.2 配置部署Rsync+sersync和rsync+inotify文件实时同步

第三步:数据源端开始部署sersync服务

[1] 在google code下载sersync软件包,里面有配置文件与可执行文件
wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@sersyncserver tmp]# wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz [root@sersyncserver tmp]# tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz GNU-Linux-x86/ GNU-Linux-x86/sersync2 GNU-Linux-x86/confxml.xml [root@sersyncserver tmp]# mv GNU-Linux-x86 sersync [root@sersyncserver tmp]# mv /tmp/sersync /opt [root@sersyncserver opt]# tree /opt/sersync/ /opt/sersync/ ├── confxml.xml  #sersync软件配置文件 ├── confxml.xml.2020-12-21 └── sersync2    #sersync软件命令  0 directories, 3 files [root@sersyncserver opt]#   

[2] 配置sersync

[root@sersyncserver opt]# cp sersync/confxml.xml sersync/confxml.xml.$(date +%F) [root@sersyncserver opt]# cd sersync/ [root@sersyncserver sersync]# ls confxml.xml  confxml.xml.2020-12-21  sersync2 [root@sersyncserver sersync]#  

[3]修改sersync配置文件24-28行

修改前的内容        23	    <sersync>     24		<localpath watch="/opt/tongbu">     25		    <remote ip="127.0.0.1" name="tongbu1"/>     26		    <!--<remote ip="192.168.8.39" name="tongbu"/>-->     27		    <!--<remote ip="192.168.8.40" name="tongbu"/>-->     28		</localpath>     29		<rsync>     30		    <commonParams params="-artuz"/>     31		    <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>     32		    <userDefinedPort start="false" port="874"/><!-- port=874 -->     33		    <timeout start="false" time="100"/><!-- timeout=100 -->     34		    <ssh start="false"/>  

[4].修改31–34行,认证部分【rsync密码认证】

修改后的内容     23	    <sersync>     24		<localpath watch="/data/www">                           #本地同步目录     25		    <remote ip="192.168.6.200" name="data"/>            #指定备份服务器模块信息     26		    <!--<remote ip="192.168.8.39" name="tongbu"/>-->     27		    <!--<remote ip="192.168.8.40" name="tongbu"/>-->     28		</localpath>     29		<rsync>     30		    <commonParams params="-artuz"/>     31		    <auth start="true" users="backup" passwordfile="/etc/rsync.password"/>    #开启用户认证,并定义认证用户和认证密码文件     32		    <userDefinedPort start="false" port="874"/><!-- port=874 --> #备份服务器的端口     33		    <timeout start="false" time="100"/><!-- timeout=100 -->  #定义超时时间     34		    <ssh start="false"/>     35		</rsync>  

[5].开启sersync守护进程同步数据

[root@sersyncserver sersync]# /opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml set the system param execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events parse the command param option: -d 	run as a daemon option: -r 	rsync all the local files to the remote servers before the sersync work option: -o 	config xml name:  /opt/sersync/confxml.xml daemon thread num: 10 parse xml config file host ip : localhost	host port: 8008 daemon start,sersync run behind the console  use rsync password-file : user is	backup passwordfile is 	/etc/rsync.password config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)  Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate ------------------------------------------ rsync the directory recursivly to the remote servers once working please wait... execute command: cd /data/www && rsync -artuz -R --delete ./ [email protected]::data --password-file=/etc/rsync.password >/dev/null 2>&1  run the sersync:  watch path is: /data/www [root@sersyncserver sersync]#    

CentOS8.2 配置rsync+inotify文件实时同步


CentOS8.2 配置rsync+inotify实现文件实时同步需求描述:线上有一套财务系统在服务器A,已经完成前期的开发,
服务器B用于推送管理层报表到管理层钉钉群。目前没有做到自动化,每次报表生产后(图片格式)服务器A,
每次需要管理员登录的服务器A把文件传送动服务器B指定目录,服务器B才能推送管理层报表到管理层钉钉群

环境:

192.168.6.198 服务端(用于备份数据,数据提供给领导查看)
192.168.6.199 客户端(产生新的文件,新文件同步到服务端)

Rsyncd 服务器 客户端
主机名 nginx01.lianglab.cn nginx02.lianglab.cn
IP信息 192.168.6.198 192.168.6.199

1、安装以下安装包lrzsz是xshell上传下载的安装包,如果通过ftp上传软件到机器可以忽略。

dnf install -y lrzsz gcc wget gcc-c++ vim 

2、关闭firewall防火墙,开机禁止启动。线上机器默认关闭,不需要配置。

第一步:开始配置服务端


【1】安装rsync

dnf install -y rsync 

【2】更改配置文件
vim /etc/rsyncd.conf

# /etc/rsyncd: configuration file for rsync daemon mode #工作中指定用户(可以不指定为0) uid = 0 gid = 0 #相当于黑洞.出错定位 use chroot = no ##有多少个客户端同时传文件 max connections =200 ##超时时间 timeout = 300 ##进程号文件 pid ifle = /var/run/rsyncd.pid lock file = /var/run/rsync.lock ##日志文件 log file = /var/log/rsyncd.log ##模块开始(这个模块就是待会儿对端写脚本的里面一个参数的名称) [msone] ##需要同步的目录(准确的说是同步过来后放到哪里的目录路径) path = /data/www ##表示出现错误忽略错误 ignore errors ##表示网络权限可写(本地控制真正可写)(亲测这里写false报错) read only = no ##这里设置IP或让不让同步 list = false #允许的ip或者ip段 hosts allow = 192.168.6.199 ##拒绝,以下表示都不拒绝 #hosts deny = 0.0.0.0/32 ##认证用户 auth users = rsyncli ##用户名和密码存放文件 secrets file = /etc/rsync.secrets 

【3】创建领导要查看文件目录,需要www这个目录其它用户可以写的权限

mkdir -p /data/www(这个是测试目录,你的系统上面应该已经有这个目录了,根据你生产需要修改这个目录) chmod 765 /data/www 

【4】添加同步需要用的用户名和密码

#useradd -s /sbin/nologin rsyncli #passwd rsyncli 密码设置liang123 

【5】创建认证文件

vi /etc/rsync.secrets 文件里面添加如下内容(格式为用户名:密码): rsyncli:liang123 

【6】为了安全起见给这个文件设置600权限。

[root@nginx01 ~]# ll /etc/rsync.secrets -rw-r--r--. 1 root root 17 Aug 10 11:54 /etc/rsync.secrets [root@nginx01 ~]# chmod 600 /etc/rsync.secrets [root@nginx01 ~]# ll /etc/rsync.secrets -rw-------. 1 root root 17 Aug 10 11:54 /etc/rsync.secrets [root@nginx01 ~]# 

【7】启动服务

[root@nginx01 ~]# systemctl start rsyncd  [root@nginx01 ~]# netstat -anplt Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 13127/rsync tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 876/sshd tcp 0 0 192.168.6.198:29550 192.168.6.199:9100 ESTABLISHED 29780/./prometheus tcp 0 48 192.168.6.198:22 10.57.237.13:53860 ESTABLISHED 12322/sshd: tdops [ tcp 0 0 192.168.6.198:22 10.57.237.13:53862 ESTABLISHED 12326/sshd: tdops [ tcp 0 0 127.0.0.1:57758 127.0.0.1:9090 ESTABLISHED 29780/./prometheus tcp6 0 0 :::9090 :::* LISTEN 29780/./prometheus tcp6 0 0 :::873 :::* LISTEN 13127/rsync tcp6 0 0 :::22 :::* LISTEN 876/sshd tcp6 0 0 ::1:9090 ::1:4852 ESTABLISHED 29780/./prometheus tcp6 0 0 127.0.0.1:9090 127.0.0.1:57758 ESTABLISHED 29780/./prometheus tcp6 0 0 ::1:4852 ::1:9090 ESTABLISHED 29780/./prometheus [root@nginx01 ~]# systemctl status rsyncd ● rsyncd.service - fast remote file copy program daemon Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2020-08-10 11:56:00 CST; 20s ago Main PID: 13127 (rsync) CGroup: /system.slice/rsyncd.service └─13127 /usr/bin/rsync --daemon --no-detach  Aug 10 11:56:00 nginx01 systemd[1]: Started fast remote file copy program daemon. Aug 10 11:56:00 nginx01 rsyncd[13127]: Unknown Parameter encountered: "pid ifle" Aug 10 11:56:00 nginx01 rsyncd[13127]: IGNORING unknown parameter "pid ifle" Aug 10 11:56:00 nginx01 rsyncd[13127]: rsyncd version 3.1.2 starting, listening on port 873 [root@nginx01 ~]# 

第二步:开始配置客户端


【1】查看当前系统是否支持inotify

[root@nginx02 tdops]# ll /proc/sys/fs/inotify/ total 0 -rw-r--r--. 1 root root 0 Aug 10 13:42 max_queued_events -rw-r--r--. 1 root root 0 Aug 10 13:42 max_user_instances -rw-r--r--. 1 root root 0 Aug 10 13:42 max_user_watches [root@nginx02 tdops]# 

【2】安装rsync和inotify(rsync只安装不启动不配置)

[root@nginx02 tdops]# dnf install -y rsync 

【3】inotify是一种强大的、细粒度的、异步的文件系统事件控制机制。
linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种事件,
利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施监控的软件。

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz tar zxf inotify-tools-3.14.tar.gz cd inotify-tools-3.14 ./configure --prefix=/usr/local/include/ make && make install 

【4】最重要的是要写这个脚本,让它自动去检测我们制定的目录下文件发生的变化,
然后再执行rsync的命令把它同步到我们的服务器端去。

touch inotify.sh chmod 755 inotify.sh vi inotify.sh  #!/bin/bash #chkconfig: 345 88 14 host=192.168.6.198 #server的ip(备份服务器) src=/data/www/ #所要监控的备份目录(此处可以自定义,但是要保证存在) des=msone #自定义的模块名,需要与client端定义的一致 password=/etc/rsync.password #密码文件 user=rsyncli #用户名(一个实际存在的账号) inotify=/usr/local/include/ #inotify的安装目录  ${inotify}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src  | while read files do rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des echo "${files} was rsynced" >>/var/log/rsync.log 2>&1 done 

【5】创建/etc/rsync.password文件(整个文件需要600的权限和服务端rsync同步用户的密码,这里是服务器端rsyncli用户的密码)

vi /etc/rsync.password liang123 chmod 600 /etc/rsync.password 

【6】设置内核参数提高数据同步性能。

[root@nginx02 opt]# sysctl -w fs.inotify.max_queued_events="200000000" fs.inotify.max_queued_events = 200000000 [root@nginx02 opt]# sysctl -w fs.inotify.max_user_watches="2000000000" fs.inotify.max_user_watches = 2000000000 [root@nginx02 opt]# sysctl -w fs.inotify.max_user_instances="65535" fs.inotify.max_user_instances = 65535 

【7】启动服务脚本

chmod +x /opt/inotify.sh [root@nginx02 opt]# nohup /opt/inotify.sh &> /dev/null & #放到后台执行 [1] 16011 [1]+ Done nohup /opt/inotify.sh &>/dev/null [root@nginx02 opt]#   

Rsync+inotify-tools与rsync+sersync架构的区别


1、rsync+inotify-tools a、inotify只能记录下被监听的目录发生了变化(增,删,改)并没有把具体是哪个文件或者哪个目录发生了变化记录下来; b、rsync在同步的时候,并不知道具体是哪个文件或目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此效率很低  2、rsync+sersync a、sersync可以记录被监听目录中发生变化的(增,删,改)具体某个文件或目录的名字; b、rsync在同步时,只同步发生变化的文件或目录(每次发生变化的数据相对整个同步目录数据来说很小,rsync在遍历查找对比文件时,速度很快),因此效率很高。