这篇文章是在windows live writer 上面写的,排版有些不好,博友看的时候体谅下~~ 嘻嘻~

======================================================================================
rsync命令是初学者应该掌握的
首先,应该知道
rsync是类unix系统下的数据镜像备份工具,从软件的命名上就可以看出来了——remote sync
那么,它的特性有哪些呢?
1.可以镜像保存整个目录树和文件系统。
2.可以很容易做到保持原来文件的权限、时间、软硬链接等等。
3.无须特殊权限即可安装。
4.快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
5.安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
6.支持匿名传输,以方便进行网站镜象。

牢记以上特性之后,我们再来一步一步的剖析它……

-----------------------------------------------------------------------------------------

第一,它可以使用在本地单机环境当中。就相当于cp、mv、rm等命令一样。
来看看实例:

[ lsp@ 59CentOS ~ ]$ alias sl
alias sl='ls -l'
[ lsp@ 59CentOS ~ ]$sl dir1           -----》列出dir1目录下的内容
total 12
-rw-r--r-- 1 lsp lsp 0 Sep 27 04:08 a
-rw-r--r-- 1 lsp lsp 0 Sep 27 04:08 b
-rw-r--r-- 1 lsp lsp 0 Sep 27 04:08 c
[ lsp@ 59CentOS ~ ]$sl dir2           -----》列出dir2目录下的内容
total 8
-rw-r--r-- 1 lsp lsp 0 Sep 27 03:07 e
-rw-r--r-- 1 lsp lsp 0 Sep 27 03:07 f
[ lsp@ 59CentOS ~ ]$rsync -avz dir1 dir2   --->使dir1目录整体复制到dir2目录下面,相当于cp
sending incremental file list
dir1/
dir1/a
dir1/b
dir1/c
sent 185 bytes  received 73 bytes  516.00 bytes/sec
total size is 0  speedup is 0.00
[ lsp@ 59CentOS ~ ]$sl dir2            ---》再次列出dir2目录下的内容。可以发现,dir1目录全都被cp过来了
total 16
drwxr-xr-x 2 lsp lsp 4096 Sep 27 04:08 dir1
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 e
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 f

注意:如果目录后面不加/ ,那么就表示将整个目录cp过去。

[ lsp@ 59CentOS ~ ]$rsync -avz dir1/ dir2      ---》将dir1/目录下的所有文件,复制到dir2目录下。如果有相同的文件,则以dir1里面的为准。
sending incremental file list
./
a
b
c
sent 170 bytes  received 72 bytes  484.00 bytes/sec
total size is 0  speedup is 0.00
[ lsp@ 59CentOS ~ ]$sl dir2
total 28
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 a
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 b
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 c
drwxr-xr-x 2 lsp lsp 4096 Sep 27 04:08 dir1
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 e
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 f


注意:如果目录后面加/ ,那么就表示将目录下的所有文件cp过去。

[ lsp@ 59CentOS ~ ]$rsync -avz dir1 dir2/dir3   ---》将dir1目录,整体拷贝到dir3下面
sending incremental file list
created directory dir2/dir3
dir1/
dir1/a
dir1/b
dir1/c
sent 185 bytes  received 73 bytes  516.00 bytes/sec
total size is 0  speedup is 0.00
[ lsp@ 59CentOS ~ ]$sl dir2
total 36
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 a
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 b
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 c
drwxr-xr-x 2 lsp lsp 4096 Sep 27 04:08 dir1
drwxr-xr-x 3 lsp lsp 4096 Sep 27 04:16 dir3
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 e
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 f
[ lsp@ 59CentOS ~ ]$sl dir2/dir3
total 8
drwxr-xr-x 2 lsp lsp 4096 Sep 27 04:08 dir1

注意:我的dir2下面并没有dir3目录,它竟然自己创建了一个。

[ lsp@ 59CentOS ~ ]$rsync -avz dir1/a dir2/i   --->将dir1/a文件,拷贝到dir2/目录下,并重命名为i文件,内容和a文件一样。
sending incremental file list
a
sent 73 bytes  received 31 bytes  208.00 bytes/sec
total size is 0  speedup is 0.00
[ lsp@ 59CentOS ~ ]$sl dir2
total 40
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 a
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 b
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 c
drwxr-xr-x 2 lsp lsp 4096 Sep 27 04:08 dir1
drwxr-xr-x 3 lsp lsp 4096 Sep 27 04:16 dir3
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 e
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 f
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 i
[ lsp@ 59CentOS ~ ]$echo "ooooooo">>dir1/a
[ lsp@ 59CentOS ~ ]$rsync -avz dir1/a dir2/p
sending incremental file list
a
sent 82 bytes  received 31 bytes  226.00 bytes/sec
total size is 8  speedup is 0.07
[ lsp@ 59CentOS ~ ]$sl dir2
total 52
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 a
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 b
-rw-r--r-- 1 lsp lsp    0 Sep 27 04:08 c
drwxr-xr-x 2 lsp lsp 4096 Sep 27 04:08 dir1
drwxr-xr-x 3 lsp lsp 4096 Sep 27 04:16 dir3
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 e
-rw-r--r-- 1 lsp lsp    0 Sep 27 03:07 f
-rw-r--r-- 1 lsp lsp    8 Sep 27 04:17 i
-rw-r--r-- 1 lsp lsp    8 Sep 27 04:17 p
[ lsp@ 59CentOS ~ ]$nl dir2/p
     1  ooooooo


