[linux] Remote Linux server to remote linux server dir copy. How?

What is the best way to copy a directory (with sub-dirs and files) from one remote Linux server to another remote Linux server? I have connected to both using SSH client (like Putty). I have root access to both.

This question is related to linux data-transfer

The answer is


Use rsync so that you can continue if the connection gets broken. And if something changes you can copy them much faster too!

Rsync works with SSH so your copy operation is secure.


scp -r <directory> <username>@<targethost>:<targetdir>

Use rsync so that you can continue if the connection gets broken. And if something changes you can copy them much faster too!

Rsync works with SSH so your copy operation is secure.


Check out scp or rsync, man scp man rsync

scp file1 file2 dir3 user@remotehost:path

Well, quick answer would to take a look at the 'scp' manpage, or perhaps rsync - depending exactly on what you need to copy. If you had to, you could even do tar-over-ssh:

tar cvf - | ssh server tar xf -

I think you can try with:

rsync -azvu -e ssh user@host1:/directory/ user@host2:/directory2/

(and I assume you are on host0 and you want to copy from host1 to host2 directly)

If the above does not work, you could try:

ssh user@host1 "/usr/bin/rsync -azvu -e ssh /directory/ user@host2:/directory2/"

in the this, it would work, if you already have setup passwordless SSH login from host1 to host2


Log in to one machine

$ scp -r /path/to/top/directory user@server:/path/to/copy


I think you can try with:

rsync -azvu -e ssh user@host1:/directory/ user@host2:/directory2/

(and I assume you are on host0 and you want to copy from host1 to host2 directly)

If the above does not work, you could try:

ssh user@host1 "/usr/bin/rsync -azvu -e ssh /directory/ user@host2:/directory2/"

in the this, it would work, if you already have setup passwordless SSH login from host1 to host2


scp -r <directory> <username>@<targethost>:<targetdir>

There are two ways I usually do this, both use ssh:

scp -r sourcedir/ [email protected]:/dest/dir/

or, the more robust and faster (in terms of transfer speed) method:

rsync -auv -e ssh --progress sourcedir/ [email protected]:/dest/dir/

Read the man pages for each command if you want more details about how they work.


scp will do the job, but there is one wrinkle: the connection to the second remote destination will use the configuration on the first remote destination, so if you use .ssh/config on the local environment, and you expect rsa and dsa keys to work, you have to forward your agent to the first remote host.


I like to pipe tar through ssh.

tar cf - [directory] | ssh [username]@[hostname] tar xf - -C [destination on remote box]

This method gives you lots of options. Since you should have root ssh disabled copying files for multiple user accounts is hard since you are logging into the remote server as a normal user. To get around this you can create a tar file on the remote box that still hold that preserves ownership.

tar cf - [directory] | ssh [username]@[hostname] "cat > output.tar"

For slow connections you can add compression, z for gzip or j for bzip2.

tar cjf - [directory] | ssh [username]@[hostname] "cat > output.tar.bz2"

tar czf - [directory] | ssh [username]@[hostname] "cat > output.tar.gz"

tar czf - [directory] | ssh [username]@[hostname] tar xzf - -C [destination on remote box]


Try unison if the task is recurring. http://www.cis.upenn.edu/~bcpierce/unison/


scp -r <directory> <username>@<targethost>:<targetdir>

I used rdiffbackup http://www.nongnu.org/rdiff-backup/index.html because it does all you need without any fancy options. It's based on the rsync algorithm. If you only need to copy one time, you can later remove the rdiff-backup-data directory on the destination host.

rdiff-backup user1@host1::/source-dir user2@host2::/dest-dir

from the doc:

rdiff-backup also preserves subdirectories, hard links, dev files, permissions, uid/gid ownership, modification times, extended attributes, acls, and resource forks.

which is an bonus to the scp -p proposals as the -p option does not preserve all (e.g. rights on directories are set badly)

install on ubuntu:

