seaweedfs/weed/shell/command_fs_meta_save.go

143 lines
3.8 KiB
Go
Raw Normal View History

2019-04-16 16:06:32 +08:00
package shell
import (
"flag"
2019-04-16 16:06:32 +08:00
"fmt"
"io"
"os"
"sync"
"sync/atomic"
"time"
2019-04-16 16:06:32 +08:00
"github.com/golang/protobuf/proto"
2019-04-16 16:06:32 +08:00
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/util"
)
func init() {
Commands = append(Commands, &commandFsMetaSave{})
2019-04-16 16:06:32 +08:00
}
type commandFsMetaSave struct {
}
func (c *commandFsMetaSave) Name() string {
return "fs.meta.save"
}
func (c *commandFsMetaSave) Help() string {
return `save all directory and file meta data to a local file for metadata backup.
2019-04-16 16:06:32 +08:00
fs.meta.save / # save from the root
fs.meta.save -v -o t.meta / # save from the root, output to t.meta file.
fs.meta.save /path/to/save # save from the directory /path/to/save
fs.meta.save . # save from current directory
fs.meta.save # save from current directory
2019-04-16 16:06:32 +08:00
The meta data will be saved into a local <filer_host>-<port>-<time>.meta file.
These meta data can be later loaded by fs.meta.load command,
2019-04-16 16:06:32 +08:00
`
}
func (c *commandFsMetaSave) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
2019-04-16 16:06:32 +08:00
fsMetaSaveCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
verbose := fsMetaSaveCommand.Bool("v", false, "print out each processed files")
outputFileName := fsMetaSaveCommand.String("o", "", "output the meta data to this file")
2020-03-24 15:08:02 +08:00
// chunksFileName := fsMetaSaveCommand.String("chunks", "", "output all the chunks to this file")
if err = fsMetaSaveCommand.Parse(args); err != nil {
return nil
}
2020-03-24 12:26:15 +08:00
path, parseErr := commandEnv.parseUrl(findInputDirectory(fsMetaSaveCommand.Args()))
2019-12-13 16:22:37 +08:00
if parseErr != nil {
return parseErr
2019-04-16 16:06:32 +08:00
}
2020-03-24 15:09:12 +08:00
fileName := *outputFileName
if fileName == "" {
t := time.Now()
fileName = fmt.Sprintf("%s-%d-%4d%02d%02d-%02d%02d%02d.meta",
commandEnv.option.FilerHost, commandEnv.option.FilerPort, t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
}
2020-03-24 15:08:02 +08:00
2020-03-24 15:09:12 +08:00
dst, openErr := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if openErr != nil {
return fmt.Errorf("failed to create file %s: %v", fileName, openErr)
}
2020-03-24 15:09:12 +08:00
defer dst.Close()
2020-03-26 17:14:48 +08:00
err = doTraverseBfsAndSaving(commandEnv, writer, path, *verbose, func(outputChan chan interface{}) {
2020-03-24 15:09:12 +08:00
sizeBuf := make([]byte, 4)
for item := range outputChan {
b := item.([]byte)
2020-03-24 15:09:12 +08:00
util.Uint32toBytes(sizeBuf, uint32(len(b)))
dst.Write(sizeBuf)
dst.Write(b)
}
}, func(entry *filer_pb.FullEntry, outputChan chan interface{}) (err error) {
2020-03-24 15:09:12 +08:00
bytes, err := proto.Marshal(entry)
if err != nil {
fmt.Fprintf(writer, "marshall error: %v\n", err)
return
}
outputChan <- bytes
return nil
})
2020-03-26 17:14:48 +08:00
if err == nil {
fmt.Fprintf(writer, "meta data for http://%s:%d%s is saved to %s\n", commandEnv.option.FilerHost, commandEnv.option.FilerPort, path, fileName)
}
return err
}
2020-04-22 12:28:47 +08:00
func doTraverseBfsAndSaving(filerClient filer_pb.FilerClient, writer io.Writer, path string, verbose bool, saveFn func(outputChan chan interface{}), genFn func(entry *filer_pb.FullEntry, outputChan chan interface{}) error) error {
2019-04-16 16:06:32 +08:00
var wg sync.WaitGroup
wg.Add(1)
outputChan := make(chan interface{}, 1024)
go func() {
2020-03-24 15:16:12 +08:00
saveFn(outputChan)
wg.Done()
}()
2019-12-13 16:22:37 +08:00
var dirCount, fileCount uint64
2019-04-16 16:06:32 +08:00
2020-04-22 12:28:47 +08:00
err := filer_pb.TraverseBfs(filerClient, util.FullPath(path), func(parentPath util.FullPath, entry *filer_pb.Entry) {
2019-04-16 16:06:32 +08:00
2019-12-13 16:22:37 +08:00
protoMessage := &filer_pb.FullEntry{
Dir: string(parentPath),
Entry: entry,
}
2019-04-16 16:06:32 +08:00
if err := genFn(protoMessage, outputChan); err != nil {
2019-12-13 16:22:37 +08:00
fmt.Fprintf(writer, "marshall error: %v\n", err)
return
}
2019-04-16 16:06:32 +08:00
2019-12-13 16:22:37 +08:00
if entry.IsDirectory {
atomic.AddUint64(&dirCount, 1)
} else {
atomic.AddUint64(&fileCount, 1)
2019-04-16 16:06:32 +08:00
}
if verbose {
2019-12-13 16:22:37 +08:00
println(parentPath.Child(entry.Name))
}
2019-04-16 16:06:32 +08:00
})
close(outputChan)
wg.Wait()
if err == nil && writer != nil {
fmt.Fprintf(writer, "total %d directories, %d files\n", dirCount, fileCount)
2019-12-13 16:22:37 +08:00
}
return err
2020-04-22 12:28:47 +08:00
}