Here’s a quick FAQ about rsync to help address some common questions:
rsync used for?rsync is primarily used for copying and synchronizing files and directories between two locations, either on the same machine or between local and remote systems. It is commonly used for backups, mirroring directories, or maintaining file consistency across multiple locations.
-a option do?The -a (archive) option preserves the following attributes of files and directories during synchronization:
-z option do?The -z option enables compression during data transfer, which can significantly reduce the amount of data that needs to be transmitted over the network, especially for large files or slow connections.
Use the -P option (which combines --progress and --partial) to display progress and allow resuming of interrupted transfers. Example:
rsync -avzP /path/to/source/ user@remote:/path/to/destination/
rsync know which files to transfer?rsync checks file modification times and sizes to determine whether a file needs to be transferred. It only transfers files that have changed, minimizing the amount of data sent.
You can use the --exclude option to specify patterns for files or directories that should not be copied. Example:
rsync -avz --exclude='*.tmp' --exclude='backup/' /path/to/source/ user@remote:/path/to/destination/
This command excludes all .tmp files and the backup/ directory.
--delete option do?The --delete option ensures that files deleted from the source are also deleted from the destination. It makes the destination an exact copy of the source, removing any extra files that are no longer in the source.
If a transfer is interrupted, you can resume it using the --partial option (which is included in -P), or you can add it manually. Example:
rsync -avzP /path/to/source/ user@remote:/path/to/destination/
rsync copy over SSH?Yes, rsync supports secure file transfers over SSH by default. To use SSH, specify the destination with the format user@host:path:
rsync -avz /path/to/source/ user@remote:/path/to/destination/
rsync?You can use the --bwlimit option to limit the bandwidth used during transfer. Example (limit to 500 KB/s):
rsync -avz --bwlimit=500 /path/to/source/ user@remote:/path/to/destination/
Yes, rsync uses a delta-transfer algorithm, which means it transfers only the parts of files that have changed, rather than the entire file. This is particularly useful for large files.
By default, rsync will replace older files in the destination with newer ones from the source. If you want to avoid overwriting newer files on the destination, use the -u (update) option:
rsync -avzu /path/to/source/ user@remote:/path/to/destination/
rsync is designed for one-way synchronization (source to destination). To perform bi-directional syncs, you’ll need to run rsync twice or use specialized tools like unison that handle bi-directional syncs.
rsync as a daemon?Yes, rsync can be run as a daemon to allow other clients to connect and synchronize files with it. This is useful in scenarios where you want to provide a centralized server for file syncing.
rsync faster?To optimize speed:
-z for compression if transferring over a slow network.--bwlimit to control bandwidth usage on high-traffic networks.rsync with --partial and --inplace to avoid duplicating data.rsync preserve file permissions and ownership?Yes, rsync preserves file permissions, ownership, and other file attributes like timestamps if you use the -a option.
/) and not using one?/), rsync copies the contents of the directory, not the directory itself.Example:
# Copy the content inside 'dir'
rsync -avz /path/to/dir/ user@remote:/path/to/destination/
# Copy the directory 'dir' itself along with its contents
rsync -avz /path/to/dir user@remote:/path/to/destination/
rsync backups with cron?You can schedule rsync to run automatically at specific intervals using cron. Edit the crontab with:
crontab -e
Add a cron job (example: run at 2 AM daily):
0 2 * * * rsync -avz /path/to/source/ user@remote:/path/to/destination/
rsync between Windows and Linux?Yes, you can use rsync between Windows and Linux using a tool like cwRsync on Windows, or through the Windows Subsystem for Linux (WSL).
rsync safe for production backups?Yes, rsync is widely used for production backups. However, make sure to use it with SSH for secure transfers, and test your backup scripts carefully. For large backups, consider using additional tools or options like --checksum for extra verification.