Skip to content

Instantly share code, notes, and snippets.

@MatejLach
Created August 11, 2025 11:57
Show Gist options
  • Save MatejLach/3bd21880b444e2a29698a2b551fdeec6 to your computer and use it in GitHub Desktop.
Save MatejLach/3bd21880b444e2a29698a2b551fdeec6 to your computer and use it in GitHub Desktop.
backup Firestore
package main
import (
"context"
"fmt"
"log"
admin "cloud.google.com/go/firestore/apiv1/admin"
"google.golang.org/genproto/googleapis/firestore/admin/v1"
)
func main() {
// TODO(developer): Set these values for your project and export destination.
projectID := "your-gcp-project-id"
databaseID := "studiodb" // Specify your named database here.
gcsOutputURI := "gs://your-gcs-bucket/path-for-export"
ctx := context.Background()
// Create the Firestore Admin client.
client, err := admin.NewFirestoreAdminClient(ctx)
if err != nil {
log.Fatalf("Failed to create Firestore admin client: %v", err)
}
defer client.Close()
// Construct the request for the export operation.
// The 'Name' field must be formatted as "projects/{projectID}/databases/{databaseID}".
req := &adminpb.ExportDocumentsRequest{
Name: fmt.Sprintf("projects/%s/databases/%s", projectID, databaseID),
OutputUriPrefix: gcsOutputURI,
// You can specify collection IDs here if you only want to export specific kinds.
// CollectionIds: []string{"kind1", "kind2"},
}
log.Printf("Starting export for database '%s' to '%s'...", databaseID, gcsOutputURI)
// The ExportDocuments method returns a long-running operation.
op, err := client.ExportDocuments(ctx, req)
if err != nil {
log.Fatalf("Failed to start export operation: %v", err)
}
// You can wait for the operation to complete.
// For very large databases, you might want to handle this asynchronously.
resp, err := op.Wait(ctx)
if err != nil {
log.Fatalf("Export operation failed: %v", err)
}
log.Printf("Successfully exported database to: %s", resp.GetOutputUriPrefix())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment