How To Use Rsync to Sync Local and Remote Directories on Linux

How To Use Rsync to Sync Local and Remote Directories on Linux

Rsync (Remote Sync) is a commonly used tool to transfer and synchronize files and directories across computer system.

By using rsync command, you can easily transfer file on remote server or can take backup on your local system remotely.

It was first announced on 19 june 1996 written by Andrew Tridgell and Paul Mackerras.

It has becoma a powerful Linux utility because of its flexibility and speed.

Some features of Rsync command
  • Support for copying devices, links, owners, groups and permissions.
  • Does not require any special root privileges to install.
  • Easily transfer files to or from a remote system.
  • rsync use remote update protocol that transfer only differences in the file which means first time it transfer complete files from source to destination but later once you made any changes in the file then it transfer the changed blocks to the destination that's the reason it is faster than scp.
  • Mostly used for backups and mirroring.
  • Rsync does not consume high bandwidth because it use compression method while transferring data.
Rsync Syntax :
# rsync options source destination

Some common options used with rsync commands

  • -v: for verbose.
  • -r: for recursive that is used to sync directory.
  • -a: for archive mode that is used to sync recursively and preserves symbolic links, modification times, group, owner, and permissions.
  • -z: for adding compression.
  • -u: for update
  • -b: for backup
  • -h: for human-readable
Install rsync in your Linux machine (Debian based systems)

You can install rsync package in your linux system by running following line of command :

# apt-get install rsync
1. Copy/Sync a File on a Local System

In this example, we will sync a single file from one location to other location on local machine.

I have a file name xyz.tar and want to sync to /tmp/backups/ folder.

# rsync -zvh xyz.tar /tmp/backups/

By running above command, you will see if destination directory is not exists then it will create directory automatically and then sync file.

2. Copy/Sync a Directory on a Local System

In this example, we will sync all files from one directory to other directory in same local machine.

I have directory /var/www/app that contains some files that will be sync to /var/www/app1 directory by running following command.

# rsync -avzh /var/www/app /var/www/app1
3. Copy/Sync a Directory from Local to Remote Server

In this example, we will copy all files of a directory from local machine to directory of remote machine.

Suppose you have a project in /var/www/app directory and you want to copy all files within this directory to remote machine then you can go with following command :

$ rsync -avz /var/www/app username@remote-host:/home/
4. Copy/Sync a Directory from Remote to Local Server

In this example, you will see how to sync a remote directory to local directory by running following command :

# rsync -avzh username@remote-host:/home/xyz/ /var/www/app

Phone: (+91) 8800417876
Noida, 201301