首页 > 其他分享 >Rsync 备份服务搭建

Rsync 备份服务搭建

时间:2023-07-25 15:13:03浏览次数:28  
标签:Rsync 01 sersync -- 备份 rsync etc root 搭建

Rsync 备份服务搭建

目录

一. 前言

#01 为什么需要文件自动同步功能?
		我们平时上传代码,可以通过 ftp、sftp 等将文件上传至服务器,耗时耗力,而且很容易出错。如果服务器数量少还好,一但服务器数量增加,压力可想而知。
		这里使用  rsync+sersync 进行文件自动同步

二. rsync 和 sersync

  • rsync 文件传输
  • sersync 实时同步
  • 定义
#01 rsync 介绍
rsync 是 linux 下同步文件的一个高效算法,用于同步更新两处计算机的文件和目录,并适当利用查找文件中的不同块以减少数据传输。rsync 的主要特点就是增量传输,只对变更的部分进行传输。

#02 sersync 介绍
sersync 利用 inotify 与 rsync 对服务器进行实时同步,其中 inotify 用于监控文件系统事件,其优点是只对文件不同的部分进行操作,所以其优势大大超过使用挂接文件系统的方式进行镜像同步。


2.1 rsync 基本语法

#01 基本使用语法
    rsync 远程复制传输
    -v, --verbose 详细模式输出
    -q, --quiet 精简输出模式
    -a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
    -r, --recursive 对子目录以递归模式处理 
    -H, --hard-links 保留硬链结 
    --delete 删除那些DST中SRC没有的文件
    --partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
    -P 等同于 --partial
    --force 强制删除目录,即使不为空
    --progress 显示备份过程
    -z, --compress 对备份的文件在传输时进行压缩处理
    --config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
    --port=PORT 指定其他的rsync服务端口
    --progress 在传输时现实传输过程
    --password-file=filename  免交互式传输文件

经常用的参数:
		rsync -avz --delete  --progress  


2.2 本地文件传输

#和cp用法一样

#01 复制目录
[root@kq_mysql ~]# rsync -r  01 /tmp/

#02 复制文件
[root@kq_mysql ~]# rsync test.txt  /tmp/


2.3 ssh 远程文件传输

#01 基于ssh 文件传输
[root@kq_mysql ~]# rsync 1.sh 192.168.5.9:/opt/

	解释下:复制 1.sh文件到 192.168.5.9服务器 /opt/目录下
	
	
#02 复制目录到远程主机
[root@kq_mysql ~]#  rsync -r 01  192.168.5.9:/opt/

2.4 基于服务 远程主机文件传输

#01 从服务器同步文件到本地
[root@localhost ~]# rsync -avzP [email protected]::test /home/test        

#02 从服务器同步文件到本地
[root@localhost ~]# rsync -avz /home/test [email protected]::test      #从本地同步文件到服务器

#03 免交互式同步文件
[root@kq_mysql ~]# rsync -avzHP  --progress  --delete  /data/rsync/  [email protected]::book --password-file=/etc/passwd.txt
		
		解释下:
				-avzHP  
				--progress  										
				--delete		
         bookuser@											rsync用户名
         192.168.5.9										要传输到主机名称
         book														传输到那?对应的位置 模块名称
        

三. 部署 rsync

3.1 项目要求

  • 服务器 master01的/data/book/ 目录,实时同步 node01的 /data/rsync/目录
  • node01:/data/rsync --->> Macter01:/data/book/
服务器 ip 安装服务 角色
Master01 10.0.0.100 Rsync (修改配置文件 指定模块名称) 备份主服务器
Node01 10.0.0.101 Rsync Sersync 客户端

3.2 环境准备

#01 关闭防火墙 selinux
vi /etc/selinux/config #编辑防火墙配置文件
#SELINUX=enforcing #注释掉  
#SELINUXTYPE=targeted #注释掉 
SELINUX=disabled #增加  
:wq!  #保存,退出


#02 开放端口 (先不执行)
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="A服务器IP" port protocol="tcp" port="873" accept" #开放端口 
systemctl restart firewalld #重启防火墙
firewall-cmd --list-all #查看规则是否生效

