Home Linux CentOS 7 Examples of Rsync (Remote Synchronization) Command

7 Examples of Rsync (Remote Synchronization) Command

What is rsync command?? What are its benefits??

Rsync commandRsync command

Rsync stands for Remote synchronization and it is used for copying/synchronizing the directories or files. It can copy data locally or remotely as well. Mostly it is used for backup purposes. The system administrator uses rsync command mostly for backup purposes. Benefits & 7 examples of Rsync (Remote Synchronization) command are given below.

Benefits

  • Copy files to local or remote pc – Copies files to and from the remote server.
  • Speed – Rsync is faster as compared with the SCP (Secure Copy).
  • Encryption – Security is added by SSH protocol encryption.
  • Consumes Less Bandwidth – It uses compression and decompression of transfer data at sending and receiving end.

Basic Syntax

# rsync options source destination

Source = may be a local or remote server
Destination = locally or remote

Remember, need to specify remote server login details.

How to install Rsync

On Ubuntu

# apt-get install rsync

On CentOS 7

 # yum install rsync  
Install rsync on CentOS

7 Examples of Rsync Command

Most commonly used examples of rsync command are given below.

1. Copy/Sync 2 directories on Local Machine

Here we are copying/syncing html directory with /root/arun directory on the same server.

$ rsync -zvrh /var/www/html /root/arun

z = compress data
v = verbose
r = recursively copies data ( timestamps and permission are not preserved)
h = human-readable

Local directory sync with rsyncLocal directory sync with rsync

NOTE: Rsync will automatically create the destination directory.

2. Copy from Remote to Local Machine

We are copying remote server directory /home/arun/htmldata  to local server /tmp/aruntesting.
Remote server = root@192.168.150.133
Remote server directory = /home/arun/htmldata

Local Server directory = /tmp/aruntesting

# rsync -avzh root@192.168.150.133:/home/arun/htmldata /tmp/aruntesting 

where a = archive mode & it allows copying directory/files recursively. Keeps the same permissions as the source has.

Copy Directory From a Remote Server to Local Machine

Copy Directory From a Remote Server to Local Machine

3. Copy from Local to Remote Server

Sync/Copy Directory From Local Machine to Remote Server

Here are the details of the files/directory that we are copying from local machine to the remote server.
Local Server Directory = /home/arun/localserver

Remote Server Details = arun@192.168.150.133:/home/arun

# rsync -avzh /home/arun/localserver arun@192.168.150.133:/home/arun/ 
Copy Directory From Local Machine to Remote Server

Copy Directory From Local Machine to Remote Server

4. Rsync with SSH

Copy/Sync a Directory from a Local Machine to a Remote Server using SSH

Benefits of SSH – Rsync allows copy using SSH (Secure Shell) protocol. The SSH protocol encrypts the data transfer process. Therefore, nobody can read the data transferred over the internet. It adds security to the rsync command.

# rsync -avzhe ssh /home/arun/localserver arun@192.168.150.133:/home/arun/
rsync with SSH Outputrsync with SSH Output

Where ‘e’ = rsync used with protocol (SSH)

5. Transfer Data Show progress

To see the transferring data progress need to use the –progress argument.

# rsync -avzhe ssh --progress /home/arun/localserver arun@192.168.150.133:/home/arun/
Rsync show transfer data progressRsync show transfer data progress

6. Delete Source File After Copying Data

To delete the source file/directory after successfully copying the data to the destination ‘–remove-source-files’ option is used.

# rsync -avzh --remove-source-files /home/arun/localserver arun@192.168.150.133:/home/arun/
Delete Source directory after copying data with rsyncDelete Source directory after copying data with rsync

7. Include & Exclude files

For example we want to copy only the .php files from a directory to the destination and rest of the files should be excluded. Here in localserver directory .php & .txt files present. We will copy only the .php files. to the remote server (destination) sitedata directory.

# rsync -avzhe ssh --include '*php' --exclude '*' /home/arun/localserver/  arun@192.168.150.133:/home/arun/sitedata 
Include option with rsyncInclude option with rsync

The output of the copied php files to the remote server (arun@192.168.150.133:/home/arun/sitedata)

Remote server output using include optionRemote server output using include option

Read: 17 linux Basic Commands with Examples

Conclusion

Rsync commands have significant benefits. In this article, you learned 7 examples of rsync (Remote Synchronization) Command. For any feedback, please feel free to write in below comment box.

LEAVE A REPLY

Please enter your comment!
Please enter your name here