Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize dates #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 84 additions & 84 deletions makesnapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@
from config import config

if (len(sys.argv) < 2):
print('Please use the parameter day, week or month.')
quit()
print('Please use the parameter day, week or month.')
quit()
else:
if sys.argv[1]=='day':
run = 'day'
date_suffix = datetime.today().strftime('%a')
elif sys.argv[1]=='week':
run = 'week'
date_suffix = datetime.today().strftime('%U')
elif sys.argv[1]=='month':
run = 'month'
date_suffix = datetime.today().strftime('%b')
else:
print('Please use the parameter day, week or month')
quit()
if sys.argv[1] == 'day':
run = 'day'
date_suffix = datetime.today().strftime('%a')
elif sys.argv[1] == 'week':
run = 'week'
date_suffix = datetime.today().strftime('%U')
elif sys.argv[1] == 'month':
run = 'month'
date_suffix = datetime.today().strftime('%b')
else:
print('Please use the parameter day, week or month')
quit()

# Message to return result via SNS
message = ""
Expand All @@ -58,7 +58,7 @@

# Setup the logging
logging.basicConfig(filename=config['log_file'], level=logging.INFO)
start_message = 'Start making ' + run + ' snapshots at ' + datetime.today().strftime('%d-%m-%Y %H:%M:%S')
start_message = 'Start making ' + run + ' snapshots at ' + str(datetime.today())
message += start_message + "\n" + "\n"
logging.info(start_message)

Expand All @@ -67,7 +67,7 @@
aws_secret_key = config['aws_secret_key']
ec2_region_name = config['ec2_region_name']
ec2_region_endpoint = config['ec2_region_endpoint']
arn= config['arn']
arn = config['arn']
proxyHost = config['proxyHost']
proxyPort = config['proxyPort']

Expand All @@ -82,81 +82,81 @@

# Connect to AWS using the credentials provided above or in Environment vars.
if proxyHost == '':
# non proxy:
conn = EC2Connection(aws_access_key,aws_secret_key,region=region)
# non proxy:
conn = EC2Connection(aws_access_key, aws_secret_key, region=region)
else:
# proxy:
conn = EC2Connection(aws_access_key,aws_secret_key,region=region,proxy=proxyHost, proxy_port=proxyPort)
# proxy:
conn = EC2Connection(aws_access_key, aws_secret_key, region=region, proxy=proxyHost, proxy_port=proxyPort)

# Connect to SNS
if proxyHost == '':
# non proxy:
sns = boto.sns.connect_to_region(ec2_region_name,aws_access_key_id=aws_access_key,aws_secret_access_key=aws_secret_key)
# non proxy:
sns = boto.sns.connect_to_region(ec2_region_name, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key)
else:
# proxy:
sns = boto.sns.connect_to_region(ec2_region_name,aws_access_key_id=aws_access_key,aws_secret_access_key=aws_secret_key,proxy=proxyHost, proxy_port=proxyPort)
# proxy:
sns = boto.sns.connect_to_region(ec2_region_name, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key, proxy=proxyHost, proxy_port=proxyPort)


vols = conn.get_all_volumes(filters={config['tag_name']: config['tag_value']})
for vol in vols:
try:
count_total += 1
logging.info(vol)
print vol
description = run + '_snapshot ' + vol.id + '_' + run + '_' + date_suffix + ' by snapshot script at ' + datetime.today().strftime('%d-%m-%Y %H:%M:%S')
if vol.create_snapshot(description):
suc_message = 'Snapshot created with description: ' + description
print ' ' + suc_message
logging.info(suc_message)
total_creates += 1
snapshots = vol.snapshots()
deletelist = []
for snap in snapshots:
sndesc = snap.description
if (sndesc.startswith('week_snapshot') and run == 'week'):
deletelist.append(snap)
elif (sndesc.startswith('day_snapshot') and run == 'day'):
deletelist.append(snap)
elif (sndesc.startswith('month_snapshot') and run == 'month'):
deletelist.append(snap)
else:
print ' Skipping, not added to deletelist: ' + sndesc
for snap in deletelist:
logging.info(snap)
logging.info(snap.start_time)
print ' Snapshots matching vol/run: ' + snap.description

def date_compare(snap1, snap2):
if snap1.start_time < snap2.start_time:
return -1
elif snap1.start_time == snap2.start_time:
return 0
return 1

deletelist.sort(date_compare)
if run=='day':
keep = keep_day
elif run=='week':
keep = keep_week
elif run=='month':
keep = keep_month
delta = len(deletelist) - keep
for i in range(delta):
del_message = ' Deleting snapshot ' + deletelist[i].description
print del_message
logging.info(del_message)
deletelist[i].delete()
total_deletes += 1
time.sleep(3)
except:
print("Unexpected error:", sys.exc_info()[0])
logging.error('Error in processing volume with id: ' + vol.id)
errmsg += 'Error in processing volume with id: ' + vol.id
count_errors +=1
else:
count_succes +=1

result= '\nFinished making snapshots at ' + datetime.today().strftime('%d-%m-%Y %H:%M:%S') + ' with ' + str(count_succes) + ' snapshots of ' + str(count_total) + ' possible.'
try:
count_total += 1
logging.info(vol)
print vol
description = run + '_snapshot ' + vol.id + '_' + run + '_' + date_suffix + ' by snapshot script at ' + str(datetime.today())
if vol.create_snapshot(description):
suc_message = 'Snapshot created with description: ' + description
print ' ' + suc_message
logging.info(suc_message)
total_creates += 1
snapshots = vol.snapshots()
deletelist = []
for snap in snapshots:
sndesc = snap.description
if (sndesc.startswith('week_snapshot') and run == 'week'):
deletelist.append(snap)
elif (sndesc.startswith('day_snapshot') and run == 'day'):
deletelist.append(snap)
elif (sndesc.startswith('month_snapshot') and run == 'month'):
deletelist.append(snap)
else:
print ' Skipping, not added to deletelist: ' + sndesc
for snap in deletelist:
logging.info(snap)
logging.info(snap.start_time)
print ' Snapshots matching vol/run: ' + snap.description

def date_compare(snap1, snap2):
if snap1.start_time < snap2.start_time:
return -1
elif snap1.start_time == snap2.start_time:
return 0
return 1

deletelist.sort(date_compare)
if run == 'day':
keep = keep_day
elif run == 'week':
keep = keep_week
elif run == 'month':
keep = keep_month
delta = len(deletelist) - keep
for i in range(delta):
del_message = ' Deleting snapshot ' + deletelist[i].description
print del_message
logging.info(del_message)
deletelist[i].delete()
total_deletes += 1
time.sleep(3)
except:
print("Unexpected error:", sys.exc_info()[0])
logging.error('Error in processing volume with id: ' + vol.id)
errmsg += 'Error in processing volume with id: ' + vol.id
count_errors += 1
else:
count_succes += 1

result = '\nFinished making snapshots at ' + str(datetime.today()) + ' with ' + str(count_succes) + ' snapshots of ' + str(count_total) + ' possible.'
message += "\n" + "\n" + result
message += "\nTotal snapshots created: " + str(total_creates)
message += "\nTotal snapshots errors: " + str(count_errors)
Expand All @@ -166,6 +166,6 @@ def date_compare(snap1, snap2):

#Reporting
if not errmsg == "":
sns.publish(arn,'Error in processing volumes: ' + errmsg,'Error with AWS Snapshot')
sns.publish(arn,message,'Finished AWS snapshotting')
sns.publish(arn, 'Error in processing volumes: ' + errmsg, 'Error with AWS Snapshot')
sns.publish(arn, message, 'Finished AWS snapshotting')
logging.info(result)