rsync命令

一。rsync中exclude的使用:如果排除的文件比较多时,使用一个文件定义要排除的文件或文件夹,使用--exclude-from=file 来执行。注意exclude前面是二个"-"

rsync的exclude非常好用,cp,mv都没有这样的exclude参数。唯一注意是eclucde后面的文件或文件夹必须是相对路径。所以最好提前进入源目录比较方便。

1.将linuxtest目录下的除了abc目录之外的内容同步到上级目录。

rsync -arv --exclude abc /home/gaoyibo/linuxtest/ /home/linuxtest/

2.使用--exclude-from
rsync -avz --exclude-from=/home/gaoyibo/search/exclude.list  /home/gaoyibo/search    root@tadev29.daodao.com:/home/gaoyibo/

exclude.list里面定义:
apache-tomcat-6.0.13*
daodaosearchserver0829.tar
dat.tar
eclipse.tar
lib*
liuding
pages
php-site
searchServer.war
src
usr*
web.xml
workspace
未命名.rtf
汉语词典快速查询算法研究.do

二。使用rsync注意

注意source/路径原则。source/与source的结果不同的。前者只同步source/下的内容。而后者会将source一起同步。

 

三。转自http://wandering.blog.51cto.com/467932/105113

6.1 显示目录内容

命令
------
a) rsync <dst-dir>
b) rsync -r <dst-dir>
c) rsync jack@192.168.0.1::<dst-dir>
d) rsync ssh_user@192.168.0.1:<dst-dir>

命令说明
---------
a) 显示<dst-dir>目录
内容(第一层)
b) 递归显示<dst-dir>目录
内容
c) 显示远程主机<dst-dir>目录内容
   *注1:端口模式, 基于rsync用户的身份验证
   *注2:rsync server上的目录必须具有xx7的权限.
d) 查看
远程主机<dst-dir>目录内容
   *注1:remote shell模式, 通过ssh连接的基于系统本地用户的身份验证
   *注2:这里只使用了一个冒号(:),同时用户名是远
程主机的ssh用户,密码也是ssh用户对应的密码。
   *注3:使用"<dst-dir>",则列出<dst-dir>文件夹本身的信息。若要列出<dst-dir>文件夹内容,应使用"<dst-dir>/"。
  
参数说明
---------
-r          对目录进行递归操作

  
6.2 本地目录之间同步

命令
------
a) rsync -av  --progress <src-dir>/ <dst-dir>             *** 注意(/) ***
b) rsync -av  --progress <src-dir>  <dst-dir>
c) rsync -avu --progress --delete <src-dir>/  <dst-dir>
d)
rsync -av  --progress --temp-dir=/tmp <src-dir>/ <dst-dir>


命令说明
---------
a) 同步src-dir目录下所有文件到dst-dir目录下
b) 同步src-dir目录下所有文件到dst-dir/src-dir目录下
c) 对src-dir目录内容向dst-dir目录下进行差异更新,有增加/更新则添加替换,有减少则对其删减
d) 比a)多了
--temp-dir=/tmp,即指定/tmp为临时交换区,这样可以避免因目标目录空间不够引起的无法同步文件的错误。


参数说明
---------
-a          相当于 -rlptgoD 的集合
-u          等同于 --update,在目标文件比源文件新的情况下不更新
-v          显示同步的文件
--progress  显示文件同步时的百分比进度、传输速率
--delete    删除目标目录中多于源目录的文件


6.3 异地主机之间同步

命令
------
a) rsync -avz  --progress <src-dir> jack@192.168.0.1::<dst-dir>/

b) rsync -avz  --progress <src-dir> jack@192.168.0.1::<dst-dir>/ --password-file=/home/jack/rsync.jack

c) rsync -avuz  --progress --delete <src-dir> jack@192.168.0.1::<dst-dir>/ --password-file=/home/jack/rsync.jack

d) rsync -avz  --progress jack@192.168.0.1::<dst-dir>/<src-dir> <dst-dir>

命令说明
---------
a) 同步本地<src-dir>目录的内容到远程主机192.168.0.1的<dst-dir>目录下,jack是rsync数据库用户(参见3. /etc/rsync.secrets)
b) 通过自动读取用户密码而实现非交互登录文件同步

c) 较b)多了-u和--delete

d) 同步远程主机内容到本地目录


参数说明
---------
-z                等同于 --compress,对传输的文件压缩,这对节约网络带宽或在网络资源紧张的情况下非常有用
--password-file   引用192.168.0.1上rsync用户jack口令的本地文件,创建方法如下
                  shell> echo "jackpwd" >> /home/jack/
rsync.jack
                  shell> chown jack:wheel
/home/jack/rsync.jack

                  shell> chmod 600 /home/jack/rsync.jack


-----------------------------------------------

转自http://ryyt1231.blog.163.com/blog/static/2070828120113260834691/
很常见的情况:我想同步/下的 /usr   /boot/ ,  但是不想复制/proc  /tmp 这些文件夹如果想避开某个路径  直接添加—exclude 即可
比如—exclude “proc”
–exclude ‘sources’
Note: the directory path is relative to the folder you are backing up.
注意:这个路径必须是一个相对路径,不能是绝对路径
例子:源服务器/home/yjwan/bashshell有一个checkout文件夹
[root@CentOS5-4 bashshell]# ls -dl checkout
drwxr-xr-x 2 root root 4096 Aug 21 09:14 checkou
现在想要完全避开复制这个文件夹内容怎么办?
  
目标服务器执行
rsync -av –exclude “checkout” yjwan@172.16.251.241:/home/yjwan/bashshell /tmp
将不会复制这个文件夹
[root@free /tmp/bashshell]# ls -d /tmp/bashshell/checkout
ls: /tmp/bashshell/checkout: No such file or directory
    
注意:
1事实上,系统会把文件和文件夹一视同仁,如果checkout是一个文件,一样不会复制
2 如果想避开复制checkout里面的内容,可以这么写—exclude “checkout/123”
3 切记不可写为 —exclude “/checkout”这样绝对路径
这样写 将不会避免checkout被复制
比如
[root@free /tmp/bashshell]# rsync -av –exclude “/checkout” yjwan@172.16.251.241:/home/yjwan/bashshell /tmp
receiving file list … done
bashshell/checkout/
4可以使用通配符 避开不想复制的内容
比如—exclude “fire*”
那么fire打头的文件或者文件夹全部不会被复制
5如果想要避开复制的文件过多,可以这么写
–exclude-from=/exclude.list
exclude.list 是一个文件,放置的位置是绝对路径的/exclude.list ,为了避免出问题,最好设置为绝对路径。
里面的内容一定要写为相对路径
比如 我想避开checkout文件夹和fire打头的文件
那么/exclude.list 写为
checkout
fire*
然后执行以下命令,注意写为–exclude-from或者–exclude-from=都可以
但是不能为—exclude
rsync -av –exclude-from=”/exclude.list” yjwan@172.16.251.241:/home/yjwan/bashshell /tmp
检查结果:确实避开了checkout文件夹和fire打头的文件
  
问题:Rsync的其他几个常见参数
1
-z        –compress              compress file data during the transfer
–compress-level=NUM    explicitly set compression level
–skip-compress=LIST    skip compressing files with suffix in LIST
压缩传输,如果网络带宽不够,那么应该压缩以后传输,消耗的当然是机器资源,但是如果内网传输的话,文件数量不是很多的话,这个参数不必要的。
  
2
-password-file=FILE
前面说过了,只有远端机器是rsync服务器,才能用这个参数
如果你以为个FILE写的是ssh 登陆的密码,那就大错特错了,不少人犯了这个错误。
 
3
–stats: Adds a little more output regarding the file transfer status.
  
4
–progress: shows the progress of each file transfer. Can be useful to know if you have large files being backup up.
  
关于这个参数:
I frequently find myself adding the -P option for large transfers. It preserves partial transfers in case of interuption, and gives a progress report on each file as it’s being uploaded.
  
I move large media files back and forth on my servers, so knowing how long the transfer has remaining is very useful.

                  shell> chown jack:wheel /home/jack/rsync.jack
                  shell> chmod 600 /home/jack/rsync.jack

posted @ 2011-10-20 11:46  highriver  阅读(5572)  评论(1编辑  收藏  举报