Learn how to use the rsync command in Linux for efficient and secure file synchronization and data backup between local and remote locations
- Efficiently copies files to or from remote systems
- Uses secure ssh connections for transport
- rsync *.conf raju:/home/ravi/configs/
- Faster than scp -copies differences in like files
Efficient network copies: rsync
rsync is a program that works in much the same way that the older rcp does, but has many more options and uses the rsync remote-update protocol to greatly increase the speed of file transfers when the destination file already exists.
The rsync remote-update protocol allows rsync to transfer just the differences between two sets of files using an efficient checksum-search algorithm. This utility is useful for tasks like updating web content because it will only transfer the changed files.
Useful options to rsync: –
- -e command specifies an external, rsh-compatible program to connect with (usually ssh)
- -a recuses subdirectories, preserving permissions, ownership, etc.
- -r recuses subdirectories without preserving permissions, etc.
- –partial continues partially downloaded files
- –progress prints a progress bar while transferring
- -P is the same as –partial –progress
See the rsync(1) man page for a complete list.
Example
1. Basic Local File Copy:
To copy a file or directory from one location to another on the same system:
rsync -av /source/path/ /destination/path/
-a
: Archive mode, which preserves permissions, ownership, timestamps, and more.-v
: Verbose mode, which shows the files being copied.
2. Local Directory Synchronization:
To synchronize the contents of two directories (updating the destination to match the source):
rsync -av /source/directory/ /destination/directory/
3. Copy Files to a Remote Server (over SSH):
To copy files or directories from the local system to a remote server using SSH:
rsync -av -e ssh /source/path/ username@remote_server:/destination/path/
4. Exclude Files or Directories:
You can exclude specific files or directories during synchronization using the --exclude
option. For example:
rsync -av --exclude="*.log" /source/directory/ /destination/directory/
This command excludes all files with the “.log” extension from the synchronization.
5. Limit Bandwidth Usage:
To limit the bandwidth used during synchronization, you can use the --bwlimit
option. For example, to limit to 1MB/s:
rsync -av --bwlimit=1000 /source/directory/ /destination/directory/
6. Dry Run (Preview):
To see what rsync
would do without actually copying any files, you can use the --dry-run
or -n
option:
rsync -av --dry-run /source/directory/ /destination/directory/
7. Delete Files on Destination:
By default, rsync
won’t delete any files on the destination that don’t exist on the source. To delete files on the destination that no longer exist on the source, use the --delete
option:
rsync -av --delete /source/directory/ /destination/directory/
These are just some common examples of how to use rsync
. It’s a versatile tool with many options, so you can tailor it to your specific needs for file synchronization and backup tasks. Always exercise caution when using rsync
with the --delete
option to avoid accidental data loss.