Linux 中 Rsync 命令的 16 个实用示例
Rsync(远程同步)是最常用的命令,用于在 Linux/Unix 系统中远程以及本地复制和同步文件和目录。
借助 rsync 命令,您可以跨目录、磁盘和网络远程和本地复制和同步数据、执行数据备份以及在两台 Linux 计算机之间进行镜像。
本文介绍了 16 使用 rsync 命令在基于 Linux 的计算机中远程和本地传输文件的基本和高级用法。您无需成为root用户即可运行rsync命令。
网站搜索
Rsync(远程同步)是最常用的命令,用于在 Linux/Unix 系统中远程以及本地复制和同步文件和目录。
借助 rsync 命令,您可以跨目录、磁盘和网络远程和本地复制和同步数据、执行数据备份以及在两台 Linux 计算机之间进行镜像。
本文介绍了 16 使用 rsync 命令在基于 Linux 的计算机中远程和本地传输文件的基本和高级用法。您无需成为root用户即可运行rsync命令。
它具有多种优势,包括:
rsync 命令遵循以下语法:
rsync [OPTIONS] SOURCE DESTINATION
以下是与 rsync 命令一起使用的不同组件和选项的说明:
-v
– 详细输出,显示有关传输的详细信息。-r
– 递归复制数据(但在传输数据时不保留时间戳和权限。-a
– 存档模式,允许递归复制文件,并且还保留符号链接、文件权限、用户和组所有权以及时间戳。-z
– 在传输过程中压缩文件以减少网络使用。-h
– 人类可读的,以人类可读格式输出数字。-P
– 显示传输过程中的进度。SOURCE
– 指定要传输的源文件或目录,可以是本地或远程位置。DESTINATION
– 指定将复制文件或目录的目标路径。与源类似,它可以是本地路径或远程位置。我们可以根据您的 Linux 发行版,在以下软件包管理器的帮助下安装 rsync 软件包。
sudo apt install rsync [On Debian, Ubuntu and Mint]
sudo yum install rsync [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/rsync [On Gentoo Linux]
sudo apk add rsync [On Alpine Linux]
sudo pacman -S rsync [On Arch Linux]
sudo zypper install rsync [On OpenSUSE]
要在本地复制或同步文件,您可以使用以下命令将本地计算机上的单个文件从一个位置同步到另一个位置。
在此示例中,需要将文件名 backup.tar 复制或同步到 /tmp/backups/ 文件夹。
[root@tecmint]# rsync -zvh backup.tar.gz /tmp/backups/
created directory /tmp/backups
backup.tar.gz
sent 224.54K bytes received 70 bytes 449.21K bytes/sec
total size is 224.40K speedup is 1.00
在上面的示例中,您可以看到,如果目标尚不存在,rsync 将自动为目标创建一个目录。
以下命令将把所有文件从一个目录传输或同步到同一台机器上的另一个目录。
在此示例中,/root/rpmpkgs 包含一些 rpm 软件包文件,并且您希望将该目录复制到 /tmp/backups/ 文件夹中。
[root@tecmint]# rsync -avzh /root/rpmpkgs /tmp/backups/
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm
rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm
sent 3.47M bytes received 96 bytes 2.32M bytes/sec
total size is 3.74M speedup is 1.08
要将目录从本地服务器复制到远程服务器,可以使用以下命令,该命令会将目录从本地计算机同步到远程计算机。
例如,如果您的本地计算机中有一个文件夹“rpmpkgs”,其中包含一些RPM软件包,并且您希望发送该本地目录的内容对于远程服务器,可以使用以下命令。
rsync -avzh /root/rpmpkgs [email :/root/
The authenticity of host '192.168.0.141 (192.168.0.141)' can't be established.
ED25519 key fingerprint is SHA256:bH2tiWQn4S5o6qmZhmtXcBROV5TU5H4t2C42QDEMx1c.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.0.141' (ED25519) to the list of known hosts.
[email 's password:
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm
rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm
sent 3.74M bytes received 96 bytes 439.88K bytes/sec
total size is 3.74M speedup is 1.00
此命令将帮助您将远程目录同步到本地目录。在此示例中,远程服务器上的目录 /root/rpmpkgs 被复制到本地计算机的 /tmp/myrpms 中。
rsync -avzh [email :/root/rpmpkgs /tmp/myrpms
[email 's password:
receiving incremental file list
created directory /tmp/myrpms
rpmpkgs/
rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm
rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm
sent 104 bytes received 3.49M bytes 997.68K bytes/sec
total size is 3.74M speedup is 1.07
通过rsync,我们可以使用SSH(Secure Shell)进行数据传输,在传输我们的数据时使用SSH协议您可以确保您的数据在安全连接中传输加密,这样当您的数据通过互联网传输时,没有人可以读取您的数据。
此外,当我们使用rsync时,我们需要提供用户/root密码来完成该特定任务,因此使用SSH 选项将以加密方式发送您的登录信息,以便您的密码安全。
要通过 SSH 使用 rsync,您可以使用 -e
选项指定远程 shell 命令,通常是 ssh,如图所示。
rsync [OPTIONS] -e ssh /path/to/source user@remote:/path/to/destination
要将文件从远程服务器同步到本地服务器,您可以使用“-e”选项和您要使用的协议名称,通过rsync指定协议。
在此示例中,我们将使用“ssh”和“-e”选项并执行数据传输。
rsync -avzhe ssh [email :/root/anaconda-ks.cfg /tmp
[email 's password:
receiving incremental file list
anaconda-ks.cfg
sent 43 bytes received 1.10K bytes 325.43 bytes/sec
total size is 1.90K speedup is 1.67
要使用 SSH 将文件从本地服务器同步到远程服务器,您可以利用以下命令,如下所示。
rsync -avzhe ssh backup.tar.gz [email :/backups/
[email 's password:
sending incremental file list
created directory /backups
backup.tar.gz
sent 224.59K bytes received 66 bytes 64.19K bytes/sec
total size is 224.40K speedup is 1.00
要显示将数据从一台计算机传输到另一台计算机时的进度,我们可以使用“--progress”
选项,该选项显示文件以及完成传输的剩余时间。
rsync -avzhe ssh --progress /root/rpmpkgs [email :/root/rpmpkgs
[email 's password:
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
1.47M 100% 31.80MB/s 0:00:00 (xfr#1, to-chk=3/5)
rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
138.01K 100% 2.69MB/s 0:00:00 (xfr#2, to-chk=2/5)
rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm
2.01M 100% 18.45MB/s 0:00:00 (xfr#3, to-chk=1/5)
rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm
120.48K 100% 1.04MB/s 0:00:00 (xfr#4, to-chk=0/5)
sent 3.74M bytes received 96 bytes 1.50M bytes/sec
total size is 3.74M speedup is 1.00
要在 rsync 操作期间包含特定文件或模式,您可以使用 --include
选项以及与所有文件匹配的扩展名。
rsync -avz --include='*.txt' /path/to/source/ user@remote:/path/to/destination/
在提供的示例中,rsync 在传输过程中将仅包含 /path/to/source/ 目录中扩展名为 .txt
的文件。
同样,要在 rsync 操作期间排除特定扩展名,您可以使用带有通配符模式的 --exclude
选项。
rsync -avz --exclude='*.ext' /path/to/source/ user@remote:/path/to/destination/
在提供的示例中,rsync 将在传输过程中排除具有指定扩展名 (*.ext)
的文件,同时包含所有其他文件和目录。
要在 rsync 操作期间包含和排除特定文件或模式,您可以将 --include
和 --exclude
选项与适当的通配符模式结合使用。
这两个选项允许我们通过指定参数包含和排除文件,这些选项帮助我们指定您想要包含在同步中的文件或目录以及排除文件和文件夹您不希望被转移。
在此示例中,rsync 命令将仅包含以 ‘R’
开头的文件和目录,并排除所有其他文件和目录。
rsync -avze ssh --include 'R*' --exclude '*' [email :/var/lib/rpm/ /root/rpm
如果文件或目录在源中不存在,但在目标中已存在,则您可能希望在同步时删除目标中的现有文件/目录。
我们可以使用“--delete
”选项来删除源目录中不存在的文件。
源和目标是同步的。现在在目标上创建一个新文件 test.txt。
[root@tecmint:~]# cd /root/rpm/
[root@tecmint:~/rpm]# touch test.txt
[root@tecmint:~/rpm]# rsync -avz --delete [email :/var/lib/rpm/ /root/rpm/
[email 's password:
receiving incremental file list
deleting test.txt
./
.dbenv.lock
.rpm.lock
Basenames
Conflictname
Dirnames
Enhancename
Filetriggername
Group
Installtid
Name
Obsoletename
Packages
Providename
Sha1header
Sigmd5
Suggestname
Supplementname
Transfiletriggername
Triggername
__db.001
__db.002
__db.003
sent 445 bytes received 18,543,954 bytes 2,472,586.53 bytes/sec
total size is 71,151,616 speedup is 3.84
目标有一个名为 test.txt 的新文件,当使用“–delete
”选项与源同步时,它删除了该文件 test.txt 。
您可以指定要传输或同步的最大文件大小。您可以使用“--max-size
”选项来完成此操作。在此示例中,最大文件大小为200k,因此此命令将仅传输等于或小于200k的文件。
rsync -avzhe ssh --max-size='200k' /var/lib/rpm/ [email :/root/tmprpm
[email 's password:
sending incremental file list
created directory /root/tmprpm
./
.dbenv.lock
.rpm.lock
Conflictname
Enhancename
Filetriggername
Group
Installtid
Name
Obsoletename
Recommendname
Requirename
Sha1header
Sigmd5
Suggestname
Supplementname
Transfiletriggername
Triggername
__db.002
sent 129.52K bytes received 396 bytes 28.87K bytes/sec
total size is 71.15M speedup is 547.66
现在,假设您有主 Web 服务器和数据备份服务器,您创建了每日备份并将其与备份服务器同步,但现在您不想在 Web 服务器中保留备份的本地副本。
那么,您会等待传输完成,然后手动删除该本地备份文件吗?当然不。可以使用“--remove-source-files
”选项来完成此自动删除。
rsync --remove-source-files -zvh backup.tar.gz [email :/tmp/backups/
[email 's password:
backup.tar.gz
sent 795 bytes received 2.33K bytes 894.29 bytes/sec
total size is 267.30K speedup is 85.40
[root@tecmint:~]# ls -l backup.tar.gz
ls: cannot access 'backup.tar.gz': No such file or directory
如果您是 rsync 的新手,并且不知道您的命令到底要做什么。 Rsync 确实可能会弄乱目标文件夹中的内容,然后进行撤消可能是一项乏味的工作。
使用此选项不会对文件进行任何更改并显示命令的输出,如果输出显示与您想要的完全相同,那么您可以删除 '--dry-run
' 命令中的选项并在终端上运行。
rsync --dry-run --remove-source-files -zvh backup.tar.gz [email :/tmp/backups/
[email 's password:
backup.tar.gz
sent 50 bytes received 19 bytes 19.71 bytes/sec
total size is 267.30K speedup is 3,873.97 (DRY RUN)
您可以借助“--bwlimit
”选项设置将数据从一台计算机传输到另一台计算机时的带宽限制。此选项帮助我们限制I/O带宽。
rsync --bwlimit=100 -avzhe ssh /var/lib/rpm/ [email :/root/tmprpm/
[email 's password:
sending incremental file list
sent 324 bytes received 12 bytes 61.09 bytes/sec
total size is 38.08M speedup is 113347.05
另外,默认情况下,rsync 仅同步更改的块和字节,如果您明确想要同步整个文件,则可以使用“-W”选项。
rsync -zvhW backup.tar /tmp/backups/backup.tar
backup.tar
sent 14.71M bytes received 31 bytes 3.27M bytes/sec
total size is 16.18M speedup is 1.10
我们对 rsync 及其功能的概述到此结束。为了进一步探索其广泛的选项和功能,我鼓励您参考可用的综合手册页(手册页)。