一、 利用/etc/init.d/启动rsync服务方式
1、可以实现方式:
a. 编写rsync启动脚本(有一定的shell能力 if case)
b. 利用xinetd服务,管理启动rsync服务
2、利用 xinetd服务 管理rsync
(1)安装xinetd软件
[root@backup
~]# yum install -y xinetd
[root@backup ~]# rpm -qa |grep xin
xinetd-2.3.14-40.el6.x86_64
(2):编辑配置文件
修改disable = yes 改为disable = no
[root@backup
~]# vim /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it
\
# allows crc checksumming
etc.
service rsync
{
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server =
/usr/bin/rsync
server_args =
--daemon
log_on_failure += USERID
}
(2)重启xinetd服务
[root@backup
~]# /etc/init.d/xinetd restart
Stopping xinetd:
[ OK ]
Starting xinetd:
[ OK ]
传输测试
[root@nfs01
~]# rsync -avzP /etc/services rsync_backup@172.16.1.41::backup
--password-file=/etc/rsync.password
sending incremental file list
sent 29 bytes received 8 bytes 74.00 bytes/sec
total size is 641020 speedup is 17324.86
二、定义变量信息实现免秘钥交互
1、 通过man手册获得方法
Some modules
on the remote daemon may require authentication. If so, you will receive
a password prompt when you connect. You can avoid
the password prompt
by setting the environment
variable RSYNC_PASSWORD to the password
you want to
use or using
the --password-file option. This may be useful when scripting
rsync.
WARNING: On some systems environment
variables are visible to all users. On those systems using --password-file is
recommended.
在远程进程的一些模块可能需要认证。如果是这样的话,你将得到一个密码提示当您连接。你可以通过设置环境变量rsync_password要使用或使用密码文件选项密码避免密码提示。这可能是有用的脚本文件。
警告:在一些系统环境变量,对所有用户都是可见的。在这些系统中使用的密码文件的建议。
2、使用 RSYNC_PASSWORD 变量实现免交互
未设置变量之前
[root@nfs01
~]# rsync -avzP /etc/services rsync_backup@172.16.1.41::backup
Password:
添加上环境变量
[root@nfs01 ~]# export RSYNC_PASSWORD=clsn123
测试
[root@nfs01
~]# rsync -avzP /etc/services rsync_backup@172.16.1.41::backup
sending incremental file list
sent 29 bytes received 8 bytes 24.67 bytes/sec
total size is 641020 speedup is 17324.86
三、 守护进程多模块功能配置
1、编写配置信息创建多模块
[root@backup
~]# vim /etc/rsyncd.conf
……
[nfsdata]
comment = "nfsdata dir by clsn"
path = /backup/nfsdata
[nfsbackup]
comment = "nfsbackup dir by clsn"
path = /backup/nfsbackup
2、创建多模块指定的目录
#
创建目录,并修改目录的权限
[root@backup ~]# mkdir /backup/nfs{data,backup} -p
[root@backup ~]# chown rsync.rsync
/backup/nfs{data,backup}
#查看:
[root@backup ~]# ll /backup/nfs{data,backup} -d
drwxr-xr-x 2 rsync rsync 4096 Oct 12 10:05 /backup/nfsbackup
drwxr-xr-x 2 rsync rsync 4096 Oct 12 10:05 /backup/nfsdata
3、利用rsync客户端进行测试
[root@nfs01
~]# rsync -avz /data/ rsync_backup@172.16.1.41::nfsdata
--password-file=/etc/rsync.passsword
sending incremental file list
./
nfs.data
sent 78 bytes received 30 bytes 216.00 bytes/sec
total size is 0 speedup is 0.00
说明:
(1)rsyncd.conf配置文件中,添加多模块信息,可以不用重启rsync服务,即时生效~
(2)全局变量参数针对所有模块生效;局部变量参数只针对指定模块生效
(3)read only参数默认配置为ture,即为只读模式
(4)全局变量发生变化,不用重启rsync服务;局部变量发生变化,需要重启rsync服务
注意:修改配置文件就重启↓
无论是全局变量发生变化,还是局部变量发生变化,都建议重启rsync服务使配置生效
四、 守护进程的排除功能实践
1、排除的方式
a) --exclude=要配置的目录或文件名称
b) --exclude-from=要排除多个目录或文件汇总文件名称
c) 在配置文件中进行修改,指定要排除的信息
2、排除测试
(1)创建模拟测试环境
[root@nfs01
data]# mkdir {a..d}
[root@nfs01 data]# touch {a..d}/{1..3}.txt
[root@nfs01 data]# tree
.
├── a
│ ├── 1.txt
│ ├── 2.txt
│ └── 3.txt
├── b
│ ├── 1.txt
│ ├── 2.txt
│ └── 3.txt
├── c
│ ├── 1.txt
│ ├── 2.txt
│ └── 3.txt
└── d
├── 1.txt
├── 2.txt
└── 3.txt
4 directories, 12 files
3、利用 --exclude参数测试排除功能
(1) 需求:不要a目录中3.txt 不要b、c目录
[root@nfs01
data]# rsync -avz /data/ --exclude=a/3.txt --exclude=b --exclude=c
rsync_backup@172.16.1.41::nfsdata
sending incremental file
list
./
a/
a/1.txt
a/2.txt
d/
d/1.txt
d/2.txt
d/3.txt
sent 300 bytes received 114 bytes 828.00 bytes/sec
total size is 0 speedup is 0.00
(2)精简方式排除
[root@nfs01
data]# rsync -avz /data/ --exclude=a/3.txt --exclude={b,c}
rsync_backup@172.16.1.41::nfsdata
sending incremental file
list
./
a/
a/1.txt
a/2.txt
d/
d/1.txt
d/2.txt
d/3.txt
sent 300 bytes received 114 bytes 828.00 bytes/sec
total size is 0 speedup is 0.00
4、利用--exclude-from 方式进行排除
(1)创建模拟测试环境
[root@nfs01
data]# mkdir {a..d}
[root@nfs01 data]# touch {a..d}/{1..3}.txt
(2)利用--exlude-from参数,测试排除功能
[root@nfs01
data]# vim /tmp/paichu.txt
a/3.txt
b
c
(3)进行排除
[root@nfs01
data]# rsync -avz /data/ --exclude-from=/tmp/paichu.txt
rsync_backup@172.16.1.41::nfsdata
sending incremental file
list
./
a/
a/1.txt
a/2.txt
d/
d/1.txt
d/2.txt
d/3.txt
sent 300 bytes received 114 bytes 828.00 bytes/sec
total size is 0 speedup is 0.00
说明:
01. 排除文件中,需要利用相对路径指定排除信息(不能利用绝对路径)
02. 相对路径指的是相对同步的目录信息而言,是对rsync -avz /data/ 后面的data目录进行相对
5、在配置文件中修改要排除的文件
(1)编写修改服务端配置文件
vim
/etc/rsyncd.conf
[nfsdata]
comment = "nfsdata dir by clsn"
path = /backup/nfsdata
exclude=a/3.txt b c
(2)重启rsync服务
killall rsync && sleep 1 && rsync --daemon
(3)进行测试
[root@nfs01
data]# rsync -avz /data/
rsync_backup@172.16.1.41::nfsdata
sending incremental file
list
./
a/
a/1.txt
a/2.txt
skipping daemon-excluded file "a/3.txt"
skipping daemon-excluded directory "b"
*** Skipping any contents from this failed directory ***
skipping daemon-excluded directory "c"
*** Skipping any contents from this failed directory
***
d/
d/1.txt
d/2.txt
d/3.txt
sent 407 bytes received 116 bytes 1046.00 bytes/sec
total size is 0 speedup is
0.00
rsync error: some files/attrs were not transferred (see previous errors) (code
23) at main.c(1039) [sender=3.0.6]
五、守护进程来创建备份目录
1、通过客户端命令创建服务端备份目录中子目录
#
推送/etc/services文件到 服务器/backup/sda/目录
[root@nfs01 ~]# rsync -avzP /etc/services
rsync_backup@172.16.1.41::backup/dba/
sending incremental file list
created directory dba
services
641020 100% 19.34MB/s
0:00:00 (xfer#1, to-check=0/1)
#
推送/etc/services文件到 服务器/backup/sa/目录
sent 127417 bytes received 27 bytes 254888.00 bytes/sec
total size is 641020 speedup is 5.03
[root@nfs01 ~]# rsync -avzP /etc/services
rsync_backup@172.16.1.41::backup/sa/
sending incremental file list
created directory sa
services
641020 100% 19.34MB/s
0:00:00 (xfer#1, to-check=0/1)
#
推送/etc/services文件到 服务器/backup/dev/目录
sent 127417 bytes received 27 bytes 254888.00 bytes/sec
total size is 641020 speedup is 5.03
[root@nfs01 ~]# rsync -avzP /etc/services
rsync_backup@172.16.1.41::backup/dev/
sending incremental file list
created directory dev
services
641020 100% 18.71MB/s
0:00:00 (xfer#1, to-check=0/1)
sent 127417 bytes received 27 bytes 254888.00 bytes/sec
total size is 641020 speedup is 5.03
2、检查结果:
[root@backup
backup]# tree
.
├── dba
│ └── services
├── dev
│ └── services
└── sa
└── services
说明:
a 目标目录名称后要加上 "/", 表示创建目录,否则变为修改传输文件名称了
b 利用客户端创建服务备份子目录时,只能创建一级子目录。
六、守护进程的访问控制配置
1、在服务端配置文件,编写白名单策略或黑名单策略(只能取其一)
vim /etc/rsyncd.conf
hosts allow = 172.16.1.0/24
#hosts deny = 0.0.0.0/32
关于访问控制的说明:
01. 白名单和黑名单同时存在时,默认控制策略为不匹配的传输数据信息全部放行
02. 白名单单一存在时,默认控制策略为不匹配的传输数据信息全部禁止
03. 黑名单单一存在时,默认控制策略为不匹配的传输数据信息全部放行
04. 全局变量修改控制策略信息,rsync服务必须重启
2、客户端进行测试
[root@nfs01 backup]# rsync -avz /etc/services
rsync_backup@10.0.0.41::data
@ERROR: Unknown module ‘data‘
rsync error: error starting client-server protocol (code 5) at main.c(1503)
[sender=3.0.6]
--------------------------------------------------------------------------------
[root@nfs01 backup]# rsync -avz /etc/services
sync_backup@172.16.1.41::data
sending incremental file list
sent 29 bytes received 8 bytes 74.00 bytes/sec
total size is 641020 speedup is 17324.86
七、守护进程无差异同步配置
1、 什么是无差异:
推模式:我有什么,你就有什么;我没有,你也不能有
拉模式:你有什么,我就有什么;你没有,我也不能有
总结:服务端客户端数据完全一致(一模一样)
2、 实现无差异同步方法
(1)创建实验环境
[root@nfs01
~]# ll /data/
total 16
drwxr-xr-x 2 root root 4096 Oct 12 10:29 a
drwxr-xr-x 2 root root 4096 Oct 12 10:40 b
drwxr-xr-x 2 root root 4096 Oct 12 10:29 c
drwxr-xr-x 2 root root 4096 Oct 12 10:29 d
(2)进行第一次数据同步
[root@nfs01
~]# rsync -avz --delete /data/
rsync_backup@172.16.1.41::backup/
sending incremental file
list
./
a/
a/1.txt
a/2.txt
a/3.txt
b/
b/1.txt
b/2.txt
b/3.txt
c/
c/1.txt
c/2.txt
c/3.txt
d/
d/1.txt
d/2.txt
d/3.txt
sent 669 bytes received 255 bytes 1848.00 bytes/sec
total size is 0 speedup is 0.00
(3)删除指定目录,并添加指定文件,测试无差异功能
#
删除客户端中的 a/ 目录,再进行无差异传输
[root@nfs01 data]# rm a/ -rf
[root@nfs01 data]# rsync -avz --delete
/data/ rsync_backup@172.16.1.41::backup/
sending incremental file list
./
deleting a/3.txt
deleting a/2.txt
deleting a/1.txt
deleting a/
sent 181 bytes received 14 bytes 390.00 bytes/sec
total size is 0 speedup is 0.00
【注意】无差异同步方法的应用
01. 实现储存数据与备份数据完全一致(慎用)
rsync -avz --delete /data/ rsync_backup@172.16.1.41::backup /
02. 快速删除大文件数据
(1)mkdir /null
--创建出一个空目录。
(2)rsync -avz --delete /null/
/bigdata/
# 删除效率高于 rm -rf /bigdata
八、守护进程的列表功能配置
1、在服务端配置文件中开启list列表功能
[root@backup
~]# vim /etc/rsyncd.conf
list = true
2、重启rsync服务
[root@backup ~]# killall rsync && sleep 1 && rsync --daemon
3、客户端查看服务端模块信息
[root@nfs01
data]# rsync rsync_backup@172.16.1.41::
backup "backup dir by
clsn"
nfsdata "nfsdata dir by
clsn"
nfsbackup "nfsbackup dir by
clsn"
说明:
为了提升备份服务器安全性,建议关闭list列表功能
九、常见问题
[root@nfs01
tmp]# rsync -avz /etc/hosts
rsync_backup@172.16.1.41::backup
Password:
sending incremental file list
hosts
rsync: mkstemp ".hosts.U5OCyR" (in backup) failed: Permission denied
(13)
sent 200 bytes received 27 bytes 13.76 bytes/sec
total size is 371 speedup is
1.63
rsync error: some files/attrs were not transferred (see previous errors) (code
23) at main.c(1039) [sender=3.0.6]
说明:备份目录权限设置不正确
解决办法:
将服务端的备份存放目录(path值),属主和属组修改为rsync。
[root@backup ~]# chown -R rsync.rsync /backup/