backup

package
v0.16.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 12 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const TSBackupVersionV21 = "2.1"

TSBackupVersionV21 is the backup version string emitted by the TS original.

Variables

View Source
var (
	MaxExportRowsPerTable = 50_000
	MaxExportCellBytes    = 4 << 20
	MaxExportPayloadBytes = int64(64 << 20)
)
View Source
var (
	// TSV21MaxRowsPerTable, TSV21MaxColumnsPerRow and TSV21MaxCellBytes mirror
	// the tables-path import limits in handler/admin (50k rows per table,
	// 128 columns per row, 4MB per cell).
	TSV21MaxRowsPerTable  = 50_000
	TSV21MaxColumnsPerRow = 128
	TSV21MaxCellBytes     = 4 << 20
)
View Source
var AccountsTables = []string{
	"sites",
	"site_api_endpoints",
	"site_disabled_models",
	"accounts",
	"account_tokens",
	"checkin_logs",
	"model_availability",
	"token_model_availability",
	"token_routes",
	"route_group_sources",
	"oauth_route_units",
	"oauth_route_unit_members",
	"route_channels",
	"proxy_video_tasks",
	"admin_background_tasks",
	"proxy_files",
	"downstream_api_keys",
	"site_announcements",
}

AccountsTables is the subset exported when type=accounts.

View Source
var AllTables = []string{
	"sites",
	"site_api_endpoints",
	"site_disabled_models",
	"accounts",
	"account_tokens",
	"checkin_logs",
	"model_availability",
	"token_model_availability",
	"token_routes",
	"route_group_sources",
	"oauth_route_units",
	"oauth_route_unit_members",
	"route_channels",
	"proxy_logs",
	"proxy_debug_traces",
	"proxy_debug_attempts",
	"proxy_video_tasks",
	"admin_background_tasks",
	"proxy_files",
	"settings",
	"admin_snapshots",
	"analytics_projection_checkpoints",
	"site_day_usage",
	"site_hour_usage",
	"model_day_usage",
	"downstream_api_keys",
	"site_announcements",
	"events",
}

AllTables is the full backup/export order, with parent tables before children so imports can replay it safely.

View Source
var ErrInvalidExportType = errors.New("invalid export type; only all/accounts/preferences are supported")
View Source
var RuntimeLocalSettingKeys = map[string]bool{

	"account_credential_secret": true,

	"auth_token": true,

	"backup_webdav_config_v1": true,

	"backup_webdav_state_v1": true,

	"db_ssl":  true,
	"db_type": true,
	"db_url":  true,

	"monitor_ldoh_cookie": true,

	"proxy_token": true,
}

RuntimeLocalSettingKeys are settings that a backup import must never set or overwrite. Both import paths (the tables-path import in handler/admin and the TS v2.1 parser in this package) share this policy.

Policy: deny-list, not allow-list. The settings table holds ~90 feature keys scattered across handler/admin, store and the schedulers, and new benign keys are added with most features; an allow-list would have to track every addition or imports would silently drop legitimate settings. The dangerous class — credentials, deployment-bound wiring, and state that background schedulers consume automatically at startup — is small and stable, so we deny it explicitly instead.

Threat model: a backup can come from an untrusted source. A malicious import that could plant backup_webdav_config_v1 would turn the backup-webdav scheduler into a periodic exfiltration job shipping the whole database to an attacker endpoint after the next restart, and a planted monitor_ldoh_cookie pins the monitor session to an attacker credential.

MAINTAINERS: any new setting that stores a credential, session token or encryption key, wires the database, or makes the server push data to a configured endpoint on its own MUST be added here.

Functions

func BuildPayload

func BuildPayload(db *sqlx.DB, exportType string) (map[string]any, error)

func IsKnownTable

func IsKnownTable(name string) bool

func IsTSV21Payload added in v0.16.3

func IsTSV21Payload(raw []byte) bool

IsTSV21Payload reports whether raw looks like a TS backup JSON carrying version "2.1".

func QueryTableAsJSON

func QueryTableAsJSON(db *sqlx.DB, table string) ([]map[string]any, error)

