-
Notifications
You must be signed in to change notification settings - Fork 127
RDS Example stop db.pl
Rob Goodridge edited this page Jul 29, 2014
·
4 revisions
Requires aws with RDS support, of course. :)
Until you are confident this does the job, make sure you have a backup of your database!
This is an example perl script to stop an RDS database instance so that it may be restarted later with the companion script start-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.
#!/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);
}
my($wait) = 3;
my($instanceId) = shift;
my($wait_arg) = shift;
my($snapshotId) = $instanceId . "-stop";
if ($wait_arg)
{
$wait = $wait_arg;
}
my($result);
# Ensure the database exists
$result = `aws.pl ddb $instanceId --max-records 20`;
$result =~ /<DBInstanceIdentifier>$instanceId<\/DBInstanceIdentifier>/s or die "DB Instance $instanceId does not exist. Operation aborted.\n";
# Delete existing snapshot
system "perl aws.pl deldbsnap $snapshotId --wait=1";
# Create a new snapshot
system "perl aws.pl cdbsnap $instanceId -s $snapshotId --wait=$wait";
# Check snapshot is available
$result = `aws.pl ddbsnap -s $snapshotId --max-records 20`;
# make sure that there is a reference to the snapshot. (If the snapshot does not exist, all snapshots may be returned)
if ( !($result =~ /<DBSnapshotIdentifier>$snapshotId<\/DBSnapshotIdentifier>/s) || !($result =~ /<Status>available<\/Status>/s) )
{
die "$result\nSnapshot $snapshotId is not available. Operation aborted.\n";
}
# Delete the database
system "perl aws.pl deldb $instanceId --skip-final-snapshot true --wait=$wait";
# Check the database has been deleted
$result = `aws.pl ddb $instanceId --max-records 20`;
# Either the instance should not exist or, if it does, its status is deleted
if (!($result =~ /<DBInstanceIdentifier>$instanceId<\/DBInstanceIdentifier>/s) || $result =~ /<Status>deleted<\/Status>/s)
{
print "DB Instance $instanceId snapshotted to $snapshotId and then deleted";
}
else
{
die "$result\nDB Instance $instanceId has not been properly deleted. Check results.";
}
sub Usage
{
print<<USAGE;
$0
Snapshot a database instance to <DB Instance Id>-stop, then delete the database instance.
Checks that snapshot is properly created before deleting the instance and then checks that the instance is properly deleted.
Waiting for the snapshot to be created may take more than 2 minutes. Whilst this is occurring, progress messages are displayed.
Similarly when deleting the database.
Param 1 - Specify DB Instance Id to stop (mandatory)
Param 2 - wait time in seconds defaults to 3. Script MUST wait so a wait time of 0 is ignored
USAGE
}