所有的格式,都是以源目录里面的文件为根源,如果目标目录下没有源目录里面的子目录或者文件,则将镜像出一份和源目录相同的文件或者目录;
如果目标目录下有和源目录下相同的文件,则以源目录为标准。多则删,少则添。

[root@Jason64-18 lspgyy]# tree
.
├── dir1
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   └── 5.txt
├── dir2
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
└── dir3
3 directories, 8 files
[root@Jason64-18 lspgyy]# rsync -avz --delete dir3 dir2
sending incremental file list
dir3/
sent 40 bytes  received 16 bytes  112.00 bytes/sec
total size is 0  speedup is 0.00
[root@Jason64-18 lspgyy]# tree
.
├── dir1
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   └── 5.txt
├── dir2
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   └── dir3
└── dir3
4 directories, 8 files
[root@Jason64-18 lspgyy]# rsync -avz --delete dir3/ dir2
sending incremental file list
./
deleting dir3/
deleting 3.txt
deleting 2.txt
deleting 1.txt
sent 29 bytes  received 15 bytes  88.00 bytes/sec
total size is 0  speedup is 0.00
[root@Jason64-18 lspgyy]# tree
.
├── dir1
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   └── 5.txt
├── dir2
└── dir3
3 directories, 5 files

可以看出来,如果想要利用长选项--delete,来删除或者是镜像文件,那必须后面是接的是文件。而不是目录。

[root@Jason64-18 lspgyy]# tree
.
├── dir1
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   └── 5.txt
├── dir2
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
└── dir3
3 directories, 8 files
[root@Jason64-18 lspgyy]# rsync -avz --delete dir2/ dir1
sending incremental file list
./
deleting 5.txt
deleting 4.txt
1.txt
2.txt
3.txt
sent 171 bytes  received 72 bytes  486.00 bytes/sec
total size is 0  speedup is 0.00
[root@Jason64-18 lspgyy]# tree
.
├── dir1
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
├── dir2
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
└── dir3
3 directories, 6 files

这样就可以,因为你后面是文件。所以,这个命令就是在dir3下面镜像/dri2下面的文件。如果dir3里面有多余的,则删掉。

那接下来看看对于文件的内容会不会有变化。

[root@Jason64-18 lspgyy]# tree
.
├── dir1
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
├── dir2
│   └── 3.txt
└── dir3
3 directories, 4 files
[root@Jason64-18 lspgyy]# cat dir1/1.txt
/lspgyy/dir1
[root@Jason64-18 lspgyy]# cat dir2/3.txt
/lspgyy/dir2
/lspgyy/dir2
[root@Jason64-18 lspgyy]# rsync -avz --delete dir1/1.txt dir2/3.txt
sending incremental file list
1.txt
sent 83 bytes  received 31 bytes  228.00 bytes/sec
total size is 13  speedup is 0.11
[root@Jason64-18 lspgyy]# cat dir2/3.txt                  
/lspgyy/dir1

可以看出来,dir2/3.txt文件里面的内容已经被改为和dir1/1.txt的内容一模一样了。

所以,可以断定,rsync是镜像目录和文件,包括文件内容的一个非常好的工具。

----------------------------------------------------------------------
第二,使用ssh ,rsh等通道来远程传输文件
来看看实例:

[root@Jason64-16 ~]# rsync -avz -e "ssh -p 22" lsp@10.0.0.123:/tmp/* /root/sdb5/mtp
receiving incremental file list
created directory /root/sdb5/mtp
rsync: opendir "/tmp/ssh-htajkG3141" failed: Permission denied (13)
rsync: opendir "/tmp/ssh-otRApl3239" failed: Permission denied (13)
1.txt
10.txt
100.txt
11.txt
12.txt
13.txt
14.txt
15.txt
16.txt
……
98.txt
99.txt
lsp
rsync: send_files failed to open "/tmp/u2dtmpS46uG3": Permission denied (13)
ssh-htajkG3141/    -->由于权限问题,某些目录普通用户不能对其操作,所以。
ssh-otRApl3239/    -->这里可以看见,提示只是复制了一个目录名过来。
sent 1957 bytes  received 5118 bytes  4716.67 bytes/sec
total size is 6  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1505) [generator=3.0.6]



rsync借助ssh通道来实现文件传输的比较少,一般都是使用它独特的运行方式,也就是守护进程的形式来进行文件传输。
所以,利用ssh通道的例子我们就简单的介绍一种,其他的都是照猫画虎,没啥大区别。
那么接下来,我们就介绍rsync的守护进程。

-----------------------------------------------------------------------
第三,守护进程方式来进行文件传输
1.服务端配置vi /etc/rsyncd.conf

[root@Jason64-17 ~]#cat /etc/rsyncd.conf
#rsync_config_______________start
#first release created by oldboy
#QQ 49000448  blog:http://oldboy.blog.51cto.com
#modification by lisp 2013年9月26日 20:15:44
#QQ 1031239088 blog:http://lspgyy.blog.51cto.com
##rsyncd.conf -------------------start##
#这是rsync以什么用户来读取本地的数据;
uid = root
gid = root
#是否可以切换路径
use chroot = no
#最大连接数是两百;
max connections = 200
#连接三百秒超时;
timeout = 300
#pid文件;
pid file = /var/run/rsyncd.pid
#锁文件;
lock file = /var/run/rsync.lock
#日志文件;
log file = /var/log/rsyncd.log
hosts allow = 10.0.0.0/24
hosts deny = 0.0.0.0/32
# #认证的虚拟用户;可以任意起名;
auth users = rsync_virtual
# #rsync认证的密钥文件;
secrets file = /etc/rsync.password
#模块;
[bak]
##路径,相当于NFS共享的目录一样;
path = /bak/
##忽略错误;
ignore errors
# #只读为假,可写;
read only = false
# #只读为假,可写;
list = false
#添加多个模块
[自定义]
path = /自定义/
##忽略错误;
ignore errors
# #只读为假,可写;
read only = false
# #只读为假,可写;
list = false
###rsyncd.conf -------------------end##


密码认证文件

cat /etc/rsync.password
rsync.virtual:lisp


2.客户端配置

cat /etc/rsync.password
lisp


=============
来看看实例:
=============
服务端配置/etc/rsyncd.conf和/etc/rsync.password
客户端配置/etc/rsync.password
服务端
启动rsync进程rsync --daemon
查看rsync进程ps -ef |grep rsync
查看rsync端口netstat -lnt |grep :873
杀掉rsync进程pkill rsync
根据rsync端口号反查进程lsof -i :873
创建/etc/rsync.password 并且授权600(虚拟用户名和密码用:隔开)
客户端
创建/etc/rsync.password 并且授权600(只需用写入密码就ok)
==========================
服务端启动rsync守护进程
==========================

[root@Jason64-16 bak]# pkill rsync
[root@Jason64-16 bak]# ps -ef |grep rsync
root      1943  1675  0 13:30 pts/0    00:00:00 grep --color=auto rsync
[root@Jason64-16 bak]# rsync --daemon
[root@Jason64-16 bak]# ps -ef |grep rsync
root      1945     1  0 13:30 ?        00:00:00 rsync --daemon
root      1947  1675  0 13:30 pts/0    00:00:00 grep --color=auto rsync
[root@Jason64-16 bak]# netstat -lnt |grep 873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN
tcp        0      0 :::873

==========================
客户端执行rsync
==========================

[ lsp@ 59CentOS ~ ]$rsync -avz /tmp/testdir/* rsync_virtual@10.0.0.16::bak --password-file=/etc/rsync.passwd
sending incremental file list
1
10
100
1000
101
102
103
104
105
106
107
108
109
11
110
111
……
……
sent 43007 bytes  received 19008 bytes  41343.33 bytes/sec
total size is 0  speedup is 0.00


=============================================
服务端查看bak模块下指定path路径下目录的改变情况
=============================================

[root@Jason64-16 bak]# ls |wc -l
1000
[root@Jason64-16 bak]# pwd
/bak

#可以看见将客户端/tmp/testdir 目录下1000个文件全部备份到/bak目录下
================================================================
————————————————————————————
服务器可以指定多个模块来使用
————————————————————————————

[root@Jason64-16 bak]# cat /etc/rsyncd.conf
……omitting……
[bak]
#模块;
path = /bak/
##路径,相当于NFS共享的目录一样;
ignore errors
##忽略错误;
read only = false
# #只读为假,可写;
list = false
[lisp]
path = /testdir/llsp
ignore errors
read only = false
list = false
……omitting……
#添加了一个模块lisp

————————————————————————————————————————————————————
测试添加的模块是否成功,那么首先要在服务器创建/testdir/llsp

mkdir /testdir/llsp

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
客户端执行测试
-------------

[root@Jason64-17 ~]# rsync -avz /testdir/ rsync_virtual@10.0.0.16::lisp --password-file=/etc/rsync.passwd
sending incremental file list
./
test01.txt
test02.txt
test03.txt
test05.txt
test06.txt
sent 359 bytes  received 106 bytes  930.00 bytes/sec
total size is 20  speedup is 0.04
[root@Jason64-17 ~]# rsync -avz /testdir/ rsync_virtual@10.0.0.16::bak --password-file=/etc/rsync.passwd
sending incremental file list
sent 152 bytes  received 8 bytes  320.00 bytes/sec
total size is 20  speedup is 0.12

成功执行!
注意!rsync的三种模式一般最常用的就是守护进程方式!以后还会涉及到/etc/xinetd.d/rsync 来启动rsync的守护进程。

当然,rsync的用法,远远不止这些。它用在数据同步的时候是个很好的工具。不过它也有不好的地方。

rsync的优点与不足:

优点: 与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
缺点: 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。