在Linux中rsync命令的实例

rsync(远程同步)是在Linux/Unix系统中用于远程以及本地复制和同步文件和目录的最常用命令。有了rsync命令的帮助,你能够在目录,磁盘和网络之间远程地和本地地复制和同步你地数据,执行数据备份并且在两台Linux机器间做镜像。

这篇文件解释了rsync命令的10基本以及高级用法,用于在基于Linux机器中远程和本地地传递你的文件。运行rsync命令,你不需要是root用户。

rsync命令的优点和特性

  1. 与远程系统之间高效复制和同步文件。
  2. 支持复制链接,设备,所有者,所属组和权限。
  3. 因为rsync使用一个远程更新协议,其允许在两个文件集合之间只传递差异,所以它快于scp(安全复制)。第一次,它从源位置向目标位置复制一个文件或者一个目录的整个内容,但从第二次开始,它只复制变化的块和字节到目标位置。
  4. 因为rsync在两端发送和接收数据时使用压缩和解压方法,所以它消耗更少的带宽。

rsync命令的基本语法

# rsync options source destination

与rsync命令一起使用的常用选项

  1. -v:详细
  2. -r:递归地复制数据(但在传递数据时不保留时间戳和权限)
  3. -a:存档模式,它允许递归的复制文件并且它也保留符号链接,文件权限,用户和组所有者,和时间戳。
  4. -z:压缩文件数据。
  5. -h:人可读,用人可读格式输出数据。

在Linux系统安装Rsync

在Rocky Linux中用dnf安装rsync软件包

[root@rockygu test]# dnf install rsync
Last metadata expiration check: 1:40:37 ago on Sat 02 Jul 2022 03:15:17 PM CST.
Package rsync-3.1.3-12.el8.x86_64 is already installed.
Dependencies resolved.
...
  rsync-3.1.3-14.el8_6.2.x86_64

Complete!

1、本地复制/同步文件和目录

在本地计算机上复制同步一个文件

以下命令将在本地机器上从一个位置到另一个位置同步一个文件。在这个示例中,一个称为base-7.0.4.1.tar.gz的文件被复制或者同步到/tmp/backups文件夹。

[root@rockygu ~]# rsync -zvh base-7.0.4.1.tar.gz /tmp/backups/
created directory /tmp/backups
base-7.0.4.1.tar.gz

sent 3.02M bytes  received 70 bytes  6.05M bytes/sec
total size is 3.02M  speedup is 1.00

在以上示例中,你会发现如果目标位置还不存在,rsync将自动为目标位置创建一个目录。

在本地计算机上复制/同步一个目录

以下命令将在相同机器上从一个目录传递或同步所有文件到一个不同目录。在这个示例中,/root/test包含一些文件并且你想要那个目录被复制到/tmp/backups文件夹。

[root@rockygu ~]# rsync -avzh test /tmp/backups/
sending incremental file list
test/
test/.hidden
test/guid.txt
test/perm.txt
test/rock.html
test/rocky.c
test/rocky.sh
test/suid.txt
test/fold/

sent 520 bytes  received 157 bytes  1.35K bytes/sec
total size is 0  speedup is 0.00
[root@rockygu ~]# ls /tmp/backups/
base-7.0.4.1.tar.gz  test

2、复制/同步文件和目录到一台服务区或从一台服务区复制/同步文件和目录

从本地服务器复制目录到一台远程服务器

这条命令将从一台本地机器同步一个目录到一台远程机器。例如,在你本地计算机中有一个文件夹test,其包含了一些文件并且你想要本地目录的内容发送到一台远程服务器,你可以使用以下命令:

[root@rockygu ~]# rsync -avzh /root/test root@192.168.50.228:/root/
The authenticity of host '192.168.50.228(192.168.50.228)' can't be established.
ECDSA key fingerprint is SHA256:yRftlbbUZid2Ua7fyyibCupkG7i56/M7yGbRe/5Wa3c.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.50.228' (ECDSA) to the list of known hosts.
root@192.168.50.228's password:
sending incremental file list
test/
test/.hidden
test/guid.txt
test/perm.txt
test/rock.html
test/rocky.c
test/rocky.sh
test/suid.txt
test/fold/

sent 524 bytes  received 161 bytes  72.11 bytes/sec
total size is 0  speedup is 0.00

从远程目录复制/同步到一台本地机器

