[IT Operation and Maintenance] Rsync realizes Linux dual-machine hot backup

An overview of Rsync

1.1. What is Rsync?

rsync is an open source, fast, multifunctional and excellent tool that can realize incremental local or remote data mirroring synchronization backup. Available on multiple platforms. As can be seen from the name of the software, it means remote sync (remote sync). It can realize full backup and incremental backup, so it is very suitable for applications such as centralized backup or off-site backup.

Official website: http://rsync.samba.org/

Port: 873

Operating mode: C/SB/S

1.2. rsync features

1. Support copying special files such as link files, devices, etc.

2. You can have the function of excluding specified files or directories from synchronization, which is equivalent to the exclusion function of tar.

3. All attributes such as permissions, time, soft and hard links, owners, groups, etc. of the original file or directory can be kept unchanged.

4. Incremental synchronization can be achieved, that is, only changed data is synchronized, so the data transmission efficiency is very high.

5. You can use rcp, rsh, ssh and other methods to transfer files (rsync itself does not encrypt data)

6. Files and data (server and client) can be transferred through socket (process mode).

7. Supports anonymous or authenticated (no system user required) process mode transmission, enabling convenient and safe data backup and mirroring

1.3. Transmission method

Push replication (upload): All master servers push local data to slave servers

image20200116202157436.png

Pull replication (download): The rsync backup server regularly pulls data from all hosts

image20200116202338681.png

In daily use, these two forms are mixed, such as:

Mass data backup
image20200116202528449.png

Offsite backup
image20200116202556118.png

Second rsync pull copy

environment:

  • Two hosts, IP addresses are 192.168.11.16, 192.168.11.100
  • The operating system is CentOS8
  • Turn off selinux and firewall

Requirement: It is hoped that the data in the /cache directory of the host 192.168.11.100 will be consistent with the data in the /cache directory of the host 192.168.11.16.

step1 Create rsync service on the host 192.168.11.16

[root@master ~]# rpm -qa | grep rsync
rsync-3.1.3-4.el8.x86_64
#检查是或否安装的相关软件包
[root@master ~]# mkdir /etc/rsyncd
[root@master ~]# vim rsyncd.conf
uid=root			#定义以哪个用户的身份启动进程	
gid=root			#定义以哪个组的身份启动进程
port=873			#此服务默认端口873
max connections=0		#最大连接数(正整数),0代表不限制。
log file=/var/log/rsyncd.log	#定义日志文件位置
pid file=/var/run/rsyncd.pid	#定义pid文件位置
lock file=/var/run/rsyncd.lock	#定义锁定文件位置,避免多开
motd file=/etc/rsyncd/rsyncd.motd	#定义欢迎信息
read only=yes		#权限为只读
hosts allow=192.168.11.0/24		#允许的网段
hosts deny=*						#拒绝所有,允许个别
[www]	#定义共享名称为www
path=/cache				#路径
list=yes					#允许别人看到
ignore errors			#忽略错误
auth users=hello		#授权的账号
secrets file=/etc/rsyncd/rsyncd.secrets	#密码文件

setp2 creates a welcome information file with arbitrary content

[root@master ~]# vim /etc/rsyncd/rsyncd.motd

step3 Create password file

[root@master ~]# vim /etc/rsyncd/rsyncd.secrets
hello:123456
[root@master ~]# chmod 600 rsyncd.secrets

step4 Create cache directories and files

[root@master ~]# mkdir /cache
[root@master ~]# touch /cache/file{1..10}

step5 start the service

[root@master ~]# rsync	--daemon		--config=/etc/rsyncd/rsyncd.conf
[root@master ~]# lsof	-i	:873

step6 Create a password file on 192.168.11.100 and modify the permissions

[root@slave ~]# vim /etc/rsync.pw
123456
[root@slave ~]# chmod 600 /etc/rsync.pw
[root@slave ~]#	mkdir /cache

step7 synchronous test

[root@slave ~]#	rsync -avzP	--delete --password-file=/etc/rsync.pw [email protected]::www /cache 