3.3 搭建服务端 rsync

#01 安装 rsync
yum -y install rsync
systemctl start rsyncd.service #启动rsync服务
systemctl enable rsyncd.service #将rsync服务 加入开机自启


#02 检查服务
ps -ef | grep rsync

#03 修改配置文件(node01)
1)修改配置文件
[root@aliyun ~]# grep -E -v '^$|^#' /etc/rsyncd.conf
log file = /var/log/rsyncd.log
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
uid = root
gid = root
use chroot = no
max connections = 1200
timeout = 600
[book]
path = /data/book/
comment = book
port=873
read only = no
list = no
auth users = bookuser

2) 配置文件详解
vim /etc/rsyncd.conf
log file = /var/log/rsyncd.log 			#日志文件位置,启动rsync后自动产生这个文件,无需提前创建
#pid file = /var/run/rsyncd.pid #pid文件的存放位置
lock file = /var/run/rsync.lock 		#支持max connections参数的锁文件
secrets file = /etc/rsync.pass 			#用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
uid = root 													#设置rsync运行权限为root
gid = root 													#设置rsync运行权限为root
use chroot = no 										#默认为true,修改为no,增加对目录文件软连接的备份
max connections = 1200 							#最大连接数
timeout = 600 											#设置超时时间

[book]   								#自定义名称
path = /data/book/ 			#rsync服务端数据目录路径
comment = book  				#模块名称与[book]自定义名称相同
port=873  						  #默认端口
read only = no         	#设置rsync服务端文件为读写权限
list = no              	#不显示rsync服务端资源列表
auth users = bookuser  	#执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 10.0.0.0/24  #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.0.1 	 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开


#04 创建用户认证文件 并设置密码
echo "bookuser:123456" >/etc/rsync.pass
cat /etc/rsync.pass
bookuser:123456

#05 由于是密码 需要设置权限
chmod 600 /etc/rsyncd.conf  #设置文件所有者读取、写入权限
chmod 600 /etc/rsync.pass  #设置文件所有者读取、写入权限


#06 重启服务
systemctl restart rsyncd


3.3 搭建客户端 rsync

#01 安装rsync 
yum -y install rsync
systemctl start rsyncd.service #启动rsync服务
systemctl enable rsyncd.service #将rsync服务 加入开机自启

#02 创建密码文件
vi /etc/passwd.txt  #编辑文件,添加以下内容
123456 #密码 ,B服务器里设置的密码
:wq! #保存退出

chmod 600 /etc/passwd.txt #设置文件权限,只设置文件所有者具有读取、写入权限即可

#03 客户端运行测试
[root@kq_mysql rsync]# rsync -avzH  --progress --delete  /data/rsync/  [email protected]::book --password-file=/etc/passwd.txt

四.部署 sersync

  • d:启用守护进程模式
  • -r:在监控前,将监控目录与远程主机用rsync命令推送一遍
  • -o:指定配置文件,默认使用confxml.xml文件

4.1 环境准备

#01 修改 inotify 默认参数(inotify 默认内核参数值太小)
vi /etc/sysctl.conf #添加以下代码
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535

:wq! #保存退出

4.2 安装服务

#01 下载链接
cd /opt/
wget http://101.132.24.74/download/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz

#02 解压到指定位置
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz  #解压
mv GNU-Linux-x86  /usr/local/sersync  #移动目录到/usr/local/sersync

#03 备份配置文件
cd  /usr/local/sersync #进入sersync安装目录
cp confxml.xml confxml.xml-back  #备份原文件


4.3 修改配置文件

