rsync 是remote sync。

一 rsync基本用法:

1 本地使用rsync


rsync [OPTION...] SRC... [DEST]
 
[root@bl25p-19 rsync]# rsync -zvr out/ out-dest 
sending incremental file list 
a 
b 
c 
options:
 
-z: 压缩
 
-v:verbose
 
-r: indicate recursive

 
主要-rvz 并不保存同步文件的熟悉,如permission等。
 
rsync –a 指定以archive模式同步,保存文件的timestamp等属性。
 
2 远程使用rsync
2.1 使用remote shell 进行传输
拉模式: 
 
rsync [OPTION...] [USER@]HOST:SRC... [DEST]

 
推模式: 
 
rsync [OPTION...] SRC... [USER@]HOST:DEST
 
例子: 从本地备份到remote
 
bl25p-18:~# rsync -avz /root/client/ root@bl25p-19.test:/root/rsync/out-dest 
root@bl25p-19.test's password: 
sending incremental file list 
./ 
Expect-1.20.tar.gz 
IO-Tty-1.07.tar.gz 
Makefile 
bin/ 
bin/cleandisk 
bin/io 
bin/io_ux 
bin/iocards.csv 
bin/pci_slot sent 211030 bytes  received 171 bytes  46933.56 bytes/sec 
total size is 210393  speedup is 1.00例子: 从remote备份到local
 
$ rsync -avz thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/
rpm/Basenames
.
sent 406 bytes  received 15810230 bytes  2432405.54 bytes/sec
total size is 45305958  speedup is 2.87
 

 
注:可以使用-e 来指定使用哪种remote shell来进行网络传输, 如 rsync -avz -e ssh thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp

 
2.1 使用rsync deamon 进行传输
 
拉模式:
 
rsync [OPTION…] [USER@]HOST::SRC …[DEST]
 
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]

 
推模式:
 
rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
 

 
===================================================================================================
 
