2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-16 01:57:10 +00:00
restic/cmd/restic/cmd_diff.go

435 lines
11 KiB
Go
Raw Normal View History

2017-11-15 21:00:41 +00:00
package main
import (
"context"
2022-01-23 22:12:29 +00:00
"encoding/json"
2017-11-15 21:00:41 +00:00
"path"
"reflect"
"sort"
2021-11-07 20:26:05 +00:00
"github.com/restic/restic/internal/backend"
2017-11-15 21:00:41 +00:00
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/ui"
2017-11-15 21:00:41 +00:00
"github.com/spf13/cobra"
)
var cmdDiff = &cobra.Command{
Use: "diff [flags] snapshot-ID snapshot-ID",
2017-11-15 21:00:41 +00:00
Short: "Show differences between two snapshots",
Long: `
The "diff" command shows differences from the first to the second snapshot. The
first characters in each line display what has happened to a particular file or
directory:
* + The item was added
* - The item was removed
* U The metadata (access mode, timestamps, ...) for the item was updated
* M The file's content was modified
* T The type was changed, e.g. a file was made a symlink
EXIT STATUS
===========
Exit status is 0 if the command was successful, and non-zero if there was any error.
2017-11-15 21:00:41 +00:00
`,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
2022-10-02 21:24:37 +00:00
return runDiff(cmd.Context(), diffOptions, globalOptions, args)
2017-11-15 21:00:41 +00:00
},
}
// DiffOptions collects all options for the diff command.
type DiffOptions struct {
ShowMetadata bool
}
var diffOptions DiffOptions
func init() {
cmdRoot.AddCommand(cmdDiff)
f := cmdDiff.Flags()
f.BoolVar(&diffOptions.ShowMetadata, "metadata", false, "print changes in metadata")
}
2021-11-07 20:26:05 +00:00
func loadSnapshot(ctx context.Context, be restic.Lister, repo restic.Repository, desc string) (*restic.Snapshot, error) {
sn, err := restic.FindSnapshot(ctx, be, repo, desc)
2017-11-15 21:00:41 +00:00
if err != nil {
return nil, errors.Fatal(err.Error())
2017-11-15 21:00:41 +00:00
}
return sn, err
2017-11-15 21:00:41 +00:00
}
// Comparer collects all things needed to compare two snapshots.
type Comparer struct {
2022-01-23 22:12:29 +00:00
repo restic.Repository
opts DiffOptions
printChange func(change *Change)
}
type Change struct {
MessageType string `json:"message_type"` // "change"
Path string `json:"path"`
Modifier string `json:"modifier"`
}
func NewChange(path string, mode string) *Change {
return &Change{MessageType: "change", Path: path, Modifier: mode}
2017-11-15 21:00:41 +00:00
}
2017-12-05 20:33:25 +00:00
// DiffStat collects stats for all types of items.
type DiffStat struct {
2022-01-23 22:12:29 +00:00
Files int `json:"files"`
Dirs int `json:"dirs"`
Others int `json:"others"`
DataBlobs int `json:"data_blobs"`
TreeBlobs int `json:"tree_blobs"`
Bytes uint64 `json:"bytes"`
2017-11-15 21:00:41 +00:00
}
2017-12-05 20:33:25 +00:00
// Add adds stats information for node to s.
func (s *DiffStat) Add(node *restic.Node) {
2017-11-15 21:00:41 +00:00
if node == nil {
return
}
switch node.Type {
case "file":
2017-12-05 20:33:25 +00:00
s.Files++
2017-11-15 21:00:41 +00:00
case "dir":
2017-12-05 20:33:25 +00:00
s.Dirs++
default:
s.Others++
2017-11-15 21:00:41 +00:00
}
}
2017-12-05 20:33:25 +00:00
// addBlobs adds the blobs of node to s.
func addBlobs(bs restic.BlobSet, node *restic.Node) {
2017-11-15 21:00:41 +00:00
if node == nil {
return
}
switch node.Type {
case "file":
for _, blob := range node.Content {
h := restic.BlobHandle{
ID: blob,
Type: restic.DataBlob,
}
2017-12-05 20:33:25 +00:00
bs.Insert(h)
2017-11-15 21:00:41 +00:00
}
case "dir":
h := restic.BlobHandle{
ID: *node.Subtree,
Type: restic.TreeBlob,
}
2017-12-05 20:33:25 +00:00
bs.Insert(h)
2017-11-15 21:00:41 +00:00
}
}
2022-01-23 22:12:29 +00:00
type DiffStatsContainer struct {
MessageType string `json:"message_type"` // "statistics"
SourceSnapshot string `json:"source_snapshot"`
TargetSnapshot string `json:"target_snapshot"`
ChangedFiles int `json:"changed_files"`
Added DiffStat `json:"added"`
Removed DiffStat `json:"removed"`
BlobsBefore, BlobsAfter, BlobsCommon restic.BlobSet `json:"-"`
2017-12-05 20:33:25 +00:00
}
2017-11-15 21:00:41 +00:00
2017-12-05 20:33:25 +00:00
// updateBlobs updates the blob counters in the stats struct.
func updateBlobs(repo restic.Repository, blobs restic.BlobSet, stats *DiffStat) {
for h := range blobs {
2017-11-15 21:00:41 +00:00
switch h.Type {
case restic.DataBlob:
2017-12-05 20:33:25 +00:00
stats.DataBlobs++
2017-11-15 21:00:41 +00:00
case restic.TreeBlob:
2017-12-05 20:33:25 +00:00
stats.TreeBlobs++
2017-11-15 21:00:41 +00:00
}
size, found := repo.LookupBlobSize(h.ID, h.Type)
if !found {
Warnf("unable to find blob size for %v\n", h)
2017-11-15 21:00:41 +00:00
continue
}
stats.Bytes += uint64(size)
2017-11-15 21:00:41 +00:00
}
}
2017-12-05 20:33:25 +00:00
func (c *Comparer) printDir(ctx context.Context, mode string, stats *DiffStat, blobs restic.BlobSet, prefix string, id restic.ID) error {
debug.Log("print %v tree %v", mode, id)
tree, err := restic.LoadTree(ctx, c.repo, id)
2017-11-15 21:00:41 +00:00
if err != nil {
return err
}
2017-12-05 20:33:25 +00:00
for _, node := range tree.Nodes {
name := path.Join(prefix, node.Name)
if node.Type == "dir" {
name += "/"
}
c.printChange(NewChange(name, mode))
2017-12-05 20:33:25 +00:00
stats.Add(node)
addBlobs(blobs, node)
if node.Type == "dir" {
err := c.printDir(ctx, mode, stats, blobs, name, *node.Subtree)
if err != nil {
Warnf("error: %v\n", err)
}
}
2017-11-15 21:00:41 +00:00
}
2017-12-05 20:33:25 +00:00
return nil
}
func (c *Comparer) collectDir(ctx context.Context, blobs restic.BlobSet, id restic.ID) error {
debug.Log("print tree %v", id)
tree, err := restic.LoadTree(ctx, c.repo, id)
if err != nil {
return err
}
for _, node := range tree.Nodes {
addBlobs(blobs, node)
if node.Type == "dir" {
err := c.collectDir(ctx, blobs, *node.Subtree)
if err != nil {
Warnf("error: %v\n", err)
}
}
}
return nil
}
2017-12-05 20:33:25 +00:00
func uniqueNodeNames(tree1, tree2 *restic.Tree) (tree1Nodes, tree2Nodes map[string]*restic.Node, uniqueNames []string) {
names := make(map[string]struct{})
tree1Nodes = make(map[string]*restic.Node)
2017-11-15 21:00:41 +00:00
for _, node := range tree1.Nodes {
tree1Nodes[node.Name] = node
2017-12-05 20:33:25 +00:00
names[node.Name] = struct{}{}
2017-11-15 21:00:41 +00:00
}
2017-12-05 20:33:25 +00:00
tree2Nodes = make(map[string]*restic.Node)
2017-11-15 21:00:41 +00:00
for _, node := range tree2.Nodes {
tree2Nodes[node.Name] = node
2017-12-05 20:33:25 +00:00
names[node.Name] = struct{}{}
2017-11-15 21:00:41 +00:00
}
2017-12-05 20:33:25 +00:00
uniqueNames = make([]string, 0, len(names))
for name := range names {
uniqueNames = append(uniqueNames, name)
2017-11-15 21:00:41 +00:00
}
2020-03-06 22:22:10 +00:00
sort.Strings(uniqueNames)
2017-12-05 20:33:25 +00:00
return tree1Nodes, tree2Nodes, uniqueNames
}
2022-01-23 22:12:29 +00:00
func (c *Comparer) diffTree(ctx context.Context, stats *DiffStatsContainer, prefix string, id1, id2 restic.ID) error {
2017-12-05 20:33:25 +00:00
debug.Log("diffing %v to %v", id1, id2)
tree1, err := restic.LoadTree(ctx, c.repo, id1)
2017-12-05 20:33:25 +00:00
if err != nil {
return err
}
tree2, err := restic.LoadTree(ctx, c.repo, id2)
2017-12-05 20:33:25 +00:00
if err != nil {
return err
}
tree1Nodes, tree2Nodes, names := uniqueNodeNames(tree1, tree2)
2017-11-15 21:00:41 +00:00
for _, name := range names {
node1, t1 := tree1Nodes[name]
node2, t2 := tree2Nodes[name]
2017-12-05 20:33:25 +00:00
addBlobs(stats.BlobsBefore, node1)
addBlobs(stats.BlobsAfter, node2)
2017-11-15 21:00:41 +00:00
switch {
case t1 && t2:
name := path.Join(prefix, name)
mod := ""
if node1.Type != node2.Type {
mod += "T"
}
if node2.Type == "dir" {
name += "/"
}
if node1.Type == "file" &&
node2.Type == "file" &&
!reflect.DeepEqual(node1.Content, node2.Content) {
2017-12-05 21:26:56 +00:00
mod += "M"
2017-12-05 20:33:25 +00:00
stats.ChangedFiles++
2017-11-15 21:00:41 +00:00
} else if c.opts.ShowMetadata && !node1.Equals(*node2) {
2017-12-05 21:26:56 +00:00
mod += "U"
2017-11-15 21:00:41 +00:00
}
if mod != "" {
2022-01-23 22:12:29 +00:00
c.printChange(NewChange(name, mod))
2017-11-15 21:00:41 +00:00
}
if node1.Type == "dir" && node2.Type == "dir" {
var err error
if (*node1.Subtree).Equal(*node2.Subtree) {
err = c.collectDir(ctx, stats.BlobsCommon, *node1.Subtree)
} else {
err = c.diffTree(ctx, stats, name, *node1.Subtree, *node2.Subtree)
}
2017-11-15 21:00:41 +00:00
if err != nil {
Warnf("error: %v\n", err)
}
}
case t1 && !t2:
2017-12-05 20:33:25 +00:00
prefix := path.Join(prefix, name)
if node1.Type == "dir" {
prefix += "/"
}
2022-01-23 22:12:29 +00:00
c.printChange(NewChange(prefix, "-"))
2017-12-05 20:33:25 +00:00
stats.Removed.Add(node1)
if node1.Type == "dir" {
err := c.printDir(ctx, "-", &stats.Removed, stats.BlobsBefore, prefix, *node1.Subtree)
if err != nil {
Warnf("error: %v\n", err)
}
2017-11-15 21:00:41 +00:00
}
case !t1 && t2:
2017-12-05 20:33:25 +00:00
prefix := path.Join(prefix, name)
if node2.Type == "dir" {
prefix += "/"
}
2022-01-23 22:12:29 +00:00
c.printChange(NewChange(prefix, "+"))
2017-12-05 20:33:25 +00:00
stats.Added.Add(node2)
if node2.Type == "dir" {
err := c.printDir(ctx, "+", &stats.Added, stats.BlobsAfter, prefix, *node2.Subtree)
if err != nil {
Warnf("error: %v\n", err)
}
2017-11-15 21:00:41 +00:00
}
}
}
return nil
}
func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []string) error {
2017-11-15 21:00:41 +00:00
if len(args) != 2 {
return errors.Fatalf("specify two snapshot IDs")
}
repo, err := OpenRepository(ctx, gopts)
2017-11-15 21:00:41 +00:00
if err != nil {
return err
}
if !gopts.NoLock {
var lock *restic.Lock
lock, ctx, err = lockRepo(ctx, repo)
2017-11-15 21:00:41 +00:00
defer unlockRepo(lock)
if err != nil {
return err
}
}
2021-11-07 20:26:05 +00:00
// cache snapshots listing
be, err := backend.MemorizeList(ctx, repo.Backend(), restic.SnapshotFile)
if err != nil {
return err
}
sn1, err := loadSnapshot(ctx, be, repo, args[0])
2017-11-15 21:00:41 +00:00
if err != nil {
return err
}
2021-11-07 20:26:05 +00:00
sn2, err := loadSnapshot(ctx, be, repo, args[1])
2017-11-15 21:00:41 +00:00
if err != nil {
return err
}
2022-01-23 22:12:29 +00:00
if !gopts.JSON {
Verbosef("comparing snapshot %v to %v:\n\n", sn1.ID().Str(), sn2.ID().Str())
}
2017-11-15 21:00:41 +00:00
if err = repo.LoadIndex(ctx); err != nil {
return err
}
2017-11-15 21:00:41 +00:00
if sn1.Tree == nil {
return errors.Errorf("snapshot %v has nil tree", sn1.ID().Str())
}
if sn2.Tree == nil {
return errors.Errorf("snapshot %v has nil tree", sn2.ID().Str())
}
c := &Comparer{
repo: repo,
opts: diffOptions,
2022-01-23 22:12:29 +00:00
printChange: func(change *Change) {
Printf("%-5s%v\n", change.Modifier, change.Path)
},
2017-11-15 21:00:41 +00:00
}
2022-01-23 22:12:29 +00:00
if gopts.JSON {
enc := json.NewEncoder(gopts.stdout)
c.printChange = func(change *Change) {
err := enc.Encode(change)
if err != nil {
Warnf("JSON encode failed: %v\n", err)
}
}
}
if gopts.Quiet {
c.printChange = func(change *Change) {}
}
stats := &DiffStatsContainer{
MessageType: "statistics",
SourceSnapshot: args[0],
TargetSnapshot: args[1],
BlobsBefore: restic.NewBlobSet(),
BlobsAfter: restic.NewBlobSet(),
BlobsCommon: restic.NewBlobSet(),
}
2020-11-13 20:11:21 +00:00
stats.BlobsBefore.Insert(restic.BlobHandle{Type: restic.TreeBlob, ID: *sn1.Tree})
stats.BlobsAfter.Insert(restic.BlobHandle{Type: restic.TreeBlob, ID: *sn2.Tree})
2017-11-15 21:00:41 +00:00
err = c.diffTree(ctx, stats, "/", *sn1.Tree, *sn2.Tree)
if err != nil {
return err
}
2017-12-05 20:33:25 +00:00
both := stats.BlobsBefore.Intersect(stats.BlobsAfter)
updateBlobs(repo, stats.BlobsBefore.Sub(both).Sub(stats.BlobsCommon), &stats.Removed)
updateBlobs(repo, stats.BlobsAfter.Sub(both).Sub(stats.BlobsCommon), &stats.Added)
2017-11-15 21:00:41 +00:00
2022-01-23 22:12:29 +00:00
if gopts.JSON {
err := json.NewEncoder(gopts.stdout).Encode(stats)
if err != nil {
Warnf("JSON encode failed: %v\n", err)
}
} else {
Printf("\n")
Printf("Files: %5d new, %5d removed, %5d changed\n", stats.Added.Files, stats.Removed.Files, stats.ChangedFiles)
Printf("Dirs: %5d new, %5d removed\n", stats.Added.Dirs, stats.Removed.Dirs)
Printf("Others: %5d new, %5d removed\n", stats.Added.Others, stats.Removed.Others)
Printf("Data Blobs: %5d new, %5d removed\n", stats.Added.DataBlobs, stats.Removed.DataBlobs)
Printf("Tree Blobs: %5d new, %5d removed\n", stats.Added.TreeBlobs, stats.Removed.TreeBlobs)
Printf(" Added: %-5s\n", ui.FormatBytes(uint64(stats.Added.Bytes)))
Printf(" Removed: %-5s\n", ui.FormatBytes(uint64(stats.Removed.Bytes)))
2022-01-23 22:12:29 +00:00
}
2017-11-15 21:00:41 +00:00
return nil
}