2024-09-03
At its heart, rsync
uses many clever techniques to optimize file transfers:
rsync
only transmits the changed portions, reducing bandwidth usage and transfer times.rsync
verifies file integrity, ensuring data consistency at the destination.rsync
can seamlessly resume from where it left off.The fundamental rsync
command structure is:
rsync [OPTIONS] source destination
Let’s look at some frequently used rsync
options:
-a
(archive): This option recursively copies directories, preserving permissions, timestamps, and symbolic links. It’s often the default choice for backups.-v
(verbose): Provides detailed output during the transfer process, showing progress and details of each file.-z
(compress): Compresses data during transfer, useful for network connections with higher latency.-P
(progress): Displays a progress bar during the transfer.-r
(recursive): Recursively copies directories. (Implied by -a
)--delete
: Deletes files in the destination that are not present in the source. Use cautiously!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.
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.