Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jbontech/d5bedb3905e8557b635f75639b624f59 to your computer and use it in GitHub Desktop.
Save jbontech/d5bedb3905e8557b635f75639b624f59 to your computer and use it in GitHub Desktop.
A script that finds the most recent snapshot of an existing rds instance and creates a new db instance based on that
from time import time, localtime, strftime, sleep, gmtime
from AWSsetup import AWS_user_secret_access_key,AWS_user_access_key_id
import boto.rds
def get_rds_instance_status(RDSconn, rds_instance_name):
# Grab the RDS instance status
instances = RDSconn.get_all_dbinstances(
instance_id = rds_instance_name,
)
# print instances #debug
instance = instances[0]
status = instance.status
# print status #debug
return status
def reboot_rds_instance(RDSconn, rds_instance_name, why="No reason"):
try:
RDSconn.reboot_dbinstance(
id = rds_instance_name
)
print "Rebooting -{0}- . Reason: {1}".format(rds_instance_name, why)
except Exception as ex:
print ex
# ========== CONSTANTS =========== #
region = "us-west-2"
db_identifier = "db0"
rds_instance_name = "db1"
PrivateSG = "sg-fb33339d" # the sec group to apply
ParamGroup = "dbparameters" # the param group to apply
db_subnet_group = "vpc-db-subnet-group"
today = strftime("%Y-%m-%d", localtime())
hour_the_snapshot_is_taken = "05"
# ========== CONSTANTS =========== #
# Format the string to search for in the snapshot results
search_snap_string = "DBSnapshot:rds:{0}-{1}-{2}".format(db_identifier,today,hour_the_snapshot_is_taken)
search_snap_string = search_snap_string.split("DBSnapshot:")[1]
print "search_snap_string --> ",search_snap_string #debug
# connect to RDS endpoint
RDSconn = boto.rds.connect_to_region(
region,
aws_access_key_id = AWS_user_access_key_id,
aws_secret_access_key = AWS_user_secret_access_key
)
# Grab all RDS snapshots
all_snaps = RDSconn.get_all_dbsnapshots()
print "all_snaps --> ",all_snaps #debug
# Find the latest RDS snapshot based on the matched string [list]
snapshots = [snap.id for snap in all_snaps if search_snap_string in snap.id]
# print snapshots #debugging
# Take the one element from the list
todays_snapshot = snapshots[0]
print "todays_snapshot --> ",todays_snapshot #debug
# ########### -- R E S T O R E -- ########################################################################################
# =========== Restore the Snapshot =========== #
# Notice: There must not exist another DB instance with the same name!
RDSconn.restore_dbinstance_from_dbsnapshot(
identifier = todays_snapshot,
instance_id = rds_instance_name,
instance_class = "db.r3.4xlarge",
multi_az = False,
auto_minor_version_upgrade = True,
db_subnet_group_name = db_subnet_group
)
# =========== Restore the Snapshot =========== #
print("--- %s creation started ---" % rds_instance_name)
rds_restore_start_time = time() # notice: start timing the restoration
########### -- M O D I F Y -- ##########################################################################################
infinite = True
while infinite is True:
# Check status
current_rds_status = get_rds_instance_status(RDSconn, rds_instance_name)
sleep(60) # check every 1 min
while current_rds_status in ("creating" , "modifying", "backing-up"):
print "### DB instance is -creating/modifying/backing-up-. Wait for it! ###"
sleep(60)
break
else:
if current_rds_status == "available":
rds_full_creation_time = time() - rds_restore_start_time
print strftime("Time to restore the RDS instance: %H:%M:%S", gmtime(rds_full_creation_time)) # notice: end of timing the restoration
print "\n--- DB is created! Continue... ---"
# =========== Modify the RDS instance ======== #
RDSconn.modify_dbinstance(
id = rds_instance_name,
param_group = ParamGroup,
vpc_security_groups = [
PrivateSG
],
apply_immediately = True
)
# =========== Modify the RDS instance ======== #
print("--- %s modification started ---" % rds_instance_name)
break # notice: get out of the infinite loop
########### -- R E B O O T -- ##########################################################################################
sleep(10)
infinite = True
while infinite is True:
# Check status
current_rds_status = get_rds_instance_status(RDSconn, rds_instance_name)
sleep(5)
# print current_rds_status # debug
while current_rds_status == "modifying":
print "### DB is -modifying-. Wait for it! ###"
sleep(5)
break
else:
if current_rds_status == "available":
sleep(30) # Give it time to apply the changes
print "\n--- DB is modified! Continue... ---"
reboot_rds_instance(RDSconn, rds_instance_name, why="sec/param group modifications")
break # notice: get out of the infinite loop
########### -- C H E C K -- ############################################################################################
infinite = True
while infinite is True:
# Check status
current_rds_status = get_rds_instance_status(RDSconn, rds_instance_name)
sleep(10)
while current_rds_status == "rebooting":
print "### DB is -rebooting-. Wait for it! ###"
sleep(10)
break
else:
if current_rds_status == "available":
print "\n--- DB is available! Have a nice day! ---"
print("\n--- REMINDER: Check the sec/parameter groups to be correct [may need to reboot]---")
break # notice: get out of the infinite loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment