目录

一、背景

二、环境

三、实验步骤

1、备份服务器操作

2、数据源服务器操作

3、测试数据同步

4、安装sersync工具,实时触发rsync进行同步

5、测试数据实时同步

四、总结

五、注意事项

一、背景

  rsync 是一个用于文件同步和传输的工具,它可以在本地和远程文件系统之间同步文件。

  rsync 通过增量传输的方式,只传输源和目标之间不同的部分,这使其在网络传输时非常高效。它通常用于备份、镜像和迁移任务。

二、环境

备份服务器:192.168.56.129;操作系统:centos7.9

数据源服务器:192.168.56.128;操作系统:centos7.9

三、实验步骤

1、备份服务器操作

关闭selinux,永久关闭防火墙

[root@mysql2 ~]# sed -i '/^SELINUX/s/enforcing/disable/' /etc/selinux/config

[root@mysql2 ~]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.

# SELINUX= can take one of these three values:

# enforcing - SELinux security policy is enforced.

# permissive - SELinux prints warnings instead of enforcing.

# disabled - No SELinux policy is loaded.

SELINUX=disabled

# SELINUXTYPE= can take one of three values:

# targeted - Targeted processes are protected,

# minimum - Modification of targeted policy. Only selected processes are protected.

# mls - Multi Level Security protection.

SELINUXTYPE=targeted

[root@mysql2 ~]# getenforce

Disabled

[root@mysql2 ~]# systemctl stop firewalld

[root@mysql2 ~]# systemctl disable firewalld

[root@mysql2 ~]# systemctl status firewalld

● firewalld.service - firewalld - dynamic firewall daemon

Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)

Active: inactive (dead)

Docs: man:firewalld(1)

安装rsync软件

[root@mysql2 ~]# yum install rsync xinetd -y

[root@mysql2 ~]# echo "/usr/bin/rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.d/rc.local

[root@mysql2 ~]# systemctl start xinetd

 创建rsyncd.conf配置文件

[root@mysql2 ~]# vim /etc/rsyncd.conf

写入

uid = root

gid = root

use chroot = yes

max connections = 0

log file = /var/log/rsyncd.log

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

secrets file = /etc/rsync.pass

motd file = /etc/rsyncd.Motd

[back_data] #配置项名称(自定义)

path = /backup #备份文件存储地址

comment = A directory in which data is stored

ignore errors = yes

read only = no

hosts allow = 192.168.56.128 #允许的IP地址(数据源服务器地址)

创建用户认证文件

[root@mysql2 ~]# vim /etc/rsync.pass

写入

#格式,用户名:密码,可以设置多个,每一行一个用户名:密码

simons:123456

设置文件权限

[root@mysql2 ~]# chmod 600 /etc/rsyncd.conf #设置文件所有者读取、写入权限

[root@mysql2 ~]# chmod 600 /etc/rsync.pass #设置文件所有者读取、写入权限

启动rsync和xinetd

[root@mysql2 ~]# /usr/bin/rsync --daemon --config=/etc/rsyncd.conf

[root@mysql2 ~]# ps aux |grep rsyncd

root 8566 0.0 0.0 114852 576 ? Ss 20:37 0:00 /usr/bin/rsync --daemon --config=/etc/rsyncd.conf

root 8572 0.0 0.0 112824 988 pts/0 S+ 20:38 0:00 grep --color=auto rsyncd

[root@mysql2 ~]# systemctl start xinetd

2、数据源服务器操作

关闭selinux,永久关闭防火墙

[root@mysql ~]# sed -i '/^SELINUX/s/enforcing/disabled/' /etc/selinux/config

[root@mysql ~]# systemctl stop firewalld

[root@mysql ~]# systemctl disable firewalld

[root@mysql ~]# systemctl status firewalld

● firewalld.service - firewalld - dynamic firewall daemon

Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)

Active: inactive (dead)

Docs: man:firewalld(1)

安装rsync软件

[root@mysql ~]# yum install rsync xinetd -y

#设置开机启动

[root@mysql ~]# echo "/usr/bin/rsync --daemon" >> /etc/rc.d/rc.local

[root@mysql ~]# cat /etc/rc.d/rc.local

#!/bin/bash

# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES

#

# It is highly advisable to create own systemd services or udev rules

# to run scripts during boot instead of using this file.

#

# In contrast to previous versions due to parallel execution during boot

# this script will NOT be run after all other services.

#

# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure

# that this script will be executed during boot.

touch /var/lock/subsys/local

ulimit -n 1000000

/usr/bin/rsync --daemon

 创建rsyncd.conf配置文件

#修改配置文件

vim /etc/rsyncd.conf

添加

log file = /var/log/rsyncd.log

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

motd file = /var/rsyncd.Motd

[Sync]

