Keep personal notes private by default and require review before publishing shared notes. Restore backups and replay forgotten notes in one transaction, and retain the installed release when an unattended rollback could bring back forgotten notes.
108 lines
4.9 KiB
Go
108 lines
4.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/samcharles93/archie-core/internal/app/statestore"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
|
)
|
|
|
|
// recoveryUsage is the whole flag surface of the offline recovery commands.
|
|
// They are subcommands of this binary rather than a recovery binary of their
|
|
// own: offline database maintenance belongs to the process that owns the file
|
|
// and its path layout.
|
|
const recoveryUsage = `usage: archie-state-store <command> [flags]
|
|
|
|
With no command, archie-state-store serves the State Store gRPC contract.
|
|
|
|
The recovery commands operate on the PostgreSQL database configured by
|
|
database_url. Install the PostgreSQL client tools (pg_dump, pg_restore and
|
|
psql) and make sure they're on PATH.
|
|
|
|
backup takes a consistent snapshot of a serving database. restore is
|
|
destructive and offline: stop every archie service first, since it refuses
|
|
while any of them holds its serving claim or the exclusive restore lock, and
|
|
every write made after the snapshot is lost. rollback refuses while the State
|
|
Store serves. There is no automatic schema rollback: the way back from a
|
|
migration is restoring the snapshot taken before it.
|
|
|
|
restore re-applies the deletions recorded outside the database, and refuses when
|
|
the record cannot account for them. Keep that record with your snapshots: a
|
|
snapshot restored without the current record cannot show which notes were
|
|
forgotten after it was taken. Nothing about a snapshot and a record can prove the
|
|
record is the newest one; that is the operator's responsibility, and this is why
|
|
a restore that cannot account for the data needs -accept-unrecorded-deletions,
|
|
which means forgotten notes may return.
|
|
|
|
backup -out FILE write a snapshot of the database
|
|
restore -from FILE replace the database with a snapshot
|
|
[-accept-unrecorded-deletions]
|
|
proceed although the record of deletions
|
|
cannot account for this data, so forgotten
|
|
notes may return
|
|
validate check the database, its stored settings and
|
|
the configuration the daemon would boot with
|
|
rollback -kind KIND replay an earlier revision of a stored
|
|
[-revision N] resource through the ordinary replace
|
|
|
|
Every command takes -config FILE and -config-overlay FILE: the configuration
|
|
the daemon boots with, which names the database.
|
|
`
|
|
|
|
// runRecovery parses and performs one offline recovery command, reporting the
|
|
// operation's summary on stdout. Exit codes follow the serve path: 0 succeeded,
|
|
// 1 the operation failed, 2 the command line was wrong.
|
|
func runRecovery(args []string, stdout, stderr io.Writer) int {
|
|
if len(args) == 0 {
|
|
_, _ = fmt.Fprint(stderr, recoveryUsage)
|
|
return 2
|
|
}
|
|
command := args[0]
|
|
options := statestore.RecoveryOptions{Operation: command}
|
|
switch command {
|
|
case statestore.RecoveryBackup, statestore.RecoveryRestore, statestore.RecoveryValidate, statestore.RecoveryRollback:
|
|
default:
|
|
_, _ = fmt.Fprintf(stderr, "archie-state-store: unknown command %q\n\n", command)
|
|
_, _ = fmt.Fprint(stderr, recoveryUsage)
|
|
return 2
|
|
}
|
|
|
|
flags := flag.NewFlagSet("archie-state-store "+command, flag.ContinueOnError)
|
|
flags.SetOutput(stderr)
|
|
flags.Usage = func() { _, _ = fmt.Fprint(stderr, recoveryUsage) }
|
|
flags.StringVar(&options.Out, "out", "", "snapshot file backup writes")
|
|
flags.StringVar(&options.From, "from", "", "snapshot file restore reads")
|
|
flags.StringVar(&options.Kind, "kind", "", "control-plane resource kind rollback replays")
|
|
flags.Int64Var(&options.Revision, "revision", 0, "revision rollback replays (default: the newest one older than the current value)")
|
|
flags.BoolVar(&options.AcceptUnrecordedDeletions, "accept-unrecorded-deletions", false,
|
|
"restore although the record of deletions cannot account for this data: forgotten notes may return")
|
|
// The configuration names the PostgreSQL database, and validate also checks
|
|
// the stored settings against it.
|
|
flags.StringVar(&options.Config, "config", configuration.DefaultConfigPath(), "configuration file or directory the daemon boots with")
|
|
flags.StringVar(&options.Overlay, "config-overlay", "", "configuration overlay file or directory")
|
|
if err := flags.Parse(args[1:]); err != nil {
|
|
// A requested help is not a wrong command line: the usage above is the
|
|
// answer, and the serve path exits 0 for the same request.
|
|
if errors.Is(err, flag.ErrHelp) {
|
|
return 0
|
|
}
|
|
return 2
|
|
}
|
|
if flags.NArg() > 0 {
|
|
_, _ = fmt.Fprintf(stderr, "archie-state-store %s: unexpected argument %q\n", command, flags.Arg(0))
|
|
return 2
|
|
}
|
|
|
|
summary, err := statestore.RunRecovery(context.Background(), options)
|
|
if err != nil {
|
|
_, _ = fmt.Fprintf(stderr, "archie-state-store %s: %v\n", command, err)
|
|
return 1
|
|
}
|
|
_, _ = fmt.Fprintln(stdout, summary)
|
|
return 0
|
|
}
|