Rsync: Difference between revisions
No edit summary |
No edit summary |
||
| (11 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
==Quick Usage Guide== | |||
== Quick Usage Guide == | ===Copying Files via SSH=== | ||
=== Copying Files via SSH === | |||
To copy files from {{code|/export/backup}} to a remote location (on server 'backup'): | To copy files from {{code|/export/backup}} to a remote location (on server 'backup'): | ||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
$ rsync -avz /export/backup/ -e ssh backup:/export/backup/ | |||
## Or with a non-standard SSH port | ## Or with a non-standard SSH port | ||
$ rsync -avz /export/backup/ -e "ssh -p 2222" backup:/export/backup/ | |||
}} | }} | ||
=== Copying files with Progress Information === | === With sudo on remote server === | ||
If you need to elevate your privileges with <code>sudo</code> when copying files on the remote system, specify an rsync path using sudo: | |||
{{Highlight | |||
| code = ## With sudo on the remote host | |||
$ rsync -avz --rsync-path="sudo rsync" -e "ssh -p 2222" user@remote:/path ./path | |||
| lang = terminal | |||
}} | |||
You also need to add <code>rsync</code> in the <code>/etc/sudoers</code> file: | |||
{{Highlight | |||
| code = username ALL=NOPASSWD:/usr/bin/rsync | |||
| lang = text | |||
}} | |||
===Copying files with Progress Information=== | |||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
$ rsync -avh --progress /src/ /dst/ | |||
}} | |||
===Limiting Bandwidth=== | |||
The transfer rate can be limited with the {{code|1=--bwlimit=kbps}} option. Bytes per second is the default if no unit is given. | |||
{{highlight|lang=terminal|code= | |||
## Limit transfer speed to 1MB/s | |||
$ rsync --bwlimit=1024 /source/ /destination/ | |||
$ rsync --bwlimit=1m /source/ /destination/ | |||
}} | |||
===Parallel Copy=== | |||
You may wish to run multiple rsync in parallel to speed up data transfers. | |||
{{Highlight | |||
| code = ## Transfers /source/directory tot /destination/directory with 5 parallel jobs | |||
$ ls -1 /source/directory/ \ | |||
{{!}} xargs -I {} -P 5 -n 1 rsync -avh --progress /source/directory/{}/ /destination/directory/{}/ | |||
| lang = terminal | |||
}} | }} | ||
== | ===Excluding a Directory=== | ||
If your source has snapshot directories (such as <code>.snapshots</code>) which aren't hidden by your filesystem by default, you can exclude it from being copied by using the <code>--exclude directory</code> option. Eg: | |||
{{Highlight | |||
| code = $ rsync --exclude .snapshots -avh --progress /source/ /destination/ | |||
| lang = terminal | |||
}} | |||
Because I've been bitten by this issue way too many times in the past couple of days, to avoid copying a directory into another directory, always have a trailing slash from the source. | ==Copying Directories== | ||
Because I've been bitten by this issue way too many times in the past couple of days, to avoid copying a directory into another directory, '''always have a trailing slash from the source'''. | |||
In more depth, suppose I have a | In more depth, suppose I have a {{code|src}} directory and I want its contents to be replicated in {{code|dst}}. | ||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
## This copies 'src' into 'dst/src' | ## This copies 'src' into 'dst/src' | ||
$ rsync -avh src dst | |||
## This too copies 'src' into 'dst/src' | ## This too copies 'src' into 'dst/src' | ||
$ rsync -avh src dst/ | |||
## Works as expected. Contents in src is now in dst | ## Works as expected. Contents in src is now in dst | ||
$ rsync -avh src/ dst | |||
## Works as expected as well | ## Works as expected as well | ||
$ rsync -avh src/ dst/ | |||
}} | }} | ||
==See Also== | |||
*https://calomel.org/rsync_tips.html | |||
{{Navbox Linux}} | |||
[[Category:Linux]] | [[Category:Linux]] | ||
[[Category:LinuxUtilities]] | [[Category:LinuxUtilities]] | ||
Latest revision as of 23:38, 10 March 2021
Quick Usage Guide
Copying Files via SSH
To copy files from /export/backup to a remote location (on server 'backup'):
$ rsync -avz /export/backup/ -e ssh backup:/export/backup/
## Or with a non-standard SSH port
$ rsync -avz /export/backup/ -e "ssh -p 2222" backup:/export/backup/
With sudo on remote server
If you need to elevate your privileges with sudo when copying files on the remote system, specify an rsync path using sudo:
## With sudo on the remote host
$ rsync -avz --rsync-path="sudo rsync" -e "ssh -p 2222" user@remote:/path ./path
You also need to add rsync in the /etc/sudoers file:
username ALL=NOPASSWD:/usr/bin/rsync
Copying files with Progress Information
$ rsync -avh --progress /src/ /dst/
Limiting Bandwidth
The transfer rate can be limited with the --bwlimit=kbps option. Bytes per second is the default if no unit is given.
## Limit transfer speed to 1MB/s
$ rsync --bwlimit=1024 /source/ /destination/
$ rsync --bwlimit=1m /source/ /destination/
Parallel Copy
You may wish to run multiple rsync in parallel to speed up data transfers.
## Transfers /source/directory tot /destination/directory with 5 parallel jobs
$ ls -1 /source/directory/ \
| xargs -I {} -P 5 -n 1 rsync -avh --progress /source/directory/{}/ /destination/directory/{}/
Excluding a Directory
If your source has snapshot directories (such as .snapshots) which aren't hidden by your filesystem by default, you can exclude it from being copied by using the --exclude directory option. Eg:
$ rsync --exclude .snapshots -avh --progress /source/ /destination/
Copying Directories
Because I've been bitten by this issue way too many times in the past couple of days, to avoid copying a directory into another directory, always have a trailing slash from the source.
In more depth, suppose I have a src directory and I want its contents to be replicated in dst.
## This copies 'src' into 'dst/src'
$ rsync -avh src dst
## This too copies 'src' into 'dst/src'
$ rsync -avh src dst/
## Works as expected. Contents in src is now in dst
$ rsync -avh src/ dst
## Works as expected as well
$ rsync -avh src/ dst/