2019-04-16 08:06:32 +00:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
2019-07-26 16:35:22 +00:00
|
|
|
"flag"
|
2019-04-16 08:06:32 +00:00
|
|
|
"fmt"
|
2022-07-29 07:17:28 +00:00
|
|
|
"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"
|
2019-12-07 06:20:59 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2019-04-16 16:40:27 +00:00
|
|
|
"time"
|
2019-04-16 08:06:32 +00:00
|
|
|
|
2022-08-17 19:05:07 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2019-12-07 06:20:59 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2019-04-16 08:06:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-06-05 08:30:24 +00:00
|
|
|
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 {
|
2019-04-16 16:40:27 +00:00
|
|
|
return `save all directory and file meta data to a local file for metadata backup.
|
2019-04-16 08:06:32 +00:00
|
|
|
|
2019-07-26 16:35:22 +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
|
|
|
|
2019-04-16 16:40:27 +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 16:40:27 +00:00
|
|
|
|
2019-04-16 08:06:32 +00:00
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2019-06-05 08:30:24 +00:00
|
|
|
func (c *commandFsMetaSave) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
2019-04-16 08:06:32 +00:00
|
|
|
|
2019-07-26 16:35:22 +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")
|
2019-07-26 16:35:22 +00:00
|
|
|
if err = fsMetaSaveCommand.Parse(args); err != nil {
|
2022-11-21 05:05:00 +00:00
|
|
|
return err
|
2019-07-26 16:35:22 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2021-09-13 05:47:52 +00:00
|
|
|
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 05:54:46 +00:00
|
|
|
}
|
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-24 05:54:46 +00:00
|
|
|
|
2020-03-26 09:14:48 +00:00
|
|
|
if err == nil {
|
2021-09-13 05:47:52 +00:00
|
|
|
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
|
|
|
|
|
2020-03-24 05:54:46 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-12-30 04:19:51 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2020-03-24 09:18:13 +00:00
|
|
|
outputChan := make(chan interface{}, 1024)
|
2019-12-30 04:19:51 +00:00
|
|
|
go func() {
|
2020-03-24 07:16:12 +00:00
|
|
|
saveFn(outputChan)
|
2019-12-30 04:19:51 +00:00
|
|
|
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
|
|
|
|
2021-09-01 06:05:56 +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
|
|
|
|
2020-03-24 05:54:46 +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
|
|
|
}
|
|
|
|
|
2020-03-24 05:54:46 +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
|
|
|
|
|
|
|
})
|
|
|
|
|
2019-12-30 04:19:51 +00:00
|
|
|
close(outputChan)
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2020-03-24 09:18:13 +00:00
|
|
|
if err == nil && writer != nil {
|
2019-12-30 04:19:51 +00:00
|
|
|
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
|
|
|
}
|