Learn how to rsync to EC2 with help of SSH protocol authenticated using private key file. Process can be used for Rsync from and between EC2.
We learned about Rsync in our last post. We learned how Rsync helps in data backup or mirroring by using less bandwidth, time on second run. Since it syncs only changes in later executions after first fresh copy operation. Now many of traditional data centers are moving to cloud services like AWS. Rsync can be useful to sync data from your local server to AWS hosted EC2 instance (if data size is not huge).
In this article we will learn about how to rsync to EC2 server in AWS. Since you know EC2 Linux instances dont use conventional used id-password combination for authentication, Key pairs needs to be used in Rsync for authentication EC2. For Rsync setup, your EC2 instance must be launched with public-private key pair and you should have private key file with you.
- To start with make sure your EC2 instance is launched with key pair.
- Upload private key file on source server (from where you are going to Rsync to EC2)
- Make sure key file set with 400 permission
- Get public IP or public DNS name of EC2 server from AWS EC2 console web page
- Confirm you are able to connect from source to EC2. (verify AWS security groups and firewall settings)
Execute Rsync to EC2
We have file to copy (testfile.tar) and private key file (mykey.pem) ready on source server.
[root@kerneltalks ~]# ll /root/mykey.pem
-r--------. 1 root root 1675 Jul 24 01:01 /root/mykey.pem
[root@kerneltalks ~]# ll testfile.tar
-rw-r--r--. 1 root root 39198720 Dec 19 2016 testfile.tar
Now, use below Rsync command :
[root@kerneltalks ~]# rsync -avz -e "ssh -i /root/mykey.pem" testfile.tar firstname.lastname@example.org:/tmp/
sending incremental file list
sent 8520069 bytes received 31 bytes 3408040.00 bytes/sec
total size is 39198720 speedup is 4.60
- -a : Archive mode preserves permission and ownership
- -v : verbose mode
- -z : compress
- -e : Choose remote shell of execution
- ssh -i keyfile : Use private key for authentication on destination using ssh protocol
- source (testfile.tar)
- Destination : Public DNS name of EC2 instance
Thats it! You file is copied over to EC2. This can be done vice versa as well. You can sync files from EC2 server to local server as well. Just switch source destination paths and you are all set to go.
Rsync between two EC2 servers
Rsync can be executed between two EC2 server i.e. from one EC2 server to another. Same above command can be used. If you are doing it for EC2 instances within same region then Internal DNS name can be used in command.
Rsync is possible from, to and between EC2 servers. Key file authenticated SSH protocol should be used in Rsync command to achieve this.