sudo apt-get install rdiff-backup

scp will do the job, but there is one wrinkle: the connection to the second remote destination will use the configuration on the first remote destination, so if you use .ssh/config on the local environment, and you expect rsa and dsa keys to work, you have to forward your agent to the first remote host.


I used rdiffbackup http://www.nongnu.org/rdiff-backup/index.html because it does all you need without any fancy options. It's based on the rsync algorithm. If you only need to copy one time, you can later remove the rdiff-backup-data directory on the destination host.

rdiff-backup user1@host1::/source-dir user2@host2::/dest-dir

from the doc:

rdiff-backup also preserves subdirectories, hard links, dev files, permissions, uid/gid ownership, modification times, extended attributes, acls, and resource forks.

which is an bonus to the scp -p proposals as the -p option does not preserve all (e.g. rights on directories are set badly)

install on ubuntu:

sudo apt-get install rdiff-backup

scp as mentioned above is usually a best way, but don't forget colon in the remote directory spec otherwise you'll get copy of source directory on local machine.


If you are serious about wanting an exact copy, you probably also want to use the -p switch to scp, if you're using that. I've found that scp reads from devices, and I've had problems with cpio, so I personally always use tar, like this:

cd /origin; find . -xdev -depth -not -path ./lost+found -print0 \
| tar --create --atime-preserve=system --null --files-from=- --format=posix \
--no-recursion --sparse | ssh targethost 'cd /target; tar --extract \
--overwrite --preserve-permissions --sparse'

I keep this incantation around in a file with various other means of copying files around. This one is for copying over SSH; the other ones are for copying to a compressed archive, for copying within the same computer, and for copying over an unencrypted TCP socket when SSH is too slow.


rsync -avlzp /path/to/folder [email protected]:/path/to/remote/folder


I think you can try with:

rsync -azvu -e ssh user@host1:/directory/ user@host2:/directory2/

(and I assume you are on host0 and you want to copy from host1 to host2 directly)

If the above does not work, you could try:

ssh user@host1 "/usr/bin/rsync -azvu -e ssh /directory/ user@host2:/directory2/"

in the this, it would work, if you already have setup passwordless SSH login from host1 to host2


scp as mentioned above is usually a best way, but don't forget colon in the remote directory spec otherwise you'll get copy of source directory on local machine.


I used rdiffbackup http://www.nongnu.org/rdiff-backup/index.html because it does all you need without any fancy options. It's based on the rsync algorithm. If you only need to copy one time, you can later remove the rdiff-backup-data directory on the destination host.

rdiff-backup user1@host1::/source-dir user2@host2::/dest-dir

from the doc:

rdiff-backup also preserves subdirectories, hard links, dev files, permissions, uid/gid ownership, modification times, extended attributes, acls, and resource forks.

which is an bonus to the scp -p proposals as the -p option does not preserve all (e.g. rights on directories are set badly)

install on ubuntu:

sudo apt-get install rdiff-backup

If you are serious about wanting an exact copy, you probably also want to use the -p switch to scp, if you're using that. I've found that scp reads from devices, and I've had problems with cpio, so I personally always use tar, like this:

cd /origin; find . -xdev -depth -not -path ./lost+found -print0 \
| tar --create --atime-preserve=system --null --files-from=- --format=posix \
--no-recursion --sparse | ssh targethost 'cd /target; tar --extract \
--overwrite --preserve-permissions --sparse'

I keep this incantation around in a file with various other means of copying files around. This one is for copying over SSH; the other ones are for copying to a compressed archive, for copying within the same computer, and for copying over an unencrypted TCP socket when SSH is too slow.


Use rsync so that you can continue if the connection gets broken. And if something changes you can copy them much faster too!

Rsync works with SSH so your copy operation is secure.


rsync -avlzp /path/to/folder [email protected]:/path/to/remote/folder


I would modify a previously suggested reply:

