seaweedfs/weed/shell/command_fs_meta_save.go

164 lines
4.4 KiB
Go
Raw Normal View History

2019-04-16 08:06:32 +00:00
package shell
import (
"flag"
2019-04-16 08:06:32 +00:00
"fmt"
"github.com/seaweedfs/seaweedfs/weed/filer"
2019-04-16 08:06:32 +00:00
"io"
"os"
2021-04-14 22:37:24 +00:00
"path/filepath"
2021-04-15 17:53:04 +00:00
"strings"
"sync"
"sync/atomic"
"time"
2019-04-16 08:06:32 +00:00
2022-08-17 19:05:07 +00:00
"google.golang.org/protobuf/proto"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
2019-04-16 08:06:32 +00:00
)
func init() {
Commands = append(Commands, &commandFsMetaSave{})
2019-04-16 08:06:32 +00: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 08:06:32 +00: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 08:06:32 +00:00
The meta data will be saved into a local <filer_host>-<port>-<time>.meta file.
2021-05-20 06:59:39 +00:00
These meta data can be later loaded by fs.meta.load command
2019-04-16 08:06:32 +00:00
`
}
func (c *commandFsMetaSave) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
2019-04-16 08:06:32 +00: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")
2021-04-14 22:37:24 +00:00
isObfuscate := fsMetaSaveCommand.Bool("obfuscate", false, "obfuscate the file names")
2020-03-24 07:08:02 +00:00
// chunksFileName := fsMetaSaveCommand.String("chunks", "", "output all the chunks to this file")
if err = fsMetaSaveCommand.Parse(args); err != nil {
return err
}
2020-03-24 04:26:15 +00:00
path, parseErr := commandEnv.parseUrl(findInputDirectory(fsMetaSaveCommand.Args()))
2019-12-13 08:22:37 +00:00
if parseErr != nil {
return parseErr
2019-04-16 08:06:32 +00:00
}
2020-03-24 07:09:12 +00:00
fileName := *outputFileName
if fileName == "" {
t := time.Now()
fileName = fmt.Sprintf("%s-%4d%02d%02d-%02d%02d%02d.meta",
commandEnv.option.FilerAddress.ToHttpAddress(), t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
2020-03-24 07:09:12 +00:00
}
2020-03-24 07:08:02 +00:00
2020-03-24 07:09:12 +00: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 07:09:12 +00:00
defer dst.Close()
2021-04-14 22:37:24 +00:00
var cipherKey util.CipherKey
if *isObfuscate {
cipherKey = util.GenCipherKey()
}
2022-01-21 20:08:58 +00:00
err = doTraverseBfsAndSaving(commandEnv, writer, path, *verbose, func(entry *filer_pb.FullEntry, outputChan chan interface{}) (err error) {
2021-04-14 22:37:24 +00:00
if !entry.Entry.IsDirectory {
ext := filepath.Ext(entry.Entry.Name)
if encrypted, encErr := util.Encrypt([]byte(entry.Entry.Name), cipherKey); encErr == nil {
entry.Entry.Name = util.Base64Encode(encrypted)[:len(entry.Entry.Name)] + ext
2021-04-15 17:53:04 +00:00
entry.Entry.Name = strings.ReplaceAll(entry.Entry.Name, "/", "x")
2021-04-14 22:37:24 +00:00
}
}
2020-03-24 07:09:12 +00:00
bytes, err := proto.Marshal(entry)
if err != nil {
fmt.Fprintf(writer, "marshall error: %v\n", err)
return
}
outputChan <- bytes
return nil
2022-01-21 20:08:58 +00:00
}, func(outputChan chan interface{}) {
sizeBuf := make([]byte, 4)
for item := range outputChan {
b := item.([]byte)
util.Uint32toBytes(sizeBuf, uint32(len(b)))
dst.Write(sizeBuf)
dst.Write(b)
}
2020-03-24 07:09:12 +00:00
})
2020-03-26 09:14:48 +00:00
if err == nil {
fmt.Fprintf(writer, "meta data for http://%s%s is saved to %s\n", commandEnv.option.FilerAddress.ToHttpAddress(), path, fileName)
2020-03-26 09:14:48 +00:00
}
return err
}
2022-01-21 20:08:58 +00:00
func doTraverseBfsAndSaving(filerClient filer_pb.FilerClient, writer io.Writer, path string, verbose bool, genFn func(entry *filer_pb.FullEntry, outputChan chan interface{}) error, saveFn func(outputChan chan interface{})) error {
2019-04-16 08:06:32 +00:00
var wg sync.WaitGroup
wg.Add(1)
outputChan := make(chan interface{}, 1024)
go func() {
2020-03-24 07:16:12 +00:00
saveFn(outputChan)
wg.Done()
}()
2019-12-13 08:22:37 +00:00
var dirCount, fileCount uint64
2019-04-16 08:06:32 +00:00
2020-04-22 04:28:47 +00:00
err := filer_pb.TraverseBfs(filerClient, util.FullPath(path), func(parentPath util.FullPath, entry *filer_pb.Entry) {
2019-04-16 08:06:32 +00:00
if strings.HasPrefix(string(parentPath), filer.SystemLogDir) {
return
}
2019-12-13 08:22:37 +00:00
protoMessage := &filer_pb.FullEntry{
Dir: string(parentPath),
Entry: entry,
}
2019-04-16 08:06:32 +00:00
if err := genFn(protoMessage, outputChan); err != nil {
2019-12-13 08:22:37 +00:00
fmt.Fprintf(writer, "marshall error: %v\n", err)
return
}
2019-04-16 08:06:32 +00:00
2019-12-13 08:22:37 +00:00
if entry.IsDirectory {
atomic.AddUint64(&dirCount, 1)
} else {
atomic.AddUint64(&fileCount, 1)
2019-04-16 08:06:32 +00:00
}
if verbose {
2019-12-13 08:22:37 +00:00
println(parentPath.Child(entry.Name))
}
2019-04-16 08:06:32 +00:00
})
close(outputChan)
wg.Wait()
if err == nil && writer != nil {
fmt.Fprintf(writer, "total %d directories, %d files\n", dirCount, fileCount)
2019-12-13 08:22:37 +00:00
}
return err
2020-04-29 20:26:02 +00:00
}