rsync 命令详解

rsync  命令介绍

rsync,remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。 rsync是用 “rsync 算法”提供了一个客户机和远程文件服务器的文件同步的快速方法,而且可以通过ssh方式来传输文件,这样其保密性也非常好,另外它还是免费的软件。

  rsync 包括如下的一些特性:

  能更新整个目录和树和文件系统;

  有选择性的保持符号链链、硬链接、文件属于、权限、设备以及时间等;
  对于安装来说,无任何特殊权限要求;
  对于多个文件来说,内部流水线减少文件等待的延时;
  能用rsh、ssh 或直接端口做为传输入端口;
  支持匿名rsync 同步文件,是理想的镜像工具;

rsync 的常用用法

            如果没有这个命令 可以 先安装一下           yum install  rsync  

            
这里一般常用的选项
rsync命令的一些选项如下:
                    -a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
                    -r, --recursive 对子目录以递归模式处理
                    -l, --links 保留软链接
                    -p, --perms 保持文件 权限
                    -t, --times 保持文件时间信息
                    -g, --group 保持文件属组信息
                    -o, --owner 保持文件属主信息
                    -D, --devices 保持设备文件信息
                    -v, --verbose 详细模式输出
                    -P, --partial 保留那些因故没有完全传输的文件,以加快随后的再次传输
-q --quiet 静默模式 ,传输没有任何信息,即使出错了,也没有信息。
                    -z, --compress 对备份的文件在传输时进行压缩处理
                    -L, --copy-links 像对待常规文件一样处理软链接
                    --port=PORT 指定其他的rsync服务端口
                    --password-file=FILE 从FILE中得到密码
 –-delete:如果源端没有此文件,那么目的端也别想拥有,删除之

 –delete-excluded:专门指定一些要在目的端删除的文件。


rsync 命令 有很多常用的选项 ,我说几个我常用的,

1) -a -v 选项

-a 递归模式,如果同步文件夹 ,这个要有的.
-v 把详细的信息打印在 shell 终端

举例说明, 我同步111 这个目录
[root@MysqlServer ~]# ll 111/
total 44
lrwxrwxrwx. 1 root root 5 Feb 11 22:09 1.link -> 1.txt
-rw-r--r--. 1 root root 42752 Feb 11 21:54 1.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 2.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 3.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 4.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 5.txt

[root@MysqlServer ~]# rsync 111 test
skipping directory 111

这样是不能同步, 可以加上 -a
[root@MysqlServer ~]# rsync -a-v 111 test
sending incremental file list
created directory test
111/
111/1.link -> 1.txt
111/1.txt
111/2.txt
111/3.txt
111/4.txt
111/5.txt

sent 43083 bytes received 114 bytes 86394.00 bytes/sec
total size is 42757 speedup is 0.99
[root@MysqlServer ~]# ll test/111/
total 44
lrwxrwxrwx. 1 root root 5 Feb 11 22:09 1.link -> 1.txt
-rw-r--r--. 1 root root 42752 Feb 11 21:54 1.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 2.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 3.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 4.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 5.txt

此时已经把111 同步到test目录下面了。

2) 选项 -l--links 保留软链接

比如在111 目录有一个1.link 的软连接,如果我们不加 -l rsync 是默认不会同步的。

[root@MysqlServer ~]# rsync -rv 111 test
sending incremental file list
created directory test
111/
skipping non-regular file "111/1.link"
111/1.txt
111/2.txt
111/3.txt
111/4.txt
111/5.txt

sent 43072 bytes received 111 bytes 86366.00 bytes/sec
total size is 42757 speedup is 0.99

此时发现 test/111 目录下面 是没有1.link 这个软链接的。
[root@MysqlServer ~]# ll test/111/
total 44
-rw-r--r--. 1 root root 42752 Feb 11 22:23 1.txt
-rw-r--r--. 1 root root 0 Feb 11 22:23 2.txt
-rw-r--r--. 1 root root 0 Feb 11 22:23 3.txt
-rw-r--r--. 1 root root 0 Feb 11 22:23 4.txt
-rw-r--r--. 1 root root 0 Feb 11 22:23 5.txt


如果同步的时候 加上 选项 -l 此时同步的时候, 就会把软连接 同步过去

root@MysqlServer ~]# rsync -rv -l111 test
sending incremental file list
created directory test
111/
111/1.link -> 1.txt
111/1.txt
111/2.txt
111/3.txt
111/4.txt
111/5.txt

