Rsync数据复制软件应用

1:Rsync介绍

  1.1:什么是Rsync  

    Rsync是一款开源的快速,可实现全量及增量的本地化或远程数据备份的功能的优秀工具。并且在复制时可用不改变原有数据的属性信息,即可实现数据的备份迁移特性。

    官方连接:http://www.samba.org/ftp/rsync/rsync.html

    官方手册:man rsync/man rsync.conf

 1.2:Rsync功能介绍

  Rsync英文全称为Remote synchronize,Rsync具有可使本地和远程两台主机之间的数据快速复制,远程备份的功能。

  Rsync命令可用实现的功能如下:

    1)实现本地数据同步复制(相当于cp)

    2)实现远程数据同步复制(相当于scp)

    3)实现数据信息的删除功能(相当于rm)

    4)实现数据信息查看功能(相当于ls)

  全量:将全部的数据进行备份

  增量:只对有变化的数据进行备份

  1.3 软件的安装

  由于centos系统本身不自带rsync,所以要安装的,本文主要是通过Yum安装的,如下

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

[root@web01 ~]# 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.

2 : Rsync工作模式介绍与实践

   Rsync有3种传输数据模式,具体如下

   2.1 本地(local)数据传输模式

    Rsync的本地数据传输模式,类似于cp本地复制命令,可以实现文件,目录的移动备份等功能,所不同的是Rsync有增量复制功能

   2.2 远程shell数据传输功能

    远程shell数据传输模式一般是借助通道(如ssh)在两台服务器之间进行复制数据,这时两台服务器时对等的,没有客户端和服务器之分,整个过程类似于scp远程复制命令,所不同的就是Rsync具有增量功能,但是缺少scp复制加密功能

      2.3 守护进程(Daemon)传输模式

     守护进程传输模式是在客户端和服务器之间进行数据复制的,通常需要服务端部署守护进程服务,然后再客户端执行命令,实现数据的拉取和推送复制。

3. 软件的使用

  3.1:作为本地复制命令应用(类似于cp),如下 

  单独复制文件 

1 [root@backup01 ~]# cd /tmp
2 [root@backup01 tmp]# ls
3 [root@backup01 tmp]# rsync /etc/hosts /tmp/
4 [root@backup01 tmp]# ls
5 hosts
6 [root@backup01 tmp]# 

  复制目录里面的文件

1 [root@backup01 ~]# mkdir /fairies-yang -p
2 [root@backup01 ~]# touch /fairies-yang/{1..5}.txt
3 [root@backup01 ~]# ls /fairies-yang/
4 1.txt  2.txt  3.txt  4.txt  5.txt
5 [root@backup01 ~]# rsync -r /fairies-yang/ /tmp
6 [root@backup01 ~]# ls /tmp
7 1.txt  2.txt  3.txt  4.txt  5.txt  hosts
8 [root@backup01 ~]# 

  3.2 作为删除数据命令应用(类似于rm),如下 

  删除目录里所有文件

1 [root@backup01 ~]# mkdir /opt/null -p
2 [root@backup01 ~]# ls /tmp
3 1.txt  2.txt  3.txt  4.txt  5.txt  hosts
4 [root@backup01 ~]# rsync -r --delete /opt/null /tmp
5 [root@backup01 ~]# rsync -r --delete /opt/null/ /tmp
6 [root@backup01 ~]# ls /tmp
7 [root@backup01 ~]#

    删除文件里的内容

1 [root@backup01 tmp]# echo "i love fairies" > fairies.txt
2 [root@backup01 tmp]# cat fairies.txt 
3 i love fairies
4 [root@backup01 tmp]# touch null.txt
5 [root@backup01 tmp]# rsync -r  --delete null.txt  fairies.txt 
6 [root@backup01 tmp]# cat fairies.txt 
7 [root@backup01 tmp]# 

3.3:作为查询命数据命令应用(类似于ls命令),如下

1 [root@backup01 tmp]# ls
2 [root@backup01 tmp]# echo "i love fairies" > fairies.txt
3 [root@backup01 tmp]# ls -l fairies.txt 
4 -rw-r--r-- 1 root root 15 10月 12 22:05 fairies.txt
5 [root@backup01 tmp]# rsync fairies.txt 
6 -rw-r--r--             15 2020/10/12 22:05:33 fairies.txt
7 [root@backup01 tmp]# 

3.4:远程shell数据传输模式

  远程shell数据传输模式有拉取和推送两种模式,拉取是从远端服务器把数据拉取到本地,推送是把本地的数据推送到远端。

  拉取(PULL)的语法如下:

rsync [option] [user@host:src] desc
rsync命令 选项 【认证用户@主机地址:源路径] 目的路径

 

    使用如下:

  

 1 root@nfs01 ~]# ls /opt
 2 [root@nfs01 ~]# rsync -av root@192.168.37.41:/opt /opt
 3 The authenticity of host '192.168.37.41 (192.168.37.41)' can't be established.
 4 ECDSA key fingerprint is SHA256:cdWRCoJZTmt+eur6uTQzl+8Kt7wJNXyuGduRHmFahDQ.
 5 ECDSA key fingerprint is MD5:d6:5e:7a:02:f2:5f:ed:82:19:d1:12:cc:af:af:07:86.
 6 Are you sure you want to continue connecting (yes/no)? yes
 7 Warning: Permanently added '192.168.37.41' (ECDSA) to the list of known hosts.
 8 root@192.168.37.41's password: 
 9 receiving incremental file list
10 opt/
11 opt/hosts
12 opt/null/
13 
14 sent 55 bytes  received 489 bytes  19.78 bytes/sec
15 total size is 334  speedup is 0.61
16 [root@nfs01 ~]# ls /opt
17 opt
18 [root@nfs01 ~]#

 

  推取(PUSH),远程shell传输推送语法如下

rsync [option] src [user@host:src]
rsync命令 选项 本地路径 【认证用户@主机地址:源路径]

 

    使用如下:

 1 #本机命令
 2 [root@nfs01 ~]# rsync -av /etc/hosts root@192.168.37.41:/opt 
 3 root@192.168.37.41's password: 
 4 sending incremental file list
 5 hosts
 6 
 7 sent 425 bytes  received 35 bytes  54.12 bytes/sec
 8 total size is 334  speedup is 0.73
 9 [root@nfs01 ~]#
10 
11 
12 #远程主机信息
13  [root@backup01 opt]# ls
14 [root@backup01 opt]# ls
15 hosts
16 [root@backup01 opt]# 

 

4:守护进程传输模式

  守护进程传输模式是在客户端和服务器之间进行数据复制,通常需要在服务端部署守护进程,然后在客户端执行相关命令,实现数据的拉取和推送复制。

  4.1:部署环境,  

    准备三台服务器,具体信息如下

主机名 网卡eth0 网卡eth1 用途
web01 192.168.37.7 172.16.1.7 前端服务器
nfs01 192.168.37.31 172.16.1.31 存储服务器
backup 192.168.37.41 172.16.1.41 备份服务器

 

 4.2:在每台机器上都安装rsync软件,具体执行过程如下

  

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

4.3:配置rsyncd.conf,如下

  首先要对原来的配置文件进行备份,如下

1 [root@backup01 ~]# cp /etc/rsyncd.conf{,.ori}

  如下修改配置文件,

 1 [root@backup01 ~]# cat>/etc/rsyncd.conf<<EOF
 2 > #rsync_config_______________start
 3 > #created by molson
 4 > uid = rsync
 5 > gid = rsync
 6 > use chroot = no
 7 > fake super = yes
 8 > max connections = 200
 9 > timeout = 600 
10 > pid file = /var/run/rsyncd.pid
11 > lock file = /var/run/rsync.lock
12 > log file = /var/log/rsyncd.log
13 > ignore errors
14 > read only = false
15 > list = false
16 > hosts allow = 172.16.1.0/24
17 > hosts deny = 0.0.0.0/32
18 > auth users = rsync_backup
19 > secrets file = /etc/rsync.password
20 > [backup]
21 > comment = welcome to molson world!
22 > path = /backup/
23 > EOF

4.4:创建用于Rsync复制的账号,密码,账号文件权限及备份目录的创建,如下

1 [root@backup01 ~]# useradd rsync -s /sbin/nologin 
2 [root@backup01 ~]# echo "rsync_backup:fairies" >/etc/rsync.password
3 [root@backup01 ~]# mkdir /backup -p
4 [root@backup01 ~]# chown -R rsync.rsync /backup
5 [root@backup01 ~]# chmod 600 /etc/rsync.password

  检查相关文件信息,如下

1 [root@backup01 ~]# tail -1 /etc/passwd
2 rsync:x:1001:1001::/home/rsync:/sbin/nologin
3 [root@backup01 ~]# cat /etc/rsync.password 
4 rsync_backup:fairies
5 [root@backup01 ~]# ll /etc/rsync.password 
6 -rw------- 1 root root 21 10月 12 22:44 /etc/rsync.password
7 [root@backup01 ~]# 

