EC2 data backup in S3
Tags: aws, ec2, s3, devops
Published: 2022-06-18 09:40:42

## 1st Step - Create S3 Bucket

In the AWS management console search S3 and click Create bucket button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0mmegy44smk4norm5d1.jpg)

Next, Type the bucket name as ec2-bucket-demo (You can any name globally unique) and keep other settings as default.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mw0gvnx0h0ad0z2p1i90.jpg)

Finally, Hit create bucket button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g1xvw0o73jujkx5374zw.jpg)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6b098rj4rck3majwxu2o.jpg)

Next, select the use case as EC2 and Hit the next button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sa0e7muofj17hpl1e774.jpg)

Next, the Permission section selects the type as AWS Managed and selects "AmazonS3FullAccess". After that hit the Next button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i24c66yhaogqhzokjihu.jpg)

Finally, give the name for the IAM role and click create button. After that, you can see your newly created role.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1rzzcetg9wguvs613bmz.jpg)

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

Select image as Ubuntu 20.04.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iuxatxawz8ychxiaioqv.jpg)

You can see your newly created EC2 instance.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gsllsfhcqk2q1m01bm9l.jpg)

Go to an EC2 instance dashboard and click EC2 instance go to actions and Security finally click Modify IAM Role.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxwlmy3k99uxwmruoo5z.jpg)

Next, Modify the IAM role and click the previously created IAM Role and click Update the IAM role.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/827b4ld5i37lx7zb39w5.jpg) 

## 4th Step - Connect EC2 Instance and Install AWS CLI

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5xv0g7i5aer3fahump93.jpg)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gi51vkukpn8ccbrl9qwm.jpg)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mr2b6oimqrezme0rzuu.jpg)

## 7th Step - Check S3 Backup files

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3zjcriag983bee5uxdh.jpg)

**Thanks for reading the Article.**

References - [Check crobjob](https://crontab.guru/)