root@v01 ~]# mkdir dir01 dir02[root@v01 ~]# lsanaconda-ks.cfg dir02 framework install.log.syslog mobiledir01 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 dir01file1A.txt fileA2.txt fileA3.txt[root@v01 ~]# ls dir02file1A.txt fileA2.txt fileA3.txt fileB1.txt fileB2.txt fileB3.txt[root@v01 ~]#
将dir01的所有文件同步到dir02内,并保留文件的属主,属组,文件权限等信息
[root@v01 ~]# rsync -avz dir01/* dir02/sending incremental file listfile1A.txtfileA2.txtfileA3.txtsent 166 bytes received 69 bytes 470.00 bytes/sectotal size is 0 speedup is 0.00
将dir01的所有文件同步到dirB内,并删除dir02内多余的文件:
[root@v01 ~]# rsync -avz --delete dir01/* dir02/sending incremental file listsent 58 bytes received 12 bytes 140.00 bytes/sectotal size is 0 speedup is 0.00
失败!!!!
[root@v01 ~]# rsync -avz --delete dir01/ dir02/sending incremental file list./deleting fileB3.txtdeleting fileB2.txtdeleting fileB1.txtsent 71 bytes received 15 bytes 172.00 bytes/sectotal 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 dir02file1A.txt fileA2.txt fileA3.txt fileB04.txt
[root@v01 ~]# rsync -avz --delete dir01/* dir02/*sending incremental file listERROR: destination must be a directory when copying more than 1 filersync 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]# lsfile1A.txt fileA2.txt fileA3.txt[root@v01 dir01]# mkdir test[root@v01 dir01]# lsfile1A.txt fileA2.txt fileA3.txt test
[root@v01 dir01]# cd ..[root@v01 ~]# lsanaconda-ks.cfg dir02 framework install.log.syslog mobiledir01 ecshop install.log localsvn online
[root@v01 ~]# rsync -arvz --exclude="test" --delete dir01/ dir02/sending incremental file list./deleting fileB04.txtsent 75 bytes received 15 bytes 180.00 bytes/sectotal size is 0 speedup is 0.00
[root@v01 ~]# ls dir02/file1A.txt fileA2.txt fileA3.txt
同步的同时也包含隐藏文件:
[root@v01 dir02]# lsfile1A.txt fileA2.txt fileA3.txt[root@v01 dir02]# mkdir .test[root@v01 dir02]# lsfile1A.txt fileA2.txt fileA3.txt[root@v01 dir02]# ll -alltotal 12drwxr-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.txtdrwxr-xr-x. 2 root root 4096 May 11 08:16 .test
[root@v01 ~]# ls dir01file1A.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/sectotal size is 0 speedup is 0.00
[root@v01 ~]# ll -all dir02total 8drwxr-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 12drwxr-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.txtdrwxr-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/sectotal size is 0 speedup is 0.00[root@v01 ~]# ls -all dir02/total 12drwxr-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.txtdrwxr-xr-x. 2 root root 4096 May 11 08:33 .kk
实现除了.svn之外保持文件同步
[root@v01 ~]# mkdir dir{01,02}/.svn[root@v01 ~]# ls -all dir01total 16drwxr-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.txtdrwxr-xr-x. 2 root root 4096 May 11 08:37 .svndrwxr-xr-x. 2 root root 4096 May 11 08:02 test[root@v01 ~]# ls -all dir02total 16drwxr-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.txtdrwxr-xr-x. 2 root root 4096 May 11 08:33 .kkdrwxr-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 listdeleting .kk/test/sent 96 bytes received 16 bytes 224.00 bytes/sectotal 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 dir01total 16drwxr-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.txtdrwxr-xr-x. 2 root root 4096 May 11 08:39 .svndrwxr-xr-x. 2 root root 4096 May 11 08:02 test[root@v01 ~]# ls -all dir02total 16drwxr-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.txtdrwxr-xr-x. 2 root root 4096 May 11 08:39 .svndrwxr-xr-x. 2 root root 4096 May 11 08:02 test