19.系统间复制文档(从零开始学)

!!!在开启一天的学习的时候,先做好快照,过程中如果出现意外的报错,解决不了的就立即恢复快照,作为初学者,省时省力,不要过于纠结哪里错了,浪费时间!!!

1. windows 和 Linux 之间传输

1.1 xftp工具

在两个窗口之间拖动文件进行上传和下载。

1.2 xshell 工具

Linux服务器安装 lrzsz 软件包。该方式传输速度比ftp慢。

[root@server ~ 21:41:00]# yum install -y lrzsz
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                    | 3.6 kB  00:00:00     
epel                                                                    | 4.3 kB  00:00:00     
extras                                                                  | 2.9 kB  00:00:00     
updates                                                                 | 2.9 kB  00:00:00     
软件包 lrzsz-0.12.20-36.el7.x86_64 已安装并且是最新版本
无须任何处理

上传:直接将文件拖拽到命令行窗口即可。

[root@server ~ 21:46:48]# ls
anaconda-ks.cfg  etc.tar               test.txt  模板  图片  下载  桌面
etc              initial-setup-ks.cfg  公共      视频  文档  音乐
[root@server ~ 21:47:03]# rz -E
rz waiting to receive.
[root@server ~ 21:47:11]# ls
10.1.8.10_2026-07-22_10_47_09.log  etc      initial-setup-ks.cfg  公共  视频  文档  音乐
anaconda-ks.cfg                    etc.tar  test.txt              模板  图片  下载  桌面
[root@server ~ 21:47:17]# ll
总用量 28
-rw-r--r--. 1 root root  6424 7月  22 10:47 10.1.8.10_2026-07-22_10_47_09.log
-rw-------. 1 root root  1777 7月  14 10:34 anaconda-ks.cfg
drwxr-xr-x. 2 root root    89 7月  27 21:36 etc
-rw-r--r--. 1 root root 10240 7月  27 21:40 etc.tar
-rw-r--r--. 1 root root  1825 7月  14 10:38 initial-setup-ks.cfg
-rw-r--r--. 1 root root     0 7月  16 10:28 test.txt
drwxr-xr-x. 2 root root     6 7月  14 14:53 公共
drwxr-xr-x. 2 root root     6 7月  14 14:53 模板
drwxr-xr-x. 2 root root     6 7月  14 14:53 视频
drwxr-xr-x. 2 root root     6 7月  14 14:53 图片
drwxr-xr-x. 2 root root     6 7月  14 14:53 文档
drwxr-xr-x. 2 root root     6 7月  14 14:53 下载
drwxr-xr-x. 2 root root     6 7月  14 14:53 音乐
drwxr-xr-x. 2 root root     6 7月  14 14:53 桌面

拉取:使用 sz 工具,并指定保存位置。

2. Linux 之间传输

2.1 scp 命令

scp,全名secure copy,也就是安全复制,是基于ssh协议,也就是说Linux服务器要开启ssh服务。

# 直接对接,不用密码
  392  2026-07-27 21:57:08 ssh-keygen -f .ssh/id_rsa -N ''
  393  2026-07-27 21:57:24 ssh-copy-id server
  394  2026-07-27 21:57:54 ssh-copy-id root@server


[fengkai@client ~ 21:51:51]$ scp root@server:/root/etc.tar .
root@server's password: 
etc.tar                                                    100%   10KB 707.9KB/s   00:00    

# 复制多个文件
[fengkai@client ~ 21:59:59]$ scp root@server:/root/{etc.tar,test.txt} .
etc.tar                                                     100%   10KB   5.4MB/s   00:00    
test.txt                                                    100%    0     0.0KB/s   00:00   

# 复制目录,使用 -r 选项
[fengkai@client ~ 22:00:40]$ scp root@server:/etc/selinux/ .
scp: /etc/selinux: not a regular file

[fengkai@client ~ 22:02:21]$ scp -r root@server:/etc/selinux/ .
...
seusers.linked                                              100%  106    78.1KB/s   00:00    
seusers                                                     100%  106    73.8KB/s   00:00    
users_extra.linked                                          100%  101    64.3KB/s   00:00    
policy.linked                                               100% 3792KB  71.8MB/s   00:00    
file_contexts.local                                         100%  140    40.6KB/s   00:00    


