Linux数据同步工具rsync怎么用

发布时间:2022-01-27 14:41:48 作者:iii
来源:亿速云 阅读:104

这篇文章主要讲解了“Linux数据同步工具rsync怎么用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Linux数据同步工具rsync怎么用”吧!

Linux数据同步工具rsync怎么用

讲解 rsync 用法之前,为了让大家对此命令有一个整体的认识,这里先举个例子:

[root@localhost ~]# rsync -av /etc/passwd /tmp/1.txtsending incremental file list

sent 34 bytes received 15 bytes 98.00 bytes/sec
total size is 1432 speedup is 29.2

此例中,通过执行 rsync 命令,实现了将 /etc/passwd 文件本地同步到 /tmp/ 目录下,并改名为 1.txt。

除此之外,rsync 命令还支持远程同步数据,也就是将本地的数据备份到远程机器上。比如说,我们知道远程机器的 IP 地址为 192.168.188.128,则使用 rsync 命令备份 passwd 文件的执行命令为:

[root@localhost ~]# rsync -av /etc/passwd 192.168.188.128:/tmp/1.txt The authenticity of host ‘192.168.188.128 (192.168.188.128)’ can’t be established. ECDSA key fingerprint is 26:e3:97:e7:bb:ae:17:33:ea:aa:Oc:5f:37:Oe:9e:fa. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘192.l68.l88.l28’ (ECDSA) to the list of known hosts. root@192.168.188.128’s password:

sent 31 bytes received 12 bytes 7.82 bytes/sec total size is 1432 speedup is 54.91

注意,首次远程连接时,会提示是否要继续连接,输入 yes 即可。另外,当成功建立连接后,需要输入目标系统的 root 密码。

通过以上 2 个实例,读者应该能对“rsync既支持本地备份数据,还支持远程备份数据”有了直观的认识。那么,rsync 命令要怎样使用呢?

rsync 命令的基本格式有多种,分别是:

[root@localhost ~]# rsync [OPTION] SRC DEST [root@localhost ~]# rsync [OPTION] SRC [USER@]HOST:DEST [root@localhost ~]# rsync [OPTION] [USER@]HOST:SRC DEST [root@localhost ~]# rsync [OPTION] [USER@]HOST::SRC DEST [root@localhost ~]# rsync [OPTION] SRC [USER@]HOST::DEST

针对以上 5 种命令格式,rsync 有 5 种不同的工作模式:

要知道,使用 rsync 在远程传输数据(备份数据)前,是需要进行登陆认证的,这个过程需要借助 ssh 协议或者 rsync 协议才能完成。在 rsync 命令中,如果使用单个冒号(:),则默认使用 ssh 协议;反之,如果使用两个冒号(::),则使用 rsync 协议。

ssh 协议和 rsync 协议的区别在于,rsync 协议在使用时需要额外配置,增加了工作量,但优势是更加安全;反之,ssh 协议使用方便,无需进行配置,但有泄漏服务器密码的风险。

另外,以上几种格式中各个参数的含义如下:

rsync 命令提供使用的 OPTION 及功能如表 1 所示。

OPTION选项功能
-a这是归档模式,表示以递归方式传输文件,并保持所有属性,它等同于-r、-l、-p、-t、-g、-o、-D 选项。-a 选项后面可以跟一个 –no-OPTION,表示关闭 -r、-l、-p、-t、-g、-o、-D 中的某一个,比如-a –no-l 等同于 -r、-p、-t、-g、-o、-D 选项。
-r表示以递归模式处理子目录,它主要是针对目录来说的,如果单独传一个文件不需要加 -r 选项,但是传输目录时必须加。
-v表示打印一些信息,比如文件列表、文件数量等。
-l表示保留软连接。
-L表示像对待常规文件一样处理软连接。如果是 SRC 中有软连接文件,则加上该选项后,将会把软连接指向的目标文件复制到 DEST。
-p表示保持文件权限。
-o表示保持文件属主信息。
-g表示保持文件属组信息。
-D表示保持设备文件信息。
-t表示保持文件时间信息。
–delete表示删除 DEST 中 SRC 没有的文件。
–exclude=PATTERN表示指定排除不需要传输的文件,等号后面跟文件名,可以是通配符模式(如 *.txt)。
–progress表示在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、 同步的文件传输速度等。
-u表示把 DEST 中比 SRC 还新的文件排除掉,不会覆盖。
-z加上该选项,将会在传输过程中压缩。

显示详细信息

以上也仅是列出了 async 命令常用的一些选项,对于初学者来说,记住最常用的几个即可,比如 -a、-v、-z、–delete 和 –exclude。

如果想查看 async 提供的所有选项,可直接执行 async 命令。

为了更好的演示各个选项的功能,需要做一些准备工作,执行如下命令:

