rsync 命令_rsync命令教程示例

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 命令

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值