RSYNC Command in Linux with Examples

Rsync is a command-line tool in Linux that stands for remote synchronization. It is used to copy files from a source location to a destination location. You can copy files, directories, and the entire file system and keep in sync the files between different directories. It does more than just copying the files. It reduces the amount of data sent over the network by only sending the files which are new or updated. That’s why is it considered a great utility for file copying and backing up. Rsync also supports the copying of files to a remote system over SSH.

In this article, I will explain how to use Rsync to copy files with some practical examples. I will also explain the use of rsync in the copying of a single file, multiple files, and directory to both local and remote systems. I will be covering how to use Rsync to:

  • Copy file/directory within the local system
  • Copy file/directory from local to the remote system
  • Copy file/directory from the remote system to local

Installing Rsync

To install RSYNC follow the below command –

[root@LinuxKings ~]# yum install rsync* -y

How to backup Linux Local server to Remote Server?

 

Here are some of the commonly used options that can be used with rsync:

–a: archive mode
–v: shows details of the copying process
–p: shows the progress bar
–r:  copies data recursively
–z: compresses data
–q: suppress output

 

The general syntax of rsync is:

rsync [option]  [destination]

 

Copy files/directories locally

If you want to copy a directory with its sub-directory and all contents from one location to another within your system, you can do so as by typing rsync followed by the source and destination directory.

rsync -av /home/source_file/directory /home/destination_directory

 

Copy files based on the maximum size

While copying, we can also specify the maximum size of files that can be copied with the “–max-size” option. For instance, to copy the files no larger than 100k from ~/Downloads to ~/Documents directory, the command would be:

rsync -a --max-size=200k /root/Downloads/* /root/Documents

This command will copy all the files from ~/Downloads to ~/Documents directory except the ones larger than 200k.

 

Copy files based on the minimum size

Similarly, we can also specify the minimum size of files that can be copied with the “–min-size” option. For instance, to copy the files not lesser than 5M from ~/Downloads to ~/Documents directory, the command would be:

rsync -a --min-size=5M /root/Downloads/ /root/Documents

This command will copy all the files from ~/Downloads to the ~/Documents directory except the ones lesser than 5M.

 

Copy files and directories remotely

With Rsync, you can copy a single file, multiple files, and directories to a remote system. For copying files and directories remotely, you will require:

  • Rsync installed on both local and remote system
  • SSH access to the remote system
  • Remote user password

Before copying files using rsync, make sure you can access the remote system via SSH:

[root@LinuxKings]# ssh remote_user@remote_ip
How to backup Linux Local server to Remote Server?
Check SSH access to the remote system

 

Copy files and directories remotely

  • If you want to copy a file from one location to another within your system, you can do so by typing rsync followed by the source file name and the destination directory
[root@LinuxKings]# rsync -v -e ssh /root/source_file/directory root@remote_ip:~/destination_file/directory

 

How to backup Linux Local server to Remote Server?
copy local files to the remote server

 

  • Update your remote server If you have added new files/directories on the local server. Every time when you add a new file or directory then you have to run this command in your local server. This command adds new files/directories to your remote backup server. 
root@Linuxkings ~]# rysnc -u -v -e ssh /root/source_file/directory root@remote_ip:~/destination_file/directory
How to backup Linux Local server to Remote Server?
Update new files after the last backup

 

  • If there are some unuseful files on the remote server and you don’t want to keep them in your backup server/remote server then run this command to delete all unnecessary files/directories from the backup server.
root@Linuxkings ~]# rysnc -av --delete -e ssh /root/source_file/directory root@remote_ip:~/destination_file/directory
How to backup Linux Local server to Remote Server?
Delete unuseful files from local to remote

 

Copy file/directory from the remote system to local

  • Sometimes some files seem redundant/unnecessary so you removed them from the local system. But suddenly we need them, what to do in that case? In such a situation, if you have these files backup on your remote backup server, then you can access those files from the remote server by giving this command.
root@Linuxkings ~]# rysnc -avz  root@remote_ip:~ source_file/directory   /directory/destination_file/directory
How to backup Linux Local server to Remote Server?
Recover deleted files from remote to a local server

 

So friends this is all about RSYNC.In this article, I discussed 3 methods to take a backup of files/directories using RSYNC. If you have any doubt or query about these 3 methods then ask in the comment box. And if you want some extra knowledge about RSYNC please write to me in the comment box, if you have doubts or any queries with this post then don’t hesitate to contact me. If you like my posts please share them with your friends.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s