How to Copy Files to Amazon EC2 Ubuntu Instance Using Rsync

Transferring files to and from your Amazon EC2 instance can be made simple using Rsync, a powerful file synchronization tool. Here’s a streamlined guide to get you started.

How to Efficiently Copy Files to Amazon EC2 Ubuntu Instance Using Rsync
Illustration Diagram

Step-by-Step Guide

Step 1: Install Rsync on Ubuntu (if not installed)

Before using Rsync, ensure it’s installed on your local Ubuntu machine. You can install it by running:

sudo apt update
sudo apt install rsync

Step 2: Copy Local Files to Remote EC2 Instance

To transfer files from your local computer to the remote EC2 instance:

rsync -e "ssh -i /path/to/cert.pem" -av /path/to/local/files ubuntu@EC2_IP_ADDRESS:/path/to/remote/directory

Replace /path/to/cert.pem with the path to your EC2 key pair file, /path/to/local/files with the source directory, and EC2_IP_ADDRESS with your EC2 instance’s IP address.

Step 3: Copy Remote Files to Local

If you want to copy files from the EC2 instance to your local system:

rsync -e "ssh -i /path/to/cert.pem" -av ubuntu@EC2_IP_ADDRESS:/path/to/remote/files /path/to/local/directory

Step 4: Exclude Specific Files During Copy

To exclude specific files when transferring:

Local:

rsync -av --exclude 'file1.txt' --exclude 'file2.txt' /source/directory /destination/directory

Remote (Amazon EC2):

rsync -e "ssh -i /path/to/cert.pem" -av --exclude 'file1.txt' --exclude 'file2.txt' /source/files ubuntu@EC2_IP_ADDRESS:/destination/files

Step 5: Exclude Directories During Copy

To exclude entire directories:

Local:

rsync -av --exclude 'dir1' --exclude 'dir2' /source/directory /destination/directory

Remote (Amazon EC2):

rsync -e "ssh -i /path/to/cert.pem" -av --exclude 'dir1' --exclude 'dir2' /source/directory ubuntu@EC2_IP_ADDRESS:/destination/directory

Step 6: Copy Using Specific File Type

Transferring files of specific types:

Local:

rsync -av --include '*.php' --include '*.html' --exclude '*' /source/directory /destination/directory

Remote (Amazon EC2):

rsync -e "ssh -i /path/to/cert.pem" -av --include '*.php' --include '*.html' --exclude '*' /source/directory ubuntu@EC2_IP_ADDRESS:/destination/directory

Step 7: Exclude Multiple Files and Folders

Create an exclusion list:

touch exclude-list.txt

Edit the list:

nano exclude-list.txt

Add the files and directories to exclude:

file1.txt
file2.txt
dir1/
dir2/.git
dir2/.env
config.php
dir3/

Utilize the exclusion list during transfer:

Local:

rsync -av --exclude-from 'exclude-list.txt' /source/directory /destination/directory

Remote (Amazon EC2):

rsync -e "ssh -i /path/to/cert.pem" -av --exclude-from 'exclude-list.txt' /source/directory ubuntu@EC2_IP_ADDRESS:/destination/directory

Final Thoughts

Ensure you have the right permissions for your key file (cert.pem) and that your EC2 security group allows inbound port (22) SSH connections. Replace placeholders with your actual file paths and EC2 details before running the commands.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.