4.5:启动rsync服务及检查确认,如下

 1 [root@backup01 ~]# systemctl start rsyncd
 2 [root@backup01 ~]# systemctl enable rsyncd
 3 Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
 4 [root@backup01 ~]# systemctl status rsyncd
 5 ● rsyncd.service - fast remote file copy program daemon
 6    Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
 7    Active: active (running) since 一 2020-10-12 22:48:19 CST; 32s ago
 8  Main PID: 879 (rsync)
 9    CGroup: /system.slice/rsyncd.service
10            └─879 /usr/bin/rsync --daemon --no-detach
11 
12 10月 12 22:48:19 backup01 systemd[1]: Started fast remote file copy program daemon.
13 [root@backup01 ~]# ps -ef | grep rsyncc
14 root        901    781  0 22:49 pts/0    00:00:00 grep --color=auto rsyncc
15 [root@backup01 ~]# ps -ef | grep rsync
16 root        879      1  0 22:48 ?        00:00:00 /usr/bin/rsync --daemon --no-detach
17 root        903    781  0 22:49 pts/0    00:00:00 grep --color=auto rsync
18 [root@backup01 ~]# netstat -lntup|grep rsync 
19 tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      879/rsync           
20 tcp6       0      0 :::873                  :::*                    LISTEN      879/rsync           
21 [root@backup01 ~]# 

 

4.6:测试数据同步效果

  

    客户端的配置,由于客户端要连接服务端,使用的账号为rsync_backup,同时客户端没有保存密码的,所以需要把账户的面膜保存到客户端,如下 

1 [root@nfs01 ~]# echo "fairies" >/etc/rsync.password

  4.6.1:推送:从客户端同步文件或目录到服务端,服务端的目录在根下的backup中,具体如下

  上传信息

1 [root@nfs01 ~]# rsync -avz /etc/hosts  rsync_backup@192.168.37.41::backup --password-file=/etc/rsync.password
2 sending incremental file list
3 hosts
4 
5 sent 224 bytes  received 43 bytes  534.00 bytes/sec
6 total size is 334  speedup is 1.25
7 [root@nfs01 ~]#

验证信息

1 [root@backup01 ~]# ll /backup
2 总用量 4
3 -rw-r--r-- 1 rsync rsync 334 10月 12 19:16 hosts

 

    上传多个文件;

  

 1 [root@nfs01 opt]# ls
 2 [root@nfs01 opt]# touch fairies{1..5}.txt
 3 [root@nfs01 opt]# ls
 4 fairies1.txt  fairies2.txt  fairies3.txt  fairies4.txt  fairies5.txt
 5 [root@nfs01 opt]# rsync -avz /opt/ rsync_backup@192.168.37.41::backup --password-file=/etc/rsync.password
 6 sending incremental file list
 7 ./
 8 fairies1.txt
 9 fairies2.txt
10 fairies3.txt
11 fairies4.txt
12 fairies5.txt
13 
14 sent 326 bytes  received 122 bytes  81.45 bytes/sec
15 total size is 0  speedup is 0.00
16 [root@nfs01 opt]# 

    多个文件进行验证,如下

 1 [root@backup01 backup]# ll
 2 总用量 4
 3 -rw-r--r-- 1 rsync rsync 334 10月 12 19:16 hosts
 4 [root@backup01 backup]# ll
 5 总用量 4
 6 -rw-r--r-- 1 rsync rsync   0 10月 12 23:02 fairies1.txt
 7 -rw-r--r-- 1 rsync rsync   0 10月 12 23:02 fairies2.txt
 8 -rw-r--r-- 1 rsync rsync   0 10月 12 23:02 fairies3.txt
 9 -rw-r--r-- 1 rsync rsync   0 10月 12 23:02 fairies4.txt
10 -rw-r--r-- 1 rsync rsync   0 10月 12 23:02 fairies5.txt
11 -rw-r--r-- 1 rsync rsync 334 10月 12 19:16 hosts
12 [root@backup01 backup]# 

 

  4.6.2:拉取:从服务器端同步数据到本地文件,如下

  选择需要拉取的数据,如下

1 [root@backup01 backup]# ls
2 [root@backup01 backup]# touch {1..10}fairies.txt
3 [root@backup01 backup]# ls
4 10fairies.txt  1fairies.txt  2fairies.txt  3fairies.txt  4fairies.txt  5fairies.txt  6fairies.txt  7fairies.txt  8fairies.txt  9fairies.txt
5 [root@backup01 backup]# 

  在客户端进行拉取并验证,如下

  

 1 [root@nfs01 opt]# ls
 2 [root@nfs01 opt]# rsync -avz rsync_backup@192.168.37.41::backup  /opt --password-file=/etc/rsync.password
 3 receiving incremental file list
 4 ./
 5 10fairies.txt
 6 1fairies.txt
 7 2fairies.txt
 8 3fairies.txt
 9 4fairies.txt
