From cdabff700645a79b664033f32bd2c92228fbada3 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 7 Feb 2021 21:55:11 +0000 Subject: [PATCH 1/3] Exclude the current dump file from the dump Always prevent the current file from being added to the dump. Fix #13618 Signed-off-by: Andrew Thornton --- cmd/dump.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/dump.go b/cmd/dump.go index 65e2c817f92b5..97e436ef1965b 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -211,6 +211,11 @@ func runDump(ctx *cli.Context) error { } defer file.Close() + absFileName, err := filepath.Abs(fileName) + if err != nil { + return err + } + verbose := ctx.Bool("verbose") outType := ctx.String("type") var iface interface{} @@ -233,7 +238,7 @@ func runDump(ctx *cli.Context) error { log.Info("Skip dumping local repositories") } else { log.Info("Dumping local repositories... %s", setting.RepoRootPath) - if err := addRecursive(w, "repos", setting.RepoRootPath, verbose); err != nil { + if err := addRecursiveExclude(w, "repos", setting.RepoRootPath, []string{absFileName}, verbose); err != nil { fatal("Failed to include repositories: %v", err) } @@ -295,7 +300,7 @@ func runDump(ctx *cli.Context) error { customDir, err := os.Stat(setting.CustomPath) if err == nil && customDir.IsDir() { if is, _ := isSubdir(setting.AppDataPath, setting.CustomPath); !is { - if err := addRecursive(w, "custom", setting.CustomPath, verbose); err != nil { + if err := addRecursiveExclude(w, "custom", setting.CustomPath, []string{absFileName}, verbose); err != nil { fatal("Failed to include custom: %v", err) } } else { @@ -325,6 +330,7 @@ func runDump(ctx *cli.Context) error { excludes = append(excludes, setting.LFS.Path) excludes = append(excludes, setting.Attachment.Path) excludes = append(excludes, setting.LogRootPath) + excludes = append(excludes, absFileName) if err := addRecursiveExclude(w, "data", setting.AppDataPath, excludes, verbose); err != nil { fatal("Failed to include data directory: %v", err) } @@ -358,7 +364,7 @@ func runDump(ctx *cli.Context) error { log.Error("Unable to check if %s exists. Error: %v", setting.LogRootPath, err) } if isExist { - if err := addRecursive(w, "log", setting.LogRootPath, verbose); err != nil { + if err := addRecursiveExclude(w, "log", setting.LogRootPath, []string{absFileName}, verbose); err != nil { fatal("Failed to include log: %v", err) } } From 8d896d069a4ec36ce714a815246b3c0a00627573 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 7 Feb 2021 22:01:49 +0000 Subject: [PATCH 2/3] Add skip custom directory option Signed-off-by: Andrew Thornton --- cmd/dump.go | 24 +++++++++++++------- docs/content/doc/usage/command-line.en-us.md | 1 + 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/cmd/dump.go b/cmd/dump.go index 97e436ef1965b..060a2c25c046d 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -157,6 +157,10 @@ It can be used for backup and capture Gitea server image to send to maintainer`, Name: "skip-log, L", Usage: "Skip the log dumping", }, + cli.BoolFlag{ + Name: "skip-custom-dir", + Usage: "Skip custom directory", + }, cli.GenericFlag{ Name: "type", Value: outputTypeEnum, @@ -297,17 +301,21 @@ func runDump(ctx *cli.Context) error { } } - customDir, err := os.Stat(setting.CustomPath) - if err == nil && customDir.IsDir() { - if is, _ := isSubdir(setting.AppDataPath, setting.CustomPath); !is { - if err := addRecursiveExclude(w, "custom", setting.CustomPath, []string{absFileName}, verbose); err != nil { - fatal("Failed to include custom: %v", err) + if ctx.IsSet("skip-custom-dir") && ctx.Bool("skip-custom-dir") { + log.Info("Skiping custom directory") + } else { + customDir, err := os.Stat(setting.CustomPath) + if err == nil && customDir.IsDir() { + if is, _ := isSubdir(setting.AppDataPath, setting.CustomPath); !is { + if err := addRecursiveExclude(w, "custom", setting.CustomPath, []string{absFileName}, verbose); err != nil { + fatal("Failed to include custom: %v", err) + } + } else { + log.Info("Custom dir %s is inside data dir %s, skipped", setting.CustomPath, setting.AppDataPath) } } else { - log.Info("Custom dir %s is inside data dir %s, skipped", setting.CustomPath, setting.AppDataPath) + log.Info("Custom dir %s doesn't exist, skipped", setting.CustomPath) } - } else { - log.Info("Custom dir %s doesn't exist, skipped", setting.CustomPath) } isExist, err := util.IsExist(setting.AppDataPath) diff --git a/docs/content/doc/usage/command-line.en-us.md b/docs/content/doc/usage/command-line.en-us.md index 192eaa2c5aa46..97882f9203b7d 100644 --- a/docs/content/doc/usage/command-line.en-us.md +++ b/docs/content/doc/usage/command-line.en-us.md @@ -253,6 +253,7 @@ in the current directory. - `--file name`, `-f name`: Name of the dump file with will be created. Optional. (default: gitea-dump-[timestamp].zip). - `--tempdir path`, `-t path`: Path to the temporary directory used. Optional. (default: /tmp). - `--skip-repository`, `-R`: Skip the repository dumping. Optional. + - `--skip-custom-dir`: Skip dumping of the custom dir. Optional. - `--database`, `-d`: Specify the database SQL syntax. Optional. - `--verbose`, `-V`: If provided, shows additional details. Optional. - Examples: From c4309919a90454e4d0d31623ee556cb723def489 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 7 Feb 2021 22:21:22 +0000 Subject: [PATCH 3/3] placate lint Signed-off-by: Andrew Thornton --- cmd/dump.go | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/cmd/dump.go b/cmd/dump.go index 060a2c25c046d..1acc69f1c8569 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -49,38 +49,6 @@ func addFile(w archiver.Writer, filePath string, absPath string, verbose bool) e }) } -func addRecursive(w archiver.Writer, dirPath string, absPath string, verbose bool) error { - if verbose { - log.Info("Adding dir %s\n", dirPath) - } - dir, err := os.Open(absPath) - if err != nil { - return fmt.Errorf("Could not open directory %s: %s", absPath, err) - } - defer dir.Close() - - files, err := dir.Readdir(0) - if err != nil { - return fmt.Errorf("Unable to list files in %s: %s", absPath, err) - } - - if err := addFile(w, dirPath, absPath, false); err != nil { - return err - } - - for _, fileInfo := range files { - if fileInfo.IsDir() { - err = addRecursive(w, filepath.Join(dirPath, fileInfo.Name()), filepath.Join(absPath, fileInfo.Name()), verbose) - } else { - err = addFile(w, filepath.Join(dirPath, fileInfo.Name()), filepath.Join(absPath, fileInfo.Name()), verbose) - } - if err != nil { - return err - } - } - return nil -} - func isSubdir(upper string, lower string) (bool, error) { if relPath, err := filepath.Rel(upper, lower); err != nil { return false, err