func TablesForExportType

func TablesForExportType(exportType string) (string, []string, error)

Types

type ExportLimitError

type ExportLimitError struct {
	// contains filtered or unexported fields
}

func (ExportLimitError) Error

func (e ExportLimitError) Error() string

type TSV21ClientError added in v0.16.3

type TSV21ClientError struct {
	Message string
}

TSV21ClientError reports a payload problem that is the caller's fault (malformed JSON, missing required fields, limit violations). Handlers map it to HTTP 400.

func (TSV21ClientError) Error added in v0.16.3

func (e TSV21ClientError) Error() string

type TSV21ImportResult added in v0.16.3

type TSV21ImportResult struct {
	// Imported maps Go table names to the number of rows inserted. Rows whose
	// primary key already existed are skipped (ON CONFLICT DO NOTHING).
	Imported map[string]int64 `json:"imported"`
	// SkippedSettings counts settings rows excluded by policy.
	SkippedSettings int64 `json:"skippedSettings,omitempty"`
	// NewDownstreamApiKeys lists the names (never the key values) of
	// downstream_api_keys entries actually inserted by this import. Importing
	// an untrusted backup is equivalent to accepting the keys it carries;
	// this list (mirrored into the events audit trail) lets operators see
	// exactly which new downstream keys were admitted.
	NewDownstreamApiKeys []string `json:"newDownstreamApiKeys,omitempty"`
	Warnings             []string `json:"warnings,omitempty"`
}

TSV21ImportResult reports what a v2.1 import actually did.

func ImportTSV21 added in v0.16.3

func ImportTSV21(db *store.DB, raw []byte) (*TSV21ImportResult, error)

ImportTSV21 parses a v2.1 payload and imports it into db inside a single transaction. Duplicate handling matches the tables-path import: rows whose primary key already exists are skipped, nothing is overwritten.

SECURITY: importing an untrusted backup is equivalent to accepting the downstream API keys it carries — newly admitted keys are working credentials for the proxy surface. They are listed by name (never by key value) in the result so callers can surface them in the response and audit trail; runtime-local settings (RuntimeLocalSettingKeys) are always skipped.

type TSV21Parsed added in v0.16.3

type TSV21Parsed struct {
	// Type is the optional top-level type ("all" | "accounts" | "preferences");
	// empty when the payload has no type field.
	Type string
	// TableOrder lists Go tables in FK-safe import order.
	TableOrder []string
	// Tables maps Go table names to converted rows.
	Tables map[string][]map[string]any
	// Warnings collects everything that was ignored (unknown fields/sections,
	// malformed rows). Imports must never fail because of them.
	Warnings []string
	// SkippedSettings counts settings rows excluded by policy (runtime-local
	// keys, empty keys).
	SkippedSettings int64
}

TSV21Parsed is a v2.1 payload normalized to canonical snake_case table rows in FK-safe import order.

func ParseTSV21 added in v0.16.3

func ParseTSV21(raw []byte) (*TSV21Parsed, error)

ParseTSV21 converts a v2.1 payload into canonical table rows. It performs all structural validation but does not touch the database.

type TSV21PreviewResult added in v0.16.3

type TSV21PreviewResult struct {
	Plan     map[string]TSV21TablePreview `json:"plan"`
	Warnings []string                     `json:"warnings,omitempty"`
}

TSV21PreviewResult is the full v2.1 import plan without writes.

func PreviewTSV21 added in v0.16.3

func PreviewTSV21(db *store.DB, raw []byte) (*TSV21PreviewResult, error)

PreviewTSV21 computes the import plan for a v2.1 payload without writing anything. PK detection mirrors the tables-path preview: "id" for every table except "settings" which uses "key".

type TSV21TablePreview added in v0.16.3

type TSV21TablePreview struct {
	Rows        int64 `json:"rows"`
	ToInsert    int64 `json:"toInsert"`
	Duplicates  int64 `json:"duplicates"`
	SkippedRows int64 `json:"skippedRows"`
}

TSV21TablePreview is the per-table preview plan for a v2.1 import.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL