Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Sun Jan 07, 2018 7:34 am 
Senior Member
User avatar

Joined: Sun Jan 24, 2016 4:30 am
Posts: 294
There are tons of ways to transfer files and directories between two systems. Here are two typical methods to help you achieve your life's goals...

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, 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/ to the serverA ~/.ssh/authorized_keys. If this is done for the root user, then that will give serverB full access to serverA.
~/.ssh/ 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:

# rsync 2 remove hosts

if [ $# -ne 2 ]; then
        echo 1>&2 Usage: rsync2 [user@]host:[dir] [user@]host:[dir]
        exit 127

if [ ! -f /usr/bin/sshfs ]; then
        echo "sshfs: command not found."
        echo "The required 'fuse-sshfs' package is not installed."
        exit 127

# 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.

:idea: I love my computer... all my friends live there.


Display posts from previous:  Sort by  
Post new topic  Reply to topic

Who is online

Users browsing this forum: No registered users and 4 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

Powered by phpBB® Forum Software © phpBB Group