rsync -avlzp /path/to/sfolder [email protected]:/path/to/remote/dfolder

as follows:

-a (for archive) implies -rlptgoD so the l and p above are superfluous. I also like to include -H, which copies hard links. It is not part of -a by default because it's expensive. So now we have this:

rsync -aHvz /path/to/sfolder [email protected]:/path/to/remote/dfolder

You also have to be careful about trailing slashes. You probably want

rsync -aHvz /path/to/sfolder/ [email protected]:/path/to/remote/dfolder

if the desire is for the contents of the source "sfolder" to appear in the destination "dfolder". Without the trailing slash, an "sfolder" subdirectory would be created in the destination "dfolder".


scp as mentioned above is usually a best way, but don't forget colon in the remote directory spec otherwise you'll get copy of source directory on local machine.


I like to pipe tar through ssh.

tar cf - [directory] | ssh [username]@[hostname] tar xf - -C [destination on remote box]

This method gives you lots of options. Since you should have root ssh disabled copying files for multiple user accounts is hard since you are logging into the remote server as a normal user. To get around this you can create a tar file on the remote box that still hold that preserves ownership.

tar cf - [directory] | ssh [username]@[hostname] "cat > output.tar"

For slow connections you can add compression, z for gzip or j for bzip2.

tar cjf - [directory] | ssh [username]@[hostname] "cat > output.tar.bz2"

tar czf - [directory] | ssh [username]@[hostname] "cat > output.tar.gz"

tar czf - [directory] | ssh [username]@[hostname] tar xzf - -C [destination on remote box]


Well, quick answer would to take a look at the 'scp' manpage, or perhaps rsync - depending exactly on what you need to copy. If you had to, you could even do tar-over-ssh:

tar cvf - | ssh server tar xf -

As non-root user ideally:

scp -r src $host:$path

If you already some of the content on $host consider using rsync with ssh as a tunnel.

/Allan


There are two ways I usually do this, both use ssh:

scp -r sourcedir/ [email protected]:/dest/dir/

or, the more robust and faster (in terms of transfer speed) method:

rsync -auv -e ssh --progress sourcedir/ [email protected]:/dest/dir/

Read the man pages for each command if you want more details about how they work.


Log in to one machine

$ scp -r /path/to/top/directory user@server:/path/to/copy


Try unison if the task is recurring. http://www.cis.upenn.edu/~bcpierce/unison/


Check out scp or rsync, man scp man rsync

scp file1 file2 dir3 user@remotehost:path

Well, quick answer would to take a look at the 'scp' manpage, or perhaps rsync - depending exactly on what you need to copy. If you had to, you could even do tar-over-ssh:

tar cvf - | ssh server tar xf -

Try unison if the task is recurring. http://www.cis.upenn.edu/~bcpierce/unison/


I would modify a previously suggested reply:

rsync -avlzp /path/to/sfolder [email protected]:/path/to/remote/dfolder

as follows:

-a (for archive) implies -rlptgoD so the l and p above are superfluous. I also like to include -H, which copies hard links. It is not part of -a by default because it's expensive. So now we have this:

rsync -aHvz /path/to/sfolder [email protected]:/path/to/remote/dfolder

You also have to be careful about trailing slashes. You probably want

rsync -aHvz /path/to/sfolder/ [email protected]:/path/to/remote/dfolder

if the desire is for the contents of the source "sfolder" to appear in the destination "dfolder". Without the trailing slash, an "sfolder" subdirectory would be created in the destination "dfolder".


Check out scp or rsync, man scp man rsync

scp file1 file2 dir3 user@remotehost:path

rsync -avlzp /path/to/folder [email protected]:/path/to/remote/folder


Log in to one machine

$ scp -r /path/to/top/directory user@server:/path/to/copy


Well, quick answer would to take a look at the 'scp' manpage, or perhaps rsync - depending exactly on what you need to copy. If you had to, you could even do tar-over-ssh:

