Rsync Command Tutorial With Examples – POFTUT

Rsync Command Tutorial With Examples

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.

  • Copying directories
  • Backup options
  • Rich feature options
  • Dry run capability
  • Run as Daemon or Server
  • Works over ssh
  • Permission retention

Syntax

Syntax of rsync is like below.

  • OPTIONS used to change the behavior of the rsync
  • SOURCE is a source of the data path
  • DATA is the destination of the data path

Help With Man Page

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

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

Copy/Sync File Locally

Copy/Sync File Locally

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.

Copy/Sync Directories Locally

Copy/Sync Directories Locally

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 

LEARN MORE  Linux cp Directory and Content

And run systemctl to start service.

And check the status of the daemon.

Start Rsync Daemon, Service

Start Rsync Daemon, Service

Copy/Sync File To A Server

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

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.

Copy/Sync Directories To A Server

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

Copy/Sync Directories To A Server

Copy/Sync Directories To A Server

Copy/Sync File From A Server

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

Copy/Sync File From A Server

Copy/Sync File From A Server

Copy/Sync Directories From A 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

Copy/Sync Directories From A Server

Copy/Sync Directories From A Server

Transmission Over Ssh

Rsync daemon provides remote transmission of files but there is a problem. It is not so much 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.

LEARN MORE  How To Specify Different Port for Linux Rsync?

Transmission Over Ssh

Transmission Over Ssh

Showing 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.

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 do not exist in the source but already exists in the destination. In this situation to sync, both sides destination copy should be deleted. –delete options provide these mechanisms to work.

Deleting Option

Deleting Option

Set Max File Size To Transfer

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

Set Max File Size To Transfer

Set Max File Size To Transfer

Delete Source After Transfer

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. 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.

Delete Source After Transfer

Delete Source After Transfer

Dry Run

Up to now after issuing command every change was real. In some situation, 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.

LEARN MORE  How To Copy Entire Directory In Linux?

Dry Run

Dry Run

Set 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.

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 logfile can be saved with –log-file option like below.

Create Log File

Create Log File

2 Responses

  1. Duncan says:

    Very useful – many thanks.

  1. 16/06/2017

    […] Rsync Command Tutorial With Examples […]

Leave a Reply

Your email address will not be published. Required fields are marked *