sent 43081 bytes received 114 bytes 86390.00 bytes/sec
total size is 42757 speedup is 0.99
[root@MysqlServer ~]# ll test/111/
total 44
lrwxrwxrwx. 1 root root 5 Feb 11 22:26 1.link -> 1.txt
-rw-r--r--. 1 root root 42752 Feb 11 22:26 1.txt
-rw-r--r--. 1 root root 0 Feb 11 22:26 2.txt
-rw-r--r--. 1 root root 0 Feb 11 22:26 3.txt
-rw-r--r--. 1 root root 0 Feb 11 22:26 4.txt
-rw-r--r--. 1 root root 0 Feb 11 22:26 5.txt

3) -p 选项 就是要保持文件权限

刚刚这样同步的时候
rsync -rv -l 111 test

我 先 把 111/2.txt 权限 更改一下
chmod 666 111/2.txt
[root@MysqlServer ~]# ll 111/
total 44
lrwxrwxrwx. 1 root root 5 Feb 11 22:09 1.link -> 1.txt
-rw-r--r--. 1 root root 42752 Feb 11 21:54 1.txt
-rw-rw-rw-. 1 root root 0 Feb 11 21:54 2.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 3.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 4.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 5.txt
进行同步 : rsync -rv -l 111 test
[root@MysqlServer ~]# ll test/111/
total 44
lrwxrwxrwx. 1 root root 5 Feb 11 22:32 1.link -> 1.txt
-rw-r--r--. 1 root root 42752 Feb 11 22:32 1.txt
-rw-r--r--.1 root root 0 Feb 11 22:32 2.txt
-rw-r--r--. 1 root root 0 Feb 11 22:32 3.txt
-rw-r--r--. 1 root root 0 Feb 11 22:32 4.txt
-rw-r--r--. 1 root root 0 Feb 11 22:32 5.txt

我们发现 2.txt 权限并不是 666 而是 644 。现在来加上 -p 选项.


[root@MysqlServer ~]# rsync -rlv -p111 test
sending incremental file list
111/1.txt
111/2.txt
111/3.txt
111/4.txt
111/5.txt

sent 43079 bytes received 108 bytes 86374.00 bytes/sec
total size is 42757 speedup is 0.99
[root@MysqlServer ~]# ll test/
total 4
drwxr-xr-x. 2 root root 4096 Feb 11 22:36 111
[root@MysqlServer ~]# ll test/111/
total 44
lrwxrwxrwx. 1 root root 5 Feb 11 22:32 1.link -> 1.txt
-rw-r--r--. 1 root root 42752 Feb 11 22:36 1.txt
-rw-rw-rw-. 1 root root 0 Feb 11 22:36 2.txt
-rw-r--r--. 1 root root 0 Feb 11 22:36 3.txt
-rw-r--r--. 1 root root 0 Feb 11 22:36 4.txt
-rw-r--r--. 1 root root 0 Feb 11 22:36 5.txt

此时 test/111/2.txt 就变成 权限 就变成了666.

4) -t 选项 保持文件的时间

这样同步 有一个问题,文件时间变成了 同步的时候的时间,而不是源文件的时间。
rsync -rlv -p 111 test

[root@MysqlServer ~]# ll test/111/
total 52
lrwxrwxrwx. 1 root root 5 Feb 11 22:42 1.link -> 1.txt
-rw-r--r--. 1 root root 42752 Feb 11 22:42 1.txt
-rw-rw-rw-. 1 root root 108 Feb 11 22:42 2.txt
-rw-r--r--. 1 root root 70 Feb 11 22:42 3.txt
-rw-r--r--. 1 root root 0 Feb 11 22:42 4.txt
-rw-r--r--. 1 root root 0 Feb 11 22:42 5.txt
[root@MysqlServer ~]# ll 111/
total 52
lrwxrwxrwx. 1 root root 5 Feb 11 22:09 1.link -> 1.txt
-rw-r--r--. 1 root root 42752 Feb 11 21:541.txt
-rw-rw-rw-. 1 root root 108 Feb 11 22:40 2.txt
-rw-r--r--. 1 root root 70 Feb 11 22:41 3.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 4.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 5.txt

要解决这个 问题, 就是要保证原文件的时间,同步 不能变,此时可以加上 -t 选项。

