forked from awsdocs/aws-doc-sdk-examples
/
rds_copy_snapshot_to_s3.go
125 lines (107 loc) · 4.07 KB
/
rds_copy_snapshot_to_s3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
// snippet-sourceauthor:[AWS]
// snippet-sourcedescription:[rds_copy_snapshot_to_s3 copies RDS cluster snapshots to S3.]
// snippet-keyword:[Amazon Relational Database Service]
// snippet-keyword:[Amazon RDS]
// snippet-keyword:[CopyDBSnaphotToS3 function]
// snippet-keyword:[Go]
// snippet-sourcesyntax:[go]
// snippet-service:[rds]
// snippet-keyword:[Code Sample]
// snippet-sourcetype:[full-example]
// snippet-sourcedate:[2019-01-30]
/*
Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
// snippet-start:[rds.go.copy_rds_snapshot_to_s3]
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/rds"
)
func main() {
if len(os.Args) != 6 {
exitErrorf("RoleArn,kmsKey,bucketName,rdsSnapshotName and exportRDSSnapshotName name required\nUsage: %s RoleArn kmsKey bucketName rdsSnapshotName exportRDSSnapshotName", os.Args[0])
}
roleArn := os.Args[1]
kmsKey := os.Args[2]
bucketName := os.Args[3]
rdsSnapshotName := os.Args[4]
exportRDSSnapshotName := os.Args[5]
// Initialize a session in us-west-2 that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-west-2")},
)
// Create RDS service client
svc := rds.New(sess)
result, err := svc.DescribeDBSnapshots(nil)
if err != nil {
exitErrorf("Unable to list snapshots, %v", err)
}
currentTime := time.Now()
sanpshotTime := currentTime.Format("2006-01-02")
rdsSnapshot := rdsSnapshotName + sanpshotTime
IndentifierSanpshotTime := currentTime.Format("20060102")
exportIdentifier := exportRDSSnapshotName + IndentifierSanpshotTime
// Getting latest snapshot from rds snapshot list
for _, s := range result.DBSnapshots {
if strings.Contains(*s.DBSnapshotArn, rdsSnapshot) {
fmt.Printf("* %s with status %s\n",
aws.StringValue(s.DBSnapshotArn), aws.StringValue(s.Status))
exportList := []*string{}
input := &rds.StartExportTaskInput{
ExportOnly: exportList, // Optional
ExportTaskIdentifier: aws.String(exportIdentifier),
IamRoleArn: aws.String(roleArn),
KmsKeyId: aws.String(kmsKey),
S3BucketName: aws.String(bucketName),
//S3Prefix: aws.String("/"), // optional
SourceArn: aws.String(*s.DBSnapshotArn),
}
result, err := svc.StartExportTask(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case rds.ErrCodeDBSnapshotAlreadyExistsFault:
fmt.Println(rds.ErrCodeDBSnapshotAlreadyExistsFault, aerr.Error())
case rds.ErrCodeDBSnapshotNotFoundFault:
fmt.Println(rds.ErrCodeDBSnapshotNotFoundFault, aerr.Error())
case rds.ErrCodeInvalidDBSnapshotStateFault:
fmt.Println(rds.ErrCodeInvalidDBSnapshotStateFault, aerr.Error())
case rds.ErrCodeSnapshotQuotaExceededFault:
fmt.Println(rds.ErrCodeSnapshotQuotaExceededFault, aerr.Error())
case rds.ErrCodeKMSKeyNotAccessibleFault:
fmt.Println(rds.ErrCodeKMSKeyNotAccessibleFault, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
}
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
// snippet-end:[rds.go.copy_rds_snapshot_to_s3]