Rsync to Amazon Ec2 Instance
Asked Answered
C

5

82

I have an EC2 instance running and I am able to SSH into it.

However, when I try to rsync, it gives me the error Permission denied (publickey).

The command I'm using is:

rsync -avL --progress -e ssh -i ~/mykeypair.pem ~/Sites/my_site/* [email protected]:/var/www/html/

I also tried

rsync -avz ~/Sites/mysite/* -e "ssh -i ~/.ssh/id_rsa.pub" [email protected]:/var/www/html/

Thanks,

Coaly answered 5/4, 2013 at 20:43 Comment(3)
How are you SSHing into it?Provence
Your second version is mostly right, except you are using the wrong key. You may also need to move the source path after the ssh command.Irreligious
similar Q ! https://mcmap.net/q/244475/-mac-to-ec2-the-source-control-triangle-problem-git-rsync-wthEpicurus
S
126

I just received that same error. I had been consistently able to ssh with:

ssh -i ~/path/mykeypair.pem \
[email protected]

But when using the longer rsync construction, it seemed to cause errors. I ended up encasing the ssh statement in quotations and using the full path to the key. In your example:

rsync -avL --progress -e "ssh -i /path/to/mykeypair.pem" \
       ~/Sites/my_site/* \ 
       [email protected]:/var/www/html/

That seemed to do the trick.

Summer answered 24/4, 2013 at 22:57 Comment(2)
if you had trouble with the verbosity of the cmd you can give a try to aws-upload.Pneumatics
If you want hidden files to transfer as well, remove * from the source path: rsync -va -e "ssh -i /path/to/key.pem" /source/path/ [email protected]:/destination/path/Desalinate
H
19

use rsync to copy files between servers

copy file from local machine to server

rsync -avz -e "ssh -i /path/to/key.pem" /path/to/file.txt  <username>@<ip/domain>:/path/to/directory/

copy file from server to local machine

rsync -avz -e "ssh -i /path/to/key.pem" <username>@<ip/domain>:/path/to/directory/file.txt  /path/to/directory/

note: use command with sudo if you are not a root user

Highminded answered 12/2, 2018 at 9:14 Comment(0)
S
17

Below is what I used and it worked. Source was ec2 and target was home machine.

 sudo rsync  -azvv -e "ssh -i /home/ubuntu/key-to-ec2.pem" [email protected]:/home/ec2-user/source/ /home/ubuntu/target/
Substitution answered 8/2, 2014 at 20:17 Comment(1)
Potentially dangerous. Erased a day's work at destination if you didn't understand what it means as it overwrites by whether files have differences.Catchascatchcan
M
12

After suffering a little bit, I believe this will help:

I am using the below command and it has worked without problems:

rsync -av --progress -e ssh /folder1/folder2/* [email protected]:/folder1/folder2

First consideration:

Use the --rsync-path

I prefer in a shell script:

#!/bin/bash

RSYNC = /usr/bin/rsync

$RSYNC [options] [source] [destination]

Second consideration:

Create a publick key by command below for communication between the servers in question. She will not be the same as provided by Amazon.

ssh-keygen -t rsa

Do not forget to enable permission on the target server in /etc/ssh/sshd_config (UBUNTU and CENTOS).

Sync files from one EC2 instance to another

http://ask-leo.com/how_can_i_automate_an_sftp_transfer_between_two_servers.html

Use -v option for verbose and better identify errors.

Third Consideration

If both servers are on EC2 make a restraint by security group

In the security group Server Destination:

inbound: Source / TCP port 22 / IP Security (or group name) of the source server

Marylou answered 12/7, 2013 at 15:1 Comment(1)
thanks for providing the second solution. you are a savior! the ask-leo site is a good resource for those trying to move files between 2 serversLabial
G
3

This worked for me:

nohup rsync -zravu --partial --progress  -e "ssh -i xxxx.pem" [email protected]:/mnt/data   /mnt2/ &
Galinagalindo answered 13/11, 2015 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.