What is rsync command?? What are its benefits??
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.
- 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.
# 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
# apt-get install rsync
On CentOS 7
# yum install rsync
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
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 = email@example.com
Remote server directory = /home/arun/htmldata
Local Server directory = /tmp/aruntesting
# rsync -avzh firstname.lastname@example.org:/home/arun/htmldata /tmp/aruntesting
where a = archive mode & it allows copying directory/files recursively. Keeps the same permissions as the source has.
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 = email@example.com:/home/arun
# rsync -avzh /home/arun/localserver firstname.lastname@example.org:/home/arun/
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 email@example.com:/home/arun/
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 firstname.lastname@example.org:/home/arun/
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 email@example.com:/home/arun/
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/ firstname.lastname@example.org:/home/arun/sitedata
The output of the copied php files to the remote server (email@example.com:/home/arun/sitedata)
Read: 17 linux Basic Commands with Examples
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.