#新建rsync目录 [root@localhost ~]# mkdir rsync 

[root@localhost ~]# cd rsync #在rsync目录中,创建test1目录 

[root@localhost rsync]# mkdir test1 

[root@localhost rsync]# cd test1 #在test1目录中,分别创建名为 1、2、3、/root.123.txt 文件 

[root@localhost test1]# touch 1 2 3 /root/123.txt 

[root@localhost test1]# ln -s /root/123.txt ./123.txt 

[root@localhost test1]# ls -l total 0 -rw-r–r–. 1 root root 0 0ct 23 07:34 1 lrwxrwxrwx. 1 root root 13 0ct 23 08:34 123.txt -> /root/123.txt -rw-r–r–. 1 root root 0 0ct 23 07:34 2 -rw-r–r–. 1 root root 0 0ct 23 07:34 3 

[root@localhost test1]# cd .. #回到rsync目录 

[root@localhost rsync]#

在此基础上,下面挑选了几个常用的 OPTION 选项,给大家举例说明它们的用法。

rsync -a 选项

首先来看看 -a 选项的用法,如下所示:

[root@localhost rsync]# rsync -a test1 test2 

[root@localhost rsync]# ls test2 test1 

[root@localhost rsync]# ls test2/test1/ 1 123.txt 2 3

这里有一个问题,我们本来是想把 test1 目录中的内容直接放到 test2 目录中,可结果 rsync 命令却新建了 test2 目录,然后把 test1 放到 test2 中。

如果想要实现将 test1 目录中的内容直接备份到 test2 目录中,需修改上面的命令为:

[root@localhost rsync]#rm -rf test2 [root@localhost rsync]# rsync -a test1/ test2/ [root@localhost rsync]# ls test2/ 1 123.txt 2 3

可以看到,只需给 test1 和 test2 目录后添加 / 斜杠即可。

前面讲过,使用 -a 选项,等同于同时使用 -r、-l、-p、-t、-g、-o、-D 选项,且 -a 还可以和 –no-OPTION 一并使用。下面再来看看 -l 选项的作用:

[root@localhost rsync]# rm -rf test2 

[root@localhost rsync]# rsync -av test1/ test2/ sending incremental file list created directory test2 ./ 1 skipping non-regular file “123.txt” 2 3

sent 200 bytes received 72 bytes 544.00 bytes/sec total size is 13 speedup is 0.05

这里使用 -v 选项,可以看到,拷贝过程中跳过了非普通文件 123.txt,其实 123.txt 是一个软链接文件,如果不使用 -l 选项,系统将不理会软链接文件。

rsync –delete选项

通过表 1 可以看到,–delete 选项用来–delete 删除 DEST 中 SRC 没有的文件。例如:

#拷贝 test1 目录下的数据 

[root@localhost rsync]# rsync -a test1/ test2 #删除 test1/123.txt 文件 

[root@localhost rsync]# rm -f test1/123.txt [root@localhost rsync]# ls test1/ 1 2 3 

[root@localhost rsync]# rsync -av test1/ test2/ sending incremental file list ./

sent 55 bytes received 15 bytes 140.00 bytes/sec total size is 0 speedup is 0.00 

[root@localhost rsync]# ls test2/ 1 123.txt 2 3

可以看到,当对 test1 目录删除了 123.txt 文件之后,再次备份并没有对 test2 目录中的 123.txt 文件产生任何影响。

下面使用 –delete 选项,再次执行拷贝命令,如下所示:

[root@localhost rsync]# rsync -av –delete test1/ test2/ sending incremental file list deleting 123.txt

sent 52 bytes received 12 bytes 128.00 bytes/sec total size is 0 speedup is 0.00 

[root@localhost rsync]# ls test2/ 1 2 3

可以看到,使用 –delete 选项进行备份数据时,test1 目录一旦做了改变,那么 test2 也会做相应改变。

不仅如此,如果在 DEST 中增加文件,而 SRC 中不包含这些文件,那么在使用 –delete 选项做同步备份操作时,DEST 新增的这些文件会被删除。例如:

[root@localhost rsync]# touch test2/4 [root@localhost rsync]# ls test1/ 1 2 3 

[root@localhost rsync]# ls test2/ 1 2 3 4 

[root@localhost rsync]# rsync -a –delete test1/ test2/ 

[root@localhost rsync]# ls test2/ 1 2 3

感谢各位的阅读,以上就是“Linux数据同步工具rsync怎么用”的内容了,经过本文的学习后,相信大家对Linux数据同步工具rsync怎么用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. rsync + inotify 实现多台服务器数据同步
  2. RHEL7构建Rsync数据同步服务器

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

rsync linux

上一篇:Win7系统无法安装显卡nvidia的解决办法是什么

下一篇:jstat命令怎么使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》