利用sersync和rsync实现Linux服务器间数据实时自动同步

47 篇文章 0 订阅
41 篇文章 0 订阅

 本文介绍在Linux服务上利用sersync和rsync来实现Linux服务器间数据实时自动同步。

  • Sersync是一个基于inotify和rsync的实时文件同步工具,主要用于Linux服务器之间的数据同步。
  • 它通过实时监控指定的目录或文件,当这些目录或文件发生变化时,Sersync会自动触发同步操作,确保数据的实时一致性。
  • Sersync依赖于rsync来进行实际的文件传输,rsync是一个非常流行的开源文件同步工具,支持增量备份和高效的数据传输。
  • Sersync强化了实时监控、文件过滤和简化配置等功能,帮助用户提高运行效率,节省时间和网络资源。
  • 它适用于需要进行实时数据同步的场景,如服务器同步、Web镜像等。

 一、在源数据服务器上准备sersync和rsync

1、sersync官网及下载链接

sersync网站

sersync_master最新版的下载链接

当前最新版为2.5.4

2、服务器上准备目录并下载
[root@localhost ~]# cd /opt
[root@localhost opt]# mkdir sersync
[root@localhost opt]# cd sersync/
[root@localhost sersync]# pwd
/opt/sersync
[root@localhost sersync]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.g
[root@localhost sersync]# mv /tmp/sersync2.5.4_64bit_binary_stable_final.tar.gz .
[root@localhost sersync]# ls -al
total 720
drwxr-x---. 2 root        root       4096 May  6 14:37 .
drwxr-xr-x. 4 root        root       4096 May  6 14:37 ..
-rw-r-----. 1 daijianbing yingjian 727290 May  6 14:35 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@localhost sersync]# tar -xzvf sersync2.5.4_64bit_binary_stable_final.tar.gz 
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
[root@localhost sersync]# mv GNU-Linux-x86 bin
[root@localhost sersync]# tree
.
├── bin
│   ├── confxml.xml
│   └── sersync2
└── sersync2.5.4_64bit_binary_stable_final.tar.gz

1 directory, 3 files
 3、修改sersync配置文件

只需改动localpath和rsync的如下部分内容

       <localpath watch="/app/jtreport_data">
            <remote ip="192.168.1.106" name="report_data"/>  #修改为目标服务器IP和和rsync任务名称
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="ruanjian" passwordfile="/etc/rsync.pas"/>#修改为目标服务器配置的同步帐号并指定存在本机的密码文件名
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync> 

4、在本机准备rsync客户端程序的同步密码文件

文件名为以上sersync配置文件,密码为目标服务器上设定的同步密码(只有密码),注意文件权限为600

[root@localhost  sersync]# cat /etc/rsync.pas
RjSHPQ_jZm2nh
[root@localhost  sersync]# ll /etc/rsync.pas
-rw-------. 1 root root 17 May  7 17:37 /etc/rsync.pa
5、在源服务器上安装rsync包
[root@localhost ~]# yum install rsync
Last metadata expiration check: 3:15:49 ago on 2024年05月16日 星期四 05时37分59秒.
Dependencies resolved.
============================================================================================================================================================
 Package                            Architecture                        Version                                       Repository                       Size
============================================================================================================================================================
Installing:
 rsync                              x86_64                              3.2.3-2.oe2203                                OS                              293 k

Transaction Summary
============================================================================================================================================================
Install  1 Package

Total download size: 293 k
Installed size: 644 k
Is this ok [y/N]: y
Downloading Packages:
rsync-3.2.3-2.oe2203.x86_64.rpm                                                                                             9.6 MB/s | 293 kB     00:00    
------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                       9.1 MB/s | 293 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                    1/1 
  Running scriptlet: rsync-3.2.3-2.oe2203.x86_64                                                                                                        1/1 
  Installing       : rsync-3.2.3-2.oe2203.x86_64                                                                                                        1/1 
  Running scriptlet: rsync-3.2.3-2.oe2203.x86_64                                                                                                        1/1 
  Verifying        : rsync-3.2.3-2.oe2203.x86_64                                                                                                        1/1 

Installed:
  rsync-3.2.3-2.oe2203.x86_64                                                                                                                               

Complete!

二、目标服务器构建rsyncd服务

1、目标服务器安装rsync
[root@localhost ~]# yum install rsync
Last metadata expiration check: 3:15:49 ago on 2024年05月16日 星期四 05时37分59秒.
Dependencies resolved.
============================================================================================================================================================
 Package                            Architecture                        Version                                       Repository                       Size
