Linux系统rsync实战操作

2020-09-16 15:00:19 浏览数 (1)

Linux系统rsync实战操作

前面介绍了一些关于rsync的功能与一些具体的命令格式、参数的使用意义等,今天我们来聊一聊整个实际生产过程中的实战操作过程

今天介绍的是第三种模式——以守护进程的模式传输数据

一:整体环境

备份服务器端(BK-S)

代码语言:javascript复制
[root@BS-S~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@BS-S ~]# uname -r
2.6.32-431.el6.x86_64

备份数据的客户端(BK-C)

代码语言:javascript复制
[root@BS-C ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@BS-C ~]# uname -r
2.6.32-431.el6.x86_64

二:配置过程

以下配置为备份数据服务器端

1、检查是否安装软件

代码语言:javascript复制
[root@BK-S ~]# rpm -qa |grep "rsync"
rsync-3.0.6-9.el6_4.1.x86_64

2、配置rsync的配置文件/etc/rsync.conf

代码语言:javascript复制
[root@BK-S ~]#cat /etc/rsyncd.conf  
cat:/etc/rsyncd.conf: No such file or directory

显示系统无此文件,因此我们直接创建文件

代码语言:javascript复制
[root@BK-S ~]# vi /etc/rsyncd.conf 
##rsync config  start                         

##created by root 2016-08-08 15:00
##rsyncd.conf config start    以上为注释部分
uid = rsync
gid = rsync
use chroot = no
max connetctions = 200  最大连接数(并发)
timeout = 100  超时时间默认S单位
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]                            模块名称可自定义任意名称
path = /backup/                 备份数据的路径
ignore errors                    忽略错误
read only = false
list = false
hosts allow = 192.168.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup 虚拟的用户用于连接认证
secrets file = /etc/rsync.password  认证的密码配置文件路径
##rsync config  end

整个配置过程中注意中间的空格与书写规范

检查配置文件

代码语言:javascript复制
[root@BK-S ~]# cat /etc/rsyncd.conf 
##rsync config  start
##created by root 2016-08-08 15:00
##rsync.conf config start
uid = rsync
gid = rsync
use chroot = no
max connetctions = 200
timeout = 100
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
path = /backup/
ignore errors
read only = false
list = false
hosts allow = 192.168.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
##rsync config  end

3、格式化配置文件

代码语言:javascript复制
[root@BK-S ~]#dos2unix /etc/rsync.conf            没有安装
-bash: dos2unix:command not found
[root@BK-S ~]#yum install dos2unix -y YUM进行安装
Loaded plugins:fastestmirror, refresh-packagekit, security
Loading mirrorspeeds from cached hostfile
 * base: mirrors.zju.edu.cn
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.zju.edu.cn
base                                              | 3.7 kB     00:00    
extras                                             |3.4 kB     00:00    
updates                                            |3.4 kB     00:00   
updates/primary_db                                 | 2.0 MB     00:01    
Setting up InstallProcess
ResolvingDependencies
--> Runningtransaction check
---> Packagedos2unix.x86_64 0:3.1-37.el6 will be installed
--> FinishedDependency Resolution
DependenciesResolved
============================================
 Package          Arch           Version               Repository    Size
======================================Installing:
 dos2unix         x86_64         3.1-37.el6            base          16 k
TransactionSummary
============================================
Install       1 Package(s)
Total downloadsize: 16 k
Installed size: 18k
DownloadingPackages:
dos2unix-3.1-37.el6.x86_64.rpm          |  16kB     00:00    
Running rpm_check_debug
RunningTransaction Test
Transaction TestSucceeded
RunningTransaction
  Installing : dos2unix-3.1-37.el6.x86_64               1/1
  Verifying : dos2unix-3.1-37.el6.x86_64                1/1
Installed:
  dos2unix.x86_64 0:3.1-37.el6                                
Complete!
[root@BK-C]#dos2unix /etc/rsync.conf
dos2unix:converting file /etc/rsync.conf to UNIX format ...

4、添加用户

代码语言:javascript复制
[root@BK-S ~]#useradd rsync -s /sbin/nologin -M
[root@BK-S /]#mkdir backup
[root@BK-S /]#chown -R rsync.rsync /backup          
[root@BK-S /]# ls -ll
drwxr-xr-x.   2 rsync rsync   4096 Aug 27 14:30 backup
dr-xr-xr-x.   2 root  root    4096 Aug 15 19:15 bin
dr-xr-xr-x.   5 root  root    1024 Aug  7 18:04 boot
drwxr-xr-x.   3 root  root    4096 Aug 21 10:07 data
drwxr-xr-x.  19 root  root    3860 Sep  2 05:05 dev

5、配置密码文件

代码语言:javascript复制
[root@BK-S /]# echo "rsync_backup:rsync.conf">>/etc/rsync.password
[root@BK-S /]# cat /etc/rsync.password                           
rsync_backup:rsync.conf              认证用户:认证密码
因密码文件为明文,所以需要改变默认权限
[root@BK-S /]# chmod 600  /etc/rsync.password
[root@BK-S /]# ls -ld /etc/rsync.password
-rw-------. 1 root root 24 Sep  2 05:43 /etc/rsync.password

6、启动服务

