rsync service and deployment

1 rsync Introduction

1.1 What is rsync

rsync: - A the FAST, Versatile, Remote (and local) File-COPYING Tool
rsync: is a fast, versatile, remote and local file copy tool
rsync --version: View the software version of rsync
backup service importance: the enterprise All data files to be manipulated, certainly before the backup operation.  
Enterprise data backup method: critical data needs to be saved on a dedicated backup server third-party network disk storage.

1.2 rsync Features

You can achieve full volume and incremental backups

1.2.1 schematic total and incremental

Total amount: of all the data from the host to the host A B
increment: A new host transmits the data to the host B
The full amount of incremental schematic _ Figure Wang .png

Principle 1.2.2 Incremental backups

A mode: the data source and data destination directory attribute information for comparison

When synchronizing the backup data, by default, Rsync through its unique "quick check" algorithm, it is only the synchronization size or last modified the file or directory time changes, of course, also be based on permissions, change of the main attributes of synchronization, but you need to specify the appropriate parameters can be achieved even synchronize only part of the contents of a file has changed, so fast synchronization of backup data can be achieved.

Second way: using the file, whether the file fingerprint same
md5 operation generates numerical information (fingerprint information) Verify that the file checksum changes
image.png
md5 sends the generated value to the target host, target host md5 verify whether or not the value of the received document is generated with the native the same md5, how different. The transmission.

1.3 rsync Software Features

That is also the rsync software command.

Similar to the cp command  For local data backup transmission
Similar to scp command  Remote backup data transmission
Similar to the rm command No difference to achieve synchronization backup
Similar to the ls command Local File View

1.3.1 rsync=cp

备份文件:rsync /etc/hosts /tmp
备份目录:rsync -a /aaa  /tmp

1.3.2 rsync=scp

scp: Remote Data transfer

传输文件:scp /etc/hosts [email protected]:/tmp
传输目录:scp -rp /aaa [email protected]:/tmp

rsync

备份文件:rsync  /etc/hosts [email protected]:/tmp
备份目录:rsync -rp /aaa [email protected]:/tmp

利用rsync传输目录数据  
目录数据后面有/  /aaa/   将目录下面的数据内容进行传输
目录数据后面没/  /aaa    将目录本身和目录下面的数据内容进行传输

1.3.3 rsync=rm

快速清空数据

mkdir null      #创建一个空目录
rsync -avz --delete /null/ /test        

注释:--delete 无差异同步参数  将两个目录中的数据保持高度一致。
可以理解为我(/null)有的东西,你也有。我(/null)没有的东西,你也不能有。null是个空目录,--delete保证了无差异同步,所以test目录的内容被清空。

1.3.4 rsync=ls

查看文件:rsync /etc/hosts
查看目录:rsync /test

1.4 rsync的7个特性总结

(1)支持拷贝普通文件与特殊文件如链接文件,设备等。
(2)可以有排除指定文件或目录同步的功能,相当于打包命令tar的排除功能。
  #tar zcvf backup_1.tar.gz  /opt/data  -exclude=clsn  
        说明:在打包/opt/data时就排除了clsn命名的目录和文件。
(3)可以做到保持原文件或目录的权限、时间、软硬链接、属主、组等所有属性均不改变-p。
(4)可实现增量同步,既只同步发生变化的数据,因此数据传输效率很高(tar -N)。
    # 将备份/home 目录自 2008-01-29 以来修改过的文件
    # tar -N 2008-01-29 -zcvf /backups/inc-backup_$(date +%F).tar.gz /home
    # 将备份 /home 目录昨天以来修改过的文件
    # tar -N $(date -d yesterday "+%F") -zcvf /backups/inc-backup_$(date +%F).tar.gz /home
    # 添加文件到已经打包的文件
    # tar -rf all.tar .gif
    说明:这条命令是将所有.gif的文件增加到all.tar的包里面去。-r是表示增加文件的意思。
(5)可以使用rcp,rsh,ssh等方式来配合进行隧道加密传输文件(rsync本身不对数据加密)
(6)可以通过socket(进程方式)传输文件和数据(服务端和客户端)
****。重点掌握
(7)支持匿名的或认证(无需系统用户)的进程模式传输,可实现方便安全的进行数据备份及镜像。

2 rsync命令使用方法

2.1 rsync的工作方式

2.1.1 本地数据备份(同步)方式

本地复制备份文件 == cp
Local:  rsync [OPTION...] SRC... [DEST]
SRC:要备份的数据
Dest:要备份到什么路径中
语法:rsync /etc/hosts /tmp

2.1.2 远程数据备份(同步)方式

 

Access via remote shell:远程数据备份文件
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]====远程主机数据拉取到本地主机
Pull:           拉取
[USER@]:            指定以什么用户身份登录到远程主机(默认是当前系统用户)
HOST:           远程主机IP地址或者主机名称
SRC:            远程要拉取的数据信息
DEST:           保存到本地路径信息
语法:rsync [email protected]:/tmp/test.txt /aaa  #将10.0.0.41主机的test.txt文件拉取到本地主机

 

Push: rsync [OPTION...] SRC... [USER@]HOST:DEST====本地主机数据推送到远端主机
[USER@]:            指定以什么用户身份登录到远程主机(默认是当前系统用户)
Push:           推送
HOST:           远程主机IP地址或者主机名称
SRC:            本地要推送的数据信息
DEST:           保存到远程路径信息
语法: rsync  /etc/hosts [email protected]:/tmp #将hosts文件推送给10.0.0.41主机/tmp目录       

 
QQ picture 20190713120902.png

