Bind the local index to its backup destination
The local index tracks which chunks and blobs already exist at the backup destination. Nothing was recording *which* destination, so changing storage_url and running a backup left the scanner treating every already-seen chunk as still-present at the new (empty) location. Uploads were skipped silently and the resulting snapshots pointed at blobs that don't exist at the new destination. Fix: record storage_url in a new local_meta key-value table on first mutating command, and refuse to proceed when the configured URL later differs from the stored one. The error explains the two recovery paths (revert the config, or run 'vaultik database purge' to discard the index and rebuild from a fresh full backup). Wired into snapshot create / prune / snapshot remove / snapshot purge / snapshot cleanup. Read-only inspection commands (snapshot list, remote info, store info) are exempt.
This commit is contained in:
53
internal/database/local_meta.go
Normal file
53
internal/database/local_meta.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// LocalMetaKeyStorageURL is the key under which the destination store's
|
||||
// URL is recorded when a mutating command first binds the local index
|
||||
// to a specific backup destination.
|
||||
const LocalMetaKeyStorageURL = "storage_url"
|
||||
|
||||
// LocalMetaRepository provides keyed access to host-local settings
|
||||
// stored in the local_meta table.
|
||||
type LocalMetaRepository struct {
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewLocalMetaRepository(db *DB) *LocalMetaRepository {
|
||||
return &LocalMetaRepository{db: db}
|
||||
}
|
||||
|
||||
// Get returns the value stored at key, or the empty string if the key
|
||||
// is not set. A missing key is not an error — the caller distinguishes
|
||||
// "unset" (bind on first use) from "set to something" (compare).
|
||||
func (r *LocalMetaRepository) Get(ctx context.Context, key string) (string, error) {
|
||||
var value string
|
||||
err := r.db.conn.QueryRowContext(ctx,
|
||||
"SELECT value FROM local_meta WHERE key = ?", key,
|
||||
).Scan(&value)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("reading local_meta %q: %w", key, err)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// Set writes key=value, replacing any prior value.
|
||||
func (r *LocalMetaRepository) Set(ctx context.Context, key, value string) error {
|
||||
_, err := r.db.ExecWithLog(ctx,
|
||||
`INSERT INTO local_meta (key, value) VALUES (?, ?)
|
||||
ON CONFLICT(key) DO UPDATE SET value = excluded.value`,
|
||||
key, value,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("writing local_meta %q: %w", key, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user