Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How to Use Rsync to Sync Local and Remote Directories on a VPS

Table Of Contents


    ## Table of Contents

    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [About Rsync](#about-rsync
    – [Basic Syntax of Rsync](#basic-syntax-of-rsync
    – [How to Use Rsync to Sync with a Remote System](#how-to-use-rsync-to-sync-with-a-remote-system
    – [Useful Options for Rsync](#useful-options-for-rsync
    – [Conclusion](#conclusion

    ## Introduction

    Rsync stands for remote sync, It is a local and remote synchronization tool. It also uses an algorithm which reduces the number of data copied by moving the parts of files that has been changed.

    ## Prerequisites

    Rsync package should be installed in your server.

    ## About Rsync

    Rsync is a flexible and network-enabled syncing tool. This would refer to the network protocol to use this tool. In this guide, we are mainly focusing on utility, but not the protocol.

    ## Basic Syntax of Rsync

    The syntax of Rsync works in a way which quite resembles scp, ssh, and cp. It is quite simple.

    To create two test folders and a couple of test files using the below command.
    “`
    cd ~
    mkdir dir1
    mkdir dir2
    touch dir1/file{1..100}
    “`
    So you will get a directory named ‘dir1’ which has 100 files in it, those are empty.

    You can see the list by entering the below following.
    “`
    ls dir1
    “`
    Now you will get output like this:

    ![lsdir](https://grid.media/assets/images/ls-dir-1.png

    We are also having an empty directory called dir2, which is created earlier.

    If you want to sync the contents of dir 1 to dir 2 on the same system, then enter the below command.
    “`
    rsync -r dir1/ dir2
    “`
    Here ‘-r’ refers recursive, and it is mandatory for directory syncing. You can also use ‘-a’ instead of ‘-r’ like this.
    “`
    rsync -a dir1/ dir2
    “`
    ### Note

    In the above syntax, there is a slash(/ at the end of the argument. It means ‘contents of dir1’. If you remove the slash(/, it means the directory inside the dir2. Then it will look like this.
    “`
    ~/dir2/dir1/[files]
    “`
    You have to cross check once, before executing the rsync command. It grants a method to do this by entering the -n or –dry-run options.
    “`
    rsync -anv dir1/ dir2
    “`
    Then output looks like this:

    ![anvdir1dir2](https://grid.media/assets/images/anv-dir1-dir-2.png

    Compare these two outputs, while entering slash or without entering the slash:
    “`
    rsync -anv dir1 dir2
    “`
    You will get output like this:

    ![](http://

    So you can observe that the directory is transferred.

    ## How to use Rsync to Sync with a Remote System

    Although it is easy to sync to a remote system if you have SSH access to the remote machine with Rsync installed on both devices. Once you have achieved the SSH access verification is done between two systems, you may sync the first dir to a remote computer with the same syntax.
    “`
    $ rsync -a ~/dir1 username@remote_host:destination_directory
    “`
    This is known as “push”. Because it pushes data from the local system to the remote system. And the opposite operation is “pull”. It means it is used to sync a remote directory to the local system. If the first directory is on the remote system instead of your local system then you have to use the below syntax.
    “`
    $ rsync -a username@remote_host:/home/username/dir1
    “`
    Place_to_sync_on_local_machine
    With ‘cp’ and other tools that are similar to ‘cp’, the source is always the first argument, and the destination is always the second.

    ## Useful Options for Rsync

    Rsync gives many options for changing the default behavior of the utility. If you are transferring files that weren’t compressed before, such as text files, you have to decrease the network transfer by adding compression using the ‘-z’ flag.
    “`
    $ rsync -az source destination
    “`
    Another flag is ‘-p’ which mixes the flags ‘-progress’ and ‘partial’. The start of those will grant you a progress bar for the transfers and second will grant you the ability to proceed the orders.
    “`
    $ rsync -azP source destination
    “`
    Then you will get the following output:

    ![sourcedestination](https://grid.media/assets/images/source-destination.png

    If you execute the same command, then you will get the same output, because there is no change in the mode. You can try it by entering this command again.
    “`
    $ rsync -azP source destination
    “`
    Then the Output looks like this:

    ![rsyncsourcedestination](https://grid.media/assets/images/rsync-source-destination-02132019.png

    You can update the modification time on some of the files by using below command.
    “`
    $ touch dir1/file{1..10}

    $ rsync -azP source destination
    “`
    Then the output looks like this:

    ![sourcedestinationoutput](https://grid.media/assets/images/source-destination-output.png

    To ensure that two directories are in sync, you have to remove files from the destination directory if they are removed from the Source. By default, rsync does not remove anything from the destination directory. We may change this behavior with –delete option. Before that use –dry-run option and test it to prevent data loss:
    “`
    $ rsync -a –delete source destination
    “`
    If you want to remove specific files or directories located in the same directory you are syncing, you can do by specifying them a comma-separated list follows the –exclude=option.
    “`
    $ rsync -a –exclude=pattern_to_exclude source destination

    “`

    If you specified a pattern to exclude, we can override that exclusion for files that match a different pattern by using the –include=option.
    “`
    $ rsync -a –exclude=pattern_to_exclude –include=pattern_to_include source destination
    “`
    Finally, rsyncs –backup is used to store backup files. It is used in conjunction with –backup-dir option.
    “`
    $ rsync -a –delete –backup –backup-dir=/path/to/backups /path/to/source destination
    “`
    ## Conclusion

    Rsync simplifies the file transfers over networked connections and also it will add robustness to local directory syncing.