Sync local and remote directories with Rsync

  • avatar
  • 1.2K Views
  • 1 Like
  • 4 mins read

Rsync is a very flexible network-enabled syncing tool. Due to its ubiquity and its popularity as a tool for system scripts, it's included on most Linux distributions by default. It efficiently copies and sync files to or from a remote system. It’s faster than scp because rsync uses a remote-update protocol which allows transferring just the differences between two sets of files. The first time, it copies the whole content of a file or a directory from source to destination but from next time, it copies only the changed blocks and bytes to the destination.

Rsync command syntax

The rsync utility expressions take the following form:

Local to Local:  rsync [option]... [src]... dest
Local to Remote: rsync [option]... [src]... [user@]host:dest
Remote to Local: rsync [option]... [user@]host:src... [dest]
  • option - rsync options .

    • -a, --archive, archive mode. This option tells rsync to syncs directories recursively, transfer special and block devices, preserve symbolic links, modification times, groups, ownership, and permissions.

    • -z, --compress. This option forces rsync to compresses the data as it is sent to the destination machine. Use this option only if the connection to the remote machine is slow.

    • -P, equivalent to --partial --progress. When this option is used, rsync shows a progress bar during the transfer and keeps the partially transferred files. It is useful when transferring large files over slow or unstable network connections.

    • --delete. When this option is used, rsync deletes extraneous files from the destination location. It is useful for mirroring.

    • -q, --quiet. Use this option if you want to suppress non-error messages.

    • -e. This option allows you to choose a different remote shell. By default, rsync is configured to use ssh.

  • src - source directory.

  • dest - destination directory.

  • user - remote username.

  • host - remote hostname or IP address.

The real power of rsync comes when synchronizing directories. The example below shows how to create a backup:

rsync -a /source_dir/ /destination_dir/

When using rsync to transfer data remotely, it must be installed on both the source and the destination machine.

rsync -a /source_dir_local/ remote_user@remote_host_or_ip:/destination_dir_remote/

To transfer data from a remote to a local machine, use the remote location as a source:

rsync -a remote_user@remote_host_or_ip:/source_dir_remote/ /destination_dir_local/

It is worth mentioning that rsync gives different treatment to the source directories with a trailing slash (/). If the source directory has a trailing slash, the command will copy only the directory contents to the destination directory. When the trailing slash is omitted, rsync copies the source directory inside the destination directory.

Exclude Files and Directories

There are two options to exclude files and directories. The first option is to use the --exclude argument and specify the files and directories you want to exclude on the command line.

rsync -a --exclude=vendor --exclude=tmp /source_dir/ /destination_dir/

The second option is to use the --exclude-from option and specify the files and directories you want to exclude in a file.

rsync -a --exclude-from='/exclusions.txt' /source_dir/ /destination_dir/

In this case, the exclusions.txt file will contain the following information:

vendor

tmp

Conclusion

Rsync is a fast and versatile command line utility for synchronizing files and directories between two locations. It provides fast incremental file transfer by transferring only the differences between the source and the destination.

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails

0 Comments

Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.