Skip to content

Commit

Permalink
RDS additions to support hibernating a db instance
Browse files Browse the repository at this point in the history
Hibernate in this case means to stop an RDS instance and then restart it
later and have the new instance be identical to the original. Can mean
big savings in aws charges. e.g. stop a db at 7pm in the evening and
then restart it at 7am in the morning - halves your costs.

See Wiki for example usage - RDS Example - stop-db.pl and start-db.pl.

Following actions have been added. Most other RDS actions have been
modified.
modify-db-instance
describe-db-instances
describe-db-snapshots
copy-db-snapshot
delete-db-snapshot
  • Loading branch information
lansalpc committed Jul 29, 2014
1 parent e9f87c2 commit cfe5f33
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"aws" is a command line program that accesses Amazon Web Services: EC2, S3, SQS, SDB, ELB, IAM, EBN
"aws" is a command line program that accesses Amazon Web Services: EC2, S3, SQS, SDB, ELB, IAM, EBN, RDS

To run "aws", all you need is a single file!

Expand Down
118 changes: 116 additions & 2 deletions aws
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,30 @@ $rds_version = "2013-09-09";
# Parameter names are the same as RDS CLI documentation, with some additions.

### Database Instances ###

# http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_ModifyDBInstance.html
# http://docs.aws.amazon.com/AmazonRDS/latest/CommandLineReference/CLIReference-cmd-ModifyDBInstance.html
["rds", "modify-db-instance mdb", ModifyDBInstance, [
["", DBInstanceIdentifier],
["allocated-storage s", AllocatedStorage],
["allow-major-version-upgrade", AllowMajorVersionUpgrade],
["apply-immediately", ApplyImmediately],
["auto-minor-version-upgrade au", AutoMinorVersionUpgrade],
["backup-retention-period r", BackupRetentionPeriod],
["db-instance-class c", DBInstanceClass],
["db-parameter-group-name g", DBParameterGroupName],
["db-security-groups a", "DBSecurityGroups.memberN"],
["engine-version v", EngineVersion],
["iops", Iops],
["master-user-password p", MasterUserPassword],
["multi-az m", MultiAZ],
["new-db-instance-identifier n", NewDBInstanceIdentifier],
["option-group og", OptionGroupName],
["preferred-backup-window b", PreferredBackupWindow],
["preferred-maintenance-window w", PreferredMaintenanceWindow],
["vpc-security-group-ids sg", "VpcSecurityGroupIds.memberN"]
]],

# http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_DeleteDBInstance.html
# http://docs.aws.amazon.com/AmazonRDS/latest/CommandLineReference/CLIReference-cmd-DeleteDBInstance.html
["rds", "delete-db-instance deldb", DeleteDBInstance, [
Expand All @@ -357,8 +381,17 @@ $rds_version = "2013-09-09";
["", ResourceName],
["tag", undef, undef, \&parse_tags_member]
]],

# http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_DescribeDBInstances.html
# http://docs.aws.amazon.com/AmazonRDS/latest/CommandLineReference/CLIReference-cmd-DescribeDBInstances.html
["rds", "describe-db-instances ddb", DescribeDBInstances, [
["", DBInstanceIdentifier],
["marker", Marker],
["max-records m", MaxRecords]
]],

### Database Snapshots and Point-In-Time Recovery ###

# http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBSnapshot.html
# http://docs.aws.amazon.com/AmazonRDS/latest/CommandLineReference/CLIReference-cmd-CreateDBSnapshot.html
["rds", "create-db-snapshot cdbsnap", CreateDBSnapshot, [
Expand All @@ -367,6 +400,16 @@ $rds_version = "2013-09-09";
["tag", undef, undef, \&parse_tags_member]
]],

# http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_DescribeDBSnapshots.html
# http://docs.aws.amazon.com/AmazonRDS/latest/CommandLineReference/CLIReference-cmd-DescribeDBSnapshots.html
["rds", "describe-db-snapshots ddbsnap", DescribeDBSnapshots, [
["db-instance-identifier i", DBInstanceIdentifier],
["db-snapshot-identifier s snapshot", DBSnapshotIdentifier],
["marker", Marker],
["max-records m", MaxRecords],
["snapshot-type t", SnapshotType]
]],

