Jaigarage

Sync Folders with Rsync

Rsync (remote synchronize) is a fast and versatile command-line utility for synchronizing files and directories between two locations. It’s widely used for backups, mirroring, and data migration due to its efficiency in transferring only the changed parts of files. This guide will explore how to use rsync for various synchronization tasks, both locally and remotely.

Understanding rsync Basics

The basic syntax for rsync is:

rsync [options] SOURCE DESTINATION

Here are some commonly used options:

Local to Local Synchronization

To copy or synchronize files and directories locally, you can use the following command:

rsync -avh /path/to/source /path/to/destination

For more detailed information, refer to the official rsync manpage: Manpage

Local to Remote Synchronization (Push)

To synchronize files from your local machine to a remote server:

rsync -avh /path/to/local/source user@remote_host:/path/to/remote/destination

Remote to Local Synchronization (Pull)

To synchronize files from a remote server to your local machine:

rsync -avh user@remote_host:/path/to/remote/source /path/to/local/destination

Deleting Files on Destination

To ensure the destination exactly mirrors the source (i.e., delete files on the destination that are no longer on the source), use the --delete option:

rsync -avh --delete /path/to/source /path/to/destination

Excluding Files or Directories

To exclude specific files or directories from the synchronization, use the --exclude option:

rsync -avh --exclude 'node_modules/' --exclude '*.log' /path/to/source /path/to/destination

Dry Run

Before executing a potentially destructive rsync command (especially with --delete), it’s highly recommended to perform a dry run using the --dry-run or -n option. This will show you what rsync would do without actually making any changes.

rsync -avhn --delete /path/to/source /path/to/destination

Common Use Cases for rsync

rsync is incredibly versatile and can be used for a variety of tasks:

Conclusion

rsync is a powerful and flexible tool for anyone needing to synchronize files and directories efficiently. Its ability to handle local and remote transfers, coupled with options for fine-grained control over the synchronization process, makes it an essential utility for system administrators, developers, and anyone managing data. By integrating rsync into your workflow, you can ensure your data is always where it needs to be, reliably and efficiently.

Tags: