rsync 命令_rsync命令教程示例

rsync 命令_rsync命令教程示例rsync 命令 Rsyncisapopu backupandsyn Rsynccanwork Rsynccanalso Popularb

rsync 命令

rsync 命令

Rsync is a popular tool used for copying, backup and synchronizing. Rsync can work locally or remotely over the network.  Rsync can also be used to clone some site to multiple sites. Popular backup tools like LuckyBackup also use rsync in the background. We will look at various examples of rsync in this tutorial. Here are some features of rsync.

Rsync是一种流行的工具,用于复制,备份和同步。 Rsync可以在本地或通过网络远程工作。 Rsync还可以用于将某些站点克隆到多个站点。 诸如LuckyBackup之类的流行备份工具也在后台使用rsync。 我们将在本教程中查看rsync的各种示例。 这是rsync的一些功能。

  • Copying directories

    复制目录

  • Backup options

    备份选项

  • Rich feature options

    丰富的功能选项

  • Dry run capability

    空转能力

  • Run as Daemon or Server

    作为守护程序或服务器运行

  • Works over ssh

    通过SSH工作

  • Permission retention

    权限保留

rsync命令语法(rsync Command Syntax)

Syntax of rsync command is like below.

rsync命令的语法如下。

rsync OPTIONS SOURCE DESTINATION
  • OPTIONS used to change the behavior of the rsync

    用于更改rsync行为的选项

  • SOURCE is a source of the data path

    SOURCE是数据路径的来源

  • DATA is the destination of the data path

    DATA是数据路径的目的地

rsync命令手册页(rsync Command Man Page)

There is a lot of options rsync command to get details about these following command can be used.

rsync命令有很多选项,可获取有关可以使用以下命令的详细信息。

$ man rsync
Help With Man Page
Help With Man Page
帮助手册页

本地复制/同步文件(Copy/Sync File Locally)

We will copy files and directories locally. This generally happens if we mounted a remote file system locally or mounted external storage. In this example, we will sync the file named tmux.tar.gz into /home/ismail/backup

我们将在本地复制文件和目录。 如果我们在本地安装了远程文件系统或安装了外部存储,通常会发生这种情况。 在此示例中,我们将名为tmux.tar.gz的文件同步到/home/ismail/backup

$ rsync -zvh tmux.tar.gz /home/ismail/backup/

本地复制/同步目录 (Copy/Sync Directories Locally)

Copying or synchronizing directories is a bit different from files. We will provide the extra option -a like below. This copy operation will run recursively.

复制或同步目录与文件有点不同。 我们将提供额外的选项-a,如下所示。 此复制操作将递归运行。

$ rsync -azvh test1 /home/ismail/backup/
Copy/Sync Directories Locally
Copy/Sync Directories Locally
本地复制/同步目录

启动Rsync守护程序,服务(Start Rsync Daemon, Service)

In our examples, we are using Ubuntu distribution but this will work other distributions as well. Rsync can be run as a daemon or service. Rsync daemon is called rsyncd and needs some configuration to start. Following simple configuration can be used as rsync configuration. Put the following configuration to the /etc/rsyncd.conf 

在我们的示例中,我们使用的是Ubuntu发行版,但这也可以在其他发行版中使用。 Rsync可以作为守护程序或服务运行。 Rsync守护程序称为rsyncd,需要一些配置才能启动。 以下简单配置可以用作rsync配置。 将以下配置放入/etc/rsyncd.conf

uid             = rsync gid             = rsync use chroot      = no pid file        = /var/run/rsyncd.pid 

And run systemctl to start service.

并运行systemctl以启动服务。

$ sudo systemctl start rsync.service

And check the status of the daemon.

并检查守护程序的状态。

$ sudo systemctl status rsync.service
Start Rsync Daemon, Service
Start Rsync Daemon, Service
启动Rsync守护程序,服务

复制/同步文件到远程服务器(Copy/Sync File To A Remote Server)

A single file named tmux.tar.gz can be copied like below.

可以如下复制一个名为tmux.tar.gz的文件。

$ rsync -zvh tmux.tar.gz ubu1:/home/ismail/backup
Copy/Sync File To A Server
Copy/Sync File To A Server
复制/同步文件到服务器

Or remote destination hostname is ubu1 we can also specify user named like below.

或远程目标主机名是ubu1,我们也可以指定用户名,如下所示。

$ rsync -zvh tmux.tar.gz[email protected]:/home/ismail/backup

将目录复制/同步到远程服务器 (Copy/Sync Directories To A Remote Server)

