CreateClusterSnapshot.go - AWS Code Sample

CreateClusterSnapshot.go

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 package main import ( "flag" "fmt" "strings" "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/rds" "github.com/aws/aws-sdk-go/service/rds/rdsiface" ) // MakeClusterSnapshot creates a snapshot for an Amazon RDS cluster // Inputs: // sess is the current session, which provides configuration for the SDK's service clients // clusterID is the ID of the cluster // Output: // If success, nil // Otherwise, an error from the call to CreateDBClusterSnapshot func MakeClusterSnapshot(svc rdsiface.RDSAPI, clusterID *string) error { // Get the current date and time to uniquely identify snapshot currentTime := time.Now() t := currentTime.Format("2006-01-02 15:04:05") // Replace space with underscore t = strings.Replace(t, " ", "_", -1) _, err := svc.CreateDBClusterSnapshot(&rds.CreateDBClusterSnapshotInput{ DBClusterIdentifier: clusterID, DBClusterSnapshotIdentifier: aws.String(*clusterID + t), }) return err } func main() { clusterID := flag.String("c", "", "The cluster ID") flag.Parse() if *clusterID == "" { fmt.Println("You must supply a cluster ID (-c CLUSTER-ID)") return } sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := rds.New(sess) err := MakeClusterSnapshot(svc, clusterID) if err != nil { fmt.Println("Got an error creating snapshot for cluster " + *clusterID) return } // Wait until snapshot is created before finishing fmt.Println("Waiting for snapshot in cluster " + *clusterID + " to be created") err = svc.WaitUntilDBSnapshotAvailable(&rds.DescribeDBSnapshotsInput{ DBInstanceIdentifier: clusterID, }) if err != nil { fmt.Println("Got an error waiting for snapshot for cluster " + *clusterID) return } fmt.Printf("Snapshot successfully created for cluster with ID " + *clusterID) }