# 上传,如果路径没有写,表示相对用户家目录。这里是上传到root用户家目录/root
[root@client ~ 22:07:09]# scp etc.tar root@server:
etc.ta                                                      100%   37MB  70.4MB/s   00:00    
[root@client ~ 22:07:24]# tar -cf etc$(date +%Y%m%d).tar /etc/
tar: 从成员名中删除开头的“/”
[root@client ~ 22:08:16]# ls
anaconda-ks.cfg  etc.tar               test.txt  模板  图片  下载  桌面
etc20260727.tar  initial-setup-ks.cfg  公共      视频  文档  音乐
[root@client ~ 22:08:17]# scp etc.tar etc20260727.tar root@server:
etc.tar                                                     100%   37MB  85.2MB/s   00:00    
etc20260727.tar                                             100%   37MB  69.6MB/s   00:00 
[root@client ~ 22:10:44]# scp -r /etc/ root@server:
···
management-agent.jar                                        100%  377   156.4KB/s   00:00    
meta-index                                                  100% 1955     1.1MB/s   00:00    
net.properties                                              100% 5352     3.1MB/s   00:00  
...

==scp 命令缺点==:不管目的位置是否有文件,总是再复制一次,可以理解为全量备份。缺少比对功能。

2.2 rsync 命令

Rsync(Remote Synchronize)是一款开源的、快速的、多功能的、远程数据同步备份工具,并且支持多种操作系统平台运行。

Rsync具有本地与远程两台主机之间的数据快速复制同步镜像、远程备份等功能,该功能类似scp,但是优于scp功能,还具有本地不同分区目录之间全量及增量复制数据。

Rsync同步数据镜像时,通过“quick check”算法,仅同步大小或最后修改时间发生变化的文件或目录,当然也可以根据权限,属主等属性变化的同步,所以可以实现快速同步。

前提:客户端和服务端都要提前安装好 rsync 软件包。

# 服务端安装 rsync
[root@server ~ 13:33:22]# yum install -y rsync

# 客户端安装 rsync
[fengkai@client ~ 13:54:08]$ sudo yum install -y rsync

2.2.1 shell 模式

shell 模式是基于 sshd 服务传送。

语法:

rsync [OPTION]… SRC DEST
rsync [OPTION]… SRC [USER@]HOST:DEST
rsync [OPTION]… [USER@]HOST:SRC DEST

常用选项

  • -n 参数执行空运行,与真实执行显示结果一致,但是没有做任何改变。
  • -v 显示执行过程中详细输出。
  • -a 代表“archive mode”,同时启用参数:-r -l -p -t -g -o -D
  • -r 递归同步整个文件夹
  • -l 同步软连接
  • -p 保留权限
  • -t 保留时间戳
  • -g 保留所属组
  • -o 保留所有者
  • -D 同步设备文件

说明:

  • -A,同步时保留ACLs内容。
  • -X 同步时保留selinux内容。-a,不同步acl selinux内容。
# 准备文件
[root@client ~ 13:55:21]# mkdir Pictures
[root@client ~ 13:55:33]# touch Pictures/snap{1..5}.jpg

# 首次同步
[root@client ~ 13:55:47]# rsync -av Pictures root@server:
The authenticity of host 'server (10.1.8.10)' can't be established.
ECDSA key fingerprint is SHA256:5KkrnsINIP6anjuKXNFLmX2lITpgeyRlv6uEWtvRkgU.
ECDSA key fingerprint is MD5:70:76:1e:4b:c6:85:44:ff:03:02:07:29:53:d9:1a:f8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server,10.1.8.10' (ECDSA) to the list of known hosts.
root@server's password: 
sending incremental file list
Pictures/
Pictures/snap1.jpg
Pictures/snap2.jpg
Pictures/snap3.jpg
Pictures/snap4.jpg
Pictures/snap5.jpg

sent 351 bytes  received 115 bytes  71.69 bytes/sec
total size is 0  speedup is 0.00

# 再次同步
[root@client ~ 13:56:22]# rsync -av Pictures root@server:
root@server's password: 
sending incremental file list

sent 149 bytes  received 17 bytes  66.40 bytes/sec
total size is 0  speedup is 0.00

# 更新部分文件时间戳,再同步
[root@client ~ 13:56:40]# touch Pictures/snap1.jpg 
[root@client ~ 13:58:36]# rsync -av Pictures root@server:
root@server's password: 
sending incremental file list
Pictures/snap1.jpg

sent 199 bytes  received 36 bytes  94.00 bytes/sec
total size is 0  speedup is 0.00

