rsync - Linux
Overview
rsync is a powerful file-transfer tool used for mirroring, copying, and synchronizing files and directories between two locations over a remote shell, as well as locally on a single machine. It is widely appreciated for its efficiency in handling both large files and large sets of files, as it only transfers the changes made rather than copying entire files over. This command is beneficial for backups, mirroring data across multiple systems, maintaining home directories in sync, and more.
Syntax
The basic syntax of rsync command is:
rsync [options] source destination
source
: The path to the source files or directories.destination
: The path to the destination where the files will be copied.
Both source
and destination
can be local paths or remote specifications.
Options/Flags
Here are some commonly used options in rsync:
-v, --verbose
: Increases the verbosity of the program.-a, --archive
: Archive mode; equals-rlptgoD
(no-H,-A,-X
).-z, --compress
: Compress file data during the transfer.-h, --human-readable
: Output numbers in a human-readable format.--progress
: Shows progress during transfer.-e, --rsh=COMMAND
: Specify the remote shell to use.--delete
: Deletes extraneous files from destination dirs.-r, --recursive
: Recurse into directories.--exclude=PATTERN
: Exclude files matching PATTERN.--include=PATTERN
: Include files matching PATTERN.-n, --dry-run
: Perform a trial run with no changes made.
Each option adjusts the behavior of the rsync command, allowing for customization based on the user’s specific needs.
Examples
Basic Syncing
rsync -avz foo/ user@remote:/backups/foo
This command syncs the directory foo
to the remote host, compresses file data during transfer, retains all file permissions, and provides verbose output.
Using Wildcards
rsync -avz /files/*.txt remote:/files/
Sync all .txt
files from the local /files
directory to the remote /files
directory.
Dry Run
rsync -avzn /test/ remote:/test/
Perform a dry run of syncing the directory /test
with the remote directory /test
, showing what would be done without making any changes.
Common Issues
Permissions Errors: Users often encounter permission errors when they lack the necessary read/write permissions on the target or source directories. Always ensure appropriate permissions or use options like --super
.
Network Issues: Slow or unstable network can disrupt the syncing process. Using the --partial
and --progress
options can help manage these issues by keeping partially transferred files which can be resumed.
Integration
rsync can be integrated into backup scripts:
#!/bin/bash
rsync -av --delete /local/directory/ user@remote:/backup/directory/
This script ensures that the local directory’s content matches the backup directory on a remote system, deleting files in the destination that are not present in the source.
Related Commands
- scp: Unlike rsync, scp copies files over network without sync features.
- cron: Useful for scheduling rsync jobs (e.g., backups).
For further reading, official documentation can be found here: