第11章Rsync数据同步工具应用

第11章Rsync数据同步工具应用

11.1 Rsync介绍

Rsync是一款开源的,快捷的,多功能的。可实现的全量及增量测本地或远程数据同步,备份的优秀工具,Rsync软件适用于Unix/Linux/windows等多种平台。

11.2 Rsync的特性

1.支持拷贝特殊文件如连接文件,设备等。

2.可以有排除指定文件或目录同步功能,相当于打包命令tar的排除功能。

3.可以做到保持原文件或目录的权限,时间,软硬链接,属主,组等所有属性均不改变-P.

4.可实现增量同步,既只同步发生变化的数据,因此数据传输效率很高,tar.

5.可以使用rcp,rsh,ssh等方式来配合传输文件(Rsync本身不对数据加密)

6.可以通过socket(进程方式)传输文件和数据。

7.支持匿名的或认证(无需系统用户)的进程模式传输,可实现方便安全的进行传输数据备份及镜像,

11.3   Rsync的企业工作场景

  1. 两个服务器之间的同步
  2. 把所有的客户端服务器实施备份

 

  1.  Rsync结合inotify的功能实时的数据同步

 

11.4   Rsync的工作方式

Rsync大致分为三种主要的传输【数据的方式,分别为:

  1. 单个主机本地之间的数据传输(此时类似于cp命令的功能)
  2. 借助rcp.ssh等通道来传输数据(此时类似scp命令的功能)
  3. 以守护进程(socket)的方式传输数据(这个是rsync自身的重要功能)

11.4.1本地数据传输的模式(local-only mode)

Rsync本地传输模式的语法为:

Rsync [OPTION…] SRC… [DEST]

语法说明:
1.Rsync 为同步的命令。

2.[OPTION…]为同步时的参数选项;

3.SRC为源,即待拷的分区,文件或者目录等;

4.[DEST]为目录分区,文件或者目录等。

相当于cp命令的用法。

例如:

[root@localhost ~]# rsync /etc/hosts /opt/   è将/etc/hosts文件同步到/opt/
[root@localhost ~]# ll /opt/
总用量 4
-rw-r--r--. 1 root root 158 10月 11 04:58 hosts

  

11.4.2借助SSH通道从远端主机拉取数据例子实践:

推例子:-avz是最常用的参数

相当于scp的用法。

[root@localhost etc]# rsync /etc/hanjiali.txt -e 'ssh -p22' [email protected]:/opt/
[email protected]'s password: 
[root@localhost etc]# ll /opt/

/etc/hanjiali.txt   è要同步的文件(绝对路径)

-e                                  è指定通道

'ssh -p22'                      è通道

Root                              è现在使用的用户

@192.168.170.133:      è指定同步的服务器地址

/opt/                               è指定同步的目的地址


  

拉例子,就是将源和目的换一下就0K。

11.4.3以守护进程(socket)的方式传输数据(重点)

11.4.3.1部署server服务端

服务端:

  1. 查看是否安装了Rsync.
[root@Server ~]# which rsync
/usr/bin/rsync 

  

  1.  查看其版本
[root@Server ~]# rsync --version
rsync  version 3.1.2  protocol version 31
Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes, prealloc
 
rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.

  

  1. 配置文件
vim /etc/rsyncd.config
 配置文件:
#rsync_config
    #created by HQ at 2017
    ##rsyncd.conf start##                           
    uid = rsync             
    gid = rsync              
    use chroot = no                 è防止出现问题    
    max connections = 200           è最大的客户端的值
    timeout = 300                     è客户端连接多长时间超时
    pid file = /var/run/rsyncd.pid      è进程号所在的文件,便于管理进程
    lock file = /var/run/rsync.lock      è锁的机制,防止冲突,更新时防止有数据进入
    log file = /var/log/rsyncd.log      è系统出错看这里
[girl]                           è模块
pash =  /hanjiali/               è处理的路径
 ignore errors                    è错误处理
    read only = false                 è不允许只读
    list = false                       è不允许列表
    hosts allow = 192.168.170.0/24     è允许的网段
    hosts deny = 0.0.0.0/32      
    auth users = rsync_backup         è虚拟用户
    secrets file = /etc/rsync.password   è密码写文件中,同步密码
  1. 启动服务
[root@Server ~]# rsync --daemon  

  

  1.  查看端口是否有rsync,查看是否被监听默认端口873
[root@Server ~]# netstat -lntup|grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      3910/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      3910/rsync       

  

   

  1. 直接报错
[root@Server ~]# cat /var/log/rsyncd.log
cat: /var/log/rsyncd.log: 没有那个文件或目录

解决方法:文件中的路径并不一定存在,创建一下:

[root@Server ~]# mkdir /hanjiali -p                  è创建路径
[root@Server ~]# chown -R rsync.rsync /hanjiali      è授权
chown: 无效的用户: "rsync.rsync" 
[root@Server ~]# useradd rsync -s /sbin/nologin      è创建一个虚拟用户
 [root@Server ~]# chown -R rsync.rsync /hanjiali

  结果:

[root@Server ~]# cat /var/log/rsyncd.log 
2019/10/14 20:49:58 [4047] rsyncd version 3.1.2 starting, listening on port 873

设置密码

 [root@Server ~]# echo "rsync backup:hanjiali" >/etc/rsync.password

 设置权限

