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
To install RSYNC follow the below command –
[root@LinuxKings ~]# yum install rsync* -y
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
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
- 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
- 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
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
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.