2015-05-16 12:24:24 +00:00
|
|
|
// +build debug
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic"
|
|
|
|
"restic/backend"
|
|
|
|
"restic/pack"
|
|
|
|
"restic/repository"
|
2016-02-04 18:37:48 +00:00
|
|
|
|
|
|
|
"restic/worker"
|
2015-05-16 12:24:24 +00:00
|
|
|
)
|
|
|
|
|
2015-06-21 11:02:56 +00:00
|
|
|
type CmdDump struct {
|
2015-06-24 18:00:01 +00:00
|
|
|
global *GlobalOptions
|
2015-08-08 15:04:06 +00:00
|
|
|
|
|
|
|
repo *repository.Repository
|
2015-06-21 11:02:56 +00:00
|
|
|
}
|
2015-05-16 12:24:24 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
_, err := parser.AddCommand("dump",
|
|
|
|
"dump data structures",
|
|
|
|
"The dump command dumps data structures from a repository as JSON documents",
|
2015-06-24 18:00:01 +00:00
|
|
|
&CmdDump{global: &globalOpts})
|
2015-05-16 12:24:24 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd CmdDump) Usage() string {
|
2016-02-04 18:37:48 +00:00
|
|
|
return "[indexes|snapshots|trees|all|packs]"
|
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 {
|
2015-05-16 12:24:24 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
for id := range repo.List(backend.Snapshot, done) {
|
|
|
|
snapshot, err := restic.LoadSnapshot(repo, id)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func printTrees(repo *repository.Repository, wr io.Writer) error {
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
trees := []backend.ID{}
|
|
|
|
|
2015-10-12 20:34:12 +00:00
|
|
|
for _, idx := range repo.Index().All() {
|
|
|
|
for blob := range idx.Each(nil) {
|
|
|
|
if blob.Type != pack.Tree {
|
|
|
|
continue
|
|
|
|
}
|
2015-05-16 12:24:24 +00:00
|
|
|
|
2015-10-12 20:34:12 +00:00
|
|
|
trees = append(trees, blob.ID)
|
|
|
|
}
|
2015-05-16 12:24:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range trees {
|
|
|
|
tree, err := restic.LoadTree(repo, id)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "LoadTree(%v): %v", id.Str(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(wr, "tree_id: %v\n", id)
|
|
|
|
|
|
|
|
prettyPrintJSON(wr, tree)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
Type pack.BlobType `json:"type"`
|
|
|
|
Length uint `json:"length"`
|
|
|
|
ID backend.ID `json:"id"`
|
|
|
|
Offset uint `json:"offset"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func printPacks(repo *repository.Repository, wr io.Writer) error {
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
f := func(job worker.Job, done <-chan struct{}) (interface{}, error) {
|
|
|
|
name := job.Data.(string)
|
|
|
|
|
|
|
|
h := backend.Handle{Type: backend.Data, Name: name}
|
|
|
|
|
2016-08-25 19:51:07 +00:00
|
|
|
blobInfo, err := repo.Backend().Stat(h)
|
2016-02-04 18:37:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-25 19:51:07 +00:00
|
|
|
blobs, err := pack.List(repo.Key(), backend.ReaderAt(repo.Backend(), h), blobInfo.Size)
|
|
|
|
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)
|
2016-02-23 22:48:55 +00:00
|
|
|
wp := worker.New(dumpPackWorkers, f, jobCh, resCh)
|
2016-02-04 18:37:48 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for name := range repo.Backend().List(backend.Data, done) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
entries := job.Result.([]pack.Blob)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-08-08 15:04:06 +00:00
|
|
|
func (cmd CmdDump) DumpIndexes() error {
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
for id := range cmd.repo.List(backend.Index, done) {
|
|
|
|
fmt.Printf("index_id: %v\n", id)
|
|
|
|
|
2016-02-24 21:41:32 +00:00
|
|
|
idx, err := repository.LoadIndex(cmd.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
|
|
|
|
}
|
|
|
|
|
2015-05-16 12:24:24 +00:00
|
|
|
func (cmd CmdDump) Execute(args []string) error {
|
|
|
|
if len(args) != 1 {
|
2016-08-28 20:19:48 +00:00
|
|
|
return restic.Fatalf("type not specified, Usage: %s", cmd.Usage())
|
2015-05-16 12:24:24 +00:00
|
|
|
}
|
|
|
|
|
2015-06-21 11:02:56 +00:00
|
|
|
repo, err := cmd.global.OpenRepository()
|
2015-05-16 12:24:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-08 15:04:06 +00:00
|
|
|
cmd.repo = repo
|
2015-05-16 12:24:24 +00:00
|
|
|
|
2015-06-27 13:05:20 +00:00
|
|
|
lock, err := lockRepo(repo)
|
|
|
|
defer unlockRepo(lock)
|
2015-06-27 12:40:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-16 12:24:24 +00:00
|
|
|
err = repo.LoadIndex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tpe := args[0]
|
|
|
|
|
|
|
|
switch tpe {
|
2015-08-08 15:04:06 +00:00
|
|
|
case "indexes":
|
|
|
|
return cmd.DumpIndexes()
|
2015-05-16 12:24:24 +00:00
|
|
|
case "snapshots":
|
2016-08-20 15:54:27 +00:00
|
|
|
return debugPrintSnapshots(repo, os.Stdout)
|
2015-05-16 12:24:24 +00:00
|
|
|
case "trees":
|
|
|
|
return printTrees(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
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("\ntrees:\n")
|
|
|
|
|
|
|
|
err = printTrees(repo, os.Stdout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-08 15:04:06 +00:00
|
|
|
fmt.Printf("\nindexes:\n")
|
|
|
|
err = cmd.DumpIndexes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-16 12:24:24 +00:00
|
|
|
return nil
|
|
|
|
default:
|
2016-08-28 20:19:48 +00:00
|
|
|
return restic.Fatalf("no such type %q", tpe)
|
2015-05-16 12:24:24 +00:00
|
|
|
}
|
|
|
|
}
|