使用rsync 的 --delete参数删除目标目录比源目录多余的文件
root@v01 ~]# mkdir dir01 dir02 [root@v01 ~]# ls anaconda-ks.cfg dir02 framework install.log.syslog mobile dir01 ecshop install.log localsvn online
[root@v01 ~]# touch dir02/{file1A.txt,fileA2.txt,fileA3.txt,fileB1.txt,fileB2.txt,fileB3.txt} [root@v01 ~]# touch dir01/{file1A.txt,fileA2.txt,fileA3.txt}
[root@v01 ~]# ls dir01 file1A.txt fileA2.txt fileA3.txt [root@v01 ~]# ls dir02 file1A.txt fileA2.txt fileA3.txt fileB1.txt fileB2.txt fileB3.txt [root@v01 ~]#
将dir01的所有文件同步到dir02内,并保留文件的属主,属组,文件权限等信息
[root@v01 ~]# rsync -avz dir01/* dir02/ sending incremental file list file1A.txt fileA2.txt fileA3.txt sent 166 bytes received 69 bytes 470.00 bytes/sec total size is 0 speedup is 0.00
将dir01的所有文件同步到dirB内,并删除dir02内多余的文件:
[root@v01 ~]# rsync -avz --delete dir01/* dir02/ sending incremental file list sent 58 bytes received 12 bytes 140.00 bytes/sec total size is 0 speedup is 0.00
失败!!!!
[root@v01 ~]# rsync -avz --delete dir01/ dir02/ sending incremental file list ./ deleting fileB3.txt deleting fileB2.txt deleting fileB1.txt sent 71 bytes received 15 bytes 172.00 bytes/sec total size is 0 speedup is 0.00
[root@v01 ~]# ls dir02/ file1A.txt fileA2.txt fileA3.txt
成功!!!!
[root@v01 ~]# touch dir02/fileB04.txt [root@v01 ~]# ls dir02 file1A.txt fileA2.txt fileA3.txt fileB04.txt
[root@v01 ~]# rsync -avz --delete dir01/* dir02/* sending incremental file list ERROR: destination must be a directory when copying more than 1 file rsync error: errors selecting input/output files, dirs (code 3) at main.c(542) [receiver=3.0.6] rsync: connection unexpectedly closed (9 bytes received so far) [sender] rsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]
将dir01目录内的test目录不同步到dir02目录内
[root@v01 dir01]# ls file1A.txt fileA2.txt fileA3.txt [root@v01 dir01]# mkdir test [root@v01 dir01]# ls file1A.txt fileA2.txt fileA3.txt test
[root@v01 dir01]# cd .. [root@v01 ~]# ls anaconda-ks.cfg dir02 framework install.log.syslog mobile dir01 ecshop install.log localsvn online
[root@v01 ~]# rsync -arvz --exclude="test" --delete dir01/ dir02/ sending incremental file list ./ deleting fileB04.txt sent 75 bytes received 15 bytes 180.00 bytes/sec total size is 0 speedup is 0.00
[root@v01 ~]# ls dir02/ file1A.txt fileA2.txt fileA3.txt
同步的同时也包含隐藏文件:
[root@v01 dir02]# ls file1A.txt fileA2.txt fileA3.txt [root@v01 dir02]# mkdir .test [root@v01 dir02]# ls file1A.txt fileA2.txt fileA3.txt [root@v01 dir02]# ll -all total 12 drwxr-xr-x. 3 root root 4096 May 11 08:16 . dr-xr-x---. 12 root root 4096 May 11 07:39 .. -rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt drwxr-xr-x. 2 root root 4096 May 11 08:16 .test
[root@v01 ~]# ls dir01 file1A.txt fileA2.txt fileA3.txt test [root@v01 ~]# rsync -arvz --exclude="test" --delete dir01/ dir02/ sending incremental file list ./ deleting .test/ sent 75 bytes received 15 bytes 180.00 bytes/sec total size is 0 speedup is 0.00
[root@v01 ~]# ll -all dir02 total 8 drwxr-xr-x. 2 root root 4096 May 11 08:02 . dr-xr-x---. 12 root root 4096 May 11 07:39 .. -rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt
[root@v01 ~]# mkdir dir02/.kk [root@v01 ~]# ls -all dir02/ total 12 drwxr-xr-x. 3 root root 4096 May 11 08:26 . dr-xr-x---. 12 root root 4096 May 11 07:39 .. -rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt drwxr-xr-x. 2 root root 4096 May 11 08:26 .kk
将dir01的所有文件除test之外 同步到 dir02,但是在dir02内除了.kk 这个文件不删之外,其他的都删除
[root@v01 ~]# rsync -arvz --exclude="test" --exclude=.kk --delete dir01/ dir02/ sending incremental file list ./ sent 75 bytes received 15 bytes 180.00 bytes/sec total size is 0 speedup is 0.00 [root@v01 ~]# ls -all dir02/ total 12 drwxr-xr-x. 3 root root 4096 May 11 08:02 . dr-xr-x---. 12 root root 4096 May 11 07:39 .. -rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt drwxr-xr-x. 2 root root 4096 May 11 08:33 .kk
实现除了.svn之外保持文件同步
[root@v01 ~]# mkdir dir{01,02}/.svn [root@v01 ~]# ls -all dir01 total 16 drwxr-xr-x. 4 root root 4096 May 11 08:37 . dr-xr-x---. 12 root root 4096 May 11 07:39 .. -rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt drwxr-xr-x. 2 root root 4096 May 11 08:37 .svn drwxr-xr-x. 2 root root 4096 May 11 08:02 test [root@v01 ~]# ls -all dir02 total 16 drwxr-xr-x. 4 root root 4096 May 11 08:37 . dr-xr-x---. 12 root root 4096 May 11 07:39 .. -rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt drwxr-xr-x. 2 root root 4096 May 11 08:33 .kk drwxr-xr-x. 2 root root 4096 May 11 08:37 .svn
[root@v01 ~]# touch dir01/.svn/test1 [root@v01 ~]# touch dir02/.svn/test2 [root@v01 ~]# rsync -arvz --exclude=".svn" --delete dir01/ dir02/sending incremental file list deleting .kk/ test/ sent 96 bytes received 16 bytes 224.00 bytes/sec total size is 0 speedup is 0.00
[root@v01 ~]# ls dir02/.svn/test2
[root@v01 ~]# ls dir01/.svn/test1 dir01/.svn/test1 [root@v01 ~]# ls -all dir01 total 16 drwxr-xr-x. 4 root root 4096 May 11 08:37 . dr-xr-x---. 12 root root 4096 May 11 07:39 .. -rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt drwxr-xr-x. 2 root root 4096 May 11 08:39 .svn drwxr-xr-x. 2 root root 4096 May 11 08:02 test [root@v01 ~]# ls -all dir02 total 16 drwxr-xr-x. 4 root root 4096 May 11 08:37 . dr-xr-x---. 12 root root 4096 May 11 07:39 .. -rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt -rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt drwxr-xr-x. 2 root root 4096 May 11 08:39 .svn drwxr-xr-x. 2 root root 4096 May 11 08:02 test
联系方式QQ:326528263 EMAIL:clnking@163.com 网名:bass 分享技术 突破难点 创新思维