# 删除 SRC 中文件,默认不会同步删除DEST中文件
[root@client ~ 13:58:39]# rm -f Pictures/snap5.jpg 
[root@client ~ 13:59:16]# rsync -av Pictures root@server:
root@server's password: 
sending incremental file list
Pictures/

sent 141 bytes  received 20 bytes  64.40 bytes/sec
total size is 0  speedup is 0.00
[root@client ~ 14:00:08]# ssh root@server "ls -l Pictures/"
root@server's password: 
总用量 0
-rw-r--r--. 1 root root 0 7月  27 13:58 snap1.jpg
-rw-r--r--. 1 root root 0 7月  27 13:55 snap2.jpg
-rw-r--r--. 1 root root 0 7月  27 13:55 snap3.jpg
-rw-r--r--. 1 root root 0 7月  27 13:55 snap4.jpg
-rw-r--r--. 1 root root 0 7月  27 13:55 snap5.jpg

# 使用选项 --delete 同步删除 DEST 中文件
[root@client ~ 14:00:14]# rsync -av Pictures root@server: --delete
root@server's password: 
sending incremental file list
deleting Pictures/snap5.jpg

sent 142 bytes  received 35 bytes  70.80 bytes/sec
total size is 0  speedup is 0.00
[root@client ~ 14:01:00]# ssh root@server "ls -l Pictures/"
root@server's password: 
总用量 0
-rw-r--r--. 1 root root 0 7月  27 13:58 snap1.jpg
-rw-r--r--. 1 root root 0 7月  27 13:55 snap2.jpg
-rw-r--r--. 1 root root 0 7月  27 13:55 snap3.jpg
-rw-r--r--. 1 root root 0 7月  27 13:55 snap4.jpg

将目标文件拉取到本地,操作过程一致。

同步注意事项:

  • 文件访问时间等属性、读写等权限、文件内容等有任何变动,都会被认为修改。
  • 目标目录下如果文件比源目录还新,则不会同步。
  • 源路径的最后是否有斜杠有不同的含义:
  • 有斜杠,只是复制目录中的文件。
  • 没有斜杠的话,不但要复制目录中的文件,还要复制目录本身。

2.2.2 daemon 模式

daemon 模式,不依赖 ssh 服务。

daemon 模式要求:服务端部署 rsynrc 服务。

  • 语法1:源路径或目的路径的主机名后面包含两个冒号。

  • 语法2:rsync://URL格式。

rsync [OPTION]… SRC [USER@]HOST::DEST
rsync [OPTION]… [USER@]HOST::SRC DEST
rsync [OPTION]… rsync://[USER@]HOST[:PORT]/SRC [DEST]

3. 项目实战:Rsync + Sersync 实现文件实时同步

服务端中数据发生变化,同步到backup端(备份服务器)。

3.1 补充:配置服务流程

  1. 安装软件包
  2. 准备相关材料,例如目录
  3. 配置服务器,例如修改配置文件
  4. 启用并启动服务
  5. 设置防火墙(关闭或者放行对应服务)
  6. 客户端测试

3.2 实验需求

实时监控 webapp.laoma.cloud主机中文件变化,并同步到 backup.laoma.cloud。

  • Sersync:负责监控数据目录变化,并调用rsync进行同步,部署在webapp端。
  • Rsync:提供备份服务,部署在backup端
10.1.8.10 webapp.laoma.cloud webapp
10.1.8.11 backup.laoma.cloud backup

# 设置backup服务器的/etc/hosts内容
[root@client ~ 08:35:27]# vim /etc/hosts

# 将文件推送给webapp服务器
[root@client ~ 08:35:31]# rsync -av /etc/hosts root@webapp:/etc/

# 设置webapp服务器
[root@server ~ 08:37:08]# hostnamectl set-hostname webapp.fengkai.cloud
[root@server ~ 08:39:02]# hostname
webapp.fengkai.cloud
[root@server ~ 08:39:05]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.8.10 webapp.fengkai.cloud webapp
10.1.8.11 backup.fengkai.cloud backup

# 设置backup服务器
[root@client ~ 08:36:48]# hostnamectl set-hostname backup.fengkai.cloud
[root@client ~ 08:38:39]# hostname
backup.fengkai.cloud
[root@client ~ 08:38:41]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.8.10 webapp.fengkai.cloud webapp
10.1.8.11 backup.fengkai.cloud backup

