一、前言

本文主要介绍部署Sersync服务并实现实时备份NFS。涉及Rsync服务端、Rsync客户端及NFS客户端共三台机器,具体如下:

主机 角色
harbor01 Rsync服务端
nfs01 Rsync客户端/NFS服务端
gitlab-01 NFS客户端

二、部署Rsync服务

2.1 服务端

1、检测是否安装

[root@harbor01 ~]# rpm -qa | grep rsync
rsync-3.1.2-12.el7_9.x86_64

如果没有,则通过以下命令进行下载

[root@harbor01 ~]# yum install -y rsync

2、编辑/etc/rsyncd.conf配置文件

[root@harbor01 ~]# cat /etc/rsyncd.conf
fake super =yes
uid = rsync
gid = rsync
use chroot = no
max connections = 2000
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
#hosts allow = 192.168.1.0/24
#hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
######################################
[backup]
comment = zq created
path = /backup
######################################
[nfsbackup]
comment = zq created
path = /nfsbackup/

3、检查

检查进程

[root@harbor01 ~]# ps -ef |grep rsync
root       5667      1  0 20:46 ?        00:00:00 /usr/bin/rsync --daemon --no-detach
root       6160   3233  0 20:47 pts/0    00:00:00 grep --color=auto rsync

检查端口

[root@harbor01 ~]# ss -lntup |grep rsync
tcp    LISTEN     0      5         *:873                   *:*                   users:(("rsync",pid=5667,fd=3))
tcp    LISTEN     0      5      [::]:873                [::]:*                   users:(("rsync",pid=5667,fd=5))

4、添加虚拟用户

[root@harbor01 ~]# useradd -s /sbin/nologin -M  rsync

如果执行不了,可以执行

[root@harbor01 ~]# sudo useradd -s /sbin/nologin -M -r rsync

5、创建密码文件

[root@harbor01 ~]# echo 'rsync_backup:111' >/etc/rsync.password
[root@harbor01 ~]# chmod 600 /etc/rsync.password

6、创建共享目录

[root@harbor01 ~]# mkdir /nfsbackup/
[root@harbor01 ~]# chown -R rsync.rsync /nfsbackup/

7、启动服务

[root@harbor01 ~]# systemctl start rsyncd  && systemctl enable rsyncd  

8、本地测试

[root@harbor01 ~]# rsync -av /etc/hostname rsync_backup@192.168.1.67::nfsbackup
Password:111

[root@harbor01 ~]# ll /nfsbackup/
total 4
-rw-r--r-- 1 rsync rsync 9 Nov 16 18:12 hostname

2.2 客户端

1、检测是否安装

[root@nfs01 ~]# rpm -qa | grep rsync
rsync-3.1.2-12.el7_9.x86_64

如果没有,则通过以下命令进行下载

[root@nfs01 ~]# yum install -y rsync

2、客户端测试

[root@nfs01 ~]# rsync -av /etc/hosts rsync_backup@192.168.1.67::nfsbackup
Password:111

在服务端进行查看

[root@harbor01 ~]# ll /backup/
total 8
-rw-r--r-- 1 rsync rsync   6 Nov 17 19:02 hostname
-rw-r--r-- 1 rsync rsync 158 Jun  7  2013 hosts

当然也可以使用免密登录测试

#客户端操作
[root@nfs01 ~]# echo '111' >/etc/rsync.client
[root@nfs01 ~]# chmod 600 /etc/rsync.client
[root@nfs01 ~]# rsync -avz /etc/passwd rsync_backup@192.168.1.67::nfsbackup --password-file=/etc/rsync.client
sending incremental file list
passwd

sent 621 bytes  received 43 bytes  1,328.00 bytes/sec
total size is 1,244  speedup is 1.87

#服务端验证
[root@harbor01 ~]# ll /backup/
total 12
-rw-r--r-- 1 rsync rsync    6 Nov 17 19:02 hostname
-rw-r--r-- 1 rsync rsync  158 Jun  7  2013 hosts
-rw-r--r-- 1 rsync rsync 1244 Dec  3 20:58 passwd

三、部署NFS服务

3.1 服务端

1、在nfs01主机上安装rpcbind nfs-utils

[root@nfs01 ~]# yum -y install rpcbind nfs-utils
[root@gitlab-01 ~]#  yum -y install rpcbind nfs-utils

2、在nfs01主机上启动服务

[root@nfs01 ~]# systemctl enable rpcbind && systemctl start rpcbind
[root@nfs01 ~]# systemctl enable nfs-server && systemctl start nfs-server

3、在nfs01主机上创建共享目录/data/,作为客户端挂载的远端入口,然后设置权限。

[root@nfs01 ~]# mkdir -p /data/
[root@nfs01 ~]# chmod 666 /data/

4、在nfs01主机上编辑/etc/exports文件

[root@nfs01 ~]# vim /etc/exports
/data/ 192.168.1.0/24(rw,sync,insecure,no_subtree_check,no_root_squash)

# 配置生效
[root@nfs01 ~]#  exportfs -r

# 重新加载NFS
[root@nfs01 ~]#  systemctl reload nfs-server

3.2 客户端

1、在gitlab-01主机上安装rpcbind nfs-utils

[root@nfs01 ~]# yum -y install rpcbind nfs-utils
[root@gitlab-01 ~]#  yum -y install rpcbind nfs-utils

2、在gitlab-01主机上进行验证

[root@gitlab-01 ~]# showmount -e 192.168.1.66
Export list for 192.168.1.66:
/data                       192.168.1.0/24

3、在gitlab-01主机上创建挂载目录/upload/并进行挂载

