在Linux中传输文件的Rsync示例
在Linux操作系统上,"rsync"代表远程同步(Remote Sync)。
它是用于将(复制)文件和目录与源(SRC)同步到目标(DEST)的实用程序。
文件和目录可以在本地主机或者远程主机上同步。
在SRC上,文件和目录将同步。
虽然,在DEST中,将进行同步文件和目录。
此外,rsync命令可以递归复制文件和目录,复制符号链接,保存(权限,组,修改时间和所有权)文件标识。
rsync可以使用两种不同的方式来同步/复制文件或者目录:
- 使用远程shell:ssh或者rsh
- 直接通过TCP使用rsync守护程序
甚至更多,rsync使用增量传输算法,该算法仅复制/同步src和dest之间的不同数据。
本教程将通过许多方法来通过实践实践和示例使用rsync命令。
我们还将解释rsync命令最常见的选项。
rsync命令语法
在深潜进入使用rsync命令的方式之前,让我们来看看它的语法:
$rsync [options] <SRC_PATH> <DEST_PATH>
1.在本地计算机上复制/同步文件
rsync命令可用于在本地计算机上复制/同步文件。
假设我们希望将文件从名为"/home/onitad"的用户'onitoad'的主目录复制到文件夹'/opt/test'。
$sudo rsync -zvh /home/theitroad/computer-networking.pdf /opt/test/ computer-networking.pdf sent 1.01K bytes received 35 bytes 2.10K bytes/sec total size is 3.03K speedup is 2.89
其中:
-Z
:压缩
-v
:verbose输出
-h
:人类可读
2.在本地计算机上复制/同步目录
要将来自SRC本地目录的所有文件传输到Dest目录,请使用选项-zavh
运行rsync命令。
假设我们要将名为'/home/onitad'的用户'onitad'的主目录复制到文件夹'/opt/backup'。
$sudo rsync -zavh /home/theitroad//opt/backup/ sending incremental file list ./ .bash_history .bash_logout .bashrc .lesshst .profile .sudo_as_admin_successful .viminfo computer-networking.pdf .ssh/ .ssh/known_hosts sent 4.85K bytes received 198 bytes 10.10K bytes/sec total size is 9.59K speedup is 1.90
其中:
-a
:复制/同步过程中的归档数据
我们可以验证rsync命令是否成功?
运行以下命令:
$ls /opt/backup/-la total 40 drwxr-xr-x 3 theitroad theitroad 4096 Thg 6 15 16:02 . drwxr-xr-x 4 root root 4096 Thg 6 15 16:25 .. -rw------- 1 theitroad theitroad 817 Thg 6 15 16:01 .bash_history -rw-r--r-- 1 theitroad theitroad 220 Thg 6 11 15:58 .bash_logout -rw-r--r-- 1 theitroad theitroad 3771 Thg 6 11 15:58 .bashrc -rw-rw-r-- 1 theitroad theitroad 3028 Thg 2 25 2016 computer-networking.pdf -rw------- 1 theitroad theitroad 40 Thg 6 15 15:51 .lesshst -rw-r--r-- 1 theitroad theitroad 807 Thg 6 11 15:58 .profile drwx------ 2 theitroad theitroad 4096 Thg 6 13 17:58 .ssh -rw-r--r-- 1 theitroad theitroad 0 Thg 6 15 16:02 .sudo_as_admin_successful -rw------- 1 theitroad theitroad 680 Thg 6 14 16:50 .viminfo
3.使用特定端口的SSH rsync
我们可以使用SSH(Secure Shell)复制/同步数据,数据将以与加密的安全连接传输。
没有人可以在互联网上转移时读取数据。
使用rsync命令时,我们需要提供用户/root密码来完成特定任务。
使用SSH选项将以加密方式发送登录,以便密码安全。
将文件从远程复制到本地与特定的SSH端口。
要指定具有rsync命令的协议,我们需要使用要使用的协议名称使用-e
选项。
例如,以下rsync命令将通过远程机器从远程计算机中的远程shell复制文件"deploy.yaml"到用户"onitad"的主目录。
$rsync -avz -e "ssh -p 22" [email protected]:/home/cas/deploy.yaml /home/theitroad/ [email protected]'s password: receiving incremental file list sent 20 bytes received 60 bytes 32.00 bytes/sec total size is 239 speedup is 2.99
4.将本地计算机复制/同步文件和目录到远程计算机
假设我们要在本地主机中将文件夹"测试数据"复制/同步到远程计算机@ 10.9.8.41'的本地主机中的本地目录,运行命令:
$rsync -azrvh /home/theitroad/computer-networking.pdf [email protected]: [email protected]'s password: sending incremental file list computer-networking.pdf sent 1.04K bytes received 35 bytes 430.80 bytes/sec total size is 3.03K speedup is 2.81
其中:
-R
:递归副本
5.将文件和目录从远程计算机复制到本地计算机
rsync命令可在本地主机中传输远程目录"文档"。
假设我们要从远程主机'[email protected]'复制/同步文件/目录到用户"onitad"的主目录。
例如:
$rsync -zavhr [email protected]:document /home/theitroad [email protected]'s password: receiving incremental file list document/ document/kubernetes.txt sent 47 bytes received 139 bytes 74.40 bytes/sec total size is 0 speedup is 0.00
6. rsync -linclude和-Exclude选项
这两个选项允许我们通过使用这些选项指定参数来包含和排除文件,帮助我们指定要在同步中包含的文件或者目录,并排除与我们不想传输的文件和文件夹。
在此示例中,rsync命令将包含这些文件和目录,只以'k'开头,并排除所有其他文件和目录。
$rsync -avhz -e "ssh -p 22" --include 'k*' --exclude '*' [email protected]: /home/theitroad [email protected]'s password: receiving incremental file list ./ k8s.yaml kubernetes.yaml kuber/ sent 88 bytes received 280 bytes 147.20 bytes/sec total size is 73 speedup is 0.20
7.设置文件的最大大小
我们可以指定要传输或者同步的最大文件大小。
我们可以使用'--max大小'选项进行。
在以下示例中,最大文件大小为200k,因此此命令仅传输那些等于或者小于200k的文件。
$rsync -zavhr --max-size='200k' [email protected]: /home/theitroad [email protected]'s password: receiving incremental file list .bash_history .bash_logout .bashrc .profile .sudo_as_admin_successful .viminfo computer-networking.pdf sent 2.58K bytes received 51.60K bytes 21.67K bytes/sec total size is 113.24K speedup is 2.09
8.成功转移后删除源文件
现在,假设我们拥有一个Web服务器和数据备份服务器,我们创建了每日备份并将其与备份服务器同时同步,现在我们不希望保留Web服务器中的备份副本。
所以,我们是否等待转移完成,然后手动删除本地备份文件?
当然不。
此自动删除可以使用" - 重放源 - 文件"选项来完成。
$ls /home/directory computer-networking.pdf deploy.yaml document k8s.yaml kuber kubernetes.yaml ----- $rsync -azvh --remove-source-files /home/theitroad/deploy.yaml [email protected]: [email protected]'s password: sending incremental file list sent 70 bytes received 20 bytes 36.00 bytes/sec total size is 239 speedup is 2.66
再次运行'ls'命令,我们可以在src上删除文件"deploy.yaml"。
$ls computer-networking.pdf document k8s.yaml kuber kubernetes.yaml
9.在SRC和DEST之间找到文件和目录的差异
为了找到SRC和DEST之间目录文件中的任何区别,可以使用"-i"选项运行rsync命令。
$rsync -avzi /home/theitroad/cloud [email protected]: [email protected]'s password: sending incremental file list cd+++++++++ cloud/ <f+++++++++ cloud/computer-networking.pdf <f+++++++++ cloud/k8s.yaml <f+++++++++ cloud/kubernetes.yaml cd+++++++++ cloud/document/ <f+++++++++ cloud/document/kubernetes.txt cd+++++++++ cloud/kuber/ <f+++++++++ cloud/kuber/test.txt sent 1,472 bytes received 127 bytes 1,066.00 bytes/sec total size is 3,101 speedup is 1.94
10.限制带宽
使用'--bwlimit = <kb/s>'选项运行rsync命令时,可以设置带宽的限制。
假设我们希望将数据传输速率限制为300kb/s,请运行命令:
$rsync -avzh --bwlimit=300 /home/theitroad/coursera.png [email protected]: [email protected]'s password: sending incremental file list coursera.png sent 2.93M bytes received 35 bytes 234.48K bytes/sec total size is 3.00M speedup is 1.02
11.在SCP中失败时恢复转让
有时,我们必须通过SCP命令传输一个非常大的文件,而是在复制时出错,并且由于大文件大小和时间消耗,无法使用SCP命令再次开始传输。
在这种情况下,我们可以使用rsync命令开始从收到错误的位置复制文件。
例如,将主目录中的大型ISO文件复制到远程主机'[email protected]'
$scp /home/theitroad/ubuntu-18.04.4-live-server-amd64.iso [email protected]: [email protected]'s password: ubuntu-18.04.4-live-server-amd64.iso 32% 278MB 86.7MB/s 00:06 ETA^
该进程已被用户杀死。
那时,32%的文件被转移了。
现在,我们可以通过运行rsync继续当前的工作:
$rsync -P -avzh ubuntu-18.04.4-live-server-amd64.iso [email protected]: [email protected]'s password: sending incremental file list ubuntu-18.04.4-live-server-amd64.iso 912.26M 100% 15.89MB/s 0:00:54 (xfr#1, to-chk=0/1) sent 541.25M bytes received 132.44K bytes 9.25M bytes/sec total size is 912.26M speedup is 1.69
12.传输数据的同时显示进度
如果要在运行rsync命令时看到传输的进度,则可以使用"--progress"选项。
它将显示文件和剩余的时间来完成副本。
例如:
$rsync -avh --progress /home/theitroad/cloud/[email protected]: [email protected]'s password: sending incremental file list ./ computer-networking.pdf 3.03K 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=8/10) coursera.png 3.00M 100% 114.38MB/s 0:00:00 (xfr#2, to-chk=7/10) k8s.yaml 39 100% 1.52kB/s 0:00:00 (xfr#3, to-chk=6/10) kubernetes.yaml 34 100% 1.33kB/s 0:00:00 (xfr#4, to-chk=5/10) ubuntu-18.04.4-live-server-amd64.iso 912.26M 100% 89.60MB/s 0:00:09 (xfr#5, to-chk=4/10) document/ document/kubernetes.txt 0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=1/10) kuber/ kuber/test.txt 0 100% 0.00kB/s 0:00:00 (xfr#7, to-chk=0/10) sent 915.49M bytes received 164 bytes 79.61M bytes/sec total size is 915.26M speedup is 1.00