Mitchell Harrington
asked on
Amazon EC2 Backup to S3 Bucket using Python Code
I have over 10 Amazon Ec2 Instances running and I want to automate their backups to a Amazon S3 Bucket. I have been told that I can do this using a Amazon Linux AMI with python code but I am unsure how to accomplish this.
Could someone please provide me a simple solution on how to accomplish this, specifically in python code.
Thank you for your time.
Could someone please provide me a simple solution on how to accomplish this, specifically in python code.
Thank you for your time.
ASKER
Hi shalomc and thank you for getting back to me.
Just wanted to clarify, does this script run on the AWS Linux AMI and effect all Instances or does it need to run separately on each instance that needs backing up? If that is the case I don't believe I can use this because most of the instances I use are windows based.
Just wanted to clarify, does this script run on the AWS Linux AMI and effect all Instances or does it need to run separately on each instance that needs backing up? If that is the case I don't believe I can use this because most of the instances I use are windows based.
This is a link to a python script that creates snapshots of volumes based on tags.
https://medium.com/chrisjerry/snapshot-creation-and-delete-snap-older-x-days-python-script-63e224a699ff
https://medium.com/chrisjerry/snapshot-creation-and-delete-snap-older-x-days-python-script-63e224a699ff
ASKER
When I run the snapshot creation script it is throwing this error:
it appears to be pointing at this line, I've looked at the code and tried to fix it but it does not appear to be making any difference
File "backup.py", line 4
SyntaxError: Non-ASCII character '\xe2' in file backup.py on line 4, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
it appears to be pointing at this line, I've looked at the code and tried to fix it but it does not appear to be making any difference
ec = boto3.client(‘ec2’,’ap-southeast-2') #mention your region to reboot
Try to add this line at the top of your code
# -*- coding: utf-8 -*-
ASKER
Invalid Syntax Error
I checked the original to see if anything was wrong but it all seems to check out
File "backup.py", line 5
ec = boto3.client(‘ec2’,’ap-southeast-2') #mention your region to reboot
I checked the original to see if anything was wrong but it all seems to check out
Looks like the code you posted has this character as single quotes: ‘
‘ec2’
Try to replace all single quotes with double quotes, lets see if it helps
ASKER
Okay,
I made the changes but then I realized that most of the script was like that so I started making changes which appeared to be working but then I got to this.
Currently the code is:
I made the changes but then I realized that most of the script was like that so I started making changes which appeared to be working but then I got to this.
File "backup.py", line 12
print "Backing up %s in %s %" (volume["VolumeId"], volume["AvailabilityZone"])
^
IndentationError: expected an indented block
Currently the code is:
# -*- coding: utf-8 -*-
import boto3
import collections
import datetime
ec = boto3.client("ec2","ap-southeast-2") #mention your region to reboot
to_tag = 0
def lambda_handler(event, context):
print 'hi'
reservations = ec.describe_volumes( Filters=[ {"Name": "tag-key", "Values": ["backup", "True"]}, ] )
print(reservations)
for volume in reservations["Volumes"]:
print "Backing up %s in %s %" (volume["VolumeId"], volume["AvailabilityZone"])
# Create snapshot
reservations = ec.create_snapshot(VolumeId=volume["VolumeId"],Description="Lambda backup for ebs" + volume["VolumeId"])
result = reservations["SnapshotId"]
print(result)
ec.create_tags(
Resources=[result],Tags=[
{‘Key’: ‘Name’, ‘Value’: ‘snapshot’ },
]
)
Well, you gotta know python to mess with python :)
Line 11 is a for loop, so you have to indent the loop block.
Line 11 is a for loop, so you have to indent the loop block.
# -*- coding: utf-8 -*-
import boto3
import collections
import datetime
ec = boto3.client("ec2","ap-southeast-2") #mention your region to reboot
to_tag = 0
def lambda_handler(event, context):
print 'hi'
reservations = ec.describe_volumes( Filters=[ {"Name": "tag-key", "Values": ["backup", "True"]}, ] )
print(reservations)
for volume in reservations["Volumes"]:
print "Backing up %s in %s %" (volume["VolumeId"], volume["AvailabilityZone"])
# Create snapshot
reservations = ec.create_snapshot(VolumeId=volume["VolumeId"],Description="Lambda backup for ebs" + volume["VolumeId"])
result = reservations["SnapshotId"]
print(result)
ec.create_tags(
Resources=[result],Tags=[
{‘Key’: ‘Name’, ‘Value’: ‘snapshot’ },
]
)
ASKER
Hi,
It may just be me but I setup the code described above and setup a instance on aws with the appropriate tags, ran the script but nothing happened and there was no errors.
Not sure what is happening, I setup a access key with the appropriate permissions, I have two instances with the tag Backup and value true.
Any thoughts?
It may just be me but I setup the code described above and setup a instance on aws with the appropriate tags, ran the script but nothing happened and there was no errors.
Not sure what is happening, I setup a access key with the appropriate permissions, I have two instances with the tag Backup and value true.
Any thoughts?
Tags are case sensitive
ASKER
I have confirmed the tags are correct. Same effect. Could it be in the User Permissions
- AmazonSSMFullAccess
- AmazonEC2FullAccess
Log the responses you receive from AWS.
See here how http://boto3.readthedocs.io/en/latest/reference/core/boto3.html
See here how http://boto3.readthedocs.io/en/latest/reference/core/boto3.html
ASKER
According the to the Security Dashboard the key has not been used yet. But I already have run "awsconfigure" on the instance.
try to run this from the command line. Does it work? Do you see your volume?
aws ec2 describe-volumes --region ap-southeast-2
ASKER
I have run the command and I do see a full list of volumes
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
The script gets the server's meta data and uses it to create a snapshot.
It still uses the old EC2 cli instead of the generic aws cli, and still works.
When using python, the concept will be identical:
* Get instance meta-data
* Create snapshots for the instance volumes
* Tag the snapshots correctly
Open in new window