How to Use Rsync for File Transfers

How to Use Rsync for File Transfers

If you previously used scp for file transfers, then you can also use rsync for file transfers or, more specifically, for synchronizing files and folders from one server to another. This makes it easier to use if you want to copy an entire folder directly to another server.

Parameters Frequently Used With Rsync

When using rsync, you usually need to use other parameters, such as -e to synchronize to a destination server that uses SSH. The following is a list of parameters frequently used with rsync

  • -v, –verbose: Displays more detailed output about the synchronization process, such as the files being copied, file sizes, and transfer speeds.
  • -r, –recursive: Copies directories recursively, including all subdirectories and files within them.
  • -z, –compress: Compresses data during transfer to save more bandwidth.
  • -a, –archive: A combination of several options, including -rlptgoD, which is useful for performing archive copies that preserve timestamps, ownership, groups, permissions, special devices, and sparse flags.
  • -e, –rsh: Specifies the program to use for remote shell execution, usually used to specify the SSH version to use. Example: -e “ssh -p 221” if the destination server uses port 221.
  • -n, –dry-run: Performs a simulation of the copy without actually changing any data at the destination.
  • -d, –dirs: Copies only directories, excluding files.
  • -p, –perms: Copies file permissions.
  • -o, –owner: Copies file ownership.
  • -g, –group: Copies file group ownership.
  • -t, –times: Copies file modification times.
  • –delete: Deletes files in the destination directory that do not exist in the source directory.
  • –exclude=PATTERN: Ignores files or directories that match a specific pattern. Example: –exclude=’*.tmp’ to ignore temporary files.

You can use each of these parameters together with other parameters, for example by using the parameter -avz then data such as timestamps, ownership, and others will be preserved with the parameter -a, then synchronization details will be displayed with the parameter -v and files will be compressed if possible with the parameter -z.

For a complete list of parameters that can be used, you can use the command rsync without any additions.

Basic Rsync Usage

For basic usage, it is actually quite similar to SCP where you only need to run the rsync command followed by the source folder location and the destination folder location.

For example, if you want to synchronize files and folders located inside the /dir1 folder into /dir2, you can use this command

rsync -av /dir1 /dir2

Using Rsync to Transfer to a Server

If you want to transfer from local to a server with IP 192.168.1.1 with user root, for example, if you want to synchronize the /public_html folder located on localhost, you can use the following command

rsync -avz /public_html [email protected]:/public_html

Of course, this method only applies if SSH uses the default port (22). If SSH uses a custom port, for example 2211, then you need to add the parameter -e

rysnc -e "ssh -p 2211" -avz /public_html [email protected]:/public_html

Using Rsync to Transfer From a Server

If you want to retrieve data from a server, you just need to reverse the folder locations in the command. For example, to synchronize public_html from a server with IP 192.168.1.1 with user root to localhost, you can use the following command

rsync -avz [email protected]:/public_html /public_html

If your server uses a custom port, the method is the same where you just need to add the parameter -e

rysnc -e "ssh -p 2211" -avz [email protected]:/public_html /public_html

Using Rsync for Inter-Server Transfers

You might think about using rsync for file transfers directly from localhost, but with both the source and destination files located on remote servers, for example with the following command

rsync -avz [email protected]:/public_html [email protected]:/public_html

Unfortunately, that command cannot be executed because to use rsync, either the source or the destination must be the device you are currently using.

So, if you want to use rsync for inter-server transfers, you can log in to one of the servers, either the destination or the source, and then, depending on the server you are logged into, you can use rsync according to one of the previous options.

Written by

Muhammad Khoirun Nasikin

I enjoy tinkering with WordPress, hosting, VPS, web development, and various small experiments related to websites. I have over 3 years of experience in the web hosting industry.

COMMENTS

Loading...