rsync – fast and easy way to copy files to another server

May 22, 2023 Author: markovk

Before making changes, it can be helpful to save the files in a safe place so that you can restore them. The rsync utility will help with this task, which minimizes traffic by copying only the changed parts of the files.

First of all, let’s install the package on both servers, if it doesn’t already exist. We use the following command:

sudo apt-get install rsync (for CentOS use yum instead of apt-get)

To copy from a remote server, we naturally need access to it, since we will have to enter a password before starting copying. In our example, the data will be copied from the directory /remote/source to /local/destination and if the directory does not exist, it will be created, and the files existing in it will not be overwritten:

rsync -avzP --stats [email protected]:/remote/source/ /local/destination/
  • -a saves information about dates, symlinks, and file permissions
  • -z archives data
  • -v increases the verbosity of messages during program operation
  • -P combines “progress” (show progress while copying) and “partial” (to continue copying when the link is broken)

If you want to make sure that everything goes well, you can additionally add the “dry-run” option – in this case, the utility will start a simulation of copying with a log entry, but the actual copying of files will not occur.

To copy to a remote server, set up a connection for the directory where the copy will be made:

rsync -avzP --stats /local/source/ [email protected]:/remote/destination/

Note about the closing slash:

When specifying the path to a directory as a source, you should pay attention to the closing slash – the character / at the end of the directory name. The closing slash means the inner content, i.e. if /source/ ends with a slash, rsync will copy the contents of /source/ to /destination/. But if there is no slash in /source, then rsync will create a dir directory inside /destination/ and copy all the contents of /source/ to /destination/source/. Hopwever, the presence or absence of a closing slash in the name of the directory to which the copy will take place does not matter.

Sources:

  • https://help.ubuntu.com/community/rsync
  • https://www.servers.ru/knowledge/linux-administration/how-to-copy-files-between-linux-servers-using-rsync

Leave a Reply

Your email address will not be published. Required fields are marked *

Begin work on your project today
Contact us on Telegram. Ask a question and get a quick response.
or
Message
in Telegram
By clicking the button, you give your consent to the processing of personal data and agree to the privacy policy.