3.3 部署 Rsync 服务

3.3.1 安装软件包

[root@backup ~ 08:42:56]#  yum install -y rsync

[root@webapp ~ 08:43:18]# yum install -y rsync

3.3.2 配置 rsync

本次实验使用验证用户同步。

# 准备同步目录,该目录任何用户都可以读写。
[root@backup ~ 08:43:02]# mkdir -m 777 /backup

# 配置rsync,不验证用户,直接同步
[root@backup ~ 08:45:15]# vim /etc/rsyncd.conf
# 设置uid和gid,指定运行rsync服务的用户和组
uid=root
gid=root

......
# 添加如下配置
[backup]
# 描述信息
comment = backup

# 备份路径
path = /backup

# 设置可写
read only = no

# 指定用户名
auth users = rsync

# 指定用户密码文件
secrets file = /etc/rsyncd.secrets

更多 rsyncd.conf 配置参考 rsyncd.conf(5)

# 创建用户凭据文件
[root@backup ~ 10:58:30]# echo 'rsync:redhat' > /etc/rsyncd.secrets
[root@backup ~ 10:59:05]# chmod 400 /etc/rsyncd.secrets

# 启用并启动rsyncd服务
[root@backup ~ 08:49:34]# systemctl enable rsyncd --now
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.

# 配置防火墙
[root@backup ~ 08:50:01]# firewall-cmd --add-service=rsyncd 
success
[root@backup ~ 08:50:36]# firewall-cmd --add-service=rsyncd --permanent 
success

# 关闭 selinux
[root@backup ~ 08:50:44]# setenforce 0

3.3.3 客户端配置和测试

# 准备密码文件
[root@webapp ~ 10:56:58]# echo redhat > rsyncd.secrets
[root@webapp ~ 10:59:33]# cat rsyncd.secrets 
redhat
[root@webapp ~ 10:59:37]# chmod 400 rsyncd.secrets

# 传输测试
[root@webapp ~ 10:59:43]# rsync -av --password-file=./rsyncd.secrets /etc/hostname rsync@backup::backup
sending incremental file list
hostname

sent 114 bytes  received 35 bytes  99.33 bytes/sec
total size is 21  speedup is 0.14
# 最后一个backup,是代表服务端的[backup]备份块。

# 验证结果
[root@backup ~ 10:59:20]# ls /backup/
hostname

3.4 部署 Sersync 服务

3.4.1 Sersync 服务介绍

sersync 使用c++编写,类似于inotify,同样用于监控,但它克服了inotify的缺点。

inotify 最大的不足是会产生重复事件,或者同一个目录下多个文件的操作会产生多个事件,例如,当监控目录中有5个文件时,删除目录时会产生6个监控事件,从而导致重复调用rsync命令。比如:vim文件时,inotify会监控到临时文件的事件,但这些事件相对于rsync来说是不应该被监控的。

sersync 优点:

  • sersync 同步效率更高,它会对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,节省了运行时耗和网络资源。
  • sersync配置很简单,其中提供了静态编译好的二进制文件和xml配置文件,直接使用即可。
  • sersync使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。
  • sersync有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。
  • sersync自带crontab功能,只需在xml配置文件中开启,按要求隔一段时间整体同步一次。
  • sersync 可以二次开发。

sersync项目地址:https://code.google.com/archive/p/sersync/

sersync下载地址:https://code.google.com/archive/p/sersync/downloads

3.4.2 安装软件包

# 提前下载软件sersync2.5.4_64bit_binary_stable_final.tar.gz,并传入到webapp
[root@webapp ~ 11:37:38]# ll
总用量 77540
···
-rw-r--r--.  1 root root   727290 7月  28 11:37 sersync2.5.4_64bit_binary_stable_final.tar.gz
····

# 解压文件
[root@webapp ~ 11:38:45]# tar -xf sersync2.5.4_64bit_binary_stable_final.tar.gz 
[root@webapp ~ 11:39:27]# ls GNU-Linux-x86/
confxml.xml  sersync2

文件说明:

  • sersync2,是二进制程序。
  • confxml.xml,是sersync2程序的配置文件。

3.5 配置 Sersync

3.5.1 配置文件说明