代码语言:javascript复制
[root@BK-S /]# rsync --daemon                   启动服务后台运行
[root@BK-S /]# netstat -lntup |grep rsync    查看服务使用端口
tcp  0   0 0.0.0.0:873     0.0.0.0:*    LISTEN    2013/rsync     
tcp  0   0 :::873             :::*              LISTEN    2013/rsync  
[root@BK-S /]# ps -ef |grep rsync
root      2013      1  0 05:45 ?        00:00:00 rsync --daemon
root      2018   1930  0 05:46 pts/0    00:00:00 grep rsync

至此备份数据的服务端配置结束

7、以下为客户配置过程

代码语言:javascript复制
[root@BK-C ~]# echo "rsync.conf">/etc/rsync.password 
[root@BK-C ~]# cat /etc/rsync.password              
rsync.conf
[root@BK-C ~]# chmod 600 /etc/rsync.password
[root@BK-C ~]# ls -ld /etc/rsync.password
-rw-------. 1 root root 11 Sep  3 13:57 /etc/rsync.password

客户端配置结束

三:测试数据备份

代码语言:javascript复制
[root@BK-C ~]# rsync -avzP /etc/hosts rsync_backup@192.168.1.2::backup --password-file=/etc/rsync.password
rsync: failed to connect to 192.168.1.2: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]

报错提示没有路由,表明服务端防火墙没有关闭

代码语言:javascript复制
[root@BK-S backup]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter [  OK  ]
iptables: Flushing firewall rules:                          [  OK  ]
iptables: Unloading modules:                              [  OK  ]

服务端关闭防火墙

代码语言:javascript复制
[root@BK-C~]# rsync -avzP /etc/hosts rsync_backup@192.168.1.2::backup --password-file=/etc/rsync.password
sending incremental file list
hosts
         322 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)
sent 141 bytes  received 27 bytes  336.00 bytes/sec

total size is 322  speedup is 1.92

显示数据备份完成

备份服务器上查看是否备份成功

代码语言:javascript复制
[root@BK-C~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
###########www.baidu.com################
###########www.baidu.com################
###########www.baidu.com################
###########www.baidu.com################
[root@BK-S backup]# ls -ll
total 4
-rw-r--r--. 1 rsync rsync 322 Aug 30 03:29 hosts
[root@BK-S backup]# cat hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
###########www.baidu.com################
###########www.baidu.com################
###########www.baidu.com################
###########www.baidu.com################

两台服务器上hosts文件内容相同,表明备份成功

代码语言:javascript复制
[root@BK-C ~]# cd /tmp
[root@BK-C tmp]# ls
[root@BK-C tmp]# ls -ll
total 0
[root@BK-C tmp]# touch 123.txt
[root@BK-C tmp]# touch 321.txt
[root@BK-C tmp]# rsync -avzP /tmp/ rsync_backup@192.168.1.2::backup --
password-file=/etc/rsync.password         
sending incremental file list
./
123.txt
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)
321.txt
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=1/4)
.ICE-unix/
sent 162 bytes  received 53 bytes  430.00 bytes/sec

total size is 0  speedup is 0.00

服务端查看数据同步情况

代码语言:javascript复制
[root@BK-S backup]# ls -ll
total 4
-rw-r--r--. 1 rsync rsync   0 Sep  9  2016 123.txt
-rw-r--r--. 1 rsync rsync   0 Sep  9  2016 321.txt
-rw-r--r--. 1 rsync rsync 322 Aug 30 03:29 hosts

测试--delete参数

代码语言:javascript复制
[root@BK-S backup]# mkdir text
[root@BK-S backup]# mkdir text1
[root@BK-S backup]# mkdir text2
[root@BK-S backup]# mkdir text3
[root@BK-S backup]# touch 4565.bak
[root@BK-S backup]# touch 4565.txt
[root@BK-S backup]# ls -ll
total 20
-rw-r--r--. 1 rsync rsync    0 Sep  9  2016 123.txt
-rw-r--r--. 1 rsync rsync    0 Sep  9  2016 321.txt
-rw-r--r--. 1 root  root     0 Sep  7 13:48 4565.bak
-rw-r--r--. 1 root  root     0 Sep  7 13:48 4565.txt
-rw-r--r--. 1 rsync rsync  322 Aug 30 03:29 hosts
drwxr-xr-x. 2 root  root  4096 Sep  7 13:48 text
drwxr-xr-x. 2 root  root  4096 Sep  7 13:48 text1
drwxr-xr-x. 2 root  root  4096 Sep  7 13:48 text2
drwxr-xr-x. 2 root  root  4096 Sep  7 13:48 text3
[root@BK-C tmp]# rsync -avzP --delete /tmp/ rsync_backup@192.168.1.2::
backup --password-file=/etc/rsync.password
sending incremental file list
./
deleting text3/
deleting text2/
deleting text1/
deleting text/
deleting hosts
deleting 4565.txt
deleting 4565.bak
sent 91 bytes  received 12 bytes  206.00 bytes/sec

total size is 0  speedup is 0.00

服务器端查看结果

代码语言:javascript复制
[root@BK-S backup]# ls -ll
total 0
-rw-r--r--. 1 rsync rsync 0 Sep  9  2016 123.txt
-rw-r--r--. 1 rsync rsync 0 Sep  9  2016 321.txt

最终结果显示delete参数的作用就是:客户端有什么,远端服务器就有什么,客户备份目录下没有的,远端服务器目录下其它文件或目录就会被删除,此参数相当危险,实际生产环境中要小心使用

0 人点赞