Directories can be copied to the remote server like below. But keep in mind that rsynd daemon user rights can prevent copying operation.

目录可以复制到远程服务器,如下所示。 但是请记住,rsynd守护程序用户权限会阻止复制操作。

$ rsync -azvh test1[email protected]:/home/ismail/backup/
Copy/Sync Directories To A Server
Copy/Sync Directories To A Server
将目录复制/同步到服务器

从远程服务器复制/同步文件(Copy/Sync File From A Remote Server)

We can use remote servers to get files to the local server. Generally used for restoring from backup.

我们可以使用远程服务器将文件获取到本地服务器。 通常用于从备份还原。

$ rsync -zvh ubu1:/home/ismail/backup/tmux.tar.gz .
Copy/Sync File From A Server
Copy/Sync File From A Server
从服务器复制/同步文件

从远程服务器复制/同步目录(Copy/Sync Directories From A Remote Server)

We can also sync directories and files from remote servers. This operation is similar to the file operations. In this example, we will sync the remote server named ubu1 with our local directory test_1 . Remote directory is /home/ismail/backup/test1

我们还可以同步来自远程服务器的目录和文件。 此操作类似于文件操作。 在此示例中,我们将使名为ubu1的远程服务器与本地目录test_1 。 远程目录为/home/ismail/backup/test1

$ rsync -azvh ubu1:/home/ismail/backup/test1 test_1
Copy/Sync Directories From A Server
Copy/Sync Directories From A Server
从服务器复制/同步目录

通过SSH进行rsync安全传输(rsync Transmission Over SSH Securely)

Rsync daemon provides remote transmission of files but there is a problem. It is not so secure because I transfer data as clear text. What is the alternative? As we always use ssh is the solution. As we know ssh have tunneling ability is can transmit data over secured encrypted tunnels. We will provide -e ssh parameter to the rsync command. Another part of the rsync command is the same.

Rsync守护程序提供文件的远程传输,但是存在问题。 它不是那么安全,因为我以明文形式传输数据。 有什么选择? 因为我们一直使用ssh是解决方案。 众所周知,ssh具有隧道功能,可以通过安全的加密隧道传输数据。 我们将向rsync命令提供-e ssh参数。 rsync命令的另一部分是相同的。

$ rsync -azvh -e ssh ubu1:/home/ismail/backup/test1 test_1
Transmission Over Ssh
Transmission Over Ssh
通过SSH传输

显示同步操作进度(Showing Sync Operation Progress)

While transferring a lot of files knowing the status of the transfer is very important. We can show the progress of sync with –progress option like below.

在传输许多文件时,了解传输状态非常重要。 我们可以使用–progress选项显示同步进度,如下所示。

$ rsync -azvh --progress ubu1:/home/ismail/backup/ tmux

删除不存在的文件 (Delete Nonexistent Files)

On the sync operations, the aim is to make source and destination directory the same. In some situations, a file or folder does not exist in the source but already exists in the destination. In this situation to sync, both side’s destination copy should be deleted. –delete options provide these mechanisms to work.

在同步操作上,目的是使源目录和目标目录相同。 在某些情况下,源中不存在文件或文件夹,但目标中已经存在文件或文件夹。 在这种情况下进行同步,应删除双方的目标副本。 –删除选项提供了这些机制。

$ rsync -avz --delete test1 test_1/
Deleting Option
Deleting Option
删除选项

设置要传输的最大文件大小(Set Max File Size To Transfer)

Another important filter option is limiting the maximum file size for transfer. We can set this filter by using –max-size option like below.

另一个重要的过滤器选项是限制传输的最大文件大小。 我们可以使用如下所示的–max-size选项来设置此过滤器。

$ rsync -avz --max-size='1K' test_1 test_1.bak
Set Max File Size To Transfer
Set Max File Size To Transfer
设置要传输的最大文件大小

传输完成后删除源(Delete Source After Transfer Is Complete)

We have already looked to delete option which will delete destination files if they do not exist in the source. There is a similar option to delete source files if they do not need it. For example, we want to delete backup files and folders after sync operation because we do not need them. Here we can use –remove-source-files option t accomplish this.

我们已经考虑过删除选项,如果源文件中不存在目标文件,它将删除目标文件。 如果不需要,可以使用类似的选项删除源文件。 例如,我们要在同步操作后删除备份文件和文件夹,因为我们不需要它们。 在这里,我们可以使用–remove-source-files选项t完成此操作。

$ rsync -avz --remove-source-files test_1 test_1.bak
Delete Source After Transfer
Delete Source After Transfer
转移后删除源

