2017-06-05 22:37:25 +00:00
|
|
|
// xbuild debug
|
2015-05-16 12:24:24 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-05 22:37:25 +00:00
|
|
|
"context"
|
2015-05-16 12:24:24 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
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"
|
2016-02-04 18:37:48 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/worker"
|
2015-05-16 12:24:24 +00:00
|
|
|
)
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
var cmdDump = &cobra.Command{
|
|
|
|
Use: "dump [indexes|snapshots|trees|all|packs]",
|
2017-09-11 16:32:44 +00:00
|
|
|
Short: "Dump data structures",
|
2016-09-17 10:36:05 +00:00
|
|
|
Long: `
|
2017-02-13 15:05:25 +00:00
|
|
|
The "dump" command dumps data structures from the repository as JSON objects. It
|
2016-09-17 10:36:05 +00:00
|
|
|
is used for debugging purposes only.`,
|
2017-08-06 19:02:16 +00:00
|
|
|
DisableAutoGenTag: true,
|
2016-09-17 10:36:05 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runDump(globalOptions, args)
|
|
|
|
},
|
2015-06-21 11:02:56 +00:00
|
|
|
}
|
2015-05-16 12:24:24 +00:00
|
|
|
|
|
|
|
func init() {
|
2016-09-17 10:36:05 +00:00
|
|
|
cmdRoot.AddCommand(cmdDump)
|
2015-05-16 12:24:24 +00: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 15:54:27 +00:00
|
|
|
func debugPrintSnapshots(repo *repository.Repository, wr io.Writer) error {
|
2017-06-05 22:37:25 +00:00
|
|
|
for id := range repo.List(context.TODO(), restic.SnapshotFile) {
|
|
|
|
snapshot, err := restic.LoadSnapshot(context.TODO(), repo, id)
|
2015-05-16 12:24:24 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "LoadSnapshot(%v): %v", id.Str(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(wr, "snapshot_id: %v\n", id)
|
|
|
|
|
|
|
|
err = prettyPrintJSON(wr, snapshot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-23 22:48:55 +00:00
|
|
|
const dumpPackWorkers = 10
|
2016-02-04 18:37:48 +00: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 14:04:29 +00:00
|
|
|
Type restic.BlobType `json:"type"`
|
|
|
|
Length uint `json:"length"`
|
|
|
|
ID restic.ID `json:"id"`
|
|
|
|
Offset uint `json:"offset"`
|
2016-02-04 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func printPacks(repo *repository.Repository, wr io.Writer) error {
|
2017-06-05 22:37:25 +00:00
|
|
|
f := func(ctx context.Context, job worker.Job) (interface{}, error) {
|
2016-02-04 18:37:48 +00:00
|
|
|
name := job.Data.(string)
|
|
|
|
|
2016-09-01 20:09:34 +00:00
|
|
|
h := restic.Handle{Type: restic.DataFile, Name: name}
|
2016-02-04 18:37:48 +00:00
|
|
|
|
2017-06-05 22:37:25 +00:00
|
|
|
blobInfo, err := repo.Backend().Stat(ctx, h)
|
2016-02-04 18:37:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-01 14:04:29 +00:00
|
|
|
blobs, err := pack.List(repo.Key(), restic.ReaderAt(repo.Backend(), h), blobInfo.Size)
|
2016-08-25 19:51:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobs, nil
|
2016-02-04 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jobCh := make(chan worker.Job)
|
|
|
|
resCh := make(chan worker.Job)
|
2017-06-05 22:37:25 +00:00
|
|
|
wp := worker.New(context.TODO(), dumpPackWorkers, f, jobCh, resCh)
|
2016-02-04 18:37:48 +00:00
|
|
|
|
|
|
|
go func() {
|
2017-06-05 22:37:25 +00:00
|
|
|
for name := range repo.Backend().List(context.TODO(), restic.DataFile) {
|
2016-02-04 18:37:48 +00:00
|
|
|
jobCh <- worker.Job{Data: name}
|
|
|
|
}
|
|
|
|
close(jobCh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for job := range resCh {
|
|
|
|
name := job.Data.(string)
|
|
|
|
|
|
|
|
if job.Error != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error for pack %v: %v\n", name, job.Error)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-09-01 14:04:29 +00:00
|
|
|
entries := job.Result.([]restic.Blob)
|
2016-02-04 18:37:48 +00:00
|
|
|
p := Pack{
|
|
|
|
Name: name,
|
|
|
|
Blobs: make([]Blob, len(entries)),
|
|
|
|
}
|
|
|
|
for i, blob := range entries {
|
|
|
|
p.Blobs[i] = Blob{
|
|
|
|
Type: blob.Type,
|
|
|
|
Length: blob.Length,
|
|
|
|
ID: blob.ID,
|
|
|
|
Offset: blob.Offset,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prettyPrintJSON(os.Stdout, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
wp.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
func dumpIndexes(repo restic.Repository) error {
|
2017-06-05 22:37:25 +00:00
|
|
|
for id := range repo.List(context.TODO(), restic.IndexFile) {
|
2015-08-08 15:04:06 +00:00
|
|
|
fmt.Printf("index_id: %v\n", id)
|
|
|
|
|
2017-06-05 22:37:25 +00:00
|
|
|
idx, err := repository.LoadIndex(context.TODO(), repo, id)
|
2015-08-08 15:04:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = idx.Dump(os.Stdout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
func runDump(gopts GlobalOptions, args []string) error {
|
2015-05-16 12:24:24 +00:00
|
|
|
if len(args) != 1 {
|
2017-02-10 18:39:49 +00:00
|
|
|
return errors.Fatal("type not specified")
|
2015-05-16 12:24:24 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
repo, err := OpenRepository(gopts)
|
2015-05-16 12:24:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
if !gopts.NoLock {
|
|
|
|
lock, err := lockRepo(repo)
|
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-27 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 22:37:25 +00:00
|
|
|
err = repo.LoadIndex(context.TODO())
|
2015-05-16 12:24:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tpe := args[0]
|
|
|
|
|
|
|
|
switch tpe {
|
2015-08-08 15:04:06 +00:00
|
|
|
case "indexes":
|
2016-09-17 10:36:05 +00:00
|
|
|
return dumpIndexes(repo)
|
2015-05-16 12:24:24 +00:00
|
|
|
case "snapshots":
|
2016-08-20 15:54:27 +00:00
|
|
|
return debugPrintSnapshots(repo, os.Stdout)
|
2016-02-04 18:37:48 +00:00
|
|
|
case "packs":
|
|
|
|
return printPacks(repo, os.Stdout)
|
2015-05-16 12:24:24 +00:00
|
|
|
case "all":
|
|
|
|
fmt.Printf("snapshots:\n")
|
2016-08-20 15:54:27 +00:00
|
|
|
err := debugPrintSnapshots(repo, os.Stdout)
|
2015-05-16 12:24:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-08 15:04:06 +00:00
|
|
|
fmt.Printf("\nindexes:\n")
|
2016-09-17 10:36:05 +00:00
|
|
|
err = dumpIndexes(repo)
|
2015-08-08 15:04:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-16 12:24:24 +00:00
|
|
|
return nil
|
|
|
|
default:
|
2016-09-01 20:17:37 +00:00
|
|
|
return errors.Fatalf("no such type %q", tpe)
|
2015-05-16 12:24:24 +00:00
|
|
|
}
|
|
|
|
}
|