这条命令将帮助你同步远程目录到一个本地目录。在这个示例中,在远程服务器上有一个/root/test目录,它被复制到你本地计算机/tmp/mytest中。

[root@rockygu ~]# rsync -avzh root@192.168.50.228:/root/test /tmp/mytest
root@192.168.50.228's password:
receiving incremental file list
created directory /tmp/mytest
test/
test/.hidden
test/guid.txt
test/perm.txt
test/rock.html
test/rocky.c
test/rocky.sh
test/suid.txt
test/fold/

sent 169 bytes  received 520 bytes  125.27 bytes/sec
total size is 0  speedup is 0.00

[root@rockygu ~]# ls  -R /tmp/mytest
/tmp/mytest:
test

/tmp/mytest/test:
fold  guid.txt  perm.txt  rock.html  rocky.c  rocky.sh  suid.txt

/tmp/mytest/test/fold:

3、通过SSH的rsync

对rsync,你可以使用SSH(安全shell)用于数据传输,在传递我们的数据时,使用SSH协议,你能够确保用一个加密的安全连接传递你的数据,因而没人能够在你的数据在网络上传输时读取它。

当我们使用rsync,我们需要提供user/root密码来完成那个特定任务,所以使用SSH选项将用加密方式发送你的登录,因而你的密码将安全。

用SSH从一台远程服务器复制一个文件到一个本地服务器

要对rsync指定一个协议,你需要给出-e选项以及你想要使用的协议名称。在这个示例中,我们将使用指定ssh的-e选项并且执行数据传输。

[root@rockygu ~]# rsync -avzhe ssh root@192.168.50.228:/root/hello.txt /tmp
root@192.168.50.228's password:
receiving incremental file list
hello.txt

sent 43 bytes  received 103 bytes  22.46 bytes/sec
total size is 12  speedup is 0.08
[root@rockygu ~]# ls /tmp/
backups  hello.txt  mytest  rock.txt 

用SSH从本地服务器复制一个文件到一台远程服务器

[root@rockygu ~]# rsync -avzhe ssh base-7.0.4.1.tar.gz  root@192.168.50.228:/root/backups/
root@192.168.50.228's password:
sending incremental file list
created directory /root/backups
base-7.0.4.1.tar.gz

sent 3.02M bytes  received 71 bytes  123.38K bytes/sec
total size is 3.02M  speedup is 1.00

 

4、在用rsync传输数据时显示进度

在从一台机器传递数据到一台不同机器时,要显示进度,你可以使用'--progress'选项。它显示完成这次传输剩余的文件和时间。

