Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/restore: restore files in trash #3657

Merged
merged 9 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func Main(args []string) error {
cmdMdtest(),
cmdWarmup(),
cmdRmr(),
cmdRecover(),
cmdSync(),
cmdDebug(),
cmdClone(),
Expand Down
123 changes: 123 additions & 0 deletions cmd/recover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package cmd

import (
"bytes"
"math/rand"
"strconv"
"sync"
"syscall"

"github.com/juicedata/juicefs/pkg/meta"
"github.com/juicedata/juicefs/pkg/utils"
"github.com/urfave/cli/v2"
)

func cmdRecover() *cli.Command {
return &cli.Command{
Name: "recover",
Action: recover,
Category: "ADMIN",
Usage: "recover files from trash",
ArgsUsage: "META HOUR ...",
Description: `
Rebuild the tree structure for trash files, and move them back to original directories.

Examples:
$ juicefs recover redis://localhost/1 2023-10-10-01`,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "move-to-original",
Usage: "move the recovered files into original directory",
},
&cli.IntFlag{
Name: "threads",
Value: 10,
Usage: "number of threads",
},
},
}
}

func recover(ctx *cli.Context) error {
Hexilee marked this conversation as resolved.
Show resolved Hide resolved
setup(ctx, 1)
removePassword(ctx.Args().Get(0))
m := meta.NewClient(ctx.Args().Get(0), &meta.Config{Retries: 10, Strict: true})
for i := 0; i+1 < ctx.NArg(); i++ {
path := ctx.Args().Get(i + 1)
err := doRecover(m, path, ctx.Bool("move-to-original"), ctx.Int("threads"))
if err != 0 {
logger.Errorf("recover %s: %s", path, err)
}
}
return nil
}

func doRecover(m meta.Meta, path string, move bool, threads int) syscall.Errno {
logger.Infof("recover files in %s", path)
ctx := meta.Background
var parent meta.Ino
var attr meta.Attr
err := m.Lookup(ctx, meta.TrashInode, path, &parent, &attr, false)
if err != 0 {
return err
}
var entries []*meta.Entry
err = m.Readdir(meta.Background, parent, 0, &entries)
if err != 0 {
return err
}
entries = entries[2:]
// to avoid conflict
rand.Shuffle(len(entries), func(i, j int) {
entries[i], entries[j] = entries[j], entries[i]
})

var parents = make(map[meta.Ino]bool)
if !move {
for _, e := range entries {
if e.Attr.Typ == meta.TypeDirectory {
parents[e.Inode] = true
}
}
}

todo := make(chan *meta.Entry, 1000)
p := utils.NewProgress(false)
recovered := p.AddCountBar("recovered", int64(len(entries)))
skipped := p.AddCountSpinner("skipped")
failed := p.AddCountSpinner("failed")
var wg sync.WaitGroup
for i := 0; i < threads; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for e := range todo {
ps := bytes.SplitN(e.Name, []byte("-"), 3)
dst, _ := strconv.Atoi(string(ps[0]))
if move || parents[meta.Ino(dst)] {
err = m.Rename(ctx, parent, string(e.Name), meta.Ino(dst), string(ps[2]), 0, nil, nil)
if err != 0 {
logger.Warnf("recover %s: %s", string(e.Name), err)
failed.Increment()
} else {
recovered.Increment()
}
} else {
skipped.Increment()
}
}
}()
}

for _, e := range entries {
todo <- e
}
close(todo)
wg.Wait()
failed.Done()
skipped.Done()
recovered.Done()
p.Done()
logger.Infof("recovered %d files in %s", recovered.Current(), path)
return 0
}