How to use Rsync to synchronise local and remote directories?
Introduction
Have you ever been in a situation where you needed to transfer files quickly and securely between different servers, or lost an important file? If the answer is yes, Rsync is the perfect tool for you. Rsync is an open source file and directory synchronisation tool that allows you to transfer files quickly and securely between local and remote servers. It is a powerful and versatile tool that can help you keep your files up to date and secure.
In this tutorial, you will learn how to use Rsync to synchronise files and directories between different servers and how to schedule automatic synchronisation tasks.
info To start this tutorial, you will need access to two servers: one local and one remote. You will also need to have rsync installed on both servers. If you don't have Rsync installed, don't worry, we'll show you how to do it in the system preparation section.
Rsync basics
Rsync is a file and directory synchronisation tool for transferring files quickly and securely between local and remote servers. It is a very popular and widely used tool in the world of computing and programming.
Here are some Rsync basics you should know before you start using it:
1. Synchronisation: Rsync uses the delta file synchronisation algorithm to transfer only the parts of files that have changed. This greatly reduces the time it takes to transfer files.
2. Local vs. Remote Copy: In local synchronisation, Rsync is used to synchronise files between two directories on the same server. In remote synchronisation, Rsync is used to synchronise files between two different servers.
3. Secure Connection: Rsync uses SSH to establish a secure connection between local and remote servers. SSH is a secure network protocol used for communication between two devices.
info Now that you know the basics of Rsync, let's move on to the next section to prepare your system for using this tool.
System preparation
Before you start using Rsync, you need to make sure that it is installed on both servers and that all the necessary requirements are met.
Step 1: Installing Rsync
If Rsync is not installed on your servers, you can install it using your operating system's package manager.
On Debian or Ubuntu based systems, you can install Rsync using the following command:
sudo apt-get install rsync
On CentOS or Fedora based systems, you can install Rsync using the following command:
sudo yum install rsync
Step 2: SSH access
Rsync uses SSH to establish a secure connection between the servers. You must therefore ensure that you have SSH access on both servers.
Step 3: Check the version
It is important to check that the version of Rsync installed on both servers is the same to avoid compatibility issues. You can check the Rsync version with the following command:
rsync --version
Now that we have set up the system and installed Rsync, we can move on to the next section to learn how to synchronise files and directories using Rsync.
Sync files and folders with Rsync
In this section we will show you how to use Rsync to synchronise files and directories between servers.
Local Sync
To synchronise two directories on the same server, you can use the following command:
rsync -avzh /route/source-directory/ /route/destination-directory/
This command synchronises the contents of the source-directory with the destination-directory. The -avzh
option indicates that Rsync should synchronise files in file mode, verbose mode, compression mode and recursive mode.
Remote Sync
To synchronise two different servers, you can use the following command:
rsync -avzh -e ssh user@ source-server:/route/source-directory/ /route/destination-directory/
This command synchronises the contents of the source-directory on the source-server with the destination-directory on the local server. The -e ssh
option indicates that Rsync must use SSH to establish a secure connection between the servers.
Dry-mode synchronisation
If you want to check which files will be synchronised before you actually synchronise, you can use dry mode. Dry mode simulates the synchronisation without performing any real action. You can use the following command to do this:
rsync -avzh --dry-run /route/source-directory/ /route/destination-directory/
info This command displays a list of files that will be synchronised when the actual synchronisation is performed.
Synchronisation of files with the exception of some
If you want to synchronise all files in a directory, except for some specific files, you can use the --exclude
option. You can use the following command:
rsync -avzh --exclude ' file1.txt' --exclude 'file2.txt' /route/ source-directory/ /route/destination-directory/
This command synchronises all files in the source-directory except file1.txt
and file2.txt
.
success These basic commands will get you started using Rsync to synchronise files and directories between local and remote servers. However, there are many more options and advanced features that you can use to optimise your synchronisation. See the Rsync documentation for more information.
Conclusions
Rsync is a very useful tool for synchronising files and directories between local and remote servers. With the basic commands we have shown you in this tutorial, you can start using Rsync effectively.
It also includes many interesting advanced tools. Some of the advanced features include file compression, incremental synchronisation, exclusion of certain files and many more.
Finally, we hope you found this tutorial useful and that you will start using Rsync to synchronise your files and directories!