DEV Community

Cover image for EC2 data backup in S3

EC2 data backup in S3

1st Step - Create S3 Bucket

In the AWS management console search S3 and click Create bucket button.
Image description

Next, Type the bucket name as ec2-bucket-demo (You can any name globally unique) and keep other settings as default.
Image description

Finally, Hit create bucket button.
Image description

2nd Step - Create IAM Role for EC2 instance

In the AWS management console search IAM and go to the Roles tab. After that Hit create role button.
Image description

Next, select the use case as EC2 and Hit the next button.
Image description

Next, the Permission section selects the type as AWS Managed and selects "AmazonS3FullAccess". After that hit the Next button.
Image description

Finally, give the name for the IAM role and click create button. After that, you can see your newly created role.
Image description

3rd Step - Create EC2 Instance and attached the IAM role for EC2 Instance

Select image as Ubuntu 20.04.
Image description

You can see your newly created EC2 instance.
Image description

Go to an EC2 instance dashboard and click EC2 instance go to actions and Security finally click Modify IAM Role.
Image description

Next, Modify the IAM role and click the previously created IAM Role and click Update the IAM role.
Image description

4th Step - Connect EC2 Instance and Install AWS CLI

Image description

5th Step - Check S3 buckets using AWS CLI and create new file

After that copy created file using AWS CLI to S3.
Use the following command.
aws s3 cp my.txt s3://{{Your-Bucket-Name}}
Image description

6th Step - Using crontab to add the S3 copy command

You can add a crontab to automate backups. You can use the below command to open crontab. You can use any terminal editor. Ex - Vim, Nano
Image description

7th Step - Check S3 Backup files

Finally, You can see your S3 bucket backup file updated.

Image description

Thanks for reading the Article.

References - Check crobjob

Top comments (0)