格式:
rsync --delete-before -a -H -v --progress --stats
选项:
--delete-before 接收者在传输之前进行删除操作
-progress 在传输时显示传输过程
-a 归档模式 表示以递归方式传输文件,并保持所有文件属性
-r 对子目录以递归方式处理
-H 保持硬连接的文件
-v 详细输出模式
举例说明:
/home/aniya/ 是一个空文件夹【无任何子目录以及文件】,/home/zhaoyj目录下有很多文件,现在要将这个目录下的N多个文件清空
[root@XKWB5705 home]# rsync --delete-before -a -H -v --progress --stats /home/aniya/ /home/zhaoyj
building file list ...
13 files to consider
./
.bash_history
2032 100% 0.00kB/s 0:00:00 (xfer#1, to-check=11/13)
.bash_logout
33 100% 8.06kB/s 0:00:00 (xfer#2, to-check=10/13)
.bash_profile
176 100% 21.48kB/s 0:00:00 (xfer#3, to-check=9/13)
.bashrc
124 100% 15.14kB/s 0:00:00 (xfer#4, to-check=8/13)
.viminfo
557 100% 45.33kB/s 0:00:00 (xfer#5, to-check=7/13)
.mozilla/
.mozilla/extensions/
.mozilla/plugins/
Maildir/
Maildir/cur/
Maildir/new/
Maildir/tmp/
Number of files: 13
Number of files transferred: 5
Total file size: 2922 bytes
Total transferred file size: 2922 bytes
Literal data: 2922 bytes
Matched data: 0 bytes
File list size: 297
File list generation time: 0.022 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 3513
Total bytes received: 178
sent 3513 bytes received 178 bytes 7382.00 bytes/sec
total size is 2922 speedup is 0.79
---------------------------------------------------------------------------------------------------------------------------------------------
当源和目的文件性质不一致的时候会报错的:
[root@XKWB5705 /]# rsync --delete-before -a -H -v --progress --stats /varOLD/ /aniya/usr.tar.gz
building file list ...
1 file to consider
ERROR: cannot overwrite non-directory with a directory
rsync error: errors selecting input/output files, dirs (code 3) at main.c(488) [receiver=2.6.8]
rsync: connection unexpectedly closed (8 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(463) [sender=2.6.8]
[root@XKWB5705 /]# ls -l aniya/
total 1049844
-rw-r--r-- 1 root root 107398 Sep 15 21:09 usr.tar.gz
---------------------------------------------------------------------------------------------------------------------------------------------
haha.txt 是空文件,usr.tar.gz是1.1G的压缩包,现在是把usr.tar.gz的大小由1.1G变成0【即清空文件包】
[root@XKWB5705 /]# touch /varOLD/haha.txt
[root@XKWB5705 /]# rsync --delete-before -a -H -v --progress --stats /varOLD/haha.txt /aniya/usr.tar.gz
building file list ...
1 file to consider
hahah
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
Number of files: 1
Number of files transferred: 1
Total file size: 0 bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 32
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 90
Total bytes received: 42
sent 90 bytes received 42 bytes 88.00 bytes/sec
total size is 0 speedup is 0.00
查看一下usr.tar.gz是否还存在,查看结果是存在的,但是包的大小变为了0
[root@XKWB5705 /]# ls -l /aniya/
total 0
-rw-r--r-- 1 root root 0 Sep 15 21:22 usr.tar.gz
-----------------------------------------------------------------------------------------------------------------------------------------------
现在清空/varOLD下的文件,使其变为空目录:
[root@XKWB5705 /]# rm -rvf /varOLD/hahah
removed `/varOLD/hahah'
[root@XKWB5705 /]# ls /varOLD/
我现在要做的是将/aniya/目录下的所有文件清空【即删除usr.tar.gz文件包】:
[root@XKWB5705 /]# rsync --delete-before -a -H -v --progress --stats /varOLD/ /aniya/
building file list ...
1 file to consider
deleting usr.tar.gz
./
Number of files: 1
Number of files transferred: 0
Total file size: 0 bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 28
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 50
Total bytes received: 26
sent 50 bytes received 26 bytes 152.00 bytes/sec
total size is 0 speedup is 0.00
/aniya/目录已经为空了
[root@XKWB5705 /]# ls /aniya/
[root@XKWB5705 /]# ls /aniya/
从以上结果可以得知:
当SRC和DEST文件性质不一致时将会报错
当SRC和DEST性质都为文件【f】时,意思是清空文件内容而不是删除文件
当SRC和DEST性质都为目录【d】时,意思是删除该目录下的所有文件,使其变为空目录
最重要的是,它的处理速度相当快,处理几个G的文件也就是秒级的事
最核心的内容是:rsync实际上用的就是替换原理
摘自:ANLJF的专栏