Table of Contents
Transferring files between directories and servers using Rsync is a powerful and efficient way to synchronize data. Here’s a guide with help, tips, and examples for using Rsync:
Basic Rsync Syntax:
- Copy files from local to remote:
rsync -avz /local/path/ username@remote_server:/remote/path/
- Copy files from remote to local:
rsync -avz username@remote_server:/remote/path/ /local/path/
Rsync Options:
- Recursive Copy:
- Use the
-r
or-a
option for recursive copying:rsync -avz /local/directory/ username@remote_server:/remote/path/
- Use the
- Verbose Output:
- Use the
-v
option for verbose output to see details of the transfer:rsync -avz -v /local/path/ username@remote_server:/remote/path/
- Use the
- Update Only:
- Use the
--update
or-u
option to update only if the source file is newer:rsync -avzu /local/path/ username@remote_server:/remote/path/
- Use the
- Delete Extraneous Files:
- Use the
--delete
option to delete files from the destination that are not present in the source:rsync -avz --delete /local/path/ username@remote_server:/remote/path/
- Use the
- Exclude Files/Directories:
- Use the
--exclude
option to exclude specific files or directories:rsync -avz --exclude='file.txt' /local/path/ username@remote_server:/remote/path/
- Use the
- SSH Key for Authentication:
- Specify the private key file with
-e
for using an SSH key:rsync -avze "ssh -i /path/to/private_key.pem" /local/path/ username@remote_server:/remote/path/
- Specify the private key file with
Examples:
- Copy a directory from local to remote with verbose output:
rsync -avz -v /local/directory/ username@remote_server:/remote/path/
- Update only if the source file is newer:
rsync -avzu /local/path/ username@remote_server:/remote/path/
- Delete extraneous files in the destination:
rsync -avz --delete /local/path/ username@remote_server:/remote/path/
- Copy files from remote to local with SSH key authentication:
rsync -avze "ssh -i /path/to/private_key.pem" username@remote_server:/remote/path/ /local/path/
Interactive Mode:
- Use interactive mode for multiple files:
rsync -avzi username@remote_server:/remote/directory/* /local/destination/
Copying Between Remote Servers:
- Copy from one remote server to another:
rsync -avz username@remote_server1:/remote/path/ username@remote_server2:/remote/path/
Dry Run (Preview):
- Perform a dry run to preview changes without actually copying:
rsync -avz --dry-run /local/path/ username@remote_server:/remote/path/
These examples cover various use cases for Rsync. Replace placeholders like /local/path/
, username@remote_server
, etc., with your actual paths and server details. Rsync is a versatile tool for efficient file synchronization with numerous customization options. Always ensure proper permissions and connectivity for successful transfers.