Learn how to rsync to EC2 with the help of SSH protocol authenticated using a private key file. The process can be used for Rsync from and between EC2.
We learned about Rsync in our last post. We learned how Rsync helps in a data backup or mirroring by using less bandwidth, time on the second run. Since it syncs only changes in later executions after the first fresh copy operation. Now many 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 the 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 don’t use a conventional used id-password combination for authentication, Key pairs need to be used in Rsync for authentication EC2. For the Rsync setup, your EC2 instance must be launched with public-private key pair and you should have a private key file with you.
- To start with making sure your EC2 instance is launched with a key pair.
- Upload private key file on the 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 testfile.tar for testing copy and private key file (mykey.pem) ready on the 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 testfile.tar 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 the private key for authentication on destination using ssh protocol
- source (testfile.tar)
- Destination: Public DNS name of EC2 instance
That’s it! Your file is copied over to EC2. This can be done vice versa as well. You can sync files from the EC2 server to the 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 servers i.e. from one EC2 server to another. The same above command can be used. If you are doing it for EC2 instances within the same region then the Internal DNS name can be used in a command.
Rsync is possible from, to, and between EC2 servers. Key file authenticated SSH protocol should be used in the Rsync command to achieve this.