============================================================================================================================================================
Installing:
 rsync                              x86_64                              3.2.3-2.oe2203                                OS                              293 k

Transaction Summary
============================================================================================================================================================
Install  1 Package

Total download size: 293 k
Installed size: 644 k
Is this ok [y/N]: y
Downloading Packages:
rsync-3.2.3-2.oe2203.x86_64.rpm                                                                                             9.6 MB/s | 293 kB     00:00    
------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                       9.1 MB/s | 293 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                    1/1 
  Running scriptlet: rsync-3.2.3-2.oe2203.x86_64                                                                                                        1/1 
  Installing       : rsync-3.2.3-2.oe2203.x86_64                                                                                                        1/1 
  Running scriptlet: rsync-3.2.3-2.oe2203.x86_64                                                                                                        1/1 
  Verifying        : rsync-3.2.3-2.oe2203.x86_64                                                                                                        1/1 

Installed:
  rsync-3.2.3-2.oe2203.x86_64                                                                                                                               

Complete!
2、修改/etc/rsyncd.conf这个rsyncd的配置文件
[root@localhost  ~]# vi /etc/rsyncd.conf
[root@localhost  ~]# cat /etc/rsyncd.conf
read only = yes
list = yes
uid = root
gid = root
use chroot = no
hosts allow = 192.168.1.105,192.168.1.105,192.168.1.107
max connections = 10000
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock

[report_data]
path = /app/jtreport_scrpit
list = yes
ignore errors = yes
auth users=ruanjian
secrets file=/etc/rsyncd.passwd

全局部分hosts allow用于指定可以访问本机服务的源主机IP,可以配置多台rsync客户端服务器

以下部分为一个数据同步任务的配置,

[report_data]
path = /app/jtreport_scrpit   #配置本同步任务的本地目标文件夹
list = yes    #是否允许list
ignore errors = yes   #是否忽略错误
auth users=ruanjian   #本任务的同步帐号
secrets file=/etc/rsyncd.passwd   #本任务的同步密码配置文件

以下任务设备可以配置多项,对于大数据量有子文件夹的同步任务,可以按子文件夹设置多个同步任务,以此来实现多进程并行同步操作。

3、准备同步密码配置文件 
[root@localhost  ~]# cat /etc/rsyncd.passwd 
ruanjian:RjSHPQ_jZm2nh
[root@localhost  ~]# ll  /etc/rsyncd.passwd
-rw-------. 1 root root 26 May  7 17:27 /etc/rsyncd.passwd

注意 ,rsyncd的密码配置文件中是包含了用户名和密码的,用:分隔;而rsync客户端的密码配置文件是只含密码的。

 4、目标服务器启动rsyncd同步服务器端程序
[root@localhost  ~]# systemctl start rsyncd
[root@localhost  ~]# 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 2024-05-13 10:37:06 CST; 2 days ago
   Main PID: 1223 (rsync)
      Tasks: 1 (limit: 408508)
     Memory: 7.4G
     CGroup: /system.slice/rsyncd.service
             └─ 1223 /usr/bin/rsync --daemon --no-detach

May 13 10:37:06 JZXN-PH-10.193.217.106 systemd[1]: Started fast remote file copy program daemon.

三、在源数据服务器上启动sersync服务

1、直接运行

bin/sersync2 -d -r -o bin/confxml.xml

2、建议用shell脚本进行管理

[root@localhost sersync]# vi runner.sh
[root@localhost sersync]# cat runner.sh 
#!/bin/bash
##__author__='daigjianbing'
basepath=$(cd `dirname $0`;pwd)

#echo $basepath
case "$1" in
     start)
         echo "stop sersync task!" && pkill sersync2
         echo "start sersync task!" && $basepath/bin/sersync2 -d -r -o $basepath/bin/confxml.xml 2>&1 &
         echo "start finished!";;
     status)
         ps -ef |grep sersync2 |grep -v grep
         echo "check status finished!";;
     stop)
         echo "stop sersync task!" && pkill sersync2
         echo "stop finished!";;
     *)
         echo 'runner.sh start|status|stop'
         exit 1;;
esac
exit 0
[root@localhost sersync]# chmod a+rx runner.sh

使用方法,"./runner.sh start"启动服务,"./runner.sh status"查看任务,"./runner.sh stop"停止服务。

当源服务器启动同步服务后,程序即自动开始监控指定的目录或文件操作情况,实时同步到目标服务器。

  • 32
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代先生.重庆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值