43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"linkport/internal/desktop"
|
|
"linkport/internal/store"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
func databaseCommand(args []string) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
|
defer cancel()
|
|
if len(args) == 2 && args[0] == "inspect-database" {
|
|
info, err := store.InspectDatabaseSnapshot(ctx, args[1])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return json.NewEncoder(os.Stdout).Encode(info)
|
|
}
|
|
if len(args) != 4 || args[0] != "restore-database" || args[2] != "--confirm-sha256" {
|
|
return errors.New("usage: linkport inspect-database <snapshot.sqlite> | restore-database <snapshot.sqlite> --confirm-sha256 <sha256>")
|
|
}
|
|
// Acquire the same lock as a running service. Never restore an open service DB.
|
|
prepared, err := desktop.Prepare()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer prepared.Close()
|
|
backup, err := store.RestoreDatabaseOffline(ctx, prepared.Data, args[1], args[3])
|
|
if backup != "" {
|
|
fmt.Fprintln(os.Stderr, "Pre-restore backup:", backup)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprintln(os.Stdout, "Database restored. Restart linkport; accounts, credentials, audit and TLS now come from the snapshot.")
|
|
return nil
|
|
}
|