seaweedfs/weed/shell/command_fs_meta_save.go

168 lines
4.4 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"
"github.com/seaweedfs/seaweedfs/weed/filer"
2019-04-16 16:06:32 +08:00
"io"
"os"
2021-04-15 06:37:24 +08:00
"path/filepath"
2021-04-16 01:53:04 +08:00
"strings"
"sync"
"sync/atomic"
"time"
2019-04-16 16:06:32 +08:00
2022-08-18 03:05:07 +08:00
"google.golang.org/protobuf/proto"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
2019-04-16 16:06:32 +08:00
)
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.
2021-05-20 14:59:39 +08:00
These meta data can be later loaded by fs.meta.load command
2019-04-16 16:06:32 +08:00
`
}
2024-09-30 01:38:22 +08:00
func (c *commandFsMetaSave) HasTag(CommandTag) bool {
return false
}
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")
2021-04-15 06:37:24 +08:00
isObfuscate := fsMetaSaveCommand.Bool("obfuscate", false, "obfuscate the file names")
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 err
}
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-%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 15:09:12 +08:00
}
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()
2021-04-15 06:37:24 +08:00
var cipherKey util.CipherKey
if *isObfuscate {
cipherKey = util.GenCipherKey()
}
2022-01-22 04:08:58 +08:00
err = doTraverseBfsAndSaving(commandEnv, writer, path, *verbose, func(entry *filer_pb.FullEntry, outputChan chan interface{}) (err error) {
2021-04-15 06:37:24 +08: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-16 01:53:04 +08:00
entry.Entry.Name = strings.ReplaceAll(entry.Entry.Name, "/", "x")
2021-04-15 06:37:24 +08:00
}
}
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
2022-01-22 04:08:58 +08: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 15:09:12 +08:00
})
2020-03-26 17:14:48 +08: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 17:14:48 +08:00
}
return err
}
2022-01-22 04:08:58 +08: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 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
if strings.HasPrefix(string(parentPath), filer.SystemLogDir) {
return
}
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-30 04:26:02 +08:00
}