backup_database
Create a backup of the PocketBase database in JSON or CSV format to protect data and enable recovery.
Instructions
Create a backup of the PocketBase database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | Export format (default: json) |
Implementation Reference
- src/tools/handlers/migration.ts:67-125 (handler)Implements the core logic of the backup_database tool: exports all PocketBase collections' schemas and records in JSON (default) or CSV format.export function createBackupDatabaseHandler(pb: PocketBase): ToolHandler { return async (args: BackupDatabaseArgs = {}) => { try { const format = args.format || "json"; // Get all collections const collections = await pb.collections.getFullList(); const backup: any = { timestamp: new Date().toISOString(), collections: {}, }; // Export each collection for (const collection of collections) { const records = await pb.collection(collection.name).getFullList(); backup.collections[collection.name] = { schema: collection, records, }; } if (format === "csv") { // For CSV, we'll export each collection separately const csvData: string[] = []; for (const [collectionName, data] of Object.entries(backup.collections)) { const collectionData = data as any; if (collectionData.records.length > 0) { csvData.push(`\n--- Collection: ${collectionName} ---`); // Format records as CSV const records = collectionData.records; if (records.length > 0) { const headers = Object.keys(records[0]); csvData.push(headers.join(",")); records.forEach((record: any) => { const values = headers.map((header) => { const value = record[header]; return typeof value === "string" && value.includes(",") ? `"${value.replace(/"/g, '""')}"` : String(value || ""); }); csvData.push(values.join(",")); }); } } } return createTextResponse(csvData.join("\n")); } return createJsonResponse(backup); } catch (error: unknown) { throw handlePocketBaseError("backup database", error); } }; }
- src/tools/schemas/migration.ts:84-93 (schema)JSON schema defining the input parameters for the backup_database tool, including optional format choice.export const backupDatabaseSchema = { type: "object", properties: { format: { type: "string", enum: ["json", "csv"], description: "Export format (default: json)", }, }, };
- src/server.ts:185-190 (registration)Registers the backup_database tool in the MCP server with its name, description, input schema, and handler function.{ name: "backup_database", description: "Create a backup of the PocketBase database", inputSchema: backupDatabaseSchema, handler: createBackupDatabaseHandler(pb), },