空运行或仅测试或模拟(Dry Run or Just Test or Simulate)

Up to now after issuing command every change was real. In some situations, we may want to only see what will happen with the command but do not want to start action we can use –dry-run option.

到目前为止,在发出命令后,所有更改都是真实的。 在某些情况下,我们可能只想查看命令会发生什么,而又不想开始执行可以使用–dry-run选项的操作。

$ rsync -avz --dry-run test_1 test_1_back
Dry Run
Dry Run
空跑

设置传输带宽(Set Transfer Bandwidth)

Another useful option for rsync is limiting bandwidth usage. This is a very important option because some networks have limited bandwidth. Enabling rsync can hurt other network traffic because of bulk transfer sizes. We can easily limit network traffic with –bwlimit option like below.

rsync的另一个有用选项是限制带宽使用。 这是非常重要的选择,因为某些网络的带宽有限。 由于批量传输大小,启用rsync可能会损害其他网络流量。 我们可以使用–bwlimit选项轻松限制网络流量,如下所示。

$ rsync -avz --bwlimit=500k test_1 ubu1:/home/ismail/web_backup
Set Bandwidth
Set Bandwidth
设置带宽

创建日志文件(Create Log File)

Rsync is generally used for non-interactive batch operations. During these operations, there will be a lot of different actions or errors like copying, deleting, permission error, etc. To get information about these events logs should be saved and reviewed. The log file can be saved with –log-file option like below.

Rsync通常用于非交互式批处理操作。 在这些操作期间,将有很多不同的操作或错误,例如复制,删除,权限错误等。要获取有关这些事件的信息,应保存并检查日志。 可以使用–log-file选项保存日志文件,如下所示。

$ rsync -avz --log-file=back.log test_1 ubu1:/home/ismail/web_backup
Create Log File
Create Log File
创建日志文件

LEARN MORE  How To Enable Powershell Remoting PSRemoting or WinRM?

了解更多信息如何启用Powershell Remoting PSRemoting或WinRM?

翻译自: https://www.poftut.com/rsync-command-tutorial-examples/

rsync 命令

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/177044.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午8:26
下一篇 2026年3月26日 下午8:27


相关推荐

  • python游戏代码200行_python 贪吃蛇

    python游戏代码200行_python 贪吃蛇python语言,总所周知是比较简单的,而且代码也不会像java那样多,下面就使用python的第三方库pygame进行开发一个贪吃蛇游戏。1.pygame的安装直接在cmd当中使用pipinstallpygame进行安装。或者在pycharm当中自动导入安装也OK2.全局变量的定义在代码当中会使用到很多这种变量的值,直接在最开始进行定义,后面获取变量即可W=600#屏幕宽H=400#高fps=12#帧率size=(W,H)ROW=

    2022年8月11日
    11
  • 为什么贝叶斯是量化工作者最常用的工具

    为什么贝叶斯是量化工作者最常用的工具▌浅谈贝叶斯不论是学习概率统计还是机器学习的过程中,贝叶斯总是是绕不过去的一道坎,大部分人在学习的时候都是在强行地背公式和套用方法,没有真正去理解其牛逼的思想内涵。我看了一下Chalmers一些涉及到贝叶斯统计的课程,content里的第一条都是PhilosophyofBayesianstatistics。▌历史背景什么事都要从头说起,贝叶斯全名为托马斯…

    2022年5月22日
    44
  • IDEA配置方法注释模板

    IDEA配置方法注释模板打开 liveTemplate 第 4 步点击 选择 liveTemplate 第 6 步输入下方内容 param return Author huangyuewang Date date time 第 8 步选择文件类型 java 第 9 步按图输入 param 和 return 代码在下面 param 参数脚本 groovyScript if 1 length 2 return

    2026年3月16日
    3
  • java中instant_Instant

    java中instant_Instant我们已经讲过 计算机存储的当前时间 本质上只是一个不断递增的整数 Java 提供的 System currentTimeM 返回的就是以毫秒表示的当前时间戳 这个当前时间戳在 java time 中以 Instant 类型表示 我们用 Instant now 获取当前时间戳 效果和 System currentTimeM 类似 importjava time public

    2026年3月19日
    1
  • 编程怎么入门_损失函数的基本概念和原理

    编程怎么入门_损失函数的基本概念和原理介绍了Socket编程的基本概念,并以UDP编程为例,给出示例。

    2022年10月18日
    4
  • Android接入穿山甲SDK并实现开屏广告 接入

    Android接入穿山甲SDK并实现开屏广告 接入

    2021年10月2日
    139

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号