前面介绍了一些关于rsync的功能与一些具体的命令格式、参数的使用意义等,今天我们来聊一聊整个实际生产过程中的实战操作过程
今天介绍的是第三种模式——以守护进程的模式传输数据
[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)
[root@BS-C ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@BS-C ~]# uname -r
2.6.32-431.el6.x86_64
[root@BK-S ~]# rpm -qa |grep "rsync"
rsync-3.0.6-9.el6_4.1.x86_64
[root@BK-S ~]#cat /etc/rsyncd.conf 
cat:/etc/rsyncd.conf: No such file or directory
[root@BK-S ~]# vi /etc/rsyncd.conf 
##rsync config  start                         
##created by root 2016-08-08 15:00
syncd.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
[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
[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
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 ...
[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
[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
[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
至此备份数据的服务端配置结束
[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
客户端配置结束
[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]
[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  ]
[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
[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################
[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
[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
[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
[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

原文:https://blog.51cto.com/mingongge/2555718