[root@webapp ~ 11:39:32]# cat GNU-Linux-x86/confxml.xml 
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <!-- hostip与port是针对插件的保留字段,对于同步功能没有任何作用,保留默认即可。  -->
    <host hostip="localhost" port="8008"></host>

    <!-- 是否开启debug模式 -->
    <debug start="false"/>

    <!-- 如果是xfs文件系统,则需要设置为true才能同步,rehat/REEL/CentOS/Fedora新版本默认都是xfs文件系统,可使用df -Th命令查看 -->
    <fileSystem xfs="true"/>

    <!-- 过滤器,设置为true则会对里面的exclude对应的正则匹配到的文件进行过滤,即不同步 -->
    <filter start="true">
        <!-- <exclude expression="(.*)\.svn"></exclude> -->
        <!-- <exclude expression="(.*)\.gz"></exclude> -->
        <!-- <exclude expression="^info/*"></exclude> -->
        <!-- <exclude expression="^static/*"></exclude> -->
        <exclude expression="^cache/*"></exclude>
    </filter>

    <!-- inotify是linux的内核功能,这里用于设置创建/删除/修改/移动文件时,是否视为文件改变(进而进行同步) -->
    <inotify>
        <!-- 删除一个文件是否视为文件改变(很明显我们要设置为true) -->
        <delete start="false"/>
        <!-- 创建一个文件夹是否视为文件改变(很明显我们要设置为true) -->
        <createFolder start="true"/>
        <!-- 创建一个文件是否触发文件改变事件(这里要设置false,因为创建一个文件除了有createFile事件还会有closeWrite事件,我们只要把closeWrite事件设置为true即可监控到创建          一个文件) -->
        <createFile start="false"/>
        <!-- 创建文件或修改文件后再关闭会触发该事件,比如vim打开一个文件,修改后用(:wq)保存,则会触发该事件,当然创建新文件一样会触发 -->
        <closeWrite start="true"/>
        <!-- 从别的地方移到被监控目录是否视为文件改变,毫无疑问要设置为true -->
        <moveFrom start="true"/>
        <!-- 被监控目录中的某个文件被移动到其他地方算不算文件改变?毫无疑问要设置为true -->
        <moveTo start="true"/>
        <!-- 文件属性改变了,是否视为文件改变?这个我们可以认为文件没有改,所以设置false -->
        <attrib start="false"/>
        <!-- 文件内容被修改了是否视为文件改变?感觉文件改变肯定要设置为true,但其实不用,因为这个改变有可能是vim(:w)保存,还没有关闭文件,所以保存的时候没必要同步,而关闭的时候会触发closeWrite,所以修改的文件也是通过closeWrite来同步的 -->
        <modify start="false"/>
    </inotify>

    <!-- servsync的模块 -->
    <sersync>
        <!-- 指定要监控(即同步)的本地目录 -->
        <localpath watch="/data">
            <!-- ip指定同步到远程的哪个服务器,name填写远程服务器中rsync配置文件中的自定义模块名称(即中括号括起来的那个名称) -->
            <remote ip="10.1.8.10" name="laoma"/>
            <!-- 如果你要同步到多台服务器,继续填写即可,每个服务器一个remote标签 -->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>

        <!-- rsync模块配置 -->
        <rsync>
            <!-- 公共参数,即我们手动执行rsync的时候要带的选项就填在这里,servsync会自动组装 -->
            <commonParams params="-azP"/>
            <!-- 密码文件及指定用户名(用户名就是rsync服务器端配置文件中的"auth user =" 指定的用户名) -->
            <auth start="true" users="rsync" passwordfile="/etc/rsyncd.secrets"/>
            <!-- 如果你rsync服务器不是默认端口873,那么就要在这里指定具体的端口,当然是默认的你也可以指定一下 -->
            <userDefinedPort start="false" port="873"/>
            <!-- rsync超时时间 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <!-- 是否使用ssh方式传输 -->
            <ssh start="false"/>
        </rsync>
        <!-- 对于失败的传输,会进行重新传送,再次失败就会写入rsync_fail_log,然后每隔一段时间(timeToExecute进行设置,单位sec)执行该脚本再次重新传送,然后清空该脚本。可以          通过path来设置日志路径。 -->
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

        <!-- 定期整体同步功能,schedule表示crontab执行间隔,单位是min -->
        <crontab start="false" schedule="600"><!--600mins-->
            <!-- 同步过滤器,要开启请把start设置为true,用于 整体同步时,排除一些文件或目录,比如缓存目录可以不需要同步 -->
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <!-- 同步完成后,执行一个插件,name表示执行哪些插件,而这个插件必须在后边用plugin标签定义 -->
        <plugin start="false" name="command"/>
    </sersync>

    <!-- 定义一个command插件(command插件类型的一种,另外的类型有socket,refreshCDN,http(目前由于兼容性问题,http插件暂时不能用)) -->
    <plugin name="command">
        <!-- command插件其实就是“.sh”结尾的shell脚本文件,prefix和subffix用于拼成一条执行shell命令的命令 -->
        <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /data/wwwroot/mmm.sh suffix-->
        <!-- 该脚本做操作时要过滤的文件正则 -->
        <filter start="false">
            <include expression="(.*)\.php"/>
            <include expression="(.*)\.sh"/>
        </filter>
    </plugin>

    <!-- 定义一个socket插件,注意插件定义了但没有调用的话,是不会被执行的 -->
    <plugin name="socket">
        <localpath watch="/data">
            <deshost ip="192.168.138.20" port="8009"/>
        </localpath>
    </plugin>

    <!-- 定义一个refreshCDN插件,主要用于同步数据到cdn -->
    <plugin name="refreshCDN">
        <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
            <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
            <sendurl base="http://pic.xoyo.com/cms"/>
            <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
        </localpath>
    </plugin>