2.1.3 守护进程方式备份(同步)数据

Access via rsync daemon====利用守护进程方式备份数据
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
            rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: 客户端验证rsync方式
(1)rsync [OPTION...] SRC... [USER@]HOST::DEST
            src:    本地要推送的数据信息
            [USER@]  认证用户名称信息
            HOST:   备份服务器IP地址或者主机名称,host后面需要有两个冒号
            DEST:   指定模块信息的名字backup
      语法:rsync -avz /etc/hosts [email protected]::backup
(2)rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST     #不常用,理解即可
  PORT:端口是之前默认的可以不写,但如果改变要加上端口
  DEST:备份的路径     

守护进程方式作用:
(1)控制备份传输数据连接数
(2)可以实现免交互传输数据
(3)可以实现认证访问功能
(4)可以实现安全访问控制

2.1.4 守护进程方式备份数据原理

QQ picture 20190713152331.jpg
文字说明:
①客户端执行命令进行传输数据,服务端要求验证用户和密码。
②服务端验证通过后,客户端开始进行传输数据。
③服务端收到数据,改变数据的身份为rsync身份。

3 rsync服务部署

3.1 服务端部署

3.1.1 安装rsync 

yum install -y rsync        #安装rsync软件
rpm -qa rsync               #查看rsync软件版本
rsync-3.1.2-4.el7.x86_64    #3.1.2版本

3.1.2 编辑配置文件

vi /etc/rsyncd.conf

#rsync_config
#created by HQ at 2017
##rsyncd.conf start##

uid = rsync                                         #虚拟用户rsync,管理备份目录
gid = rsync                                         #虚拟用户组rsync,管理备份目录
port = 873                                          #服务默认端口号873
fake super = yes                                #伪装管理员功能开启  便于可以执行chgrp命令
use chroot = no                                 #安全配置
max connections = 200                       #最大连接数  同时支持200个客户端连接
timeout = 300                                       #连接超时时间 单位:秒
pid file = /var/run/rsyncd.pid  #进程信息文件,便于获取进程号停止进程,判断服务是否启动,避免反复启动
lock file = /var/run/rsync.lock #锁文件:在最大连接数达到200时,会进行限制连接操作
log file = /var/log/rsyncd.log  #服务运行时日志文件
ignore errors                                       #忽略传输过程中一些小的问题
read only = false                               #指定备份目录的权限为可读可写 false:假
list = false        
hosts allow = 172.16.1.0/24         #安全策略配置,设置一个白名单,只允许172.16.1这个网段进行访问
hosts deny = 0.0.0.0/32                 #安全策略配置  设置一个黑名单,不允许哪个网段进行访问
auth users = rsync_backup               #定义认证用户 rsync_backup为认证用户
secrets file = /etc/rsync.password      #密码文件 用户密码信息  格式:rsync_backup:oldboy123
[backup]                                #模块的名称(可以任意起名)
comment = "backup dir by oldboy"            #模块注释说明信息
path = /backup                      #备份的目录

以上为rsync配置文件的说明,如需配置请下载rsyncd.zip或者将说明中#号注释的全部删掉。

3.1.3 创建虚拟用户rsync

id rsync        #查看以下是否有rsync这个用户
useradd rsync -M -s /sbin/nologin       #创建rsync虚拟用户

3.1.4 创建密码文件并修改访问权限为600

echo "rsync_backup:oldboy123" >/etc/rsync.password
chmod 600 /etc/rsync.password           #只能root用户才能查看密码

3.1.5 创建备份目录/修改目录属主和属组信息

mkdir /backup
chown rsync.rsync /backup

3.1.6 启动服务程序/重启服务程序

systemctl start rsyncd
systemctl restart rsyncd
systemctl enable  rsyncd

3.1.7 客户端备份存储测试

rsync -avz /etc/hosts [email protected]::backup

执行命令后显示如下图,则证明服务端部署成功。在这里注意的是输入的密码是3.1.4步骤中的密码。
image.png

3.2 客户端部署

3.2.1 创建密码文件 并修改权限

echo oldboy123 >/etc/rsync.password     #将3.1.4步骤中的密码写入到rsync.password(可任意起名)
chmod 600 /etc/rsync.password                   #修改权限只能root查看密码

3.2.2 交互式传输数据

rsync -avz /etc/hosts [email protected]::backup

3.2.3 免交互式传输数据

rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password 

执行命令后显示如下图,则证明客户端部署成功。
image.png

4 rsync命令参数说明

-avz 常用参数
-a An archive parameter belongs, a parameter equal to the direct effect of using -rlptgoD
-v The display data transmission procedure information
-with When the file transfer, the file compression process (to improve transmission efficiency to save bandwidth)
-r Recursively backs up data transmission
-l Remains linked file attributes unchanged
-p Property rights remain unchanged ordinary file
-t Keep the file time information is not changed (mtime modification time)
-g Keep the file belongs to the same group information
-O Keep the file owner information unchanged
-D Keep the device file attribute information unchanged
--delete  Achieve no difference synchronization parameters 
so that data server data backup and directory information in the directory highly consistent 
--exclude=PATTERN Exclude specified files do not need to transfer information
--exclude-from=file Filename directory file, which can be achieved exclude multiple files
--bwlimit = RATE Speed ​​function, in units of M

Guess you like

Origin www.cnblogs.com/basa/p/11184146.html