Syncing Repositories with rsync
Introduction
rsync
is a powerful tool for synchronizing files and directories between two locations over a network or locally. It is highly efficient and versatile, making it a favorite among system administrators and developers.
Installation
rsync
is typically pre-installed on most Unix-based systems. To check if rsync
is installed, run:
rsync --version
If it’s not installed, you can install it using your package manager.
For Debian-based systems (e.g., Ubuntu):
sudo apt-get install rsync
For Red Hat-based systems (e.g., CentOS):
sudo yum install rsync
For macOS (using Homebrew):
brew install rsync
Basic Usage
The basic syntax for rsync is:
rsync [options] source destination
Examples
Copy a file from local to remote system:
rsync -avz /path/to/local/file user@remote:/path/to/destination/
Copy a directory from remote to local system:
rsync -avz user@remote:/path/to/remote/directory/ /path/to/local/destination/
Sync two local directories:
rsync -avz /path/to/source/directory/ /path/to/destination/directory/
Common Options
Option | Description |
---|---|
-a, --archive | Archive mode, equivalent to -rlptgoD . This option tells rsync to sync directories recursively, transfer special and block devices, preserve symbolic links, modification times, groups, ownership, and permissions. |
-z, --compress | This option forces rsync to compress the data as it is sent to the destination machine. Use this option only if the connection to the remote machine is slow. |
-P | Equivalent to --partial--progress . When this option is used, rsync shows a progress bar during the transfer and keeps the partially transferred files. It is useful when transferring large files over slow or unstable network connections. |
--delete | When this option is used, rsync deletes extraneous files from the destination location. It is useful for mirroring. |
-q, --quiet | Use this option if you want to suppress non-error messages. |
-e | This option allows you to choose a different remote shell. By default, rsync is configured to use ssh. |
Example with Options
rsync -avz --delete --progress /path/to/source/ user@remote:/path/to/destination/
Excluding Files and Directories
You can exclude specific files or directories using the --exclude
option.
rsync -avz --exclude 'node_modules' /path/to/source/ user@remote:/path/to/destination/
Synchronizing Over SSH
By default, rsync uses ssh for remote transfers. You can specify a different remote shell using the -e option.
rsync -avz -e ssh /path/to/source/ user@remote:/path/to/destination/
Dry Run
To see what changes will be made without actually transferring any files, use the --dry-run
option.
rsync -avz --dry-run /path/to/source/ user@remote:/path/to/destination/
Enjoy Reading This Article?
Here are some more articles you might like to read next: