在对rsync服务器配置结束以后,下一步就需要在客户端发出rsync命令来实现将服务器端的文件备份到客户端来。rsync是一个功能非常强大的工具,其命令也有很多功能特色选项,我们下面就对它的选项一一进行分析说明。 Rsync的命令格式可以为以下六种: 复制代码 1 rsync [OPTION]... SRC DEST 2 rsync [OPTION]... SRC [USER@]HOST:DEST 3 rsync [OPTION]... [USER@]HOST:SRC DEST 4 rsync [OPTION]... [USER@]HOST::SRC DEST 5 rsync [OPTION]... SRC [USER@]HOST::DEST 6 rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST] 复制代码 对应于以上六种命令格式,rsync有六种不同的工作模式:   1)拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号”:”分隔符时就启动这种工作模式。如:rsync -a /data /backup   2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号”:”分隔符时启动该模式。如:rsync -avz *.c foo:src   3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号”:”分隔符时启动该模式。如:rsync -avz foo:src/bar /data   4)从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含”::”分隔符时启动该模式。如:rsync -av root@172.16.78.192::www /databack   5)从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含”::”分隔符时启动该模式。如:rsync -av /databack root@172.16.78.192::www   6)列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。如:rsync -v rsync://172.16.78.192/www rsync参数的具体解释如下: 复制代码 1 -v, --verbose 详细模式输出 2 -q, --quiet 精简输出模式 3 -c, --checksum 打开校验开关,强制对文件传输进行校验 4 -a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD 5 -r, --recursive 对子目录以递归模式处理 6 -R, --relative 使用相对路径信息 7 -b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。 8 --backup-dir 将备份文件(如~filename)存放在在目录下。 9 -suffix=SUFFIX 定义备份文件前缀 10 -u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件) 11 -l, --links 保留软链结 12 -L, --copy-links 想对待常规文件一样处理软链结 13 --copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结 14 --safe-links 忽略指向SRC路径目录树以外的链结 15 -H, --hard-links 保留硬链结 16 -p, --perms 保持文件权限 17 -o, --owner 保持文件属主信息 18 -g, --group 保持文件属组信息 19 -D, --devices 保持设备文件信息 20 -t, --times 保持文件时间信息 21 -S, --sparse 对稀疏文件进行特殊处理以节省DST的空间 22 -n, --dry-run现实哪些文件将被传输 23 -W, --whole-file 拷贝文件,不进行增量检测 24 -x, --one-file-system 不要跨越文件系统边界 25 -B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节 26 -e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步 27 --rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息 28 -C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件 29 --existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件 30 --delete 删除那些DST中SRC没有的文件 31 --delete-excluded 同样删除接收端那些被该选项指定排除的文件 32 --delete-after 传输结束以后再删除 33 --ignore-errors 及时出现IO错误也进行删除 34 --max-delete=NUM 最多删除NUM个文件 35 --partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输 36 --force 强制删除目录,即使不为空 37 --numeric-ids 不将数字的用户和组ID匹配为用户名和组名 38 --timeout=TIME IP超时时间,单位为秒 39 -I, --ignore-times 不跳过那些有同样的时间和长度的文件 40 --size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间 41 --modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0 42 -T --temp-dir=DIR 在DIR中创建临时文件 43 --compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份 44 -P 等同于 --partial 45 --progress 显示备份过程 46 -z, --compress 对备份的文件在传输时进行压缩处理 47 --exclude=PATTERN 指定排除不需要传输的文件模式 48 --include=PATTERN 指定不排除而需要传输的文件模式 49 --exclude-from=FILE 排除FILE中指定模式的文件 50 --include-from=FILE 不排除FILE指定模式匹配的文件 51 --version 打印版本信息 52 --address 绑定到特定的地址 53 --config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件 54 --port=PORT 指定其他的rsync服务端口 55 --blocking-io 对远程shell使用阻塞IO 56 -stats 给出某些文件的传输状态 57 --progress 在传输时现实传输过程 58 --log-format=formAT 指定日志文件格式 59 --password-file=FILE 从FILE中得到密码 60 --bwlimit=KBPS 限制I/O带宽,KBytes per second 61 -h, --help 显示帮助信息 rsync常用命令及格式   rsync在同步文件夹内容这个工作上应用非常广泛,但是rsync本身命令还是比较复杂,本文总结一下: rsync = remote sync的简称 ,它 被用于在linux/unix系统中执行备份操作。rsnync用于从一个位置到另外一个位置同步文件和文件夹。备份的地址可以是本地也可以是remote server。 rsync的重要功能: speed 首次使用时,rsync在source和destination folder之间复制全部内容。下次使用时,rsync只传输变更的块或字节到目的地,而这个机制将大大提升传输速度 security rsync允许对数据使用ssh协议加密 less bandwidth rsync使用对数据块压缩和解压缩的办法降低带宽需求。 privileges 无需特殊的特权来运行rsync 语法 # rsync options source destination source和destination可以是本地或者远程目录。对于远程的情况,需要指定login name, remote server name and location 例1:在本地服务器上同步两个目录 在本地机器上同步两个目录,使用rsync -zvr命令 $ rsync -zvr /var/opt/installation/inventory/ /root/temp building file list ... done sva.xml svB.xml . sent 26385 bytes received 1098 bytes 54966.00 bytes/sec total size is 44867 speedup is 1.63 上述命令中: -z 打开压缩功能 -v verbose更多打印信息 -r recursive 执行上述命令后,你会发现rsync copy会影响到文件的timestamp信息,这时因为默认rsync并不保护timestamp信息 例2:在sync时,保留时间戳 -a(achive mode:recursive mode, 保留符号链接,保留权限信息,时间戳,以及owner,group信息) $ rsync -azv /var/opt/installation/inventory/ /root/temp/ building file list ... done ./ sva.xml svB.xml . sent 26499 bytes received 1104 bytes 55206.00 bytes/sec total size is 44867 speedup is 1.63 $注意这时你会发现source,dest文件的时间戳等信息是不变的 $ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml -r--r--r-- 1 root bin 949 Jun 18 2009 /var/opt/installation/inventory/sva.xml -r--r--r-- 1 root bin 949 Jun 18 2009 /root/temp/sva.xml 例3:只同步一个文件 只要在rsync命令中指定文件名称即可: $ rsync -v /var/lib/rpm/Pubkeys /root/temp/ Pubkeys sent 42 bytes received 12380 bytes 3549.14 bytes/sec total size is 12288 speedup is 0.99 例4:从本地到远端 $ rsync -avz /root/temp/ thegeekstuff@192.168.200.10:/home/thegeekstuff/temp/ Password: building file list ... done ./ rpm/ rpm/Basenames rpm/Conflictname sent 15810261 bytes received 412 bytes 2432411.23 bytes/sec total size is 45305958 speedup is 2.87 当执行和remote server同步的动作时,你需要指定username,ip。也要指定远程服务器上的目的地目录,格式是: username@machineIP:Path 这个过程中,rsync会要求输入密码。但是如果你有一个脚本自动运行这个备份动作,你可能希望不要手动输入密码. 例5:从远程到本地 $ 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 例6:remote shell for synchronization rsync允许你指定你想使用的remote shell,你可以使用rsync -e ssh来enable the secured remote connection $ rsync -avz -e ssh 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 例7:不覆盖目的地址上已经修改过的文件 典型情况下,如果一个文件在destination被修改的话,我们可能并不希望使用来自source的老文件去覆盖修改 使用rsync -u选项达到这个目的(即:如果目的地上修改过,那么不要覆盖它)在下面的例子中,Basenames文件在destination上做了修改,因此如果使用-u选项,则不会被修改 $ ls -l /root/temp/Basenames total 39088 -rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames $ rsync -avzu thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ sent 122 bytes received 505 bytes 114.00 bytes/sec total size is 45305958 speedup is 72258.31 $ ls -lrt total 39088 -rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames 例8:只同步目录tree structure(而不同步文件) 使用-d想想将只从source到destination同步文件夹的tree structure,下面的例子,只会递归同步目录树,而目录中的文件不会同步 $ rsync -v -d thegeekstuff@192.168.200.10:/var/lib/ . Password: receiving file list ... done logrotate.status CAM/ YaST2/ acpi/ sent 240 bytes received 1830 bytes 318.46 bytes/sec total size is 956 speedup is 0.46 例9:查看rsnync传输进度 当使用rsync来做备份时,你可能希望知道backup的进度,比如有多少个文件已经copy了,以及copy的速度等信息, rsync -progress将会打印rsync执行中的详细信息: $ rsync -avz --progress thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... 19 files to consider ./ Basenames 5357568 100% 14.98MB/s 0:00:00 (xfer#1, to-check=17/19) Conflictname 12288 100% 35.09kB/s 0:00:00 (xfer#2, to-check=16/19) . . . sent 406 bytes received 15810211 bytes 2108082.27 bytes/sec total size is 45305958 speedup is 2.87 例10:删除在Targe上创建的文件 如果在source这一侧并不存在一个文件,而这个文件本身又在destination上存在,那么你可以指定删除这个文件,-delete选项完成这个功能 # 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 注意:new-file.txt文件将在rsync过程中被删除 例11:在destination(target)上不创建新文件 如果你喜欢,你可以只update(sync)那些在target上已经存在的文件。如果source有新的文件,而这个文件本身在target上并不存在,那么你可以通过-existing选项避免在destination上创建这些新文件 首先在source上创建一个new-file.txt文件 [/var/lib/rpm ]$ > new-file.txt $ rsync -avz --existing root@192.168.1.2:/var/lib/rpm/ . root@192.168.1.2's password: receiving file list ... done ./ sent 26 bytes received 419 bytes 46.84 bytes/sec total size is 88551424 speedup is 198991.96 例12:查看source/destination之间的变更 At 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 At destination: $ ls -l /root/temp -rw-r--r-- 1 root root 0 Sep 3 06:39 Basenames -rw-r--r-- 1 root root 12288 May 28 2008 Conflictname -rw-r--r-- 1 bin bin 1179648 Jun 24 05:27 Dirnames 在这里source和destination有两个不同。owner/group,以及size不同。 $ 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 在上面的例子中,在Basenames, Dirnames文件的前面有一些奇怪的信息,其实它非常重要: > 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. 例13:在文件传输中包含和排除Pattern rsync允许你给一个pattern,指定你希望在做同步过程中包含或者排除的文件或者目录 $ 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 上面的例子中它将仅仅包含那些以P打头的文件或者文件夹并且排除所有其他文件 例14:不传输大的文件 你可以告诉rsync不要传输大于指定大小尺寸的文件,使用-max-size选项 $ rsync -avz --max-size='100K' thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done ./ Conflictname Group Installtid Name Sha1header Sigmd5 Triggername sent 252 bytes received 123081 bytes 18974.31 bytes/sec total size is 45305958 speedup is 367.35 上面的例子使得rsync只传输那些小于100K大小的文件。你也可以指定M或G 例15:传输整个文件 rsync的一个重要功能是它只传输一个文件的变更的块到目的地,而不是传输文件本省。如果网络带宽本身并不是什么问题,你可以传输整个文件,通过-Wxuanxiang 这将加速rsync的处理速度,因为他不需要再在source和destination做checksum的运算了。 # rsync -avzW thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp Password: receiving file list ... done ./ Basenames Conflictname Dirnames Filemd5s Group Installtid Name sent 406 bytes received 15810211 bytes 2874657.64 bytes/sec total size is 45305958 speedup is 2.87 #^_^ 本文原文来自于: http://www.thegeekstuff.com/2010/09/rsync-command-examples/ 参考:https://rsync.samba.org/how-rsync-works.html --exclude 指定跳过的源目录或文件的相对路径的目录或文件 rsync --exlude=要排除的目录或文件 源目录或文件 备份目录或文件