Rsync Command Tutorial With Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/rsync-command-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM

Rsync is popular tool used for copying, backup and synchrnizing. Rsync can work locally or remotely over network.  Rsync can also be used to clone some site to multiple site. Popular backup tools like LuckyBackup also uses rsync in the background. We will look various examples of rsync in this tutorial. Here is 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 behavior of the rsync
  • SOURCE is source of the data path
  • DATA is destination of data path

Help With Man Page

There are 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 is generally happen if we mounted a remote file system locally or mounted an external storage.

In this example we will sync 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 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 following configuration to the /etc/rsyncd.conf 

LEARN MORE  How To Use Rsync Over Ssh In A Secure Manner?

And run systemctl to start service.

And check the status of daemon

Start Rsync Daemon, Service

Start Rsync Daemon, Service

Copy/Sync File To A Server

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 host name 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 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 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. Other part of the rsync command is the same.

LEARN MORE  Linux Rsync Exclude File,Directory and Folder Examples

Transmission Over Ssh

Transmission Over Ssh

Showing Progress

While transferring a lot of files  knowing the status of transfer is  very important. We can show 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 source but all ready exists in the destination. In this situation to sync both sides destination copy should be deleted. –delete options provides these mechanism 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 exists in the source. There is similar option to delete source files if they do not needed. 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  Linux Rsync Resume Partial File Transfers

Dry Run

Dry Run

Set Bandwidth

Another useful option for rsync is limiting bandwidth usage. This is 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. Log file can be saved with –log-file option like below.

Create Log File

Create Log File

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/rsync-command-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

1 Response

  1. 16/06/2017

    […] Rsync Command Tutorial With Examples […]

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)