[root@MysqlServer ~]# ll 111/
total 52
lrwxrwxrwx. 1 root root 5 Feb 11 22:09 1.link -> 1.txt
-rw-r--r--. 1 root root 42752 Feb 11 21:541.txt
-rw-rw-rw-. 1 root root 108 Feb 11 22:40 2.txt
-rw-r--r--. 1 root root 70 Feb 11 22:413.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 4.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 5.txt
[root@MysqlServer ~]# rsync -rvpl -t111 test
sending incremental file list
111/
111/1.txt
111/2.txt
111/3.txt
111/4.txt
111/5.txt
sent 43287 bytes received 114 bytes 86802.00 bytes/sec
total size is 42935 speedup is 0.99
[root@MysqlServer ~]# ll test/111/
total 52
lrwxrwxrwx. 1 root root 5 Feb 11 22:091.link -> 1.txt
-rw-r--r--. 1 root root 42752 Feb 11 21:54 1.txt
-rw-rw-rw-. 1 root root 108 Feb 11 22:40 2.txt
-rw-r--r--. 1 root root 70 Feb 11 22:41 3.txt
-rw-r--r--. 1 root root 0 Feb 11 21:544.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 5.txt

此时可以发现 ,文件的时间信息也同时被同步过来啦。

5) -o 保持文件所属主

现在 把1.txt 所属主 改为abiao
chown abiao 1.txt


[root@MysqlServer ~]# ll 111/
total 52
lrwxrwxrwx. 1 root root 5 Feb 11 22:09 1.link -> 1.txt
-rw-r--r--. 1 abiao root 42752 Feb 11 21:54 1.txt
-rw-rw-rw-. 1 root root 108 Feb 11 22:40 2.txt
-rw-r--r--. 1 root root 70 Feb 11 22:41 3.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 4.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 5.txt

[root@MysqlServer ~]# rsync -rvl 111 test
sending incremental file list
111/1.link -> 1.txt
111/1.txt
111/2.txt
111/3.txt
111/4.txt
111/5.txt

[root@MysqlServer ~]# ll test/111/
total 52
lrwxrwxrwx. 1 root root 5 Feb 11 23:02 1.link -> 1.txt
-rw-r--r--. 1 root root 42752 Feb 11 23:02 1.txt
-rw-r--r--. 1 root root 108 Feb 11 23:02 2.txt
-rw-r--r--. 1 root root 70 Feb 11 23:02 3.txt
-rw-r--r--. 1 root root 0 Feb 11 23:02 4.txt
-rw-r--r--. 1 root root 0 Feb 11 23:02 5.txt

发现 1.txt 所属主 并没有变成abiao ,这个时候 如果想保持文件的所属组,就是加一个 -o
[root@MysqlServer ~]# rsync -rvl -o 111 test
sending incremental file list
created directory test
111/
111/1.link -> 1.txt
111/1.txt
111/2.txt
111/3.txt
111/4.txt
111/5.txt

sent 43298 bytes received 114 bytes 86824.00 bytes/sec
total size is 42935 speedup is 0.99
[root@MysqlServer ~]# ll test/111/
total 52
lrwxrwxrwx. 1 root root 5 Feb 11 23:07 1.link -> 1.txt
-rw-r--r--. 1 abiao root 42752 Feb 11 23:07 1.txt
-rw-r--r--. 1 root root 108 Feb 11 23:07 2.txt
-rw-r--r--. 1 root root 70 Feb 11 23:07 3.txt
-rw-r--r--. 1 root root 0 Feb 11 23:07 4.txt
-rw-r--r--. 1 root root 0 Feb 11 23:07 5.txt
此时 1.txt 所属主 已经变成abiao

6) -g 保持文件属组

 -g, --group 保持文件属组信息

还拿1.txt 说事情, 把 1.txt 的所属组 改为dp 组
chown :dp 1.txt

[root@MysqlServer ~]# ll 111/
total 52
lrwxrwxrwx. 1 root root 5 Feb 11 22:09 1.link -> 1.txt
-rw-r--r--. 1 abiao dp 42752 Feb 11 21:54 1.txt
-rw-rw-rw-. 1 root root 108 Feb 11 22:40 2.txt
-rw-r--r--. 1 root root 70 Feb 11 22:41 3.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 4.txt
-rw-r--r--. 1 root root 0 Feb 11 21:54 5.txt


[root@MysqlServer ~]# rsync -rvlo 111 test

[root@MysqlServer ~]# ll test/111/
total 52
lrwxrwxrwx. 1 root root 5 Feb 11 23:13 1.link -> 1.txt
-rw-r--r--. 1 abiao root 42752 Feb 11 23:13 1.txt
-rw-r--r--. 1 root root 108 Feb 11 23:13 2.txt
-rw-r--r--. 1 root root 70 Feb 11 23:13 3.txt
-rw-r--r--. 1 root root 0 Feb 11 23:13 4.txt
-rw-r--r--. 1 root root 0 Feb 11 23:13 5.txt

所属组 并没有变成 dp ,
此时 可以用 -g 选项.
[root@MysqlServer ~]# rsync -rvlo -g 111 test

[root@MysqlServer ~]# ll test/111/
total 52
lrwxrwxrwx. 1 root root 5 Feb 11 23:13 1.link -> 1.txt
-rw-r--r--. 1 abiao dp42752 Feb 11 23:14 1.txt
-rw-r--r--. 1 root root 108 Feb 11 23:14 2.txt
-rw-r--r--. 1 root root 70 Feb 11 23:14 3.txt
-rw-r--r--. 1 root root 0 Feb 11 23:14 4.txt
-rw-r--r--. 1 root root 0 Feb 11 23:14 5.txt

此时1.txt 已经所属组 已经变成了 dp

7) -z 这个就是传输的时候,压缩一下。

8)--delete

 –-delete:如果源端没有此文件,那么目的端也别想拥有,删除之

tree 111/
111/
├── 1.link -> 1.txt
├── 1.txt
├── 222
├── 2.txt
├── 3.txt
├── 4.txt
└── 5.txt
[root@MysqlServer ~]# rsync -rlv 111 test
sending incremental file list
111/1.txt
111/2.txt
111/3.txt
111/4.txt
111/5.txt


现在 我吧 111 目录下面的 2.txt 删除
rm -f 2.txt

此时 再一次进行同步
[root@MysqlServer ~]# rsync -rlv --delete 111 test
sending incremental file list
deleting 111/2.txt
111/1.txt
111/3.txt
111/4.txt
111/5.txt

sent 43136 bytes received 90 bytes 86452.00 bytes/sec
total size is 42827 speedup is 0.99

此时test 已经
[root@MysqlServer ~]# ls test/111/
1.link 1.txt 222 3.txt 4.txt 5.txt
此时已经没有了 2.txt ,这就是 --delete 的作用。 如果源文件删除了,目标文件也相应的删除了。

9) --exclude

--exclude 同步时候排除一些文件。

root@MysqlServer ~]# tree 111/
111/
├── 1.link -> 1.txt
├── 1.txt
├── 222
│   └── 22.txt
├── 333
│   └── 33.txt
├── 3.txt
├── 4.txt
└── 5.txt

2 directories, 7 files
[root@MysqlServer ~]# rsync -rvl --exclude="3.txt" --exclude="1.txt" 111/ test
sending incremental file list
created directory test
./
1.link -> 1.txt
4.txt
5.txt
222/
222/22.txt
333/
333/33.txt

sent 331 bytes received 102 bytes 866.00 bytes/sec
total size is 5 speedup is 0.01

此时发现 同步的时候可以 排除一些 文件,防止被同步,也是对 源文件的一种保护。

10) --delete-excluded 这个选项


这个选项 要和 --exclude 配合起来,比如我开始同步 111/ 到test/ 一开始我完全同步了。
后来 我不想 完全同步了,我不想把 1.txt 同步 ,我该 怎么办呢?
此时可以加上 --exclude 这个选项。 但是之前已经把 1.txt 进行了同步,所以此时 要想把目标文件夹中 删除 1.txt , 这个时候 ,就可以加上 。 --delete-excluded 这个选项, 此时目标文件夹,就删除了 1.txt. 看下面的例子。

第一步,完全同步。
[root@MysqlServer ~]# rsync -rvl 111/ test/
sending incremental file list
1.txt
3.txt
4.txt
5.txt
222/22.txt
333/33.txt

第二步,不想同步 1.txt
[root@MysqlServer ~]# rsync -rvl --exclude="1.txt" 111/ test/
sending incremental file list
3.txt
4.txt
5.txt
222/22.txt
333/33.txt

sent 372 bytes received 109 bytes 962.00 bytes/sec
total size is 5 speedup is 0.01
第三步, 此时想删除 同步过的文件 1.txt ,这个时候,就可以用 --delete-excluded 这个选项了, 此时就删除了 目标文件中,exclude 的文件了。
[root@MysqlServer ~]# rsync -rvl --exclude="1.txt" --delete-excluded 111/ test/
sending incremental file list
deleting 1.txt
3.txt
4.txt
5.txt
222/22.txt
333/33.txt


其实rsync 命令 是一个非常好用的命令,用法也非常丰富,当然这里只是介绍了一些 常用的一些用法.



分享快乐,留住感动。




  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值