</head>

3.5.2 配置文件示例

本次实验使用的示例文件。

[root@webapp ~ 11:42:08]# vim confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>
    <fileSystem xfs="true"/>
    <filter start="true">
        <exclude expression="^cache/*"></exclude>
    </filter>
    <inotify>
        <delete start="true"/>
        <createFolder start="true"/>
        <createFile start="false"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="true"/>
        <modify start="true"/>
    </inotify>
    <sersync>
        <localpath watch="/app_data">
            <remote ip="10.1.8.11" name="backup"/>
        </localpath>
        <rsync>
            <commonParams params="-azP"/>
            <auth start="true" users="rsync" passwordfile="/root/rsyncd.secrets"/>
            <userDefinedPort start="false" port="873"/>
            <timeout start="false" time="100"/>
            <ssh start="false"/>
        </rsync>
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>
        <crontab start="false" schedule="600"><!--600mins-->
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <plugin start="false" name="command"/>
    </sersync>
    <plugin name="command">
        <param prefix="/bin/sh" suffix="" ignoreError="true"/>
        <filter start="false">
            <include expression="(.*)\.php"/>
            <include expression="(.*)\.sh"/>
        </filter>
    </plugin>
</head>

3.5.3 运行 Sersync

# 准备同步目录
[root@webapp ~ 11:43:04]# mkdir /app_data

# 复制程序到$PATH中
[root@webapp ~ 11:43:37]# cp GNU-Linux-x86/sersync2 /usr/local/bin/
[root@webapp ~ 11:43:57]# sersync2 -h
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
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
________________________________________________________________

# 运行 Sersync
[root@webapp ~ 11:44:09]# sersync2 -o ./confxml.xml -d
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: -o 	config xml name:  ./confxml.xml
option: -d 	run as a daemon
daemon thread num: 10
parse xml config file
host ip : localhost	host port: 8008
now the filter work ,if you set the crontab,you have to set crontab filter 
WARNING XFS FILE SYSTEM WORK
daemon start,sersync run behind the console 
use rsync password-file :
user is	rsync
passwordfile is 	/root/rsyncd.secrets
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
run the sersync: 
watch path is: /app_data

3.6 测试

# server 端监控目录 /backup
[root@backup ~ 11:45:48]# watch -n 1 tree /backup/

# 客户端创建文件和目录
[root@webapp ~ 11:44:54]# echo test > /app_data/test.txt
[root@webapp ~ 11:46:55]# mkdir /app_data/dbdata

# backup服务器文件
[root@backup ~ 11:48:37]# ls -ld /backup/*
drwxr-xr-x. 2 root root  6 7月  28 11:47 /backup/dbdata
-rw-r--r--. 1 root root 21 7月  28 08:39 /backup/hostname
-rw-r--r--. 1 root root  5 7月  28 11:46 /backup/test.txt

# 客户端删除文件
[root@webapp ~ 11:47:28]# rm -fr /app_data/*

# backup服务器文件
[root@backup ~ 11:48:39]# ls -ld /backup/*
-rw-r--r--. 1 root root 21 7月  28 08:39 /backup/hostname

3.7 配置 systemd 管理 Sersync

