一、软件简介
rsync是Linux系统下的数据镜像备份工具,从软件的命名上就可以看出来了——remote sync。rsync支持大多数的类Unix系统,无论是Linux、Solaris还是BSD上都经过了良好的测试。它的特性如下: 1、可以镜像保存整个目录树和文件系统。
2、可以很容易做到保持原来文件的权限、时间、软硬链接等等。 3、无须特殊权限即可安装。
4、优化的流程,文件传输效率高。
5、可以使用rcp、ssh等方式来传输文件,当然也可以通过直接的socket连接。 本文介绍了如何使用rsync服务从Linux到Windows和linux进行远程备份
二、安装软件
rysnc的主页地址为:http://rsync.samba.org/,可以从这里下载rsync的安装软件。 编译安装
rsync的编译安装非常简单,只需要以下简单的几步(在两台服务器中都要安装):
1 2 3 4 5 | $ tar xvf rsync -2.6.3.tgz
$ cd rsync -2.6.3
$ . /configure
$ make
$ make install
|
//如果是已安装了RPM版的Rsync,并且要安装新版的Rsync,需要先删除Rsync #rpm -e rsync
(Redhat会有一个包依赖rsync,可以使用rpm -e rsync --nodeps卸载)
另外,RPM版的rsync带了一个服务端的启动脚本,/etc/xinetd.d/rsync可以保留
但是需要注意的是 :必须在主服务器和同步服务器上都安装rsync,其中主服务器上是以服务器模式运行rsync,而同步上则以客户端方式运行rsync。这样在主服务器上运行rsyn守护进程,在同步上用crond定时运行客户程序来同步主服务器上需要同步的内容
三、配置rsync服务器端
rsync服务器的配置文件为/etc/rsyncd.conf,其控制认证、访问、日志记录等等。
该文件是由一个或多个模块结构组成。一个模块定义以方括弧中的模块名开始,直到下一个模块定义开始或者文件结束,模块中包含格式为name=value的参数定义。每个模块其实就对应需要备份的一个目录树,比方说在我们的实例环境中,有三个目录树需要备份:/www/和/mirror/file0/和/mirror/file1/目录,那么就需要在配置文件中定义三个模块,分别对应三个目录树。配置文件是行为单位的,也就是每个新行都表示一个新的注释、模块定义或者参数赋值。
1 、配置rsync server
1 2 3 4 5 6 7 8 9 10 11 12 | vi /etc/xinetd .d /rsync
将disable= yes 改为no
service rsync {
disable = yes
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
|
确保etc/services中rsync端口号正确
1 2 3 4 5 6 | [root@ test etc]
rsync 873 /tcp
rsync 873 /udp
[root@ test etc]
schoo:scihoo
|
配置rsync密码文件权限
1 2 | [root@ test home]
[root@ test home]
|
启动配置
1 2 | [root@ test home]
[root@ test home]
|
RSYNC服务端启动的几种方法
1,以xinetd为守护进程启动 /etc/init.d/xinetd restart
2,启动rsync服务端(独立启动) [root@test home]# /usr/bin/rsync --daemon
3,开机启动
加入rc.local,可以修改使系统启动时把rsync --daemon加载进去。
1 2 | vi /etc/rc . local
/usr/local/rsync –daemon
|
重新载入rsync配置
检查rsync是否启动
1 2 3 | [root@ test home]
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
xinetd 4396 root 5u IPv4 633387 TCP *: rsync LISTEN)
|
=========================================================================================
rsync 服务器端配置文件
例子:
手动创建rsync的配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 | vi /etc/rsyncd .conf(前面以
uid = nobody
gid = nobody
use chroot = no 建议设置成只能读取你要备份的目录和文件即可
max connections = 0
port = 873
pid file = /var/log/rsync/rsyncd .pid
lock file = /var/log/rsync/rsync .lock
log file = /var/log/rsync/rsyncd .log
motd file = /var/log/rsync/rsyncd .motd
strict modes = yes
|
上面是全局配置文件:
下面模块自己定义:
1 2 3 4 5 6 7 8 9 10 11 | [attachment]
path = /www/htdocs/pub/attachment/
comment = BACKUP attachment
ignore errors
read only = yes
list = false
auth users = msyn
secrets file = /etc/rsyncd .scrt
|
[root@www /etc]# cat /etc/rsyncd.scrt
www:xxxxxxxxx
注意:该文件只能是root用户可读写的,注意,出于安全目的,这个文件的属性必需是只有属主可读,否则rsync将拒绝运行。
我们可以设置它的属性为600:
[root@www /etc]# chmod 600 /etc/rsyncd.scrt
这些文件配置完毕以后,就需要在主服务器上启动rsyncd服务:
[root@www /etc]# /usr/local/bin/rsync --daemon
执行完毕上面的命令后,rsync即可启动。
rsync默认服务端口为873,服务器在该端口接收客户的匿名或者认证方式的备份请求。
四、配置客户端
1、配置密码文件 (注:为了安全,设定密码档案的属性为:600。rsync.scrt的密码一定要和Rsync Server
密码设定案里的密码一样)
1 2 3 4 | [root@aj1 home]
123456
[root@aj1 home]
[root@aj1 home]
|
2. linux下执行rsync客户端命令
下一步就要运行rsync客户端的启动命令了:
[backup@backup /]#/usr/bin/rsync -vzrltogp --progress --delete user@x.x.x.168::attachment /usr/local/apache/htdocs/pub/html/ --password-file=/etc/rsync.scrt
上面这个命令行中-vzrtopg代表的意思是: -v表示verbose(详细) -z表示zip(压缩)
-r表示recursive(递归目录)
-topg是保持文件原有属性如o=owner(属主)、t=times(时间)、p=perms(权限)、g=group(组)的参数 -H表示保持硬连接
-l:保留软链结
-u:表示只更新源文件比目标时间新的文件
--progress是指显示出详细的进度情况
--delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致。user@x.x.x.168::attachment 是表示对服务器x.x.x.168中的attachment模块进行备份,登录帐号是user --password-file = /etc/rsync.scrt来指定密码文件,这里需要注意的是这份密码文件权限属性要设得只有属主可读(权限为600),属主为正在运行此命令的用户。
-a:(-a, --archive equivalent to -rlptgoD)以archive模式操作,复制目录、符号连接在这里面
========================================================================================
扩展知识:也是重点
1、Rsync的命令格式可以为以下六种:
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
rsync [OPTION]... [USER@]HOST::SRC DEST
rsync [OPTION]... SRC [USER@]HOST::DEST
rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
rsync参数的具体解释如下:
-v, --verbose 详细模式输出
-q, --quiet 精简输出模式
-c, --checksum 打开校验开关,强制对文件传输进行校验
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive 对子目录以递归模式处理
-R, --relative 使用相对路径信息
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
--backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, --links 保留软链结
-L, --copy-links 想对待常规文件一样处理软链结
--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
--safe-links 忽略指向SRC路径目录树以外的链结
-H, --hard-links 保留硬链结
-p, --perms 保持文件权限
-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-D, --devices 保持设备文件信息
-t, --times 保持文件时间信息
-S, --sparse 对稀疏文件进行特殊处理以节省DST的空间
-n, --dry-run现实哪些文件将被传输
-W, --whole-file 拷贝文件,不进行增量检测
-x, --one-file-system 不要跨越文件系统边界
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
--existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
--delete 删除那些DST中SRC没有的文件
--delete-excluded 同样删除接收端那些被该选项指定排除的文件
--delete-after 传输结束以后再删除
--ignore-errors 及时出现IO错误也进行删除
--max-delete=NUM 最多删除NUM个文件
--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
--force 强制删除目录,即使不为空
--numeric-ids 不将数字的用户和组ID匹配为用户名和组名
--timeout=TIME IP超时时间,单位为秒
-I, --ignore-times 不跳过那些有同样的时间和长度的文件
--size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
--modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
-T --temp-dir=DIR 在DIR中创建临时文件
--compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
-P 等同于 --partial
--progress 显示备份过程
-z, --compress 对备份的文件在传输时进行压缩处理
--exclude=PATTERN 指定排除不需要传输的文件模式
--include=PATTERN 指定不排除而需要传输的文件模式
--exclude-from=FILE 排除FILE中指定模式的文件
--include-from=FILE 不排除FILE指定模式匹配的文件
--version 打印版本信息
--address 绑定到特定的地址
--config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
--port=PORT 指定其他的rsync服务端口
--blocking-io 对远程shell使用阻塞IO
-stats 给出某些文件的传输状态
--progress 在传输时现实传输过程
--log-format=formAT 指定日志文件格式
--password-file=FILE 从FILE中得到密码
--bwlimit=KBPS 限制I/O带宽,KBytes per second
-h, --help 显示帮助信息
一般都使用azv选项
2、常用为以下两种:以客户端为标杆,俗称,下载,推送
(1)下载、rsync [OPTION]... [USER@]HOST::SRC DEST
从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。
如:rsync -vzrlpotgt user@172.16.78.192::www /databack
www 为在服务器端定义的模块名字 /databack 为备份到本地的路径
(2)推送、rsync [OPTION]... SRC [USER@]HOST::DEST
从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。
如:rsync -av /databack root@172.16.78.192::www
3、下面为实例:
服务器ip为192.168.8.126,客户端ip为192.168.8.122
(1)、把服务器上的/home/hyj/workspace/test文件夹中的内容备份到客户端,也就是客户端的下载模式的/usr/local/share/rsync_backup中:
在192.168.8.122客户端中如下操作:
192.168.8.126 服务器端的
1 2 3 4 5 6 7 8 9 | /etc/rsyncd .conf中模块的内容
[ test ]
path = /home/hyj/workspace/test --服务器端的路径
comment = rsync files
ignore errors
read only = yes
list = no
auth users = rsync
secrets file = /etc/rsync . passwd
|
这一文件,那么客户端也相应把文件删除,保持真正的一致。
(2)、上面的命令需要在备份的时候需要输入密码,可以在客户端建立一个密码文件,在命令中把密码文件作为参数带入:
[root@hyj rsync_backup]# vim /etc/rsync.pass
密码文件中不用输入用户名,只需输入密码即可:
这份密码文件权限属性要设得只有root可读,不然会报错,修改属性:
[root@hyj rsync_backup]# chmod 600 /etc/rsync.pass,
用下面这条命令,可以不输入密码:
1 | /usr/bin/rsync -vzrtopg --delete --progress rsync @192.168.8.126:: test /usr/local/share/rsync_backup --password- file = /etc/rsync .pass
|
4、 带exclude 参数:
把服务器上的/home/hyj/workspace/test文件夹中的内容备份到客户端的/usr/local/share/rsync_backup中,但不包括:res目录和default.properties文件:
1 | /usr/bin/rsync -vzrtopg --delete --exclude "res/" --exclude "default.properties" --progress rsync @192.168.8.126:: test /usr/local/share/rsync_backup --password- file = /etc/rsync .pass
|
exclude/include规则实例
1 2 3 4 5 6 7 8 | Here are some exclude/include examples:
--exclude "*.o" would exclude all filenames matching *.o
--exclude "/foo" would exclude a file in the base directory called foo
--exclude "foo/" would exclude any directory called foo.
--exclude "/foobar" would exclude any file called bar two or more levels below a base directory called foo.
--include "*/" --include "*.c" --exclude "*" would include all directories and C source files
--include "foo/" --include "foo/bar.c" --exclude "*" would include only foo/bar.c
(the foo/ directory must be explicitly included or it would be excluded by the "*")
|
5、 把客户端上的/home/hyj/vitest文件夹中的内容备份到(客户端的推送模式)服务器的/usr/local/share/rsync_backup中,在客户端执行如下命令:
相当于从客户端上传到服务器端
1 | /usr/bin/rsync -vzrtopg --delete --progress /home/hyj/vitest rsync @192.168.8.126::clientdata --password-
请您注册登录超级码客,加载全部码客文章内容... |
|