Chinaunix首页 | 论坛 | 博客
  • 博客访问: 611285
  • 博文数量: 244
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-27 09:53
个人简介

记录学习,记录成长

文章分类

全部博文(244)

我的朋友

分类: LINUX

2015-12-05 15:30:29

数据镜像备份工具rsync
一.rsync介绍

1.什么是rsync
rsync是Linux/Unix系统下的文件同步和数据传输工具,可通过LAN/WAN快速同步多台主机间的文件,它采用rsync算法使客户机和远程服务器之间的文件同步。运行rsync server的机器也叫backup server,一个rsync server可同时备份多个client的数据;也可以多个rsync server备份一个client的数据。rsync可以搭配ssh甚至使用daemon模式。rsync server会打开一个873的服务通道(port),等待对方rsync连接。连接时,rsync server会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。第一次连通完成时,会把整份文件传输一次,下一次就只传送二个文件之间不同的部份。

2.rsync特性
                可以镜像保存整个目录树和文件系统;

                可以增量同步数据,文件传输效率高,同步时间短;

                可以保持原有文件的权限,时间,所有者等属性;

                同步过程加密传输数据,保证了数据的安全性;

                可以使用rcp,ssh等方式来传输文件,也可以直接通过socket连接传输文件;

                支持匿名传输;

3.rsync的工作模式
3.1  本地shell模式
主要用于同步本地指定目录到另一个目录,如:
[root@localhost ~]# rsync -av /mnt /media/
sending incremental file list
mnt/
mnt/hgfs/
sent 57 bytes  received 20 bytes  154.00 bytes/sec
total size is 0  speedup is 0.00
查看:
[root@localhost media]# ls
mnt

[root@localhost media]# rm -rf *
[root@localhost ~]# rsync -av /mnt/ /media/
sending incremental file list
./
hgfs/
sent 49 bytes  received 19 bytes  136.00 bytes/sec
total size is 0  speedup is 0.00
再次查看:
[root@localhost media]# ls
hgfs
这两个结果的差异在于源参数末尾的斜杠,如果源参数末尾没有斜杠,就将指定的源目录复制到指定的目的目录中;如果有斜杠,就会复制指定源目录中的内容到目的目录中,而不会复制目录本身;
注意:
            删除/mnt/下的文件不会同步删除/media/中的,除非加入--delete选项

            文件访问时间等属性、读写等权限、文件内容等有任何变动,都会被认为修改

            目标目录下如果文件比源目录还新,则不会同步

3.2  远程shell模式
可以把指定的本地目录同步到远程主机中,如:
[root@localhost ~]# rsync -av /mnt 192.168.85.143:/media/
The authenticity of host '192.168.85.143 (192.168.85.143)' can't be established.
RSA key fingerprint is 25:dd:87:d0:07:79:7e:56:05:e9:81:03:e2:10:46:b8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.85.143' (RSA) to the list of known hosts.
root@192.168.85.143's password: 
sending incremental file list
mnt/
mnt/hgfs/
sent 57 bytes  received 20 bytes  3.76 bytes/sec
total size is 0  speedup is 0.00

验证:
[root@localhost media]# ll
total 4
drwxr-xr-x 3 root root 4096 Oct 29 20:22 mnt

3.3  列表模式
在此模式下,rsync与ls命令有相似的功能,如:
[root@localhost mnt]# rsync -a /mnt/
drwxr-xr-x        4096 2015/12/04 20:19:13 .
drwxr-xr-x        4096 2015/10/29 20:22:14 hgfs

[root@localhost mnt]# rsync -a 192.168.85.143:/media/
root@192.168.85.143's password: 
drwxr-xr-x        4096 2015/12/04 20:15:15 .
drwxr-xr-x        4096 2015/12/04 20:16:43 mnt
-rw-r--r--           0 2015/12/04 20:16:25 mnt/a.txt
drwxr-xr-x        4096 2015/10/29 20:22:14 mnt/hgfs

3.4  服务器模式
这种模式时基于C/S模式的,这种模式下,rsync在后台启用一个守护进程,这个守护进程在rsync服务器端一直运行,用于接收文件传输请求,因此客户端可以把文件发送给守护进程,也可以向守护进程请求文件,以下是其示例;

二.rsync配置
rsync安装:在centos6上已经默认安装了rsync,如果没有可以yum install rsync或者编译安装,这里使用已经安装好的;

1.环境
服务器端:192.168.85.128+rsync
客户端:192.168.85.143+rsync

2.在服务器端配置rsync
rsync的配置文件为/etc/rsync.conf但是安装后没有提供这个文件,需要手动建立,该文件由多个模块构成,包括全局参数和模块参数,模块参数以[]号为标识;
详细配置文件语法可以看这里:

例如:
[root@localhost ~]# cat /etc/rsync.conf
uid=root
gid=root
use chroot=no
max connections=5
strict modes=yes
port=873
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log

[test]
path=/test
comment=This is rsync test
ignore errors
read only=no
write only=no
hosts allow=192.168.85.143
hosts deny=*
list=no
uid=root
gid=root
auth users=backup
secrets file=/etc/rsync_server.passwd

其中:
uid:指定当该模块传输文件时守护进程应该具有的用户id,默认是系统的nobody用户;

gid:指定当该模块传输文件时守护进程应该具有的用户组id,默认是系统的nobody用户;

use chroot:是否启用chroot功能;

max connections:指定模块的最大并发连接数,超过限制的连接请求将被暂时限制,默认值为0表示无限制;

strict modes:是否检查口令文件的权限,默认检查,除了运行rsync进程的用户,其他任何用户都不可读;