# http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CopyDBSnapshot.html
# http://docs.aws.amazon.com/AmazonRDS/latest/CommandLineReference/CLIReference-cmd-CopyDBSnapshot.html
["rds", "copy-db-snapshot cpdbsnap", CopyDBSnapshot, [
Expand All @@ -377,7 +420,7 @@ $rds_version = "2013-09-09";

# http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBInstanceFromDBSnapshot.html
# http://docs.aws.amazon.com/AmazonRDS/latest/CommandLineReference/CLIReference-cmd-RestoreDBInstanceFromDBSnapshot.html
["rds", "restore-db-instance-from-db-snapshot restoredb", RestoreDBInstanceFromDBSnapshot, [
["rds", "restore-db-instance-from-db-snapshot rdb", RestoreDBInstanceFromDBSnapshot, [
["", DBInstanceIdentifier],
["db-snapshot-identifier s snapshot", DBSnapshotIdentifier],
["auto-minor-version-upgrade au", AutoMinorVersionUpgrade],
Expand Down Expand Up @@ -2254,6 +2297,72 @@ if (!$cmd_data)
$result = qx[$0 --cmd0 --xml --region=$region describe-instances @instanceId];
}
}
elsif ($result =~ /<CreateDBSnapshotResponse|<DeleteDBSnapshotResponse|<CopyDBSnapshotResponse/ && ($simple || $wait))
{
#<CreateDBSnapshotResponse xmlns="http://rds.amazonaws.com/doc/2013-09-09/">
# <CreateDBSnapshotResult>
# <DBSnapshot>
# <Port>3306</Port>
# <OptionGroupName>default:mysql-5-1</OptionGroupName>
# <Engine>mysql</Engine>
# <Status>creating</Status>
# <SnapshotType>manual</SnapshotType>
# <LicenseModel>general-public-license</LicenseModel>
# <DBInstanceIdentifier>dbinstance</DBInstanceIdentifier>
# <EngineVersion>5.1.69</EngineVersion>
# <DBSnapshotIdentifier>asnapshot</DBSnapshotIdentifier>
# <VpcId>vpc-30zzzzz</VpcId>
# <AvailabilityZone>ap-southeast-2b</AvailabilityZone>
# <InstanceCreateTime>2014-07-16T05:27:55.909Z</InstanceCreateTime>
# <PercentProgress>0</PercentProgress>
# <AllocatedStorage>5</AllocatedStorage>
# <MasterUsername>user</MasterUsername>
# </DBSnapshot>
# </CreateDBSnapshotResult>
# <ResponseMetadata>
# <RequestId>2f1b87fa-12ed-11e4-be67-5b99b2d10c58</RequestId>
# </ResponseMetadata>
#</CreateDBSnapshotResponse>

for (;;)
{
my($instanceId) = $result =~ /<DBInstanceIdentifier>(.*?)<\/DBInstanceIdentifier>/s;
my($snapshotId) = $result =~ /<DBSnapshotIdentifier>(.*?)<\/DBSnapshotIdentifier>/s;
my($progress) = $result =~ /<PercentProgress>(.*?)<\/PercentProgress>/s;
my($status) = $result =~ /<Status>(.*?)<\/Status>/s;

my($pending) += $status eq "creating" || $status eq "deleting";

print "$instanceId\t$snapshotId\t$status\t$progress\n";

last unless $wait && $pending;

sleep $wait;
$result = qx[$0 --cmd0 --xml --region=$region describe-db-snapshots -s $snapshotId];
}
}
elsif ($result =~ /<RestoreDBInstanceFromDBSnapshotResponse|<DeleteDBInstanceResponse|<ModifyDBInstanceResponse/ && ($simple || $wait))
{
# The current status of the instance.
# Valid values: available | backing-up | creating | deleted | deleting | failed | incompatible-restore |
# incompatible-parameters | modifying | rebooting | resetting-master-credentials | storage-full
for (;;)
{
my($instanceId) = $result =~ /<DBInstanceIdentifier>(.*?)<\/DBInstanceIdentifier>/s;
my($zone) = $result =~ /<AvailabilityZone>(.*?)<\/AvailabilityZone>/s; # Zone is not available for RestoreDBInstanceFromDBSnapshotResponse
my($engine) = $result =~ /<Engine>(.*?)<\/Engine>/s;
my($status) = $result =~ /<DBInstanceStatus>(.*?)<\/DBInstanceStatus>/s;

my($pending) += $status ne "available";

print "$instanceId\t$engine\t$status\t$zone\n";

last unless $wait && $pending;

sleep $wait;
$result = qx[$0 --cmd0 --xml --region=$region describe-db-instances $instanceId];
}
}
elsif ($result =~ /<ListQueuesResult/)
{
if ($simple)
Expand Down Expand Up @@ -2404,6 +2513,12 @@ if (!$cmd_data)
}
else
{
# If there is an error response and we haven't yet set an error return, do it now
if ( $result =~ /<ErrorResponse/ && !$ExitCode)
{
$ExitCode = 1;
}

print xml2tab($result) || xmlpp($result);
}

Expand Down Expand Up @@ -3548,7 +3663,6 @@ sub parse_tags_member
["Tags.member.N.Key" => $key, "Tags.member.N.Value" => $value];
}


sub cq
{
# quote for sending to curl via shell
Expand Down

0 comments on commit cfe5f33

Please sign in to comment.