Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2c087be577 | ||
|
|
37d525c322 |
@@ -382,7 +382,9 @@ on the destination in one go, use `vaultik remote nuke --force`.
|
||||
store — its remote key (or an unambiguous leading part of it) as shown
|
||||
by `snapshot list`. See
|
||||
[restoring on another machine](#restoring-on-another-machine).
|
||||
* Optional path arguments to restore specific files/directories (default: all)
|
||||
* Optional path arguments to restore specific files/directories (default: all).
|
||||
A path that matches nothing in the snapshot is named in a warning, and the
|
||||
command exits non-zero after restoring the paths that match.
|
||||
* Preserves file permissions, timestamps, ownership (ownership requires root),
|
||||
symlinks, and empty directories
|
||||
* `--verify`: After restoring, verify every restored file's chunk hashes
|
||||
@@ -422,11 +424,12 @@ under `blobs/` whose name is not a blob hash is not counted as a blob,
|
||||
since `prune` never deletes it.
|
||||
* `--json`: Output as JSON
|
||||
|
||||
**`remote nuke`**: Delete every snapshot's metadata and every blob from the
|
||||
backup destination store, leaving the bucket prefix empty. Destructive and
|
||||
irreversible. This is the single supported way to wipe the entire
|
||||
destination store. If anything cannot be deleted, the command exits
|
||||
non-zero; running it again deletes what is left.
|
||||
**`remote nuke`**: Delete every object under `metadata/` and `blobs/` on
|
||||
the backup destination store, whatever its name, leaving the bucket prefix
|
||||
empty. Destructive and irreversible. This is the single supported way to
|
||||
wipe the entire destination store. If a listing afterwards still finds
|
||||
anything there, the command names each object left and exits non-zero;
|
||||
running it again deletes what is left.
|
||||
* `--force`: Required to confirm destruction.
|
||||
|
||||
**`database delete`**: Delete the local SQLite state database file
|
||||
|
||||
@@ -30,6 +30,8 @@ This command will restore files from the specified snapshot to the
|
||||
target directory.
|
||||
If no paths are specified, all files are restored.
|
||||
If paths are specified, only matching files/directories are restored.
|
||||
A path that matches nothing in the snapshot is named in a warning, and
|
||||
the command exits non-zero after restoring the paths that match.
|
||||
|
||||
The snapshot may be named by its ID or, when restoring on a host with no
|
||||
local index, by the remote key that 'snapshot list' prints for a
|
||||
|
||||
+6
-40
@@ -30,12 +30,6 @@ var (
|
||||
"storage URL must not carry credentials; " +
|
||||
"set s3.access_key_id and s3.secret_access_key in the config instead")
|
||||
ErrURLUnknownParam = errors.New("unknown query parameter in storage URL")
|
||||
ErrURLBadQuery = errors.New(
|
||||
"storage URL query cannot be decoded; " +
|
||||
"check it for a ; or a % not followed by two hex digits")
|
||||
ErrRcloneRemoteParams = errors.New(
|
||||
"rclone URL must not carry parameters in the remote name; " +
|
||||
"set them on the remote with rclone config instead")
|
||||
)
|
||||
|
||||
// URL represents a parsed storage URL.
|
||||
@@ -96,9 +90,8 @@ func parseFileURL(rawPath string) (*URL, error) {
|
||||
}
|
||||
|
||||
// parseS3URL parses an s3://bucket/prefix URL. It rejects credentials in
|
||||
// the userinfo or in the endpoint parameter's userinfo, a query that
|
||||
// cannot be decoded, and any query parameter other than endpoint, region
|
||||
// and ssl, so a credential-bearing URL is never stored or echoed.
|
||||
// the userinfo and any query parameter other than endpoint, region and
|
||||
// ssl, so a credential-bearing URL is never stored or echoed.
|
||||
func parseS3URL(rawURL string) (*URL, error) {
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
@@ -114,26 +107,13 @@ func parseS3URL(rawURL string) (*URL, error) {
|
||||
return nil, ErrMissingBucket
|
||||
}
|
||||
|
||||
// u.Query() would silently drop a part it cannot decode, endpoint
|
||||
// included. The decode error is not wrapped: it can quote the value.
|
||||
query, err := url.ParseQuery(u.RawQuery)
|
||||
if err != nil {
|
||||
return nil, ErrURLBadQuery
|
||||
}
|
||||
query := u.Query()
|
||||
|
||||
err = rejectUnknownParams(query, "endpoint", "region", "ssl")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// A host name never contains "@", so one in an endpoint, with or
|
||||
// without a scheme, can only introduce userinfo.
|
||||
for _, endpoint := range query["endpoint"] {
|
||||
if strings.Contains(endpoint, "@") {
|
||||
return nil, ErrURLCredentials
|
||||
}
|
||||
}
|
||||
|
||||
return &URL{
|
||||
Scheme: schemeS3,
|
||||
Bucket: bucket,
|
||||
@@ -145,23 +125,9 @@ func parseS3URL(rawURL string) (*URL, error) {
|
||||
}
|
||||
|
||||
// parseRcloneURL parses an rclone://remote/path URL. rclone:// takes no
|
||||
// query parameters, so credentials in the userinfo, parameters in the
|
||||
// remote name and any query parameter at all are rejected rather than
|
||||
// silently ignored.
|
||||
// query parameters, so credentials in the userinfo and any parameter at
|
||||
// all are rejected rather than silently ignored.
|
||||
func parseRcloneURL(rawURL string) (*URL, error) {
|
||||
// The remote name runs up to the first "/", "?" or "#". It never
|
||||
// contains a comma; one starts rclone's remote,param=value form,
|
||||
// whose values can be credentials. This is checked before url.Parse,
|
||||
// whose error for such a name can print part of it.
|
||||
remote := strings.TrimPrefix(rawURL, "rclone://")
|
||||
if end := strings.IndexAny(remote, "/?#"); end >= 0 {
|
||||
remote = remote[:end]
|
||||
}
|
||||
|
||||
if strings.Contains(remote, ",") {
|
||||
return nil, ErrRcloneRemoteParams
|
||||
}
|
||||
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
return nil, wrapParseError(err)
|
||||
@@ -171,7 +137,7 @@ func parseRcloneURL(rawURL string) (*URL, error) {
|
||||
return nil, ErrURLCredentials
|
||||
}
|
||||
|
||||
remote = u.Host
|
||||
remote := u.Host
|
||||
if remote == "" {
|
||||
return nil, ErrMissingRemote
|
||||
}
|
||||
|
||||
@@ -150,19 +150,6 @@ func TestParseStorageURLRejectsCredentials(t *testing.T) {
|
||||
wantErr: storage.ErrURLCredentials,
|
||||
secrets: []string{key, secret, "mybucket"},
|
||||
},
|
||||
{
|
||||
name: "s3 userinfo in endpoint",
|
||||
raw: "s3://mybucket?endpoint=https://" +
|
||||
key + ":" + secret + "@minio.example.com",
|
||||
wantErr: storage.ErrURLCredentials,
|
||||
secrets: []string{key, secret},
|
||||
},
|
||||
{
|
||||
name: "s3 userinfo in endpoint without scheme",
|
||||
raw: "s3://mybucket?endpoint=" + key + ":" + secret + "@minio.example.com",
|
||||
wantErr: storage.ErrURLCredentials,
|
||||
secrets: []string{key, secret},
|
||||
},
|
||||
{
|
||||
name: "s3 unknown query param",
|
||||
raw: "s3://mybucket?access_key=" + key + "&secret=" + secret,
|
||||
@@ -216,104 +203,6 @@ func TestParseStorageURLRejectsCredentials(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseStorageURLRejectsRcloneRemoteParams checks that an rclone
|
||||
// remote name carrying parameters (rclone's "remote,param=value" form,
|
||||
// which can hold a credential) is rejected without echoing the value,
|
||||
// including the URLs that url.Parse itself refuses.
|
||||
func TestParseStorageURLRejectsRcloneRemoteParams(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
raw string
|
||||
secrets []string // must not appear in the error message
|
||||
}{
|
||||
{
|
||||
name: "configured remote",
|
||||
raw: "rclone://gdrive,client_secret=topsecret/backups",
|
||||
secrets: []string{"topsecret"},
|
||||
},
|
||||
{
|
||||
name: "remote defined inline",
|
||||
raw: "rclone://:s3,access_key_id=KEY,secret_access_key=SECRET/bucket",
|
||||
secrets: []string{"KEY", "SECRET"},
|
||||
},
|
||||
{
|
||||
name: "colon in a parameter value",
|
||||
raw: "rclone://gdrive,client_secret=ab:cd/backups",
|
||||
secrets: []string{"cd"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := storage.ParseStorageURL(tc.raw)
|
||||
if !errors.Is(err, storage.ErrRcloneRemoteParams) {
|
||||
t.Fatalf("ParseStorageURL(%q) error = %v, want %v",
|
||||
tc.raw, err, storage.ErrRcloneRemoteParams)
|
||||
}
|
||||
|
||||
for _, secret := range tc.secrets {
|
||||
if strings.Contains(err.Error(), secret) {
|
||||
t.Errorf("error message leaked %q: %v", secret, err.Error())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseStorageURLRejectsUndecodableQuery checks that an s3 URL whose
|
||||
// query cannot be decoded is rejected, not read with that part of the
|
||||
// query left out, and that the error does not echo it.
|
||||
func TestParseStorageURLRejectsUndecodableQuery(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const (
|
||||
key = "AKIAKEY"
|
||||
secret = "topsecret"
|
||||
)
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
raw string
|
||||
}{
|
||||
{
|
||||
name: "semicolon",
|
||||
raw: "s3://mybucket/prefix?endpoint=https://" + key + ":" + secret +
|
||||
"@minio.example.com:9000;region=us-east-1",
|
||||
},
|
||||
{
|
||||
name: "bad percent escape",
|
||||
raw: "s3://mybucket/prefix?endpoint=https://" + key + ":" + secret +
|
||||
"%zz@minio.example.com:9000",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := storage.ParseStorageURL(tc.raw)
|
||||
if err == nil {
|
||||
t.Fatalf("ParseStorageURL(%q) returned no error", tc.raw)
|
||||
}
|
||||
|
||||
if !errors.Is(err, storage.ErrURLBadQuery) {
|
||||
t.Errorf("ParseStorageURL(%q) error = %v, want %v",
|
||||
tc.raw, err, storage.ErrURLBadQuery)
|
||||
}
|
||||
|
||||
for _, s := range []string{key, secret, "minio.example.com"} {
|
||||
if strings.Contains(err.Error(), s) {
|
||||
t.Errorf("error message leaked %q: %v", s, err.Error())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseStorageURLParseFailureHidesURL checks that when url.Parse
|
||||
// itself fails, the wrapped error carries only the inner cause, not the
|
||||
// *url.Error whose text embeds the raw (possibly credential-bearing) URL.
|
||||
|
||||
@@ -18,7 +18,8 @@ import (
|
||||
// TestNukeRemoteLeavesNoFiles checks that remote nuke leaves no file
|
||||
// under a file:// destination, including the `.partial` files uploads
|
||||
// killed part-way leave next to a snapshot's metadata and next to where a
|
||||
// blob would have been.
|
||||
// blob would have been, a snapshot's database whose manifest was never
|
||||
// uploaded, and a file under blobs/ whose name is not a blob hash.
|
||||
func TestNukeRemoteLeavesNoFiles(t *testing.T) {
|
||||
log.Initialize(log.Config{})
|
||||
t.Parallel()
|
||||
@@ -38,6 +39,9 @@ func TestNukeRemoteLeavesNoFiles(t *testing.T) {
|
||||
"db.zst.age-123456.partial"),
|
||||
filepath.Join(storeDir, "blobs", hash[:2], hash[2:4],
|
||||
hash+"-123456.partial"),
|
||||
filepath.Join(storeDir, "metadata",
|
||||
snapshot.RemoteSnapshotKey("no-manifest"), "db.zst.age"),
|
||||
filepath.Join(storeDir, "blobs", "0b", "01", ".DS_Store"),
|
||||
}
|
||||
|
||||
for _, leftover := range leftovers {
|
||||
@@ -100,6 +104,34 @@ func TestNukeRemoteFailsWhenADeleteFails(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestNukeRemoteNamesWhatIsLeft checks that remote nuke, when an object
|
||||
// it does not recognise cannot be deleted, does not report the store
|
||||
// empty but names that object and fails.
|
||||
func TestNukeRemoteNamesWhatIsLeft(t *testing.T) {
|
||||
log.Initialize(log.Config{})
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
storeDir := filepath.Join(t.TempDir(), "store")
|
||||
v, _, out := backUpToFileDestination(ctx, t, storeDir)
|
||||
|
||||
const leftKey = "blobs/0b/01/.DS_Store"
|
||||
|
||||
leftPath := filepath.Join(storeDir, filepath.FromSlash(leftKey))
|
||||
require.NoError(t, os.MkdirAll(filepath.Dir(leftPath), 0o750))
|
||||
require.NoError(t, os.WriteFile(leftPath, []byte("finder"), 0o600))
|
||||
|
||||
store := faultstore.New(v.Storage)
|
||||
store.OnDelete = func(key string) bool { return key == leftKey }
|
||||
v.Storage = store
|
||||
|
||||
require.Error(t, v.NukeRemote(true))
|
||||
|
||||
assert.Contains(t, out.String(), leftKey)
|
||||
assert.NotContains(t, out.String(), "now empty")
|
||||
assert.Contains(t, out.String(), "vaultik remote nuke --force")
|
||||
}
|
||||
|
||||
// TestNukeRemoteRefusesAnotherDestinationWithoutRetryAdvice checks that
|
||||
// remote nuke, when the local index belongs to a different destination
|
||||
// than the configured one, fails without advising to run nuke again:
|
||||
|
||||
+60
-16
@@ -22,6 +22,11 @@ var errNukeRequiresForce = errors.New(
|
||||
// errBlobDeleteFailed reports unreferenced blobs prune could not delete.
|
||||
var errBlobDeleteFailed = errors.New("blob(s) could not be deleted")
|
||||
|
||||
// errObjectsLeft reports objects remote nuke still finds on the backup
|
||||
// destination store after deleting.
|
||||
var errObjectsLeft = errors.New(
|
||||
"object(s) still on the backup destination store")
|
||||
|
||||
// nukeCommandHint is the command suggested when a remote nuke could not
|
||||
// delete everything: running it again deletes what is left, which
|
||||
// `vaultik prune` does not do for snapshot metadata.
|
||||
@@ -35,10 +40,11 @@ const metadataDirName = "metadata"
|
||||
// (blobs/<aa>/<bb>/<hash>).
|
||||
const blobKeyParts = 4
|
||||
|
||||
// NukeRemote deletes every snapshot's metadata and every blob from remote
|
||||
// storage, along with any object an upload cut off part-way left under a
|
||||
// temporary `.partial` name. After this returns successfully the bucket
|
||||
// prefix is empty and the next backup starts from scratch.
|
||||
// NukeRemote deletes every object under metadata/ and blobs/ in remote
|
||||
// storage, whatever its name, along with any object an upload cut off
|
||||
// part-way left under a temporary `.partial` name. It returns
|
||||
// successfully only when a listing afterwards finds nothing left there,
|
||||
// and the next backup then starts from scratch.
|
||||
//
|
||||
// Refuses to run unless force is true. The caller is responsible for
|
||||
// confirming with the user.
|
||||
@@ -68,7 +74,8 @@ func (v *Vaultik) NukeRemote(force bool) error {
|
||||
}
|
||||
|
||||
// emptyDestinationStore does the deleting for NukeRemote and stops at
|
||||
// the first step that fails.
|
||||
// the first step that fails. It also fails when objects are still
|
||||
// listed at the end, naming each one.
|
||||
func (v *Vaultik) emptyDestinationStore() error {
|
||||
v.UI.Beginf("Removing all snapshot metadata from backup destination store.")
|
||||
|
||||
@@ -77,25 +84,62 @@ func (v *Vaultik) emptyDestinationStore() error {
|
||||
return fmt.Errorf("removing all snapshots: %w", err)
|
||||
}
|
||||
|
||||
v.UI.Beginf("Removing any blobs still present in backup destination store.")
|
||||
v.UI.Beginf("Removing everything still present in backup destination store.")
|
||||
|
||||
err = v.PruneBlobs(&PruneOptions{Force: true})
|
||||
if err != nil {
|
||||
return fmt.Errorf("pruning blobs: %w", err)
|
||||
var left []string
|
||||
|
||||
for _, prefix := range []string{"metadata/", "blobs/"} {
|
||||
keys, err := v.deleteEverythingUnder(prefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
left = append(left, keys...)
|
||||
}
|
||||
|
||||
// The file and rclone listings skip `.partial` objects, so the two
|
||||
// steps above never delete them.
|
||||
for _, prefix := range []string{"metadata/", "blobs/"} {
|
||||
err = v.Storage.DeletePartialUploads(v.ctx, prefix)
|
||||
if err != nil {
|
||||
return fmt.Errorf("deleting partial uploads: %w", err)
|
||||
}
|
||||
for _, key := range left {
|
||||
v.UI.Warningf("Still on the backup destination store: %s", key)
|
||||
}
|
||||
|
||||
if len(left) > 0 {
|
||||
return fmt.Errorf("%d %w", len(left), errObjectsLeft)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// deleteEverythingUnder deletes every object under prefix, whatever its
|
||||
// name, and returns the keys a listing afterwards still finds. A delete
|
||||
// that fails is logged and the others still run; that object then
|
||||
// shows up in the listing afterwards.
|
||||
func (v *Vaultik) deleteEverythingUnder(prefix string) ([]string, error) {
|
||||
keys, err := v.Storage.List(v.ctx, prefix)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing %s: %w", prefix, err)
|
||||
}
|
||||
|
||||
for _, key := range keys {
|
||||
err = v.Storage.Delete(v.ctx, key)
|
||||
if err != nil {
|
||||
log.Error("Failed to delete object", "key", key, "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// The file and rclone listings skip `.partial` objects, so the
|
||||
// deletes above never reach them.
|
||||
err = v.Storage.DeletePartialUploads(v.ctx, prefix)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("deleting partial uploads: %w", err)
|
||||
}
|
||||
|
||||
left, err := v.Storage.List(v.ctx, prefix)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing %s: %w", prefix, err)
|
||||
}
|
||||
|
||||
return left, nil
|
||||
}
|
||||
|
||||
// PruneBlobsResult contains the result of a blob prune operation
|
||||
//
|
||||
//nolint:tagliatelle // snake_case is the established JSON output format
|
||||
|
||||
+55
-24
@@ -27,6 +27,7 @@ import (
|
||||
// Sentinel errors for restore failures.
|
||||
var (
|
||||
errFilesFailedRestore = errors.New("file(s) failed to restore")
|
||||
errPathsNotInSnapshot = errors.New("requested path(s) not in the snapshot")
|
||||
errFilesFailedVerify = errors.New("files failed verification")
|
||||
errDecryptionKeyRequired = errors.New(
|
||||
"decryption key required for restore\n\n" +
|
||||
@@ -150,16 +151,20 @@ func (v *Vaultik) Restore(opts *RestoreOptions) error {
|
||||
repos := database.NewRepositories(tempDB)
|
||||
|
||||
// Step 2: Get list of files to restore
|
||||
files, err := v.getFilesToRestore(v.ctx, repos, opts.Paths)
|
||||
files, unmatched, err := v.getFilesToRestore(v.ctx, repos, opts.Paths)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting files to restore: %w", err)
|
||||
}
|
||||
|
||||
// Named before anything is restored, so the names are shown even
|
||||
// when the restore stops on an error.
|
||||
unmatchedErr := v.reportUnmatchedPaths(unmatched)
|
||||
|
||||
if len(files) == 0 {
|
||||
log.Warn("No files found to restore")
|
||||
v.UI.Warningf("No files found to restore.")
|
||||
|
||||
return nil
|
||||
return unmatchedErr
|
||||
}
|
||||
|
||||
log.Info("Found files to restore", "count", len(files))
|
||||
@@ -185,6 +190,20 @@ func (v *Vaultik) Restore(opts *RestoreOptions) error {
|
||||
|
||||
result.Duration = time.Since(startTime)
|
||||
|
||||
return v.finishRestore(repos, files, unmatchedErr, opts, result)
|
||||
}
|
||||
|
||||
// finishRestore prints the restore summary and the post-restore
|
||||
// warnings, runs optional verification, and converts any failed-file
|
||||
// count into an error. Otherwise it returns unmatchedErr, the error for
|
||||
// the requested paths that matched nothing (nil when there are none).
|
||||
func (v *Vaultik) finishRestore(
|
||||
repos *database.Repositories,
|
||||
files []*database.File,
|
||||
unmatchedErr error,
|
||||
opts *RestoreOptions,
|
||||
result *RestoreResult,
|
||||
) error {
|
||||
log.Info("Restore complete",
|
||||
"files_restored", result.FilesRestored,
|
||||
"bytes_restored", ubytes(result.BytesRestored),
|
||||
@@ -199,17 +218,6 @@ func (v *Vaultik) Restore(opts *RestoreOptions) error {
|
||||
v.UI.Duration(result.Duration),
|
||||
)
|
||||
|
||||
return v.finishRestore(repos, files, opts, result)
|
||||
}
|
||||
|
||||
// finishRestore emits the post-restore warnings, runs optional
|
||||
// verification, and converts any failed-file count into an error.
|
||||
func (v *Vaultik) finishRestore(
|
||||
repos *database.Repositories,
|
||||
files []*database.File,
|
||||
opts *RestoreOptions,
|
||||
result *RestoreResult,
|
||||
) error {
|
||||
if os.Geteuid() != 0 {
|
||||
v.UI.Warningf("Restore did not preserve file ownership: chown(2) " +
|
||||
"requires root. Re-run as root (e.g. with sudo) if you need " +
|
||||
@@ -236,7 +244,23 @@ func (v *Vaultik) finishRestore(
|
||||
return fmt.Errorf("%d %w", result.FilesFailed, errFilesFailedRestore)
|
||||
}
|
||||
|
||||
return nil
|
||||
return unmatchedErr
|
||||
}
|
||||
|
||||
// reportUnmatchedPaths warns, by name, about each requested path that
|
||||
// matched nothing in the snapshot, and returns an error counting them,
|
||||
// or nil when there are none.
|
||||
func (v *Vaultik) reportUnmatchedPaths(unmatched []string) error {
|
||||
if len(unmatched) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, path := range unmatched {
|
||||
log.Warn("Requested path matches nothing in the snapshot", "path", path)
|
||||
v.UI.Warningf("%s matches nothing in the snapshot.", v.UI.Path(path))
|
||||
}
|
||||
|
||||
return fmt.Errorf("%d %w", len(unmatched), errPathsNotInSnapshot)
|
||||
}
|
||||
|
||||
// restoreIdentities parses the configured age secret key once into every
|
||||
@@ -840,27 +864,34 @@ func (v *Vaultik) materializeSnapshotDB(
|
||||
return db, tempDir, nil
|
||||
}
|
||||
|
||||
// getFilesToRestore returns the list of files to restore based on path filters
|
||||
// getFilesToRestore returns the list of files to restore based on path
|
||||
// filters, and the filters, as given, that matched no file.
|
||||
func (v *Vaultik) getFilesToRestore(
|
||||
ctx context.Context, repos *database.Repositories, pathFilters []string,
|
||||
) ([]*database.File, error) {
|
||||
) ([]*database.File, []string, error) {
|
||||
// If no filters, get all files
|
||||
if len(pathFilters) == 0 {
|
||||
return repos.Files.ListAll(ctx)
|
||||
files, err := repos.Files.ListAll(ctx)
|
||||
|
||||
return files, nil, err
|
||||
}
|
||||
|
||||
// Get files matching the path filters
|
||||
var result []*database.File
|
||||
var (
|
||||
result []*database.File
|
||||
unmatched []string
|
||||
)
|
||||
|
||||
seen := make(map[string]bool)
|
||||
|
||||
for _, filter := range pathFilters {
|
||||
// Normalize the filter path
|
||||
filter = filepath.Clean(filter)
|
||||
|
||||
files, err := repos.Files.ListUnderPath(ctx, filter)
|
||||
files, err := repos.Files.ListUnderPath(ctx, filepath.Clean(filter))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing files under %s: %w", filter, err)
|
||||
return nil, nil, fmt.Errorf("listing files under %s: %w", filter, err)
|
||||
}
|
||||
|
||||
if len(files) == 0 {
|
||||
unmatched = append(unmatched, filter)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
@@ -871,7 +902,7 @@ func (v *Vaultik) getFilesToRestore(
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
return result, unmatched, nil
|
||||
}
|
||||
|
||||
// buildChunkToBlobMap creates a mapping from chunk hash to blob information
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
package vaultik_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"sneak.berlin/go/vaultik/internal/config"
|
||||
"sneak.berlin/go/vaultik/internal/database"
|
||||
"sneak.berlin/go/vaultik/internal/log"
|
||||
"sneak.berlin/go/vaultik/internal/storage"
|
||||
"sneak.berlin/go/vaultik/internal/storage/faultstore"
|
||||
"sneak.berlin/go/vaultik/internal/ui"
|
||||
"sneak.berlin/go/vaultik/internal/vaultik"
|
||||
)
|
||||
|
||||
// These tests cover https://git.eeqj.de/sneak/vaultik/issues/309: a path
|
||||
// given to restore that matches nothing in the snapshot is named in a
|
||||
// warning and makes the restore fail, after the paths that do match are
|
||||
// restored.
|
||||
|
||||
func TestRestoreNamesUnmatchedPathAndRestoresTheMatchingOne(t *testing.T) {
|
||||
log.Initialize(log.Config{})
|
||||
t.Parallel()
|
||||
|
||||
fs := afero.NewOsFs()
|
||||
tempDir := t.TempDir()
|
||||
dataDir := filepath.Join(tempDir, "src")
|
||||
storeDir := filepath.Join(tempDir, "remote")
|
||||
restoreDir := filepath.Join(tempDir, "restored")
|
||||
dbPath := filepath.Join(tempDir, "index.sqlite")
|
||||
|
||||
ctx := context.Background()
|
||||
cfg := faultTestConfig()
|
||||
files := writeFaultSourceTree(t, fs, dataDir)
|
||||
|
||||
store, err := storage.NewFileStorer(storeDir)
|
||||
require.NoError(t, err)
|
||||
|
||||
db, err := database.New(ctx, dbPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
id := fullFaultBackup(ctx, t, fs, store, cfg,
|
||||
database.NewRepositories(db), dataDir, dbPath, "unmatched")
|
||||
require.NoError(t, db.Close())
|
||||
|
||||
matched := filepath.Join(dataDir, "small.txt")
|
||||
unmatched := filepath.Join(dataDir, "typo.txt")
|
||||
|
||||
var out bytes.Buffer
|
||||
|
||||
v := newReaderVaultik(ctx, cfg, store, nil, fs)
|
||||
v.UI = ui.NewWithColor(&out, false)
|
||||
|
||||
err = v.Restore(&vaultik.RestoreOptions{
|
||||
SnapshotID: id,
|
||||
TargetDir: restoreDir,
|
||||
Paths: []string{matched, unmatched},
|
||||
})
|
||||
|
||||
require.Error(t, err, "a path that matches nothing must fail the restore")
|
||||
assert.Contains(t, out.String(), unmatched,
|
||||
"the path that matches nothing must be named")
|
||||
assert.NotContains(t, out.String(), matched,
|
||||
"the path that matches must not be reported")
|
||||
|
||||
got, err := afero.ReadFile(fs, filepath.Join(restoreDir, matched))
|
||||
require.NoError(t, err, "the path that matches must still be restored")
|
||||
assert.Equal(t, files[matched], got)
|
||||
}
|
||||
|
||||
// When no requested path matches, nothing is restored and each path is
|
||||
// named. One path is relative. The other goes through the symlink that
|
||||
// is the snapshot's configured path, while the entries are stored under
|
||||
// the directory the symlink points to.
|
||||
func TestRestoreNamesEveryPathWhenNothingMatches(t *testing.T) {
|
||||
log.Initialize(log.Config{})
|
||||
t.Parallel()
|
||||
|
||||
const snapshotName = "linked"
|
||||
|
||||
fs := afero.NewOsFs()
|
||||
tempDir := t.TempDir()
|
||||
dataDir := filepath.Join(tempDir, "src")
|
||||
linkDir := filepath.Join(tempDir, "srclink")
|
||||
storeDir := filepath.Join(tempDir, "remote")
|
||||
restoreDir := filepath.Join(tempDir, "restored")
|
||||
dbPath := filepath.Join(tempDir, "index.sqlite")
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
writeFaultSourceTree(t, fs, dataDir)
|
||||
require.NoError(t, os.Symlink(dataDir, linkDir))
|
||||
|
||||
cfg := faultTestConfig()
|
||||
cfg.IndexPath = dbPath
|
||||
cfg.ChunkSize = config.Size(faultChunkSize)
|
||||
cfg.Snapshots = map[string]config.SnapshotConfig{
|
||||
snapshotName: {Paths: []string{linkDir}},
|
||||
}
|
||||
|
||||
store, err := storage.NewFileStorer(storeDir)
|
||||
require.NoError(t, err)
|
||||
|
||||
db, err := database.New(ctx, dbPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
repos := database.NewRepositories(db)
|
||||
v := newBackupVaultik(ctx, cfg, store, repos, db, fs)
|
||||
|
||||
require.NoError(t, backUp(v, snapshotName))
|
||||
|
||||
id := localSnapshotID(ctx, t, repos, snapshotName)
|
||||
require.NoError(t, db.Close())
|
||||
|
||||
throughLink := filepath.Join(linkDir, "sub")
|
||||
relative := strings.TrimPrefix(filepath.Join(dataDir, "small.txt"), "/")
|
||||
|
||||
var out bytes.Buffer
|
||||
|
||||
reader := newReaderVaultik(ctx, cfg, store, nil, fs)
|
||||
reader.UI = ui.NewWithColor(&out, false)
|
||||
|
||||
err = reader.Restore(&vaultik.RestoreOptions{
|
||||
SnapshotID: id,
|
||||
TargetDir: restoreDir,
|
||||
Paths: []string{throughLink, relative},
|
||||
})
|
||||
|
||||
require.Error(t, err, "a restore that matches nothing must fail")
|
||||
assert.Contains(t, out.String(), throughLink,
|
||||
"the path through the symlink must be named")
|
||||
assert.Contains(t, out.String(), relative,
|
||||
"the relative path must be named")
|
||||
|
||||
_, err = fs.Stat(restoreDir)
|
||||
assert.True(t, os.IsNotExist(err), "nothing must be restored")
|
||||
}
|
||||
|
||||
// Without --skip-errors a damaged blob stops the restore part way. The
|
||||
// path that matches nothing must still be named.
|
||||
func TestRestoreNamesUnmatchedPathWhenADamagedBlobStopsIt(t *testing.T) {
|
||||
log.Initialize(log.Config{})
|
||||
t.Parallel()
|
||||
|
||||
fs := afero.NewOsFs()
|
||||
tempDir := t.TempDir()
|
||||
dataDir := filepath.Join(tempDir, "src")
|
||||
storeDir := filepath.Join(tempDir, "remote")
|
||||
restoreDir := filepath.Join(tempDir, "restored")
|
||||
dbPath := filepath.Join(tempDir, "index.sqlite")
|
||||
|
||||
ctx := context.Background()
|
||||
cfg := faultTestConfig()
|
||||
|
||||
writeFaultSourceTree(t, fs, dataDir)
|
||||
|
||||
inner, err := storage.NewFileStorer(storeDir)
|
||||
require.NoError(t, err)
|
||||
|
||||
db, err := database.New(ctx, dbPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
id := fullFaultBackup(ctx, t, fs, inner, cfg,
|
||||
database.NewRepositories(db), dataDir, dbPath, "damaged")
|
||||
require.NoError(t, db.Close())
|
||||
|
||||
damaged := faultstore.New(inner)
|
||||
damaged.OnGet = func(key string) faultstore.GetFault {
|
||||
if strings.HasPrefix(key, "blobs/") {
|
||||
return faultstore.GetCorrupt
|
||||
}
|
||||
|
||||
return faultstore.GetNormal
|
||||
}
|
||||
|
||||
matched := filepath.Join(dataDir, "small.txt")
|
||||
unmatched := filepath.Join(dataDir, "typo.txt")
|
||||
|
||||
var out bytes.Buffer
|
||||
|
||||
v := newReaderVaultik(ctx, cfg, damaged, nil, fs)
|
||||
v.UI = ui.NewWithColor(&out, false)
|
||||
|
||||
err = v.Restore(&vaultik.RestoreOptions{
|
||||
SnapshotID: id,
|
||||
TargetDir: restoreDir,
|
||||
Paths: []string{matched, unmatched},
|
||||
})
|
||||
|
||||
require.Error(t, err, "a damaged blob must stop the restore")
|
||||
assert.Contains(t, err.Error(), "blob",
|
||||
"the restore must stop on the damaged blob")
|
||||
assert.Contains(t, out.String(), unmatched,
|
||||
"the path that matches nothing must be named")
|
||||
}
|
||||
Reference in New Issue
Block a user