Skip to content

RDS Example start db.pl

Rob Goodridge edited this page Jul 29, 2014 · 2 revisions

Requires aws with RDS support, of course. :)

This is an example perl script to start an RDS database instance which was stopped earlier by the companion script stop-db.pl.

Its only been used on Windows hence the use of the 'pl' extension. It should work on Linux by just removing the '.pl' in the script.

Even though its perl, the code is commented. ;) It should be clear how it works.

I've not relied on return codes to know if an action has succeeded. All important commands are followed by a describe to ensure the database or snapshot instance is in the correct state.

The database instance will be exactly the same as it was when it was stopped, in the same AZ, though with a specified security group and the backup retention set to 1. The latter may be removed for mysql 5.6. Its required for mysql 5.1 otherwise the backup retention is set to 0.

This script has an embedded security group in it which must be changed or an appropriate security group may be specified on the command line.

    #!/usr/bin/perl -w
    
    use strict;
    use Getopt::Std;
    use File::Path;
    use Tie::File;
    use Fcntl;
    
    # -----------------------------------------------------------------------------
    # Simple subs to make it clear when we're testing for BOOL values
    # -----------------------------------------------------------------------------
    
    sub TRUE   {return(1);} # BOOLEAN TRUE
    sub FALSE  {return(0);} # BOOLEAN FALSE
    
    # -----------------------------------------------------------------------------
    # Use system rather than backticks so that STDOUT is continually received
    # by the caller providing progress messages
    # -----------------------------------------------------------------------------
    
    if (@ARGV < 1 || @ARGV > 2)
    {
        Usage();
        exit(-1);
    }
    
    # Change me!
    my($sg) = "sg-1e749271";
    my($wait) = 3;
    my($instanceId) = shift;
    my($sg_arg) = shift;
    my($snapshotId) = $instanceId . "-stop";
    
    if ($sg_arg)
    {
        $sg = $sg_arg;
    }
    
    my($result);
    
    # Ensure the database does not already exist
    $result = `aws.pl ddb $instanceId --max-records 20`;
    
    !($result =~ /<DBInstanceIdentifier>$instanceId<\/DBInstanceIdentifier>/s) or die "DB Instance $instanceId already exists. Operation aborted.\n";
    
    # Obtain the AZ from the Snapshot
    $result = `aws.pl ddbsnap -s $snapshotId --max-records 20`;
    
    if ( !($result =~ /<DBSnapshotIdentifier>$snapshotId<\/DBSnapshotIdentifier>/s) || !($result =~ /<AvailabilityZone>(.*?)<\/AvailabilityZone>/s) )
    {
        die "$result\nSnapshot $snapshotId is not available. Operation aborted.\n";
    }
    my($az) = $1;
    
    # Create a new database from snapshot
    system "perl aws.pl rdb $instanceId -s $snapshotId -z $az --wait=$wait";
    
    # Check the database has been created
    $result = `aws.pl ddb $instanceId --max-records 20`;
    
    $result =~ /<DBInstanceIdentifier>$instanceId<\/DBInstanceIdentifier>/s or die "$result\nDB Instance $instanceId has not been created. Check results.\n";
    
    # DB Instance must be 'available' before it may be modified, so the rdb above MUST wait
    print "Modifying DB Instance $instanceId\n";
    system "perl aws.pl mdb $instanceId -r 1 -sg $sg --apply-immediately true --wait=$wait";
    
    # Either the instance should not exist or, if it does, its status is deleted
    if ($result =~ /<DBInstanceStatus>available<\/DBInstanceStatus>/s)
    {
        print "DB Instance $instanceId has been created and is available";
    }
    else
    {
        die "$result\nDB Instance $instanceId has not been properly created. Its not 'available'. Check results.\n";
    }
    
    sub Usage
    {
    print<<USAGE;
    $0
    Start a database from a database snapshot called <DB Instance Id>-stop.
    Database is created in the AZ where the database was originally running with a backup retention period of 1.
    The DB instance needs to be modified which may only occur when DB Instance is 'available', so the restoring
    of the database waits until its available.
    Param 1 - Specify DB Instance Id to stop (mandatory)
    Param 2 - vpc security group to assign
    USAGE
    }