Synchronizing files with Rsync
In this recipe, we will learn how to use the Rsync utility to synchronize files between two directories or between two servers.
How to do it…
Follow these steps to synchronize files with Rsync:
Set up key-based authentication between source and destination servers. We can use password authentication as well, which is described later in this recipe.
Create a sample directory structure on the source server. You can use existing files as well:
ubuntu@src$ mkdir sampledir ubuntu@src$ touch sampledir/file{1..10}
Now, use the following command to synchronize the entire directory from the source server to your local system. Note the
/
aftersampledir
. This will copy contents ofsampledir
in thebackup
. Without/
, the entiresampledir
will be copied to thebackup
:ubuntu@dest$ rsync -azP -e ssh [email protected]:/home/ubuntu/sampledir/ backup
As this is the first time, all files from
sampledir
on the remote server will be downloaded in abackup
directory on your local system...