Skip to content

Commit 22ff2bf

Browse files
authoredMar 26, 2024
Add "compression" flag to use native CloudSQL export file compression (#84)
1 parent b63f7ad commit 22ff2bf

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed
 

‎main.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var (
2222
bucket = app.Flag("bucket", "Google Cloud Storage bucket name").Required().String()
2323
project = app.Flag("project", "GCP project ID").Required().String()
2424
instance = app.Flag("instance", "Cloud SQL instance name, if not specified all within the project will be enumerated").String()
25+
compression = app.Flag("compression", "Enable compression for exported SQL files").Bool()
2526
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()
2627
)
2728

@@ -70,7 +71,15 @@ func main() {
7071
}
7172
}
7273

73-
err := cloudsql.ExportCloudSQLDatabase(ctx, sqlAdminSvc, databases, *project, string(instance), *bucket, time.Now().Format(time.RFC3339Nano))
74+
var objectName string
75+
76+
if *compression {
77+
objectName = time.Now().Format(time.RFC3339Nano) + ".sql.gz"
78+
} else {
79+
objectName = time.Now().Format(time.RFC3339Nano) + ".sql"
80+
}
81+
82+
err := cloudsql.ExportCloudSQLDatabase(ctx, sqlAdminSvc, databases, *project, string(instance), *bucket, objectName)
7483
if err != nil {
7584
log.Fatal(err)
7685
}

‎pkg/cloudsql/cloudsql.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func ExportCloudSQLDatabase(ctx context.Context, sqlAdminSvc *sqladmin.Service,
126126
FileType: "SQL",
127127
Kind: "sql#exportContext",
128128
Databases: []string{database},
129-
Uri: fmt.Sprintf("gs://%s/%s/%s/%s/%s.sql", bucketName, projectID, instanceID, database, objectName),
129+
Uri: fmt.Sprintf("gs://%s/%s/%s/%s/%s", bucketName, projectID, instanceID, database, objectName),
130130
},
131131
}
132132

0 commit comments

Comments
 (0)