#01 配置文件
[root@kq_mysql sersync]# cat 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="false"/>
    <filter start="false">
	<exclude expression="(.*)\.svn"></exclude>
	<exclude expression="(.*)\.gz"></exclude>
	<exclude expression="^info/*"></exclude>
	<exclude expression="^static/*"></exclude>
    </filter>
    <inotify>
	<delete start="true"/>
	<createFolder start="true"/>
	<createFile start="false"/>
	<closeWrite start="true"/>
	<moveFrom start="true"/>
	<moveTo start="true"/>
	<attrib start="false"/>
	<modify start="false"/>
    </inotify>

    <sersync>
	<localpath watch="/data/rsync">
	    <remote ip="10.0.0.100" name="book"/>
	    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
	    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
	</localpath>
	<rsync>
	    <commonParams params="-artuz"/>
	    <auth start="true" users="bookuser" passwordfile="/etc/passwd.txt"/>
	    <userDefinedPort start="false" port="874"/><!-- port=874 -->
	    <timeout start="false" time="100"/><!-- timeout=100 -->
	    <ssh start="false"/>
	</rsync>
	<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
	<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"/>	<!--prefix /opt/tongbu/mmm.sh suffix-->
	<filter start="false">
	    <include expression="(.*)\.php"/>
	    <include expression="(.*)\.sh"/>
	</filter>
    </plugin>

    <plugin name="socket">
	<localpath watch="/opt/tongbu">
	    <deshost ip="192.168.138.20" port="8009"/>
	</localpath>
    </plugin>
    <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>
  • 参数解释
<localpath watch="/data/book">  				#源服务器同步目录
<remote ip="10.0.0.100" name="book"/>   #目标服务器ip,每行一个 
name="book":   													#目标服务器rsync同步目录模块名称
<auth start="true" users="bookuser" passwordfile="/etc/passwd.txt"/>
start="true"  #设置为true,每隔600分钟执行一次全盘同步
users="bookuser": #目标服务器rsync同步用户名
passwordfile="/etc/passwd.txt": #目标服务器rsync同步用户的密码在源服务器的存放路径
failLog path="/tmp/rsync_fail_log.sh"  #脚本运行失败日志记录

4.4 运行测试

#01 运行测试
[root@kq_mysql rsync]# /usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.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/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost	host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is	bookuser
passwordfile is 	/etc/passwd.txt
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 /data/rsync && rsync -artuz -R --delete ./ [email protected]::book --password-file=/etc/passwd.txt >/dev/null 2>&1
run the sersync:
watch path is: /data/rsync

#02 检查目录 发现同步成功
1)源目录
[root@kq_mysql rsync]# ll
总用量 4
drwxr-xr-x 3 root root 16 7月  25 12:30 01
-rw-r--r-- 1 root root  2 7月  25 14:07 eee.txt

2)同步到目录
[root@centos_4c8g book]# ll
总用量 4
drwxr-xr-x 3 root root 16 7月  25 12:30 01
-rw-r--r-- 1 root root  2 7月  25 14:07 eee.txt


## 此客户端 10.0.0.101 node01 目录 /data/rsync 自动向 服务端 msater01 10.0.0.100 book模块自动同步
10.0.0.101 node01 -->>输入--->>  msater01 10.0.0.100 book




#命令参数解释: /usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml
    -d:启用守护进程模式
    -r:在监控前,将监控目录与远程主机用rsync命令推送一遍
    -o:指定配置文件,默认使用confxml.xml文件


4.5 重启服务

#01 如果修改了配置文件 则需要重启服务
1)关闭进程
killall sersync2 #杀掉全部近程

2)启动服务
/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml



4.6 开机自启

#01 方式一
vi /etc/rc.d/rc.local  #编辑,在最后添加一行
/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml  #设置开机自动运行脚本

:wq!  #保存退出

#02 方式2
vim  /etc/init.d/sercync.sh
#!/bin/bash
# description: zhangyuzhou
/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml 


#03 授权并加入系统服务中
chmod  +x  sersync.sh
chkconfig --add  sersync.sh
chkconfig  sersync.sh					#开机自启动
	
	解释下:
			chkconfig 命令用于检查,设置系统的各种服务
			--add			添加到系统服务


###删除系统服务 如果不想使用了 可执行该文件
chkconfig --del sersync.sh

标签:Rsync,01,sersync,--,备份,rsync,etc,root,搭建
From: https://www.cnblogs.com/saas-open/p/17579898.html

相关文章

  • 使用sql脚本建立sql server备份作业
    使用SQL脚本建立SQLServer备份作业在SQLServer中,备份数据是一项非常重要的任务。为了简化备份过程并确保数据的安全性,可以使用SQLServer的作业来自动执行备份任务。本文将介绍如何使用SQL脚本建立SQLServer备份作业。创建备份目录首先,我们需要创建一个用于存储备份文件的目......
  • 直播平台搭建源码,Fragment 显示 隐藏 监听
    直播平台搭建源码,Fragment显示隐藏监听 @OverridepublicvoidonHiddenChanged(booleanhidden){super.onHiddenChanged(hidden);if(!hidden){updateUserinfo();}}@OverridepublicvoidsetUserVisibleHint(booleanisVisibleToUser){super.setUserVisibleHint(isVisibl......
  • JavaWeb--环境搭建(idea,tomcat)到跑测试中我犯下的滔天大罪
    1.在网上copy时路径没有写对点击查看代码<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation......
  • 搭建k8s集群
    一、k8smaster部署Master节点上会运行的组件:etcd,kube-apiserver,kube-controller-manager,kuctl,kubeadm.kubelet,kube-proxy,flannel,docker Kubeadm,官方k8s一键部署工具Flannel,网络插件,确保节点间能够互相通信 环境初始化: 1)hosts解析cat>>/etc/hosts <<EOF0.......
  • PowerShell实现Win11环境变量追加及备份
    最近重装了系统,之前配置的环境又得重新配置,感觉很繁琐所以查阅资料实现了用脚本配置环境变量,这样只要保留原先的JDK等环境文件夹就可以一键配置了。一开始我准备采用比较熟悉的bat脚本来实现setxPATH"%PATH%;%myPath%"/m但setx会覆盖原有path!!,故最后使用PowerShell实现,脚......
  • hadoop-eclipse开发环境搭建及error: failure to login错误
    对于Hadoop开发者来讲,通过JAVAAPI编程是进入Map-Reduce分布式开发的第一步。由于Eclipse本身并没有提供对MapReduce编程模式的支持,所以需要一些简单的步骤来实现。1.安装Hadoop。本文的Hadoop是部署在虚拟机上的伪分布模式。相关软件环境如下:JDK:sunjdk1.6.0_30Hadoop:hadoop-0......
  • Tool-Gitlab-备份恢复-迁移
    Tool-Gitlab-备份恢复-迁移备份sudogitlab-rakegitlab:backup:create使用命令会在/var/opt/gitlab/backups目录下创建一个压缩包,这个压缩包就是Gitlab整个的完整部分。需要在gitlab运行时操作。gitlab.rb和gitlab-secrets.json两个文件包含敏感信息。未被备份到备份文......
  • OLAP系列之分析型数据库clickhouse备份方式(五)
    一、常见备份方式1.1备份方式备份方式特点物理文件备份对物理文件进行拷贝,备份期间禁止数据写入dump数据导入导出备份方式灵活,但备份速度慢快照表备份制作_bak表进行备份FREEZE备份表(分区表、非分区表)分区备份,可通过attach进行装载恢复FETCH备份......
  • mysql在liunx下面的自动备份
    由于系统基本开发完成,为了保证数据安全,需要将数据库自动备份,以下是实现自动备份的步骤:一、在本地自动备份数据库1.使用mysql自带的服务mysqldump实现自动备份,首先使用vim命令编辑以下命令并且保存#!/bin/bashDB_USER='***'DB_PASSWORD='*******'DB_NAME='**......
  • 3.1 开发环境搭建
    一、Ubuntu和Windows文件互传①开启Ubuntu的FTP服务:下载vsftpd:sudoapt-getinstallvsftpd;打开vsftpd.conf:sudonvim/etc/vsftpd.conf;确保这两行代码没有被注释:之后重启FTP服务:②Windows下载FTP客户端:客户端-FileZilla中文网③FileZilla软件设置:  Ubuntu作为FT......