rsync

rsync #

What is rsync? #

rsync is a command-line tool used to synchronize files and directories between different systems. It is designed to be fast, efficient, and flexible, making it a popular choice for tasks such as backups, mirroring, and remote file transfers. It is built on top of the Unix file transfer protocol (FTP) and uses a sophisticated algorithm to determine which files need to be transferred, ensuring that only the changes are transmitted.

Syntax #

The basic syntax for rsync is as follows:

rsync [options] source destination

where source is the path of the file or directory to be copied, and destination is the path of the target directory where the file or directory will be copied to.

Rsync Options #

Here are some of the most commonly used options for rsync:

Option Description
-a, --archive Preserve file permissions, ownership, and timestamps
-v, --verbose Display verbose output
-z, --compress Compress data during transfer
--delete Delete files from the destination that don't exist in the source
--exclude=PATTERN Exclude files matching the specified pattern
--progress Show progress during transfer
-h, --human-readable Show file sizes in a human-readable format

Example #

For example, to synchronize the contents of the /home/user/Documents directory on the local machine to the /mnt/backup/Documents directory on an external drive, using compression and verbose output, the following command can be used:

rsync -avz /home/user/Documents /mnt/backup

This will copy all the files and subdirectories in /home/user/Documents to /mnt/backup/Documents, preserving their ownership and permissions, compressing the data during transfer, and displaying verbose output.

Remote Host Push and Pull #

Rsync can also be used to transfer files between a local machine and a remote host, using either the push or pull method.

To push files from the local machine to a remote host, the following command can be used:

rsync [options] source user@host:destination

For example, to transfer the /home/user/Documents directory on the local machine to the Documents directory on a remote host with the IP address 192.168.0.100, using compression and verbose output, the following command can be used:

rsync -avz /home/user/Documents user@192.168.0.100:/home/user/Documents

To pull files from a remote host to the local machine, the following command can be used:

rsync [options] user@host:source destination

For example, to transfer the Documents directory on the remote host to the /mnt/backup directory on the local machine, using compression and verbose output, the following command can be used:

rsync -avz user@192.168.0.100:/home/user/Documents /mnt/backup