2.11.13. Create and restore site files copy using rsync
rsync is a utility for synchronizing files and directories between a local PC and a remote server. Data is transmitted in encrypted form. The transfer is performed in a single stream, and during subsequent synchronizations, only the changed data is transmitted, which provides a higher speed than copying files via FTP, SFTP, or SCP.
rsync is available for Linux and macOS operating systems. On Windows, it can be used through WSL (Windows Subsystem for Linux).
Create local copy of site files
To create a copy of the site directory content on a local PC, you need to execute the following command in the console on this PC:
rsync -avzh --delete --progress example@example.ftp.tools:/path/to/root/directory/of/site/ ~/local/directory/
In command:
- Keys:
-a— archive mode, recursive copying of data while preserving symlinks, access rights, and other information.-v— output detailed information during operation.-z— compression of transmitted data.-h— output information in a human-readable format.–delete— deletion in the destination directory of files that are not present in the source.–progress— display the progress of each file transfer.
example@example.ftp.tools— login and host from SSH connection credentials.- Paths:
/path/to/site/root/directory/— the full path to the directory whose contents need to be saved on the local PC. Attention! The trailing slash at the end of the path affects the placement of data in the target directory — if the slash is specified, the contents of the source directory will be copied directly into the target directory; if not specified, the contents of the source directory will be copied into a subdirectory named after the source directory.~/local/directory/— the directory on the local PC where the data needs to be saved. The trailing slash in the target directory does not matter.
After executing the command, you need to enter the SSH access password. When entering or pasting it from the clipboard, the characters are not displayed on the screen. To avoid entering the password each time the command is executed, it is sufficient to configure key-based SSH authentication.
Restore site files on hosting from local copy
To restore the site from a copy on a local PC, it is sufficient to swap the source and destination in the command:
rsync -avzh --delete ~/local/directory/ example@example.ftp.tools:/path/to/root/directory/of/site/