Skip to content

Instantly share code, notes, and snippets.

@NotHarshhaa
Created July 12, 2024 06:57
Show Gist options
  • Save NotHarshhaa/ad821d3298bfef8eb32630557f1a6f3d to your computer and use it in GitHub Desktop.
Save NotHarshhaa/ad821d3298bfef8eb32630557f1a6f3d to your computer and use it in GitHub Desktop.

How to Take Backup of AWS EBS Volume or Snapshot to S3 Bucket or Local Machine

To take a backup of an AWS EBS volume, you typically create a snapshot of the volume, which can then be copied to an S3 bucket or downloaded to a local machine. Here's how you can do it:

1. Create a Snapshot of the EBS Volume

Using the AWS Management Console:

  1. Open the Amazon EC2 console.
  2. In the navigation pane, choose Volumes.
  3. Select the volume you want to back up.
  4. Choose Actions, then Create Snapshot.
  5. Enter a description for the snapshot and choose Create Snapshot.

Using the AWS CLI:

aws ec2 create-snapshot --volume-id vol-0123456789abcdef0 --description "Backup snapshot"

2. Copy the Snapshot to an S3 Bucket

AWS does not directly support copying EBS snapshots to S3, but you can use AWS Data Lifecycle Manager (DLM) to automate this. Alternatively, you can create an AMI from the snapshot, copy the AMI to S3 using VM Import/Export, and then extract the data.

3. Download the Snapshot to a Local Machine

To download the snapshot to a local machine, you'll need to perform several steps:

  1. Create an AMI from the Snapshot:

    aws ec2 create-image --instance-id i-0123456789abcdef0 --name "My server" --no-reboot
  2. Export the AMI to S3:

    aws ec2 create-instance-export-task --instance-id i-0123456789abcdef0 --target-environment vmware --export-to-s3-task DiskImageFormat=VMDK,ContainerFormat=ova,S3BucketName=my-s3-bucket
  3. Download the Exported Image from S3: Use the AWS Management Console, AWS CLI, or any S3 client to download the exported image from the S3 bucket to your local machine.

Using the AWS CLI:

To download a file from an S3 bucket:

aws s3 cp s3://my-s3-bucket/my-exported-image.ova ./my-exported-image.ova

Example Scripts

AWS CLI Script to Create Snapshot and Copy to S3

# Variables
VOLUME_ID="vol-0123456789abcdef0"
DESCRIPTION="Backup snapshot"
BUCKET_NAME="my-s3-bucket"

# Step 1: Create Snapshot
SNAPSHOT_ID=$(aws ec2 create-snapshot --volume-id $VOLUME_ID --description "$DESCRIPTION" --query SnapshotId --output text)

echo "Snapshot created with ID: $SNAPSHOT_ID"

# Step 2: Create Image from Snapshot
IMAGE_ID=$(aws ec2 create-image --instance-id i-0123456789abcdef0 --name "Backup Image" --no-reboot --query ImageId --output text)

echo "Image created with ID: $IMAGE_ID"

# Step 3: Export Image to S3
EXPORT_TASK_ID=$(aws ec2 create-instance-export-task --instance-id i-0123456789abcdef0 --target-environment vmware --export-to-s3-task DiskImageFormat=VMDK,ContainerFormat=ova,S3BucketName=$BUCKET_NAME --query ExportTaskId --output text)

echo "Export task created with ID: $EXPORT_TASK_ID"

# Step 4: Check the status of the export task
aws ec2 describe-export-tasks --export-task-ids $EXPORT_TASK_ID

AWS CLI Script to Download from S3 to Local Machine

# Variables
S3_BUCKET="my-s3-bucket"
S3_KEY="my-exported-image.ova"
LOCAL_PATH="./my-exported-image.ova"

# Step 1: Copy from S3 to Local Machine
aws s3 cp s3://$S3_BUCKET/$S3_KEY $LOCAL_PATH

echo "Downloaded $S3_KEY to $LOCAL_PATH"

This process covers creating a snapshot, exporting it to S3, and downloading it locally. Be sure to adjust variables and steps according to your specific requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment