[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.
(on serverB)
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
(on serverA)
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
(on serverB)
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 user@serverA /home/user
or by using rsync
rsync -e "ssh" --safe-links -tarxlzhP user@serverA /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 [user@]host:[dir] [user@]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 user@serverA:/home/user user@serverB:/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.
8 Replies
I tried server to server with scp but when I execute the command
scp -C -i ~/.ssh/tempkey -p -r root@ip.of.server.a /home/user
I get the response
cp: cannot stat 'root@ip.of.server.a': No such file or directory
scp -C -i ~/.ssh/tempkey -p -r root@ip.of.server.a /home/user
cp: cannot stat 'root@ip.of.server.a': No such file or directory
The reason for the error is that scp is treating the root@… 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 root@ip.of.server.a:/source/directory /home/user/dest/directory
When I try to execute the following
scp -C -i ~/.ssh/tempkey -p -r root@ip.of.server.a:/source/directory /home/user/dest/directory
I get
ssh: connect to host [obscured] port 22: Connection refused
Both of my servers are in the same Linode cluster (as in, they are in the same geographical location) and I have followed the steps above as detailed.
If port 22 is refusing connection, it may be because it's either closed or filtered. A good way to find out is by doing a port scan of your IP address using nmap:
nmap $yourIP -p 22
That'll show you the status of that port.
If it's filtered, it's most likely an issue with your firewall, and you'll need to open that port. We have a related post here that shows you how to do this:
How do I open a port in my Linode's firewall?
If the post is closed, it could mean that the service listening on it (SSH) needs to be started. You can access your Linode via LISH and restart it using the command that corresponds to your distro.
For distros that use systemd, run the following:
sudo systemctl restart sshd
Distros using SystemV or Upstart:
sudo service sshd restart
For further troubleshooting, we have a guide which gives you some additional steps you can take:
ssh-keygen ssh-keygen -t ed25519 -f ~/.ssh/tempkey
syntax error above
This tutorial is of no help. There are two different typos
tempkey.pub and tempkey are interchangeable and should not be.
and
duplicate ssh-keygen syntax error.
Even with these corrected, I still get connection refused even thought the port is open.
Turned off the firewall and no help.
ok, seems both servers have to be using the same port number.