How to use rsync

This article demonstrates how to use rsync.

When to use rsync

The rsync program enables you to synchronize directories locally or between two computers. Rsync automatically determines which files are different or new between the two locations, and then synchronizes the directories.

You can use this functionality to accomplish a number of tasks. For example, you can use rsync to back up your web site to another computer. Alternatively, suppose that you have a web site development environment already installed on another server. You could use rsync to deploy this environment to your A2 Hosting account.

If you are synchronizing directories between two separate computers, they both must have rsync installed.

How to use rsync

The rsync program has many features, but the following command-line options are enough to get started:

  • The -a option enables archive mode. In archive mode, rsync preserves all file permissions, ownership, and modification times.
  • The -e option specifies the remote shell to use. This is often set to ssh.
  • The -l option preserves any symbolic links.
    If you use the -a option, this option is enabled automatically.
  • The -r option makes rsync recurse through any subdirectories.
    If you use the -a option, this option is enabled automatically.
  • The -v option enables verbose mode, which means rsync displays information while it is running. (By default, rsync does not display any information.) You can omit this option, or to see even more detailed information, use the -vv option.
  • The -z option enables compression for file transfers.

Using these rsync options, we can go through some examples. Suppose you want to back up your web site files to another server. To do this, follow these steps:

  1. Log in to your account using SSH.
  2. To make sure you are in your home directory, type the following command:
    cd ~
  3. To back up (or “push”) your public_html directory to the user account on a remote server at example.com, type the following command:

    rsync -avz -e ssh public_html [email protected]:~/backup

    This command assumes that the remote server is running SSH on port 22. If you were backing up to another A2 Hosting server, for example, you would use the following command because SSH runs on port 7822:

    rsync -avz -e "ssh -p7822" public_html [email protected]:~/backup
  4. After rsync finishes, the user on the remote server at example.com has a backup/public_html directory in their account that is an exact copy of the public_html directory in your account.

Another common scenario is “pulling” files from a remote location. For example, you may have a development environment set up on a remote server. You could use rsync to pull files from the remote repository into your A2 Hosting account. With each subsequent “pull”, rsync will only update the files that have changed since the last update, thereby saving time and bandwidth. To do this, run the following command from your home directory:

rsync -avz -e ssh [email protected]:~/public_html .

After rsync finishes, your public_html directory contains a copy of the files in the remote server's public_html directory.

More Information

This article is only a brief introduction to rsync's extensive capabilities. To view the official online documentation for the rsync program, please visit http://rsync.samba.org/documentation.html.

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.