Rsync is a useful utility that allows you to easily move or synchronize files between two locations, on same or different servers, without using an ftp. Rsync uses its own protocol and port for this purpose. In this article, we will look at how to rsync files between two servers.
How to Rsync Files Between Two Servers
Before you proceed, it is important to have 2 linux servers with rsync installed. You also need to have permission to run rsync command on both servers. Also, the server that initiates file transfer should have access to receiving server’s SSH port 22.
Depending on your command rsync can perform push or pull operation for file transfer. Here are the basic syntax of rsync
GENERAL SYNTAX
rsync [-options] SOURCE DESTINATION
PUSH
rsync [-options] SOURCE user@x.x.x.x:DESTINATION
PULL
rsync [-options] user@x.x.x.x:SOURCE DESTINATION
In the above commands, x.x.x.x is the IP address of remote server. If SOURCE is local location and DESTINATION is remote location then rsync will initiate a push. If SOURCE is remote location and DESTINATION is local location, then it will initiate a pull.
Also read : How to Install ReactJS in Ubuntu
File Transfer using Rsync
Here is the example of push file transfer
$ sudo rsync /home/test.txt root@54.23.21.34/home/ubuntu
The above command will push the file test.txt from local directory to remote folder /home/ubuntu.
Similarly, the following command will pull the file test.txt from remote location /home/ubuntu to local folder /home
$ sudo rsync root@54.23.21.34/home/ubuntu/test.txt /home
Also read : How to Run Shell Script in Linux
Directory Transfer using Rsync
If you want to transfer a directory just use rsync with -d option.
Here is an example of push directory transfer of local folder /home/data to remote location /home/ubuntu
$ sudo rsync -d /home/data root@54.23.21.34/home/ubuntu
Here is an example of pull directory transfer of remote folder /home/ubuntu/data to local folder /home
$ sudo rsync -d root@54.23.21.34/home/ubuntu/data /home
If you want to recursively copy all files & subfolders above you need to use -r option.
In all the above cases, the copied files will have the new time stamp set at the time of copying. If you also want the timestamp to be copied, use -t option.
Also read : How to Rename Multiple Files in Linux
Sync files & Directories using rsync
If you want to sync files between source and destination you need to use -u –delete option as shown below.
$ sudo rsync -rtu --delete SOURCE root@x.x.x.x:/DESTINATION
We have already seen the uses of -r and -t options for recursive copy and time stamp copy respectively. In the above command, we use -u option to update only different/changed/added files from source to destination location. The –delete option will delete any files and folders that are present in destination but not in source. You can use other options to symlink (-l option) or copy the different/changed/added files.
In this article, we have learnt how to easily sync files & folders between two servers, along with some commonly used options. rsync is a great tool to easily sync files & folders at different locations. It is generally run as a cronjob to periodically sync to file locations.
Also read : How to Extract .bz2 file in Linux
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.