rsync

2024-09-03

Understanding rsync’s Core Functionality

At its heart, rsync uses many clever techniques to optimize file transfers:

Basic rsync Syntax

The fundamental rsync command structure is:

rsync [OPTIONS] source destination

Essential rsync Options

Let’s look at some frequently used rsync options:

Practical rsync Examples

1. Local File Copying with Archiving:

This command copies the my_documents directory to a new directory named backup_documents, preserving all attributes:

rsync -avz my_documents backup_documents

2. Copying Files to a Remote Server using SSH:

This example copies the website directory to a remote server at user@remote_host:/path/to/destination, using SSH and compression:

rsync -avz website user@remote_host:/path/to/destination

Note: You’ll need SSH keys set up for passwordless authentication to the remote server.

3. Synchronizing Directories with Deletion:

This command synchronizes the project directory with a remote server, deleting files from the remote server that are no longer present in the local project directory. Proceed with caution, as this option deletes files!

rsync -avz --delete project user@remote_host:/path/to/destination

4. Using rsync with Excluding Specific Files or Directories:

This command copies the source_directory, but excludes the temp directory and any .log files:

rsync -avz --exclude="temp" --exclude="*.log" source_directory destination_directory

You can specify multiple --exclude options.

5. Resuming an Interrupted Transfer:

If a transfer is interrupted, rsync can often resume. Simply rerun the same command; rsync will intelligently detect what’s already been transferred.

Advanced rsync Techniques

rsync offers many more advanced features, including:

These examples provide a solid foundation for using rsync. Remember to always test your rsync commands on a small sample before applying them to large datasets. Thorough testing will prevent accidental data loss.