-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
90 lines (72 loc) · 2.69 KB
/
main.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
package main
import (
"context"
"log"
"os"
"time"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/sqladmin/v1"
"google.golang.org/api/storage/v1"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/trufflesecurity/cloudsql-exporter/pkg/cloudsql"
"github.com/trufflesecurity/cloudsql-exporter/pkg/version"
)
var (
app = kingpin.New("cloudsql-backup", "Export Cloud SQL databases to Google Cloud Storage")
bucket = app.Flag("bucket", "Google Cloud Storage bucket name").Required().String()
project = app.Flag("project", "GCP project ID").Required().String()
instance = app.Flag("instance", "Cloud SQL instance name, if not specified all within the project will be enumerated").String()
compression = app.Flag("compression", "Enable compression for exported SQL files").Bool()
ensureIamBindings = app.Flag("ensure-iam-bindings", "Ensure that the Cloud SQL service account has the required IAM role binding to export and validate the backup").Bool()
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
kingpin.MustParse(app.Parse(os.Args[1:]))
app.Version("cloudsql-exporter " + version.BuildVersion)
hc, err := google.DefaultClient(ctx, sqladmin.SqlserviceAdminScope)
if err != nil {
log.Fatal(err)
}
sqlAdminSvc, err := sqladmin.NewService(ctx, option.WithHTTPClient(hc))
if err != nil {
log.Fatal(err)
}
storageSvc, err := storage.NewService(ctx, option.WithHTTPClient(hc))
if err != nil {
log.Fatal(err)
}
instances, err := cloudsql.EnumerateCloudSQLDatabaseInstances(ctx, sqlAdminSvc, *project, *instance)
if err != nil {
log.Fatal(err)
}
for instance, databases := range instances {
log.Printf("Exporting backup for instance %s", instance)
if *ensureIamBindings {
sqlAdminSvcAccount, err := cloudsql.GetSvcAcctForCloudSQLInstance(ctx, sqlAdminSvc, *project, string(instance), "")
if err != nil {
log.Fatal(err)
}
err = cloudsql.AddRoleBindingToGCSBucket(ctx, storageSvc, *project, *bucket, "roles/storage.objectCreator", sqlAdminSvcAccount, string(instance))
if err != nil {
log.Fatal(err)
}
err = cloudsql.AddRoleBindingToGCSBucket(ctx, storageSvc, *project, *bucket, "roles/storage.objectViewer", sqlAdminSvcAccount, string(instance))
if err != nil {
log.Fatal(err)
}
}
var objectName string
if *compression {
objectName = time.Now().Format(time.RFC3339Nano) + ".sql.gz"
} else {
objectName = time.Now().Format(time.RFC3339Nano) + ".sql"
}
err := cloudsql.ExportCloudSQLDatabase(ctx, sqlAdminSvc, databases, *project, string(instance), *bucket, objectName)
if err != nil {
log.Fatal(err)
}
}
log.Println("Backup complete")
}