Files
pocket-id/backend/internal/cmds/export.go
Alessandro (Ale) Segala 2e48aaf0a4 feat: export and import Pocket ID's own data against a standalone runtime
With a standalone Francis runtime the actor data lives in the runtime's
store, which Pocket ID cannot reach: the Francis protocol has no backup
or restore operation, and a restore refuses to run while any host is
connected, so a CLI that joined the cluster would block its own restore.

Rather than refusing outright, both commands now cover everything Pocket
ID does own and say plainly what they leave out, pointing at the
runtime's own backup and restore commands for the rest. The export writes
no francis.bin entry, which the import side already tolerates, and the
import refuses an archive that carries one, since restoring only its
Pocket ID half would leave the runtime holding another deployment's actor
state. The import also skips the exclusive-access lease, which lives in
the actor tables of a database this deployment does not use, and warns
that the replicas have to be stopped by hand.
2026-08-31 05:26:59 +00:00

100 lines
2.5 KiB
Go

package cmds
import (
"context"
"fmt"
"io"
"os"
"github.com/spf13/cobra"
"github.com/pocket-id/pocket-id/backend/internal/bootstrap"
"github.com/pocket-id/pocket-id/backend/internal/common"
"github.com/pocket-id/pocket-id/backend/internal/service"
)
type exportFlags struct {
Path string
}
func init() {
var flags exportFlags
exportCmd := &cobra.Command{
Use: "export",
Short: "Exports all data of Pocket ID into a ZIP file",
RunE: func(cmd *cobra.Command, args []string) error {
return runExport(cmd.Context(), flags)
},
}
exportCmd.Flags().StringVarP(&flags.Path, "path", "p", "pocket-id-export.zip", "Path to the ZIP file to export the data to, or '-' to write to stdout")
rootCmd.AddCommand(exportCmd)
}
// runExport orchestrates the export flow
func runExport(ctx context.Context, flags exportFlags) error {
db, pg, err := bootstrap.NewDatabase(ctx)
if err != nil {
return fmt.Errorf("failed to connect to database: %w", err)
}
storage, err := bootstrap.InitStorage(ctx, db)
if err != nil {
return fmt.Errorf("failed to initialize storage: %w", err)
}
// Close filesystem storage handles before the command exits
defer func() {
_ = storage.Close()
}()
// The actor data lives outside of the Pocket ID schema, so it's exported through Francis
// A standalone runtime keeps it in its own store, out of reach from here, and the export service leaves the entry out of the archive when no provider is passed
var actorsProvider service.ActorsBackupProvider
if common.EnvConfig.HasEmbeddedFrancisRuntime() {
providerOpts, provErr := bootstrap.ActorsProviderOptions(db, pg)
if provErr != nil {
return provErr
}
provider, provErr := bootstrap.NewActorsBackupProvider(ctx, providerOpts)
if provErr != nil {
return fmt.Errorf("failed to initialize the actor host's data provider: %w", provErr)
}
defer func() {
_ = provider.Close()
}()
actorsProvider = provider
} else {
printRemoteActorDataNotice("The actor data is NOT included in this export", "back it up separately with: francis runtime backup -f actors.bin")
}
exportService := service.NewExportService(db, storage, actorsProvider)
var w io.Writer
if flags.Path == "-" {
w = os.Stdout
} else {
file, err := os.Create(flags.Path)
if err != nil {
return fmt.Errorf("failed to create export file: %w", err)
}
defer file.Close()
w = file
}
if err := exportService.ExportToZip(ctx, w); err != nil {
return fmt.Errorf("failed to export data: %w", err)
}
if flags.Path != "-" {
fmt.Printf("Exported data to %s\n", flags.Path)
}
return nil
}