10 5fairies.txt
11 6fairies.txt
12 7fairies.txt
13 8fairies.txt
14 9fairies.txt
15 
16 sent 221 bytes  received 654 bytes  159.09 bytes/sec
17 total size is 0  speedup is 0.00
18 [root@nfs01 opt]#ls -l
19 10fairies.txt  1fairies.txt  2fairies.txt  3fairies.txt  4fairies.txt  5fairies.txt  6fairies.txt  7fairies.txt  8fairies.txt  9fairies.txt
20 [root@nfs01 opt]# 

  由以上可知,拉取和推送同步数据成功

5:多模块复制应用

  最简单的办法就是在配置文件中增加新的字段,具体如下

 1 [root@backup01 ~]# cat /etc/rsyncd.conf
 2 #rsync_config_______________start
 3 #created by molson
 4 uid = rsync
 5 gid = rsync
 6 use chroot = no
 7 fake super = yes
 8 max connections = 200
 9 timeout = 600 
10 pid file = /var/run/rsyncd.pid
11 lock file = /var/run/rsync.lock
12 log file = /var/log/rsyncd.log
13 ignore errors
14 read only = false
15 list = false
16 hosts allow = 172.16.1.0/24
17 hosts deny = 0.0.0.0/32
18 auth users = rsync_backup
19 secrets file = /etc/rsync.password
20 [backup]
21 comment = welcome to molson world!
22 path = /backup/
23 [lianfu]                    =====》增加一个新的字段
24 comment = welcome to lianfu world!
25 path = /lianfu/         ======》路径在根下的lianfu
26 [root@backup01 ~]# 

  在根下创建lianfu目录并给rsync账号权限及验证,如下

1 [root@backup01 ~]# mkdir /lianfu -p
2 [root@backup01 ~]# chown -R rsync.rsync /lianfu
3 [root@backup01 ~]# rsync /lianfu
4 drwxr-xr-x              6 2020/10/12 23:16:56 lianfu
5 [root@backup01 ~]# 

  推送:选择上传的文件上传并验证,如下

  

 1 [root@nfs01 ~]# cd /opt
 2 [root@nfs01 opt]# touch lianfu.txt
 3 [root@nfs01 opt]# rsync -avz /opt/ rsync_backup@192.168.37.41::lianfu --password-file=/etc/rsync.password
 4 sending incremental file list
 5 ./
 6 10fairies.txt
 7 1fairies.txt
 8 2fairies.txt
 9 3fairies.txt
10 4fairies.txt
11 5fairies.txt
12 6fairies.txt
13 7fairies.txt
14 8fairies.txt
15 9fairies.txt
16 lianfu.txt
17 
18 sent 709 bytes  received 236 bytes  630.00 bytes/sec
19 total size is 0  speedup is 0.00
20 [root@nfs01 opt]# 

  客户端验证  

1 [root@backup01 ~]# ls /lianfu
2 [root@backup01 ~]# ls /lianfu
3 10fairies.txt  2fairies.txt  4fairies.txt  6fairies.txt  8fairies.txt  lianfu.txt
4 1fairies.txt   3fairies.txt  5fairies.txt  7fairies.txt  9fairies.txt
5 [root@backup01 ~]# 

 

  拉取文件,选择要拉取的文件,并验证

  选择拉取的文件,如下 

1 [root@backup01 lianfu]# ls 
2 [root@backup01 lianfu]# touch zhanglianfu.txt
3 [root@backup01 lianfu]# ls
4 zhanglianfu.txt
5 [root@backup01 lianfu]# 

  拉取并验证,如下

 1 [root@nfs01 opt]# ls
 2 [root@nfs01 opt]# rsync -avz rsync_backup@192.168.37.41::lianfu /opt  --password-file=/etc/rsync.password
 3 receiving incremental file list
 4 ./
 5 zhanglianfu.txt
 6 
 7 sent 50 bytes  received 125 bytes  31.82 bytes/sec
 8 total size is 0  speedup is 0.00
 9 [root@nfs01 opt]# ls 
10 zhanglianfu.txt
11 [root@nfs01 opt]# 

 

  

 

posted @ 2020-10-12 23:31  Molson  阅读(159)  评论(0编辑  收藏  举报