What is Rsync?
Rsync (Remote Sync) is a powerful command-line utility used for file synchronization and transfer between local and remote systems. It efficiently copies and synchronizes files, directories, or even whole file systems with minimal data transfer. Rsync is widely used for backup, mirroring, and data migration tasks due to its speed, reliability, and ability to resume interrupted transfers.
The basic syntax of rsync is as follows:
rsync [options] source destination
source: The path of the files or directories you want to synchronize or copy.
destination: The target location where you want to copy the data.
-a(archive): This option preserves permissions, timestamps, symbolic links, and recursive copying. It is often used for typical backup and synchronization tasks.
-v(verbose): Enables verbose output, providing more information about the files being transferred.
-z(compress): Compresses data during transfer, which can be useful for slow or limited bandwidth connections.
--delete: Deletes files in the destination that are not present in the source. Use this with caution as data loss can occur if not used carefully.
-P(progress): Displays progress during the transfer, showing the percentage completed and estimated time.
- Copy local files/directories to a remote server:
rsync -avz /path/to/local/source user@remote_server:/path/to/destination
- Copy files/directories from a remote server to the local system:
rsync -avz user@remote_server:/path/to/source /path/to/local/destination
- Synchronize two directories (local or remote):
rsync -avz /path/to/source/ /path/to/destination/
Exclude Files or Directories:
You can exclude certain files or directories from the rsync process using the
--exclude option. This is particularly useful when you want to omit specific files or directories from being synchronized.
rsync -avz --exclude 'file_to_exclude' /path/to/source/ /path/to/destination/
Before performing the actual sync, you can use the
--dry-run option to see what rsync would do without actually making any changes. This allows you to preview the outcome and verify if the command behaves as expected.
rsync -avz --dry-run /path/to/source/ /path/to/destination/