[root@rockygu ~]# rsync -avzhe ssh --progress base-7.0.4.1.tar.gz  root@192.168.50.228:/root/backups/
root@192.168.50.228's password:
sending incremental file list
created directory /root/backups
base-7.0.4.1.tar.gz
          3.02M 100%  599.18kB/s    0:00:04 (xfr#1, to-chk=0/1)


sent 3.02M bytes  received 71 bytes  118.54K bytes/sec
total size is 3.02M  speedup is 1.00

5、--include和--exclude选项的使用

这两个选项允许我们通过与这些选项一起指定参数包含和排除文件,帮助我们指定你想要在你的同步中你想要包含哪些文件或目录以及排除你不要行传输的文件和文件夹。

在这个示例中,rsync命令将包含其名称以'R'起始的文件和目录并且排除所有其它文件和目录。

[root@rockygu test]# rsync -avze ssh --include 'R*' --exclude '*'  ./ root@192.168.50.228:/root/want
sending incremental file list
created directory /root/want
./
Rock.txt
Rocky/

sent 158 bytes  received 79 bytes  474.00 bytes/sec
total size is 5  speedup is 0.02

6、delelte选项的用法

如果一个文件或目录在源位置不存在,但已经在目标地址存在了,在同步时,你会想要删除在目标位置已有的文件/目录。

我们可以使用'--delete'选项删除在源目录中没有的文件。

源位置和目标位置在同步中。现在在目标位置创建一个新文件hello.txt。

[root@rockygu test]# rsync -avz --delete ./ 192.168.50.228:/root/want
root@106.13.171.84's password:
sending incremental file list
deleting hello.txt
./
.hidden
guid.txt
perm.txt
rock.html
rocky.c
rocky.sh
suid.txt
Rocky/1.txt
Rocky/2.txt
Rocky/3.txt
Rocky/4.txt
Rocky/5.txt
fold/

sent 831 bytes  received 269 bytes  244.44 bytes/sec
total size is 5  speedup is 0.00

目标位置有称为hello.txt的新文件,当用'--delete'与源位置同步时,它移除文件hello.txt。

7、设置要传输文件的最大尺寸

你可以指定要传输或者同步的最大文件尺寸。你可以用"--max-size"选项做这件事。在这个示例中,最大文件尺寸是1M,因此这条命令将传输那些等于或小于1M的文件。

[root@rockygu test]# ls
base-7.0.4.1.tar.gz  fold  guid.txt  perm.txt  rock.html  Rock.txt  Rocky  rocky.c  rocky.sh  suid.txt
[root@rockygu test]# rsync -avzhe ssh --max-size='1m' ./ root@192.168.50.228:/root/tmp/
root@192.168.50.228's password:
sending incremental file list
created directory /root/tmp
./
.hidden
Rock.txt
guid.txt
perm.txt
rock.html
rocky.c
rocky.sh
suid.txt
Rocky/
Rocky/1.txt
Rocky/2.txt
Rocky/3.txt
Rocky/4.txt
Rocky/5.txt
fold/

sent 918 bytes  received 310 bytes  98.24 bytes/sec
total size is 3.02M  speedup is 2,460.44

8、现在,假设你有主网服务器和一个数据备份服务器,你创建一个每日备份并且与你的备份服务器同步它,现在你不想要在你主网服务器中保存备份的当地副本。

所以你将等待传输结束并且接着手动删除本地副本?当然不用,可以使用--remove-source-file选项自动进行删除。

base-7.0.4.1.tar.gz  fold  guid.txt  perm.txt  rock.html  Rock.txt  Rocky  rocky.c  rocky.sh  suid.txt
[root@rockygu test]# rsync --remove-source-files -zvh base-7.0.4.1.tar.gz root@192.168.50.228:/root/tmp
root@192.168.50.228's password:
base-7.0.4.1.tar.gz

sent 3.02M bytes  received 43 bytes  118.54K bytes/sec
total size is 3.02M  speedup is 1.00
[root@rockygu test]# ls
fold  guid.txt  perm.txt  rock.html  Rock.txt  Rocky  rocky.c  rocky.sh  suid.txt

9、用rsync进行一次干跑

如果你是一个使用rsync的新人并且不知道你的命令确切做什么。rsync真的会在你的目标文件夹中弄乱东西并且撤销会是枯燥工作。

使用这个选项将不对文件做任何更改并且显示此命令的输出,如果输出确切显示你想要的相同,则你可以从你的命令移除'--dry-run'选项并且在终端上运行。

[root@rockygu test]# ls
fold  guid.txt  perm.txt  rock.html  Rock.txt  Rocky  rocky.c  rocky.sh  suid.txt  test.tar.gz
[root@rockygu test]# rsync --dry-run --remove-source-files -zvh test.tar.gz 
root@106.13.171.84's password:
test.tar.gz

sent 48 bytes  received 19 bytes  4.06 bytes/sec
total size is 3.02M  speedup is 45,095.81 (DRY RUN)
[root@rockygu test]# ls
fold  guid.txt  perm.txt  rock.html  Rock.txt  Rocky  rocky.c  rocky.sh  suid.txt  test.tar.gz

10、rsync设置带宽限制并且传递文件

你用'bwlimit'选项在从一台机器传输数据到另一台机器时,你可以设置带宽限制。这个选项帮助我们限制I/O带宽。

[root@rockygu test]# rsync --bwlimit=100 -avzhe ssh test.tar.gz  root@192.168.50.228:/root/tmp
root@192.168.50.228's password:
sending incremental file list
test.tar.gz

sent 3.02M bytes  received 35 bytes  87.62K bytes/sec
total size is 3.02M  speedup is 1.00

默认rsync只同步变化的块和字节,如果你想要显式地同步整个文件,则你与它一起使用'-W'选项。

[root@rockygu test]# rsync -zvhW test.tar.gz  root@192.168.50.228:/root/tmp/test.tar.gz
root@192.168.50.228's password:

sent 45 bytes  received 12 bytes  16.29 bytes/sec
total size is 3.02M  speedup is 53,007.35
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值