tar cvf - | ssh server tar xf -

As non-root user ideally:

scp -r src $host:$path

If you already some of the content on $host consider using rsync with ssh as a tunnel.

/Allan


If you are serious about wanting an exact copy, you probably also want to use the -p switch to scp, if you're using that. I've found that scp reads from devices, and I've had problems with cpio, so I personally always use tar, like this:

cd /origin; find . -xdev -depth -not -path ./lost+found -print0 \
| tar --create --atime-preserve=system --null --files-from=- --format=posix \
--no-recursion --sparse | ssh targethost 'cd /target; tar --extract \
--overwrite --preserve-permissions --sparse'

I keep this incantation around in a file with various other means of copying files around. This one is for copying over SSH; the other ones are for copying to a compressed archive, for copying within the same computer, and for copying over an unencrypted TCP socket when SSH is too slow.


Use rsync so that you can continue if the connection gets broken. And if something changes you can copy them much faster too!

Rsync works with SSH so your copy operation is secure.


There are two ways I usually do this, both use ssh:

scp -r sourcedir/ [email protected]:/dest/dir/

or, the more robust and faster (in terms of transfer speed) method:

rsync -auv -e ssh --progress sourcedir/ [email protected]:/dest/dir/

Read the man pages for each command if you want more details about how they work.


Log in to one machine

$ scp -r /path/to/top/directory user@server:/path/to/copy


Check out scp or rsync, man scp man rsync

scp file1 file2 dir3 user@remotehost:path

I think you can try with:

rsync -azvu -e ssh user@host1:/directory/ user@host2:/directory2/

(and I assume you are on host0 and you want to copy from host1 to host2 directly)

If the above does not work, you could try:

ssh user@host1 "/usr/bin/rsync -azvu -e ssh /directory/ user@host2:/directory2/"

in the this, it would work, if you already have setup passwordless SSH login from host1 to host2


I used rdiffbackup http://www.nongnu.org/rdiff-backup/index.html because it does all you need without any fancy options. It's based on the rsync algorithm. If you only need to copy one time, you can later remove the rdiff-backup-data directory on the destination host.

rdiff-backup user1@host1::/source-dir user2@host2::/dest-dir

from the doc:

rdiff-backup also preserves subdirectories, hard links, dev files, permissions, uid/gid ownership, modification times, extended attributes, acls, and resource forks.

which is an bonus to the scp -p proposals as the -p option does not preserve all (e.g. rights on directories are set badly)

install on ubuntu:

sudo apt-get install rdiff-backup

Try unison if the task is recurring. http://www.cis.upenn.edu/~bcpierce/unison/


scp as mentioned above is usually a best way, but don't forget colon in the remote directory spec otherwise you'll get copy of source directory on local machine.


As non-root user ideally:

scp -r src $host:$path

If you already some of the content on $host consider using rsync with ssh as a tunnel.

/Allan


There are two ways I usually do this, both use ssh:

scp -r sourcedir/ [email protected]:/dest/dir/

or, the more robust and faster (in terms of transfer speed) method:

rsync -auv -e ssh --progress sourcedir/ [email protected]:/dest/dir/

Read the man pages for each command if you want more details about how they work.


scp -r <directory> <username>@<targethost>:<targetdir>

If you are serious about wanting an exact copy, you probably also want to use the -p switch to scp, if you're using that. I've found that scp reads from devices, and I've had problems with cpio, so I personally always use tar, like this:

cd /origin; find . -xdev -depth -not -path ./lost+found -print0 \
| tar --create --atime-preserve=system --null --files-from=- --format=posix \
--no-recursion --sparse | ssh targethost 'cd /target; tar --extract \
--overwrite --preserve-permissions --sparse'

I keep this incantation around in a file with various other means of copying files around. This one is for copying over SSH; the other ones are for copying to a compressed archive, for copying within the same computer, and for copying over an unencrypted TCP socket when SSH is too slow.