注意:以下部分翻译自http://www.thegeekstuff.com/2010/09/rsync-command-examples/

 
Example 7. 不覆盖在目的目录中被修改的文件
在一个常见的使用rsync的场景中, 如果在目的目录中的文件被修改而源目录中文件没有被修改,你或许不想使用源目录中没有被修改的文件来覆盖目的目录中新的文件,使用rsync可以如下做:使用rsync –u 选项
如: rsync -avzu thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Example 8. 仅同步目录树结构(而不同步目录里面的文件)
使用rsync –d 选项,可以将源目录下目录结构同步到目的目录当中。
Example 9. 查看rsync的执行过程和进度
如果你使用rsync来做备份,你或许还想了解当前备份的进度,如,当前共有多少个文件正被拷贝,以什么样的速率进行拷贝。你可以使用—progress 选项来完成这个目的。
如:
bl25p-18:~# rsync -avz --progress /root/client/ backer@bl25p-19.test::out-dest 
Password: 
sending incremental file list 
./ 
Expect-1.20.tar.gz 
       53388 100%   50.25MB/s    0:00:00 (xfer#1, to-check=12/14) 
IO-Tty-1.07.tar.gz 
       22004 100%   20.98MB/s    0:00:00 (xfer#2, to-check=11/14) 
Makefile 
         784 100%  765.62kB/s    0:00:00 (xfer#3, to-check=10/14) 
a/ 
a/b/ 
a/b/c/ 
a/b/c/d/ 
bin/ 
bin/cleandisk 
        1377 100%    1.31MB/s    0:00:00 (xfer#4, to-check=4/14) 
bin/io 
       20395 100%   19.45MB/s    0:00:00 (xfer#5, to-check=3/14) 
bin/io_ux 
        6081 100%    2.90MB/s    0:00:00 (xfer#6, to-check=2/14) 
bin/iocards.csv 
      106074 100%   33.72MB/s    0:00:00 (xfer#7, to-check=1/14) 
bin/pci_slot 
         290 100%   94.40kB/s    0:00:00 (xfer#8, to-check=0/14) sent 620 bytes  received 2013 bytes  752.29 bytes/sec 
total size is 210393  speedup is 79.91Example 10. 删除在target目录中创建的,但不在源目录中存在的文件
如果一个文件是在target目录中创建的,但并存在于source目录当中,你或许希望在同步的过程中删除这个文件。
 
# Source and target are in sync. Now creating new file at the target.
$ > new-file.txt

$ rsync -avz --delete thegeekstuff@192.168.200.10:/var/lib/rpm/ .
Password:
receiving file list ... done
deleting new-file.txt
./

sent 26 bytes  received 390 bytes  48.94 bytes/sec
total size is 45305958  speedup is 108908.55

 
Example 11. 不在target目录中创建源目录中新创建的文件,仅更新已有的文件
如果在source目录中新增加了一个文件,但是你仅想更新target目录中已经存在的文件,你可以使用—existing option.
 
rsync -avz --existing root@192.168.1.2:/var/lib/rpm/ .
 
Example 12. View the Changes Between Source and Destination
option: –i 这个选项是用来查看在source目录和target目录中文件或目录之间的不同
 source 目录:
 
$ ls -l /var/lib/rpm
-rw-r--r-- 1 root root  5357568 2010-06-24 08:57 Basenames
-rw-r--r-- 1 root root    12288 2008-05-28 22:03 Conflictname
-rw-r--r-- 1 root root  1179648 2010-06-24 08:57 Dirnames

 
destination 目录:
 
$ ls -l /root/temp
-rw-r--r-- 1 root root    12288 May 28  2008 Conflictname
-rw-r--r-- 1 bin  bin   1179648 Jun 24 05:27 Dirnames
-rw-r--r-- 1 root root        0 Sep  3 06:39 Basenames

 
$ rsync -avzi thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
>f.st.... Basenames
.f....og. Dirnames

sent 48 bytes  received 2182544 bytes  291012.27 bytes/sec
total size is 45305958  speedup is 20.76

 
可以看到,在结果的输出当中,每个不同的文件或目录之前有九个字符
 
> specifies that a file is being transferred to the local host.
f represents that it is a file.
s represents size changes are there.
t represents timestamp changes are there.
o owner changed
g group changed.
 

 
Example 13. Include and Exclude Pattern during File Transfer
 
$ rsync -avz --include 'P*' --exclude '*' thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
./
Packages
Providename
Provideversion
Pubkeys

sent 129 bytes  received 10286798 bytes  2285983.78 bytes/sec
total size is 32768000  speedup is 3.19
 
在这个例子当中,rsync仅同步文件和目录是以P开头的entry。
 

 
Example 14. Do Not Transfer Large Files

 
$ rsync -avz --max-size='100K'
 
max-size 使得rsync仅传输文件的大小小于或者等于100K的文件或目录。
 
Example 15. Transfer the Whole File
 -W选项
rsync的一个特色是可以仅传输source和target中文件中不同的部分,以节省带宽。但是当网络带宽足够充足,可以使用-W选项来进行整个全文件传输,而不是仅同步不同的部分。这样的会加速rsync process的执行速度,因为rsync不需要额外的文件比较工作用以查询出文件的不同部分。
分隔符
======================================================================================================
rsync deamon 配置:
rsyncd.conf 配置文件
secrets file = /etc/rsyncd.secrets #设置用户名和密码 
motd file = /etc/rsyncd.motd #设置rsyncd的message of the day 信息, 
log file = /var/log/rsync.log #rsyncd的日志文件 
read only = no 
list = yes 
uid = root 
gid = root [test] #module 名称 
comment = For your eyes only 
path = /root/rsync/out 
auth users = backer [out-dest] 
comment = For your eyes only 
path = /root/rsync/out-dest 
auth users = backerrsyncd.secrets 配置文件
backer:123456
 这里定义了一个rsync用户backer, 密码是123456
注意:rsyncd.secrets文件的owner必须为root,permission是0600
通过xinetd启动rsyncd:
/etc/xinetd.d/rsync
service rsync 
{ 
    disable = no #yes 则禁用rsync service 
    flags       = IPv6 
    socket_type     = stream 
    wait            = no 
    user            = root 
    server          = /usr/bin/rsync 
    server_args     = --daemon 
    log_on_failure  += USERID 
}然后可以通过service xinetd restart 使得xinetd来启动rsyncd
FAQ:
[1] rsync: chdir /home/test failed : Permission denied (13) 

If I shutdown xinetd and start standalone daemon ( rsync --daemon 
--config /etc/rsyncd.conf) everything works as expected.  
检查SELinux是否配置正确,通过关闭selinux来进行验证
 
setenforce, getenforce 或者查看/etc/selinux/config 来验证


如: 执行getenforce命令,结果是Enforcing 说明当前打开了selinux