Rsync allows you to synchronize directories locally or between two servers. Rsync determines which files are different or new between two locations and then synchronizes the directories. Rsync can be used to copy folders or entire directories.
In my examples I use the following Rsync command line options.
The -a option enables archive mode which will preserve file permissions, ownership, and modification times.
The -e option specifies the remote shell to use. This is most commonly set to ssh.
The -v option enables verbose mode which will display info while running.
The first thing to do is make sure that Rsync is installed on both servers that you will be performing an rsync on.
After you’ve installed rsync on both servers that you plan to push and pull files/directories from, navigate to the locations on both servers where you plan to push and pull the files from. In my example I navigated to the /tmp directory and ran an ls –l command to see what files were in each directory. You can see on the server to the left that I have an ubuntu-15.10-server-i386.iso file that I’d like to copy over to the server on the right.
You can then either “push” or “pull” the files/directories from the server of your choice. This would be an example of pushing a file over to a server as the server on the right now has the file that the server on the left pushed over to it.
rsync -av -e ssh <file or directory> root@<destination serverIP>:<destination folder on receiving server>
In this example I “pull” the same file over from the server on the left. (I deleted the file so I could pull it again.) Also to note is the port specification in my example. If your server runs ssh on a different port other than 22, specify it with the same syntax in my command example.
rsync -av -e "ssh -p <port>" root@<source serverIP>:<full path of file on server pulling from> <destination folder>
Now I’m going to show you how to rsync directories over. In this example I created a Test directory in my /tmp directory on the source server and created 2 files called file1 and file2 within it. After running the command, rsync will connect to the destination server and create the needed directories and sync the files accordingly.
rsync -av -e ssh <source server folder> root@<destination serverIP>:<destination folder on receiving server>
And there you have how to use Rsync to sync files and directories between two servers over ssh connectivity.