chmod 600 /etc/rsync.password

关闭防火墙和关闭selinux:

vim /etc/selinux/config 
SELINUX=disabled
修改重启生reboot
[root@server ~]# getenforce
Disabled
[root@server ~]# systemctl stop firewalld.service
^[[A[root@server systemctl stop firewalld.service
[root@server ~]# systemctl stop firewalld 
[root@server ~]# iptables -F

11.4.3.2部署客户端

客户端:

  1.  安装软件Rsync
  2.  设置密码和权限
Last login: Mon Oct 14 18:34:53 2019 from 192.168.170.19
[root@client1 ~]#  echo "hanjiali" >/etc/rsync.password
[root@client1 ~]# chmod 600 /etc/rsync.password 
[root@client1 ~]# ls -l /etc/rsync.password 
-rw-------. 1 root root 9 10月 14 21:06 /etc/rsync.password
[root@client1 ~]# cat /etc/rsync.password 
hanjiali
[root@client1 ~]#  

数据流都是由客户端到服务器,

    Access via rsync daemon:

    拉;     Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]

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

            //

    推:   Push: rsync [OPTION...] SRC... [USER@]HOST::DEST

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

  1.  登陆成功
[root@client1 ~]# rsync -avz rsync_backup@192.168.170.133::hanjiali /data1
Password: 
receiving incremental file list

sent 27 bytes  received 57 bytes  11.20 bytes/sec

 
 

total size is 0  speedup is 0.00

 

验证

服务器上:

[root@server hanjiali]# tree /hanjiali/
/hanjiali/
└── test

0 directories, 1 file

 
 

[root@server hanjiali]#

 

客户端上:

[root@client1 ~]# tree /data1
/data1
└── test

0 directories, 1 file

 
 

[root@client1 ~]#

 

 通过密码文件进行“拉”

[root@client1 ~]# rsync -avz rsync_backup@192.168.170.133::hanjiali /data1 --password-file=/etc/rsync.password
receiving incremental file list
 
sent 24 bytes  received 71 bytes  27.14 bytes/sec
total size is 0  speedup is 0.00

将客户端的文件进行“推”到服务端

[root@client1 ~]# cd /data1
[root@client1 data1]# ll
总用量 0
-rw-r--r--. 1 root root 0 10月 15 01:21 1
-rw-r--r--. 1 root root 0 10月 15 01:21 10
-rw-r--r--. 1 root root 0 10月 15 01:21 2
-rw-r--r--. 1 root root 0 10月 15 01:21 3
-rw-r--r--. 1 root root 0 10月 15 01:21 4
-rw-r--r--. 1 root root 0 10月 15 01:21 5
-rw-r--r--. 1 root root 0 10月 15 01:21 6
-rw-r--r--. 1 root root 0 10月 15 01:21 7
-rw-r--r--. 1 root root 0 10月 15 01:21 8
-rw-r--r--. 1 root root 0 10月 15 01:21 9
-rw-r--r--. 1 root root 0 10月 15 2019 test
[root@client1 data1]# rsync -avz /data1/ rsync_backup@192.168.170.133::hanjiali --password-file=/etc/rsync.password
sending incremental file list
1
10
2
3
4
5
6
7
8
9
rsync: chgrp "/.1.cjxFLa" (in hanjiali) failed: Operation not permitted (1)
rsync: chgrp "/.10.Lgyg1G" (in hanjiali) failed: Operation not permitted (1)
rsync: chgrp "/.2.6Qc4gd" (in hanjiali) failed: Operation not permitted (1)
rsync: chgrp "/.3.diTYwJ" (in hanjiali) failed: Operation not permitted (1)
rsync: chgrp "/.4.O8B0Mf" (in hanjiali) failed: Operation not permitted (1)
rsync: chgrp "/.5.BZF92L" (in hanjiali) failed: Operation not permitted (1)
rsync: chgrp "/.6.Tdktji" (in hanjiali) failed: Operation not permitted (1)
rsync: chgrp "/.7.WABTzO" (in hanjiali) failed: Operation not permitted (1)
rsync: chgrp "/.8.QddpQk" (in hanjiali) failed: Operation not permitted (1)
rsync: chgrp "/.9.PL8Z6Q" (in hanjiali) failed: Operation not permitted (1)
 
sent 560 bytes  received 1,015 bytes  630.00 bytes/sec
total size is 0  speedup is 0.00

服务端验证:

[root@server hanjiali]# ll
总用量 0
-rw------- 1 rsync rsync 0 10月 15 03:51 1
-rw------- 1 rsync rsync 0 10月 15 03:51 10
-rw------- 1 rsync rsync 0 10月 15 03:51 2
-rw------- 1 rsync rsync 0 10月 15 03:51 3
-rw------- 1 rsync rsync 0 10月 15 03:51 4
-rw------- 1 rsync rsync 0 10月 15 03:51 5
-rw------- 1 rsync rsync 0 10月 15 03:51 6
-rw------- 1 rsync rsync 0 10月 15 03:51 7
-rw------- 1 rsync rsync 0 10月 15 03:51 8
-rw------- 1 rsync rsync 0 10月 15 03:51 9
-rw-r--r-- 1 root  root  0 10月 15 03:34 test

猜你喜欢

转载自www.cnblogs.com/hanjiali/p/11673859.html