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

rsync Command Syntax

Syntax of rsync command is like below.

rsync OPTIONS  SOURCE  DESTINATION
  • 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

rsync Command Man Page

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

$ 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

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

$ rsync -azvh test1 /home/ismail/backup/
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 

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

And run systemctl to start service.

$ 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

Copy/Sync File To A Remote Server

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

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

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

$ 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

$ rsync -azvh  ubu1:/home/ismail/backup/test1 test_1
Copy/Sync Directories From A Server
Copy/Sync Directories From A Server

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 -azvh -e ssh  ubu1:/home/ismail/backup/test1 test_1
Transmission Over Ssh
Transmission Over 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.

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

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

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

$ 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 -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 -avz --log-file=back.log test_1 ubu1:/home/ismail/web_backup
Create Log File
Create Log File

LEARN MORE  How To Use mRemoteNG For RDP, VNC, SSH Remote Connections?

2 thoughts on “rsync Command Tutorial With Examples”

Leave a Comment