restic/cmd/restic/cmd_debug.go

168 lines
3.6 KiB
Go
Raw Normal View History

2017-10-12 18:14:48 +00:00
// +build debug
package main
import (
2017-06-05 22:37:25 +00:00
"context"
"encoding/json"
"fmt"
"io"
2016-09-17 10:36:05 +00:00
"github.com/spf13/cobra"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/pack"
"github.com/restic/restic/internal/repository"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
)
2017-10-12 18:18:45 +00:00
var cmdDebug = &cobra.Command{
Use: "debug",
Short: "Debug commands",
}
var cmdDebugDump = &cobra.Command{
Use: "dump [indexes|snapshots|all|packs]",
Short: "Dump data structures",
2016-09-17 10:36:05 +00:00
Long: `
The "dump" command dumps data structures from the repository as JSON objects. It
is used for debugging purposes only.
EXIT STATUS
===========
Exit status is 0 if the command was successful, and non-zero if there was any error.
`,
DisableAutoGenTag: true,
2016-09-17 10:36:05 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
return runDebugDump(globalOptions, args)
2016-09-17 10:36:05 +00:00
},
}
func init() {
2017-10-12 18:18:45 +00:00
cmdRoot.AddCommand(cmdDebug)
cmdDebug.AddCommand(cmdDebugDump)
}
func prettyPrintJSON(wr io.Writer, item interface{}) error {
buf, err := json.MarshalIndent(item, "", " ")
if err != nil {
return err
}
_, err = wr.Write(append(buf, '\n'))
return err
}
func debugPrintSnapshots(ctx context.Context, repo *repository.Repository, wr io.Writer) error {
2020-11-28 07:59:12 +00:00
return restic.ForAllSnapshots(ctx, repo, nil, func(id restic.ID, snapshot *restic.Snapshot, err error) error {
if err != nil {
return err
}
fmt.Fprintf(wr, "snapshot_id: %v\n", id)
return prettyPrintJSON(wr, snapshot)
})
}
// Pack is the struct used in printPacks.
type Pack struct {
Name string `json:"name"`
Blobs []Blob `json:"blobs"`
}
// Blob is the struct used in printPacks.
type Blob struct {
2016-09-01 14:04:29 +00:00
Type restic.BlobType `json:"type"`
Length uint `json:"length"`
ID restic.ID `json:"id"`
Offset uint `json:"offset"`
}
func printPacks(ctx context.Context, repo *repository.Repository, wr io.Writer) error {
return repo.List(ctx, restic.PackFile, func(id restic.ID, size int64) error {
h := restic.Handle{Type: restic.PackFile, Name: id.String()}
2020-11-29 17:44:36 +00:00
blobs, _, err := pack.List(repo.Key(), restic.ReaderAt(ctx, repo.Backend(), h), size)
if err != nil {
Warnf("error for pack %v: %v\n", id.Str(), err)
return nil
}
p := Pack{
Name: id.String(),
Blobs: make([]Blob, len(blobs)),
}
for i, blob := range blobs {
p.Blobs[i] = Blob{
Type: blob.Type,
Length: blob.Length,
ID: blob.ID,
Offset: blob.Offset,
}
}
return prettyPrintJSON(wr, p)
})
}
func dumpIndexes(ctx context.Context, repo restic.Repository, wr io.Writer) error {
2020-11-07 17:19:25 +00:00
return repository.ForAllIndexes(ctx, repo, func(id restic.ID, idx *repository.Index, oldFormat bool, err error) error {
Printf("index_id: %v\n", id)
2015-08-08 15:04:06 +00:00
if err != nil {
return err
}
return idx.Dump(wr)
})
2015-08-08 15:04:06 +00:00
}
func runDebugDump(gopts GlobalOptions, args []string) error {
if len(args) != 1 {
return errors.Fatal("type not specified")
}
2016-09-17 10:36:05 +00:00
repo, err := OpenRepository(gopts)
if err != nil {
return err
}
2016-09-17 10:36:05 +00:00
if !gopts.NoLock {
2020-08-09 11:24:47 +00:00
lock, err := lockRepo(gopts.ctx, repo)
2016-09-17 10:36:05 +00:00
defer unlockRepo(lock)
if err != nil {
return err
}
2015-06-27 12:40:18 +00:00
}
tpe := args[0]
switch tpe {
2015-08-08 15:04:06 +00:00
case "indexes":
return dumpIndexes(gopts.ctx, repo, gopts.stdout)
case "snapshots":
return debugPrintSnapshots(gopts.ctx, repo, gopts.stdout)
case "packs":
return printPacks(gopts.ctx, repo, gopts.stdout)
case "all":
Printf("snapshots:\n")
err := debugPrintSnapshots(gopts.ctx, repo, gopts.stdout)
if err != nil {
return err
}
Printf("\nindexes:\n")
err = dumpIndexes(gopts.ctx, repo, gopts.stdout)
2015-08-08 15:04:06 +00:00
if err != nil {
return err
}
return nil
default:
2016-09-01 20:17:37 +00:00
return errors.Fatalf("no such type %q", tpe)
}
}