[root@gitlab-01 ~]# mkdir /upload/
[root@gitlab-01 ~]# mount -t nfs 192.168.1.66:/data/ /upload/

验证

[root@gitlab-01 ~]# df -h /upload/
Filesystem          Size  Used Avail Use% Mounted on
192.168.1.66:/data   48G  3.0G   45G   7% /upload

四、部署Sersync服务

1、在nfs01主机上下载sersync包

[root@nfs01 ~]# wget https://github.com/wsgzao/sersync/blob/master/sersync2.5.4_64bit_binary_stable_final.tar.gz

如果下载的文件不完整,点击https://github.com/wsgzao/sersync进行文件下载即可

2、在nfs01主机上进行目录规划

#新建目录
[root@nfs01 ~]# mkdir -p /app/tools/sersync/{bin,conf}

#解压
[root@nfs01 ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz

#移动文件
[root@nfs01 ~]# mv GNU-Linux-x86/sersync2 /app/tools/sersync/bin/
[root@nfs01 ~]# mv GNU-Linux-x86/confxml.xml /app/tools/sersync/conf/

#验证查看
[root@nfs01 ~]# tree -F /app/tools/
/app/tools/
└── sersync/
    ├── bin/
       └── sersync2*
    └── conf/
        └── confxml.xml*

3、在nfs01主机上修改confxml.xml文件

[root@nfs01 ~]# vim /app/tools/sersync/conf/confxml.xml
#修改第24行中共享目录为/data/
#修改第25行中remote ip为rsync服务端IP及模板名字为nfsbackup
#修改第30行中rsync选项为az
#修改第31行中开启auth start免密码模式、指定用户为rsync_backup,免密文件为/etc/rsync.client
#修改第36行中failLog path路径为/var/log/rsync_fail.log
...
...
 23     <sersync>
 24         <localpath watch="/data/">
 25             <remote ip="192.168.1.67" name="nfsbackup"/>
 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="-az"/>
 31             <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.client"/>
 32             <userDefinedPort start="false" port="874"/><!-- port=874 -->
 33             <timeout start="false" time="100"/><!-- timeout=100 -->
 34             <ssh start="false"/>
 35         </rsync>
 36         <failLog path="/var/log/rsync_fail.log" timeToExecute="60"/><!--default every 60mins execute once-->
 37         <crontab start="false" schedule="600"><!--600mins-->
 38             <crontabfilter start="false">
 39                 <exclude expression="*.php"></exclude>
 40                 <exclude expression="info/*"></exclude>
 41             </crontabfilter>
 42         </crontab>
 43         <plugin start="false" name="command"/>
 44     </sersync>
...
...

4、在nfs01主机上启动Sersync服务

#创建软链接
[root@nfs01 ~]# ln -s /app/tools/sersync/bin/sersync2 /bin/

#启动
[root@nfs01 ~]# sersync2 -rdo /app/tools/sersync/conf/confxml.xml

#验证
[root@nfs01 ~]# ps -ef | grep sersync
root       3608      1  0 16:54 ?        00:00:00 sersync2 -rdo /app/tools/sersync/conf/confxml.xml
root       3624   1791  0 16:54 pts/0    00:00:00 grep --color=auto sersync

注意:一个.xml文件对应一个Sersync服务,不能同一个.xml文件起多个进程。如果需要多个共享目录,则需要配置不同的.xml文件

五、全网实时备份

5.1 联调测试

1、在gitlab-01主机上创建文件

[root@gitlab-01 ~]# touch /upload/{1..10}.txt

在nfs01主机上进行验证查看

[root@nfs01 ~]# ll /data/
total 0
-rw-r--r-- 1 root root 0 Dec  4 17:15 10.txt
-rw-r--r-- 1 root root 0 Dec  4 17:15 1.txt
-rw-r--r-- 1 root root 0 Dec  4 17:15 2.txt
-rw-r--r-- 1 root root 0 Dec  4 17:15 3.txt
-rw-r--r-- 1 root root 0 Dec  4 17:15 4.txt
-rw-r--r-- 1 root root 0 Dec  4 17:15 5.txt
-rw-r--r-- 1 root root 0 Dec  4 17:15 6.txt
-rw-r--r-- 1 root root 0 Dec  4 17:15 7.txt
-rw-r--r-- 1 root root 0 Dec  4 17:15 8.txt
-rw-r--r-- 1 root root 0 Dec  4 17:15 9.txt

在harbor01主机(rsync服务端)上进行验证查看

[root@harbor01 ~]# ll /nfsbackup/
total 0
-rw-r--r-- 1 rsync rsync 0 Dec  4 17:15 10.txt
-rw-r--r-- 1 rsync rsync 0 Dec  4 17:15 1.txt
-rw-r--r-- 1 rsync rsync 0 Dec  4 17:15 2.txt
-rw-r--r-- 1 rsync rsync 0 Dec  4 17:15 3.txt
-rw-r--r-- 1 rsync rsync 0 Dec  4 17:15 4.txt
-rw-r--r-- 1 rsync rsync 0 Dec  4 17:15 5.txt
-rw-r--r-- 1 rsync rsync 0 Dec  4 17:15 6.txt
-rw-r--r-- 1 rsync rsync 0 Dec  4 17:15 7.txt
-rw-r--r-- 1 rsync rsync 0 Dec  4 17:15 8.txt
-rw-r--r-- 1 rsync rsync 0 Dec  4 17:15 9.txt

2、在gitlab-01主机上删除文件

[root@gitlab-01 ~]# rm -f  /upload/*

在nfs01主机上进行验证查看

[root@nfs01 ~]# ll /data/
total 0

在harbor01主机(rsync服务端)上进行验证查看

[root@harbor01 ~]# ll /nfsbackup/
total 0