# 将之前同步的配置命令,配置成为服务
[root@webapp ~ 11:49:18]# cp confxml.xml /etc/sersyncd.conf
[root@webapp ~ 11:50:29]# vim /etc/systemd/system/sersyncd.service

[Unit]
Description=SerSync server daemon

[Service]
Type=forking
ExecStart=/usr/local/bin/sersync2 -o /etc/sersyncd.conf -d

[Install]
WantedBy=multi-user.target

:wq

# 设置服务开机自启动
[root@webapp ~ 11:51:34]# systemctl daemon-reload 
[root@webapp ~ 11:51:43]# systemctl enable sersyncd.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/sersyncd.service to /etc/systemd/system/sersyncd.service.

# 重启之后定义的sersyncd.service服务生效了
[root@webapp ~ 13:27:41]# reboot 
[fengkai@webapp ~ 13:28:34]$ su -l root
密码:
上一次登录:二 7月 28 08:43:18 CST 2026pts/0 上
[root@webapp ~ 13:28:40]# systemctl status sersyncd.service 
● sersyncd.service - SerSync server daemon
   Loaded: loaded (/etc/systemd/system/sersyncd.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2026-07-28 13:28:10 CST; 43s ago
  Process: 760 ExecStart=/usr/local/bin/sersync2 -o /etc/sersyncd.conf -d (code=exited, status=0/SUCCESS)
 Main PID: 782 (sersync2)
    Tasks: 12
   CGroup: /system.slice/sersyncd.service
           └─782 /usr/local/bin/sersync2 -o /etc/sersyncd.conf -d

7月 28 13:28:09 webapp.fengkai.cloud sersync2[760]: user is        rsync
7月 28 13:28:09 webapp.fengkai.cloud sersync2[760]: passwordfile is         /root/rsyncd....ts
7月 28 13:28:09 webapp.fengkai.cloud sersync2[760]: config xml parse success
7月 28 13:28:09 webapp.fengkai.cloud sersync2[760]: please set /etc/rsyncd.conf max conne...ly
7月 28 13:28:09 webapp.fengkai.cloud sersync2[760]: sersync working thread 12  = 1(primar...s)
7月 28 13:28:09 webapp.fengkai.cloud sersync2[760]: Max threads numbers is: 22 = 12(Threa...s)
7月 28 13:28:09 webapp.fengkai.cloud sersync2[760]: please according your cpu ,use -n pa…rate
7月 28 13:28:09 webapp.fengkai.cloud sersync2[760]: run the sersync:
7月 28 13:28:09 webapp.fengkai.cloud sersync2[760]: watch path is: /app_data
7月 28 13:28:10 webapp.fengkai.cloud systemd[1]: Started SerSync server daemon.
Hint: Some lines were ellipsized, use -l to show in full.
[root@webapp ~ 13:28:54]# touch /app_data/newfile

[root@backup ~ 13:29:27]# ls -ld /backup/*
-rw-r--r--. 1 root root  0 7月  28 13:27 /backup/f1
-rw-r--r--. 1 root root 21 7月  28 08:39 /backup/hostname
-rw-r--r--. 1 root root  0 7月  28 13:29 /backup/newfile

4.一句话总结

  • 极简版 Windows 与 Linux 可用 Xftp、lrzsz 互传文件,Linux 之间临时传输用基于 SSH 的 scp 全量拷贝,日常增量同步选用 rsync,搭配 sersync 监控文件改动即可实现业务目录向备份机的自动实时增量同步。

  • 原理完整版 Xftp/lrzsz 解决 Windows↔Linux 文件互传;scp 依托 SSH 做简单安全的全量文件复制;rsync 依靠比对文件差异实现高效增量传输,分 SSH 外壳模式与独立 daemon 服务模式;sersync 封装优化 inotify 事件监听、过滤无效文件变动,主动调用 rsync,最终搭建出源端监控变更 + 备份端 rsync 接收的稳定实时数据备份架构。

  • 运维实操版 跨系统文件传输:Windows 对接 Linux 优先 Xftp,命令行环境使用 lrzsz;服务器之间临时拷贝用 scp,定时 / 手动增量备份使用 rsync daemon 服务,生产环境实时热备份则通过webapp 部署 sersync 监听目录变化、backup 部署 rsync 接收数据的组合方案落地,还可配置 systemd 实现 sersync 开机自启保障同步常驻运行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值