rsync参数
-a	归档模式传输,相当于-rlptgoD一起使用
-v	详细模式输出
-z	传输时进行压缩以提高效率
-r	递归传输目录及子目录,即目录下得所有目录都同样传输
-t	保持文件时间信息
-o	保持文件属主信息
-p	保持文件权限
-g	保持文件属组信息
-l 	保留软连接
-P	显示同步的过程及传输时的进度等信息
-D	保持设备文件信息
-L	保留软连接指向的目标文件
--exclude=PATTERN	指定排除不需要传输的文件模式
--bwlimit=1m	限速传输
--delete	让目标目录和源目录数据保持一致
--password-file	指定密码文件位置

step8 Define scheduled tasks

[root@slave ~]#	crontab -e
* * * * * rsync -avzP	--delete --password-file=/etc/rsync.pw hello@192.168.11.16::www /cache

step9 Adjust the files on the 11.16 host and observe whether there are changes

Three Rsync Push Copy

environment:

  • Two hosts, IP addresses are 192.168.11.16, 192.168.11.100
  • The operating system is CentOS8
  • Turn off selinux and firewall

Requirement: It is hoped that the data in the /cache directory of the host 192.168.11.100 will be consistent with the data in the /cache directory of the host 192.168.11.16.

Note: Stop the rsync service on the 192.168.11.16 host.
Step 1 Create an rsync service on the 192.168.11.100 host.

[root@slave ~]# rpm -qa | grep rsync
rsync-3.1.3-4.el8.x86_64
#检查是或否安装的相关软件包
[root@slave ~]# mkdir /etc/rsyncd
[root@slave ~]# vim rsyncd.conf
uid=root			#定义以哪个用户的身份启动进程	
gid=root			#定义以哪个组的身份启动进程
port=873			#此服务默认端口873
max connections=0		#最大连接数(正整数),0代表不限制。
log file=/var/log/rsyncd.log	#定义日志文件位置
pid file=/var/run/rsyncd.pid	#定义pid文件位置
lock file=/var/run/rsyncd.lock	#定义锁定文件位置,避免多开
motd file=/etc/rsyncd/rsyncd.motd	#定义欢迎信息
read only=no		#权限为不只读(可写)
hosts allow=192.168.11.0/24		#允许的网段
hosts deny=*						#拒绝所有,允许个别
[www]	#定义共享名称为www
path=/cache				#路径
list=yes					#允许别人看到
ignore errors			#忽略错误
auth users=hello		#授权的账号
secrets file=/etc/rsyncd/rsyncd.secrets	#密码文件

setp2 creates a welcome information file with arbitrary content

[root@slave ~]# vim /etc/rsyncd/rsyncd.motd

step3 Create password file

[root@slave ~]# vim /etc/rsyncd/rsyncd.secrets
hello:123456
[root@slave ~]# chmod 600 rsyncd.secrets

step4 Create cache directory

[root@slave ~]# mkdir /cache

step5 start the service

[root@slave ~]# rsync	--daemon		--config=/etc/rsyncd/rsyncd.conf
[root@slave ~]# lsof	-i	:873

step6 Install monitoring software on 192.168.11.16

[root@master ~]# tar fx sersync2.5_32bit_binary_stable_final.tar.gz -C /usr/src/
[root@master ~]# cd /usr/src/GNU-Linux-x86/

step7 Configure monitoring software

[root@master ~]# vim confxml.xml 
<sersync>
        <localpath watch="/cache">
            <remote ip="192.168.11.100" name="www"/>
						.
						.
						.
					<rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="hello" passwordfile="/etc/rsync.pw"/>

[root@master ~]# vim /etc/rsync.pw
123456
[root@master ~]# chmod 600 rsync.pw 

step8 Start monitoring software

[root@master ~]# ./sersync2	-r	#第一次启动加-r 可以查看到工作流程

step9 Re-open a terminal, create and delete files on 192.168.11.16, and check whether the synchronization is successful on 192.168.11.100

Guess you like

Origin blog.csdn.net/qq_45277554/article/details/131201228