[TOP TIP] How to transfer files/directories between servers
METHOD 1 - direct server to server
Lets pretend that we have two systems, serverA and serverB. We want to transfer the /home/user directory from serverA to serverB. This can be achieved by generating a temporary ssh key on serverB and by using the generated public key in serverA.
first we generate a temporary ssh key, the following command will generate a new key under ~/.ssh named tempkey (tempkey, tempkey.pub) of type ed25519.
ssh-keygen ssh-keygen -t ed25519 -f ~/.ssh/tempkey
next, we need to send the generated public key to serverA, this can be done either via plain copy/paste or by using ssh-copy-id. In any case, the idea is to copy our serverB ~/.ssh/tempkey.pub to the serverA ~/.ssh/authorized_keys. If this is done for the root user, then that will give serverB full access to serverA.
~/.ssh/tempkey.pub copy/paste to ~/.ssh/authorized_keys
now that our serverB has access to serverA, we may issue our copy command, for example:
by using scp
scp -C -i ~/.ssh/tempkey -p -r [email protected] /home/user
or by using rsync
rsync -e "ssh" --safe-links -tarxlzhP [email protected] /home/user
METHOD 2 - indirect server to server via a local system
Lets pretend that we have three systems, a desktop, serverA and serverB. We want to transfer the /home/user directory from serverA to serverB but without allowing the two servers to interact directly. This can be achieved by using sshfs on the desktop, which is provided by the fuse-sshfs rpm package.
The flow of data looks like this: serverA (sshfs)-> desktop (rsync over ssh)-> serverB
In essence, we mount serverA to a local directory and use rsync to copy files/directories to serverB, so here is a quick and dirty little script that does that for you:
#!/bin/bash # rsync 2 remove hosts if [ $# -ne 2 ]; then echo 1>&2 Usage: rsync2 [[email protected]]host:[dir] [[email protected]]host:[dir] exit 127 fi if [ ! -f /usr/bin/sshfs ]; then echo "sshfs: command not found." echo "The required 'fuse-sshfs' package is not installed." exit 127 fi # remove stale tmp directory rm -rf /tmp/sshfstmp 2>/dev/null # create temporary directory mkdir /tmp/sshfstmp chmod go-wrx /tmp/sshfstmp # mount sshfs sshfs "$1" /tmp/sshfstmp # rsync rsync --safe-links -tarxlzhP /tmp/sshfstmp/ "$2" # unmount fusermount -u /tmp/sshfstmp # remove tmp directory rm -rf /tmp/sshfstmp 2>/dev/null
Save it as ~/bin/rsync2 and use it like this:
rsync2 [email protected]:/home/user [email protected]:/home/user
Be warned that this method is considerably slower but has its uses when you don't want the two servers to access one another.
scp -C -i ~/.ssh/tempkey -p -r email@example.com /home/user
cp: cannot stat '[email protected]': No such file or directory
The reason for the error is that scp is treating the [email protected]… as a local file name (not a network resource).
You'll want to add a ":" and a directory name like this:
scp -C -i ~/.ssh/tempkey -p -r [email protected]:/source/directory /home/user/dest/directory