port:进程监听的端口,默认为873;

pid file:指定rsync进程对应的pid文件,如果文件已经存在,rsync守护进程将终止,而不是覆盖该文件。

lock file:指定支持max connections的锁文件,确保不会超过最大连接数,默认为/var/run/rsyncd.lock

log file:指定rsync的日志输出文件;

[test]:定义一个test模块;

path:指定需要备份的文件或目录,必须要设置;

comment:定义模块的描述;

ignore errors:可以忽略一些无关的I/O错误;

read only:指定客户端是否可以上传文件,yes表示不能上传,no表示如果守护进程允许就可以上传文件;
注意:Note that "auth users" can override this setting on a per-user basis.

write only:制定客户端是否可以下载文件,yes表示不能下载,no表示如果守护进程允许就可以下载文件;

hosts allow:设置可以连接rsync服务器的主机,*表示允许任何主机连接;

hosts deny:设置禁止连接rsync服务器的主机地址;

list:设置当客户端请求可以使用的模块列表时,该列表是否被列出。默认为true,如果需要建立隐藏的模块,设置为yes

auth users:定义可以连接该模块的用户,多个用户之间用空格或逗号隔开。要注意的是,这里的用户和系统用户没有任何关系,

secrets file:指定一个包含  用户名:密码 格式的文件,用户名是auth users定义的用户,密码可以任意指定,只要和客户端的secrets file文件对应起来即可。只有auth users被定义时,该文件才起作用,文件权限为600;

密码文件为:
[root@localhost ~]# cat /etc/rsync_server.passwd
backup:redhat
[root@localhost ~]# ll /etc/rsync_server.passwd
-rw------- 1 root root 14 Dec  4 20:56 /etc/rsync_server.passwd

各个字段说明可以借鉴这里:http://blog.chinaunix.net/uid-10362953-id-2961174.html

3.在服务器端启动服务
修改/etc/xinet.d/rsync文件中的dsiable = yes为no,执行service xinetd restart后会一起重启rsync后台进程,默认使用配置文件/etc/rsyncd.conf。也可以使用/usr/bin/rsync --daemon --config=/etc/rsyncd.conf指定配置文件

[root@localhost ~]# cat /etc/xinetd.d/rsync
service rsync
{
        disable = no
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
查看是否启动:
[root@server test]# netstat -ntlp | grep :873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      5406/rsync          
tcp        0      0 :::873                      :::*                        LISTEN      5406/rsync          

4.  客户端中提供密码文件
[root@client ~]# cat /etc/rsync_server.passwd 
redhat

[root@client ~]# chmod 600 /etc/rsync_server.passwd

5.  备份测试
选项说明:
v或--verbose:详细模式输出;

z或--compress:在出传输时对备份的文件进行压缩处理;

r或--recursive:对子目录以递归方式出处理;

t或times:保持文件时间信息;

o或--owner:保持文件属主信息;

p或--perms:保持文件权限;

g或--group:保持文件属组信息;

--delete:指定以rsync服务器端为基准进行数据同步,即保持rsync服务器端目录与客户端目录的完全一致;

--password-file:指定客户端上存放密码文件的位置,使同步时不需要输入密码;
更多选项请man rsync

5.1  将服务器端文件同步到本地(备份):
[root@client media]# rsync -vzrtopg --delete backup@192.168.85.128::test /media --password-file=/etc/rsync_server.passwd 
receiving incremental file list
./
a.txt
b.txt
c.txt
sent 116 bytes  received 281 bytes  794.00 bytes/sec
total size is 18  speedup is 0.05
[root@client media]# ll
total 12
-rw-r--r-- 1 root root 6 Dec  5 14:33 a.txt
-rw-r--r-- 1 root root 6 Dec  5 14:34 b.txt
-rw-r--r-- 1 root root 6 Dec  5 14:34 c.txt

5.2  将本地文件同步到服务器端(恢复)
[root@client media]# rsync -vzrtopg --delete /mnt/ backup@192.168.85.128::test --password-file=/etc/rsync_server.passwd 
sending incremental file list
./
deleting c.txt
deleting b.txt
deleting a.txt
hgfs/
sent 49 bytes  received 15 bytes  128.00 bytes/sec
total size is 0  speedup is 0.00

[root@server test]# ls
hgfs

从上面两个命令可以看到,其实这里的服务器与客户端的概念是很模糊的,rsync daemon都运行在远程192.168.85.128上,第一条命令是本地(客户端)主动向远程(服务器)索取文件,本地用来备份;第二条命令是本地主动推送目录到远程,远程用来备份的。

6.设置定时备份策略
客户端在同步数据后,如果服务器端有数据更新,那么还需要执行命令手动同步,而客户端不会自动同步,因此可以设置一个定时任务去让客户端自动同步;

如:每天凌晨2点33分将客户端数据备份到服务器端
[root@client media]# crontab -e

33 2 * * * /usr/bin/rsync -vzrtopg --delete --progress /www/ back@192.168.85.128::test --password-file=/etc/rs
ync_server.passwd

关于备份示例:

7.  rsync的优点与不足
与传统的cp tar备份方式相比,rsync具有安全性高,备份迅速,支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据进行备份;

但是rsync在备份数据时,需要扫描所有文件后进行对比,然后进行差量传输,如果文件数量多达百万或者千万,扫描所有文件是非常耗时的,而且有时发生变化的只是其中很少的一部分,因此rsync非常低效。其次,rsync不能实时监测,同步数据,无法在出现故障时完全恢复数据。基于以上原因,可以使用rsync+inotify组合来实现数据实时备份。
阅读(731) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~