comment = Sync

uid = root

gid = root

port = 873

 启动xinetd服务管理rsync服务

[root@mysql ~]# systemctl start xinetd

创建认证密码文件

[root@mysql ~]# vim /etc/passwd.txt

#编辑文件,添加以下内容,该密码应与目标服务器中的/etc/rsync.pass中的密码一致

123456

设置文件权限,只设置文件所有者具有读取、写入权限即可

[root@mysql ~]# chmod 600 /etc/passwd.txt

3、测试数据同步

数据源服务器192.168.56.128到备份服务器192.168.56.129,之间的数据

 (在数据源服务器上的操作)

[root@mysql ~]# rsync -avH --port=873 --progress --delete /backup(要备份的数据源目录) root@192.168.56.129::back_data --password file=/etc/passwd.txt

出现如下视图,表示成功

sending incremental file list

backup/

backup/all_db.sql

875,989 100% 160.83MB/s 0:00:00 (xfr#1, to-chk=0/2)

sent 876,325 bytes received 39 bytes 1,752,728.00 bytes/sec

total size is 875,989 speedup is 1.00

4、安装sersync工具,实时触发rsync进行同步

在数据源服务器上修改inotify默认参数(inotify默认内核参数太小),修改参数:

[root@mysql backup]# sysctl -w fs.inotify.max_queued_events="99999999"

fs.inotify.max_queued_events = 99999999

[root@mysql backup]# sysctl -w fs.inotify.max_user_watches="99999999"

fs.inotify.max_user_watches = 99999999

[root@mysql backup]# sysctl -w fs.inotify.max_user_instances="65535"

fs.inotify.max_user_instances = 65535

(以上为临时修改,想要达到永久修改的目的需要写入配置文件,如下:)

[root@mysql backup]# vim /etc/sysctl.cnf

写入

fs.inotify.max_queued_events=99999999

fs.inotify.max_user_watches=99999999

fs.inotify.max_user_instances=65535

安装sersync

[root@mysql backup]# wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz

--2024-03-17 10:51:26-- http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz

正在解析主机 down.whsir.com (down.whsir.com)... 111.180.191.24

正在连接 down.whsir.com (down.whsir.com)|111.180.191.24|:80... 已连接。

已发出 HTTP 请求,正在等待回应... 301 Moved Permanently

位置:https://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz [跟随至新的 URL]

--2024-03-17 10:51:26-- https://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz

正在连接 down.whsir.com (down.whsir.com)|111.180.191.24|:443... 已连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:727290 (710K) [application/octet-stream]

正在保存至: “sersync2.5.4_64bit_binary_stable_final.tar.gz”

100%[==========================================================================================>] 727,290 4.22MB/s 用时 0.2s

2024-03-17 10:51:26 (4.22 MB/s) - 已保存 “sersync2.5.4_64bit_binary_stable_final.tar.gz” [727290/727290])

解压sersync2.5.4_64bit_binary_stable_final.tar.gz 

[root@mysql backup]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz

移动解压后的文件到/usr/local改名为sersync

[root@mysql backup]# mv GNU-Linux-x86/ /usr/local/sersync

创建resync

进入目录/usr/local/sersync,备份内容

[root@mysql sersync]# cp confxml.xml confxml.xml.bak

[root@mysql sersync]# cp confxml.xml data_configxml.xml

(data_configxml.xml才是后面需要使用的配置文件)

修改data_configxml.xml配置文件

[root@mysql sersync]# vim data_configxml.xml

24

25

26

27

28

29

30

31

32

33

34

启动sersync服务

[root@mysql sersync]# /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/data_configxml.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: /usr/local/sersync/data_configxml.xml

daemon thread num: 10

parse xml config file

host ip : localhost host port: 8008

daemon start,sersync run behind the console

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 /backup && rsync -artuz -R --delete ./ 192.168.56.131::back_data >/dev/null 2>&1

run the sersync:

watch path is: /backup

设置sersync监控开机自动执行

[root@mysql sersync]# echo "/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/data_configxml.xml" >>/etc/rc.d/rc.local

5、测试数据实时同步

在数据源服务器监听的/backup目录下,新建或者删除文件在备份服务器上对应的/backup目录下,查看效果

四、总结

        结合使用 rsync 和 sersync 来实现高效的实时文件同步。例如,使用 sersync 来监控本地目录的变化,并在变化发生时触发 rsync 命令将文件同步到远程服务器。这样可以实现一个实时的、高效的、自动化的文件同步解决方案。

五、注意事项

确保源和目标服务器之间的网络连接稳定可靠。根据需要配置合适的过滤规则,以避免不必要的同步。监控同步过程,确保没有错误或遗漏。在生产环境中使用前,先在测试环境中验证同步的准确性和性能。

相关文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。