Copy directory from one ssh server to another

3

I need to copy a directory to a server with a script .sh running on another server. I read about the scp command and I saw some usage examples, but I did not figure out how I put the password of the server I'm accessing to copy right into my script , here is an example of the scp command %:

scp /path/to/file username@a:/path/to/destination

It does not have to be just one line, it may be more ...

    
asked by anonymous 05.08.2016 / 14:56

2 answers

3

1 - Create the keys as already reported in the comments. Use the commands below:

ssh-keygen
  • Answer the questions, you can type for all

2- Copy the key to the target machine

ssh-copy-id host_ou_ip_destino

If the target machine uses another user or port in SSH, make a copy like this:

ssh-copy-id "[email protected] -p 2200"

Confirm the options that appear for finger-print recording.

3 - Testing authentication with keys.

ssh host_ou_ip_destino

To copy the directory, although sending a tar is more efficient as commented in your post by Daniel Omine , use:

scp -rp dir_origem [email protected]:/dir_destino

EDITING TO GENERATE TAR LOCAL
5- To generate a local tar with the remote files, use:

ssh [email protected] "tar -cvzf - /dir_remoto/origem/" > local.tar
  

Source: Copy directory : Generate   and copy key :    link

    
08.08.2016 / 20:08
1

You can use sshpass :

  

sshpass is a utility designed for running ssh using the standard   to as "keyboard-interactive " password authentication, but in   non-interactive mode.

To install on Ubuntu / Debian:

sudo apt-get install sshpass

No CentOS:

yum -y install sshpass

To use it, do so:

#!/bin/bash
sshpass -p "<senha>" scp -r /path/to/file [email protected]:/path/to/destination

The -r option of scp is used to recursively copy the directory.

    
05.08.2016 / 15:38