2017-10-12 20:14:48 +02:00
|
|
|
// +build debug
|
2015-05-16 14:24:24 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-06 00:37:25 +02:00
|
|
|
"context"
|
2015-05-16 14:24:24 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2017-07-23 14:21:03 +02:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/pack"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-07-24 17:42:25 +02:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2015-05-16 14:24:24 +02:00
|
|
|
)
|
|
|
|
|
2017-10-12 20:18:45 +02:00
|
|
|
var cmdDebug = &cobra.Command{
|
|
|
|
Use: "debug",
|
|
|
|
Short: "Debug commands",
|
|
|
|
}
|
|
|
|
|
2017-10-14 13:55:00 +02:00
|
|
|
var cmdDebugDump = &cobra.Command{
|
2017-10-01 08:04:52 -07:00
|
|
|
Use: "dump [indexes|snapshots|all|packs]",
|
2017-09-11 09:32:44 -07:00
|
|
|
Short: "Dump data structures",
|
2016-09-17 12:36:05 +02:00
|
|
|
Long: `
|
2017-02-13 16:05:25 +01:00
|
|
|
The "dump" command dumps data structures from the repository as JSON objects. It
|
2019-11-04 22:03:38 -08:00
|
|
|
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.
|
|
|
|
`,
|
2017-08-06 21:02:16 +02:00
|
|
|
DisableAutoGenTag: true,
|
2016-09-17 12:36:05 +02:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-10-14 13:55:00 +02:00
|
|
|
return runDebugDump(globalOptions, args)
|
2016-09-17 12:36:05 +02:00
|
|
|
},
|
2015-06-21 13:02:56 +02:00
|
|
|
}
|
2015-05-16 14:24:24 +02:00
|
|
|
|
|
|
|
func init() {
|
2017-10-12 20:18:45 +02:00
|
|
|
cmdRoot.AddCommand(cmdDebug)
|
2017-10-14 13:55:00 +02:00
|
|
|
cmdDebug.AddCommand(cmdDebugDump)
|
2015-05-16 14:24:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-08-20 17:54:27 +02:00
|
|
|
func debugPrintSnapshots(repo *repository.Repository, wr io.Writer) error {
|
2018-01-21 17:25:36 +01:00
|
|
|
return repo.List(context.TODO(), restic.SnapshotFile, func(id restic.ID, size int64) error {
|
2017-06-06 00:37:25 +02:00
|
|
|
snapshot, err := restic.LoadSnapshot(context.TODO(), repo, id)
|
2015-05-16 14:24:24 +02:00
|
|
|
if err != nil {
|
2018-01-21 17:25:36 +01:00
|
|
|
return err
|
2015-05-16 14:24:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(wr, "snapshot_id: %v\n", id)
|
|
|
|
|
2018-01-21 17:25:36 +01:00
|
|
|
return prettyPrintJSON(wr, snapshot)
|
|
|
|
})
|
2015-05-16 14:24:24 +02:00
|
|
|
}
|
|
|
|
|
2016-02-04 19:37:48 +01:00
|
|
|
// 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 16:04:29 +02:00
|
|
|
Type restic.BlobType `json:"type"`
|
|
|
|
Length uint `json:"length"`
|
|
|
|
ID restic.ID `json:"id"`
|
|
|
|
Offset uint `json:"offset"`
|
2016-02-04 19:37:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func printPacks(repo *repository.Repository, wr io.Writer) error {
|
|
|
|
|
2018-01-21 17:25:36 +01:00
|
|
|
return repo.List(context.TODO(), restic.DataFile, func(id restic.ID, size int64) error {
|
|
|
|
h := restic.Handle{Type: restic.DataFile, Name: id.String()}
|
2016-02-04 19:37:48 +01:00
|
|
|
|
2018-01-21 17:25:36 +01:00
|
|
|
blobs, err := pack.List(repo.Key(), restic.ReaderAt(repo.Backend(), h), size)
|
2016-02-04 19:37:48 +01:00
|
|
|
if err != nil {
|
2020-04-04 00:00:21 +02:00
|
|
|
fmt.Fprintf(globalOptions.stderr, "error for pack %v: %v\n", id.Str(), err)
|
2018-01-21 17:25:36 +01:00
|
|
|
return nil
|
2016-02-04 19:37:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
p := Pack{
|
2018-01-21 17:25:36 +01:00
|
|
|
Name: id.String(),
|
|
|
|
Blobs: make([]Blob, len(blobs)),
|
2016-02-04 19:37:48 +01:00
|
|
|
}
|
2018-01-21 17:25:36 +01:00
|
|
|
for i, blob := range blobs {
|
2016-02-04 19:37:48 +01:00
|
|
|
p.Blobs[i] = Blob{
|
|
|
|
Type: blob.Type,
|
|
|
|
Length: blob.Length,
|
|
|
|
ID: blob.ID,
|
|
|
|
Offset: blob.Offset,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 22:45:20 +02:00
|
|
|
return prettyPrintJSON(wr, p)
|
2018-01-21 17:25:36 +01:00
|
|
|
})
|
2016-02-04 19:37:48 +01:00
|
|
|
}
|
|
|
|
|
2020-04-02 22:45:20 +02:00
|
|
|
func dumpIndexes(repo restic.Repository, wr io.Writer) error {
|
2018-01-21 17:25:36 +01:00
|
|
|
return repo.List(context.TODO(), restic.IndexFile, func(id restic.ID, size int64) error {
|
2015-08-08 17:04:06 +02:00
|
|
|
fmt.Printf("index_id: %v\n", id)
|
|
|
|
|
2017-06-06 00:37:25 +02:00
|
|
|
idx, err := repository.LoadIndex(context.TODO(), repo, id)
|
2015-08-08 17:04:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-02 22:45:20 +02:00
|
|
|
return idx.Dump(wr)
|
2018-01-21 17:25:36 +01:00
|
|
|
})
|
2015-08-08 17:04:06 +02:00
|
|
|
}
|
|
|
|
|
2017-10-14 13:55:00 +02:00
|
|
|
func runDebugDump(gopts GlobalOptions, args []string) error {
|
2015-05-16 14:24:24 +02:00
|
|
|
if len(args) != 1 {
|
2017-02-10 19:39:49 +01:00
|
|
|
return errors.Fatal("type not specified")
|
2015-05-16 14:24:24 +02:00
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
repo, err := OpenRepository(gopts)
|
2015-05-16 14:24:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
if !gopts.NoLock {
|
|
|
|
lock, err := lockRepo(repo)
|
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-27 14:40:18 +02:00
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +02:00
|
|
|
tpe := args[0]
|
|
|
|
|
|
|
|
switch tpe {
|
2015-08-08 17:04:06 +02:00
|
|
|
case "indexes":
|
2020-04-04 00:00:21 +02:00
|
|
|
return dumpIndexes(repo, gopts.stdout)
|
2015-05-16 14:24:24 +02:00
|
|
|
case "snapshots":
|
2020-04-04 00:00:21 +02:00
|
|
|
return debugPrintSnapshots(repo, gopts.stdout)
|
2016-02-04 19:37:48 +01:00
|
|
|
case "packs":
|
2020-04-04 00:00:21 +02:00
|
|
|
return printPacks(repo, gopts.stdout)
|
2015-05-16 14:24:24 +02:00
|
|
|
case "all":
|
|
|
|
fmt.Printf("snapshots:\n")
|
2020-04-04 00:00:21 +02:00
|
|
|
err := debugPrintSnapshots(repo, gopts.stdout)
|
2015-05-16 14:24:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-08 17:04:06 +02:00
|
|
|
fmt.Printf("\nindexes:\n")
|
2020-04-04 00:00:21 +02:00
|
|
|
err = dumpIndexes(repo, gopts.stdout)
|
2015-08-08 17:04:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +02:00
|
|
|
return nil
|
|
|
|
default:
|
2016-09-01 22:17:37 +02:00
|
|
|
return errors.Fatalf("no such type %q", tpe)
|
2015-05-16 14:24:24 +02:00
|
|
|
}
|
|
|
|
}
|