Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fileType options #99

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Exporting your database to a separate Google Cloud Storage bucket, preferrably i

```bash
$ cloudsql-exporter --help
usage: cloudsql-backup --bucket=BUCKET --project=PROJECT [<flags>]
usage: cloudsql-exporter --bucket=BUCKET --project=PROJECT [<flags>]

Export Cloud SQL databases to Google Cloud Storage

Expand All @@ -31,6 +31,7 @@ Flags:
--ensure-iam-bindings Ensure that the Cloud SQL service account has the
required IAM role binding to export and validate the
backup
--fileType Type of file to export (SQL, SQL_FILE_TYPE_UNSPECIFIED, BAK, CSV) [Default SQL]
```

## Installation
Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"time"
"strings"

"golang.org/x/oauth2/google"
"google.golang.org/api/option"
Expand All @@ -17,13 +18,14 @@ import (
)

var (
app = kingpin.New("cloudsql-backup", "Export Cloud SQL databases to Google Cloud Storage")
app = kingpin.New("cloudsql-exporter", "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()
fileType = app.Flag("fileType", "Type of file to export (SQL, SQL_FILE_TYPE_UNSPECIFIED, BAK, CSV) [Default SQL]").Default("SQL").String()
)

func main() {
Expand Down Expand Up @@ -71,15 +73,13 @@ func main() {
}
}

var objectName string
var objectName string = time.Now().Format(time.RFC3339Nano) + "." + strings.ToLower(*fileType)

if *compression {
objectName = time.Now().Format(time.RFC3339Nano) + ".sql.gz"
} else {
objectName = time.Now().Format(time.RFC3339Nano) + ".sql"
objectName = objectName + ".gz"
}

err := cloudsql.ExportCloudSQLDatabase(ctx, sqlAdminSvc, databases, *project, string(instance), *bucket, objectName)
err := cloudsql.ExportCloudSQLDatabase(ctx, sqlAdminSvc, databases, *project, string(instance), *bucket, objectName, strings.ToUpper(*fileType))
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudsql/cloudsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ func ListDatabasesForCloudSQLInstance(ctx context.Context, sqlAdminSvc *sqladmin
}

// ExportCloudSQLDatabase exports a Cloud SQL database to a Google Cloud Storage bucket.
func ExportCloudSQLDatabase(ctx context.Context, sqlAdminSvc *sqladmin.Service, databases []string, projectID, instanceID, bucketName, objectName string) error {
func ExportCloudSQLDatabase(ctx context.Context, sqlAdminSvc *sqladmin.Service, databases []string, projectID, instanceID, bucketName, objectName string, fileType string) error {
for _, database := range databases {
log.Printf("Exporting database %s for instance %s", database, instanceID)

req := &sqladmin.InstancesExportRequest{
ExportContext: &sqladmin.ExportContext{
FileType: "SQL",
FileType: fileType,
Kind: "sql#exportContext",
Databases: []string{database},
Uri: fmt.Sprintf("gs://%s/%s/%s/%s/%s", bucketName, projectID, instanceID, database, objectName),
Expand Down