Introduction to Rsync

rsync is a powerful and widely used command-line utility in Unix-like operating systems that facilitates efficient and reliable file synchronization and data transfer between directories or across different machines. It is particularly useful for remote backups, mirroring, and incremental transfers. The name “rsync” stands for “remote synchronization.”

Basic Syncing

Syncing Folder src into dest

To synchronize the contents of a local folder src into another local folder dest, you can use the following command:

1
rsync --progress -avz ./src /dest
  • --progress: Displays the progress of the synchronization.
  • -a: Archive mode, which preserves various file attributes and ensures recursive copying.
  • -v: Verbose mode, providing more detailed output.
  • -z: Enables compression during data transfer to reduce bandwidth usage.

Syncing with Custom Port

You can specify a custom SSH port while using rsync to sync files over SSH. For example:

1
rsync -e "ssh -p $portNumber" --progress -avz ./src /dest

Replace $portNumber with the actual port number you want to use. This command establishes an SSH connection with the specified port for secure data transfer.

Syncing the Content of src into dest

To synchronize the content of the source directory src into the destination directory dest (so that the contents of src are directly placed inside dest), you can use the following command:

1
rsync --progress -avz ./src/ /dest

Incremental Sync

Incremental syncing is useful for transferring only the changed parts of files or new files, minimizing the amount of data transferred. Here’s a command for incremental syncing:

1
rsync -abPv --backup-dir=old_`date +%F-%T` --delete --exclude=old_* ./source ./destination

Explanation of options used:

  • -a: Archive mode, includes recursive copying and preserves attributes.
  • -b: Creates backup copies of files that are being replaced or deleted.
  • -P: Combines --progress and --partial for better handling of interrupted transfers.
  • -v: Verbose mode for detailed output.
  • --backup-dir: Specifies a directory where backup copies of replaced/deleted files are stored.
  • --delete: Deletes extraneous files from the destination that don’t exist in the source.
  • --exclude: Excludes files or patterns from the sync process.

The date +%F-%T command generates a timestamp in the format YYYY-MM-DD-HH:MM:SS, which is used to create a timestamped backup directory.

Overall, rsync is a versatile tool for efficient and reliable file synchronization, making it an essential utility for managing and transferring data across systems.

0%