rsync命令

摘要:
请注意,在exclude前面有两个“-”rsync排除项,非常容易使用。cp和mv都没有这样的排除参数。使用rsync时,请注意源/路径原则。参数描述-------r目录上的递归操作6.2本地目录之间的同步命令-------a)rsync av--progress/***注意(/)***b)rsync av--progresc)rsync av--progresse--delete/d)rsync av--progress-temp dir=/tmp˃/˂dst-dir替换,如果减少,将删除。d) 大于a)--temp-dir=/tmp,即/tmp被指定为临时交换区域。这可以避免由于目标目录空间不足而无法同步文件的错误。

一。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

免责声明:文章转载自《rsync命令》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Vue 使用 Antd 简单实现左侧菜单栏和面包屑功能前端使用lodop插件进行打印设置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ASP.NET Core中添加MIME 类型

目录 #事故现场 #解决方法 #事故现场 在asp.net core 中使用pdf.js插件,然后遇到一个问题,发现pdf的工具条的文字都是英文的;打开浏览器的控制台,发现有一个报错:http://localhost:2076/lib/pdf.js/web/locale/zh-CN/viewer.properties这个文件404了。而且还报了很...

busybox的init初始化进程

busybox的init   除了基本的命令之外,BusyBox还支持init功能,如同其它的init一样,busybox的init也是完成系统的初始化工作,关机前的工作等等,我们知道在Linux的内核被载入之后,机器就把控制权转交给内核,linux的内核启动之后,做了一些工作,然后找到根文件系统里面的init程序,并执行它,BusyBox的init进程会...

C#操作目录和文件

C#操作目录和文件 创建目录和文件 1、通过Path类的Combine方法可以合并路径。 string activeDir = @"C:myDir"; string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne"); 2、目录的创建。 创建...

go 学习 (一):环境配置

Go 下载地址:https://golang.google.cn/dl/ 1、右键我的电脑 --> 左上方 “高级系统设置” ---> 环境变量 --> 第二个菜单栏 “系统变量” --> 找到变量名为 “Path” --> 双击并点击新建,添加 go/bin 目录 --> 点击确定才会生效 2、新建文件夹作为源码的...

mac 终端 常用命令

http://blog.csdn.net/y2888886/article/details/50468198 mac 终端 常用命令 基本命令1、列出文件ls 参数 目录名        例: 看看驱动目录下有什么:ls /System/Library/Extensions参数 -w 显示中文,-l 详细信息, -a 包括隐藏文件2、转换目录   ...

小程序开发笔记(六)--使用七牛云上传图片

选取相册及本地预览 选择手机相册 preUpload() { let that = this; // let imglist=[]; wx.chooseImage({ count: 9, //最多可以选择的图片张数 sizeType: ["original", "compressed"],...