Bash rsync
- A fast, versatile, remote (and local) file-copying tool
Using the rsync
Command
The rsync
command is used to efficiently transfer and synchronize files across computer systems, by checking the timestamp and size of files.
Basic Usage
To synchronize a directory to a remote host, use rsync -avz source user@hostname:/path
:
Example
rsync -avz /local/dir/ [email protected]:/remote/dir/
Understanding Rsync Output
The output of the rsync
command can vary depending on the options used. Here are some common elements:
- File List: Lists the files being transferred.
- Transfer Progress: Shows the progress of each file transfer.
- Compression Ratio: Indicates the effectiveness of compression if used.
- Speed: The speed at which files are being transferred.
Rsync Options Overview
Here are some common options you can use with the rsync
command:
-a
- Archive mode-v
- Increase verbosity-z
- Compress file data--delete
- Delete extraneous files-r
- Recurse into directories-u
- Skip files that are newer on the receiver--progress
- Show progress during transfer
Option: -a (Archive Mode)
The -a
option enables archive mode, which preserves permissions, times, symbolic links, and more. It's like a combination of several options.
Example: Archive Mode
rsync -a /local/dir/ [email protected]:/remote/dir/
Option: -v (Verbose)
The -v
option increases verbosity, providing detailed output of the rsync process.
Example: Verbose Output
rsync -av /local/dir/ [email protected]:/remote/dir/
Option: -z (Compression)
The -z
option compresses file data during transfer, which can speed up the transfer process.
Example: Enable Compression
rsync -az /local/dir/ [email protected]:/remote/dir/
Option: --delete
The --delete
option deletes files from the destination that are not present in the source.
Example: Delete Extraneous Files
rsync -avz --delete /local/dir/ [email protected]:/remote/dir/
Option: -r (Recursive)
The -r
option allows rsync to recurse into directories, copying all files and subdirectories.
Example: Recursive Copy
rsync -ar /local/dir/ [email protected]:/remote/dir/
Option: -u (Update)
The -u
option skips files that are newer on the receiver, avoiding overwriting newer files.
Example: Skip Newer Files
rsync -avu /local/dir/ [email protected]:/remote/dir/
Option: --progress
The --progress
option shows progress during transfer, providing detailed information about the transfer status.
Example: Show Progress
rsync --progress /local/dir/ [email protected]:/remote/dir/
Troubleshooting Common Issues
Common issues with rsync include "Permission Denied" or "Connection Timeout". Here are some troubleshooting tips:
- Permission Denied: Ensure you have the correct permissions on both the source and destination directories. Check user permissions and SSH key access.
- Connection Timeout: Verify network connectivity and ensure the remote server is reachable. Check firewall settings and SSH configurations.