2.11.13. Creating and restoring a copy of site files with rsync

rsync is a utility for synchronizing files and directories between a local PC and a remote server. The data is transmitted in encrypted form. The transfer is performed in a single stream and only changed data is transferred during resynchronization, which provides a higher speed than when copying files via FTP, SFTP or SCP.

rsync is available for Linux and macOS operating systems. On Windows, it can be used through WSL (Linux Subsystem for Windows).

To create a copy of the contents of the site directory on a local PC, you need to run the following command on this PC in the console:

rsync -avzh --delete --progress example@example.ftp.tools:/way/to/root/catalog/site/ ~/local/catalog/

In a team:

  • Keys:
    • -a — archiving mode, recursive copying of data while maintaining symbolic links, access rights and other information.
    • -v — output of detailed information in the course of work.
    • -z — compression of transmitted data.
    • -h — information output in a human—friendly form.
    • –delete — deleting files in the destination directory that are not in the source.
    • –progress — displaying the progress of the transfer of each file.
  • example@example.ftp.tools - login and host from SSH connection data.
  • Paths:
    • /path/to/root/directory/site/ — full path to the directory, the contents of which should be saved on the local PC. Attention! The closing slash at the end of the path affects the location of data in the target directory — if a slash is specified, the contents of the source directory will be copied directly to the target directory, if not specified, the contents of the source directory will be copied to a subdirectory with the name of the source directory.
    • ~/local/directory/ — the directory on the local PC where you want to save the data. The trailing slash of the target directory does not matter.

After running the command, you need to enter SSH password... When you enter it or paste it from the clipboard, characters are not displayed on the screen. In order not to enter a password each time you run the command, you just need to configure SSH key authentication.

The first execution of the command may take a long time, as all the files and directories of the site will be transferred over the network. Restarts will be faster because only the data that has changed will be transferred.

To restore a site from a copy on a local PC, it is enough to swap the source and destination in the command:

rsync -avzh --delete ~/local/catalog/ example@example.ftp.tools:/way/to/root/catalog/site/