Linux use of rsync with examples

What is rsync

RYNC is one of the most useful tools of Linux. rsync can copy or synchronize files locally as well as remotely. It can be used for mirroring of data or backup of data across network, locally, over multiple disks. RYNC has inbuilt capability to utilize less bandwidth while copying data, as it copies only difference between two files. Also rsync can compress and decompress data which makes it more efficient utility.

Install rsync

First check whether rsync is installed in your system or not.
rpm –q  rsync
If not installed, install it using yum command.
yum install rysnc
Note: rsync should be installed in both local and remote machine, so as use it for data transfer between both systems.

RSYNC uses with example

  1. Local sync or copy files or directories

    If you want to sync or copy files on local system use
    Syntax

    rsync file_to_sync location_for_sync

    Example

    rsync test.doc /home/ap/test/

    In case destination directory does not exist, a new directory will be created.
    Now for sync / copy of directory use command.
    Syntax

    rsync –r directory_to_sync location_for_sync

    Example

    rsync –r /home/ap/test/ /temp/backup

    Use of –r switch means recursively sync directory. But using –r does not sync modified date, ownership, permissions, groups, etc. of the files.
    To sync files with modified date, ownership, permissions, groups, etc. of the files use –a switch (archive switch).
    Syntax

    rsync –a directory_to_sync location_for_sync

    Example

    sync –a /home/ap/test/ /temp/backup

    Also note in source directory has a trailing / (slash), this slash means all contents of test folder will be copied to backup folder. If trailing slash is not used then backup directory will have test folder inside it and test folder will have all of its contents within itself.

  2. Remote sync or copy files or directories

    Sync or copy from local host to remote server
    First command we use is for copying data from local to remote server.
    Syntax

    rsync –a directory_to_sync username@remote_host:location_for_sync

    Example

    rsync –a /home/ap/test/ root@192.168.1.17:/temp/backup

    Sync or copy from remote server to local host
    Here we will copy files from remote server to local folder.
    Syntax

    rsync –a username@remote_host:location_for_sync local_directory_to_sync

    Example

    rsync –a root@192.168.1.17:/temp/backup /home/ap/test/


  3. Sync/copy file using rsync with SSH

    rsync has capability to use SSH for copy/sync file or folders from local to remote server. When using SSH your password will be send in encrypted form. Your password is safer this way. Also data copied is also encrypted while being transferred.
    Sync or copy from local host to remote server
    Syntax

    rsync –ae ssh local_directory_to_sync username@remote_host:location_for_sync

    Example

    rsync –ae ssh /home/ap/test/ root@192.168.1.17:/temp/backup

    Sync or copy from remote server to local host
    This command will copy/sync files from remote location to local host
    Syntax

    rsync –ae ssh username@remote_host:location_for_sync local_directory_to_sync

    Example

    rsync –ae ssh root@192.168.1.17:/temp/backup /home/ap/test/
  4. Use rsync with progress and partial

    When using rsync with progress switch, you are shown transfer speed and time for transfer
    Syntax

    rsync –a –progress username@remote_host:location_for_sync local_directory_to_sync

    Example

    rsync –a --progress root@192.168.1.17:/temp/backup /home/ap/test/

    Another switch available with rsync is partial, it allows you to download partial files which later can be continued later. By default rsync doesn’t keep partial downloads.
    Syntax

    rsync –a –partial username@remote_host:location_for_sync local_directory_to_sync

    Example

    rsync –a --partial root@192.168.1.17:/temp/backup /home/ap/test/

    Also –P switch is available, using it will result in combined impact of partial as well progress switch.
    Syntax

    rsync –aP username@remote_host:location_for_sync local_directory_to_sync

    Example

    rsync –aP root@192.168.1.17:/temp/backup /home/ap/test/
  5. Use of –delete option with rsync

    The –delete switch when used for sync of files from remote location will remove files which are present at local folder but not available at remote destination
    Syntax

    rsync –aP –delete username@remote_host:location_for_sync local_directory_to_sync

    Example

    rsync –aP –delete root@192.168.1.17:/temp/backup /home/ap/test/
  6. Compress file to reduce transfer data

    If you are copying text files which are not compressed or if you have slow network speeds use –z switch which compresses file for transfer.
    Syntax

    rsync –azP username@remote_host:location_for_sync local_directory_to_sync

    Example

    rsync –azP root@192.168.1.17:/temp/backup /home/ap/test/

  7. Set limit for file size to be synced

    Maximum file size can be specified at time of executing rsync command. It can be done by use of switch –max-size
    Syntax

    rsync –azP –max-size=’size’ username@remote_host:location_for_sync local_directory_to_sync

    Example

    rsync –azP –max-size=’500k’ root@192.168.1.17:/temp/backup /home/ap/test/

    This command will transfer file size below 500kb