Skip to content

Commit 3477e61

Browse files
zeripath6543
andauthored
Exclude the current dump file from the dump (#14606)
* 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 <art27@cantab.net> * Add skip custom directory option Signed-off-by: Andrew Thornton <art27@cantab.net> * placate lint Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de>
1 parent e65cfab commit 3477e61

File tree

2 files changed

+25
-42
lines changed

2 files changed

+25
-42
lines changed

cmd/dump.go

+24-42
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,6 @@ func addFile(w archiver.Writer, filePath string, absPath string, verbose bool) e
4949
})
5050
}
5151

52-
func addRecursive(w archiver.Writer, dirPath string, absPath string, verbose bool) error {
53-
if verbose {
54-
log.Info("Adding dir %s\n", dirPath)
55-
}
56-
dir, err := os.Open(absPath)
57-
if err != nil {
58-
return fmt.Errorf("Could not open directory %s: %s", absPath, err)
59-
}
60-
defer dir.Close()
61-
62-
files, err := dir.Readdir(0)
63-
if err != nil {
64-
return fmt.Errorf("Unable to list files in %s: %s", absPath, err)
65-
}
66-
67-
if err := addFile(w, dirPath, absPath, false); err != nil {
68-
return err
69-
}
70-
71-
for _, fileInfo := range files {
72-
if fileInfo.IsDir() {
73-
err = addRecursive(w, filepath.Join(dirPath, fileInfo.Name()), filepath.Join(absPath, fileInfo.Name()), verbose)
74-
} else {
75-
err = addFile(w, filepath.Join(dirPath, fileInfo.Name()), filepath.Join(absPath, fileInfo.Name()), verbose)
76-
}
77-
if err != nil {
78-
return err
79-
}
80-
}
81-
return nil
82-
}
83-
8452
func isSubdir(upper string, lower string) (bool, error) {
8553
if relPath, err := filepath.Rel(upper, lower); err != nil {
8654
return false, err
@@ -157,6 +125,10 @@ It can be used for backup and capture Gitea server image to send to maintainer`,
157125
Name: "skip-log, L",
158126
Usage: "Skip the log dumping",
159127
},
128+
cli.BoolFlag{
129+
Name: "skip-custom-dir",
130+
Usage: "Skip custom directory",
131+
},
160132
cli.GenericFlag{
161133
Name: "type",
162134
Value: outputTypeEnum,
@@ -211,6 +183,11 @@ func runDump(ctx *cli.Context) error {
211183
}
212184
defer file.Close()
213185

186+
absFileName, err := filepath.Abs(fileName)
187+
if err != nil {
188+
return err
189+
}
190+
214191
verbose := ctx.Bool("verbose")
215192
outType := ctx.String("type")
216193
var iface interface{}
@@ -233,7 +210,7 @@ func runDump(ctx *cli.Context) error {
233210
log.Info("Skip dumping local repositories")
234211
} else {
235212
log.Info("Dumping local repositories... %s", setting.RepoRootPath)
236-
if err := addRecursive(w, "repos", setting.RepoRootPath, verbose); err != nil {
213+
if err := addRecursiveExclude(w, "repos", setting.RepoRootPath, []string{absFileName}, verbose); err != nil {
237214
fatal("Failed to include repositories: %v", err)
238215
}
239216

@@ -292,17 +269,21 @@ func runDump(ctx *cli.Context) error {
292269
}
293270
}
294271

295-
customDir, err := os.Stat(setting.CustomPath)
296-
if err == nil && customDir.IsDir() {
297-
if is, _ := isSubdir(setting.AppDataPath, setting.CustomPath); !is {
298-
if err := addRecursive(w, "custom", setting.CustomPath, verbose); err != nil {
299-
fatal("Failed to include custom: %v", err)
272+
if ctx.IsSet("skip-custom-dir") && ctx.Bool("skip-custom-dir") {
273+
log.Info("Skiping custom directory")
274+
} else {
275+
customDir, err := os.Stat(setting.CustomPath)
276+
if err == nil && customDir.IsDir() {
277+
if is, _ := isSubdir(setting.AppDataPath, setting.CustomPath); !is {
278+
if err := addRecursiveExclude(w, "custom", setting.CustomPath, []string{absFileName}, verbose); err != nil {
279+
fatal("Failed to include custom: %v", err)
280+
}
281+
} else {
282+
log.Info("Custom dir %s is inside data dir %s, skipped", setting.CustomPath, setting.AppDataPath)
300283
}
301284
} else {
302-
log.Info("Custom dir %s is inside data dir %s, skipped", setting.CustomPath, setting.AppDataPath)
285+
log.Info("Custom dir %s doesn't exist, skipped", setting.CustomPath)
303286
}
304-
} else {
305-
log.Info("Custom dir %s doesn't exist, skipped", setting.CustomPath)
306287
}
307288

308289
isExist, err := util.IsExist(setting.AppDataPath)
@@ -325,6 +306,7 @@ func runDump(ctx *cli.Context) error {
325306
excludes = append(excludes, setting.LFS.Path)
326307
excludes = append(excludes, setting.Attachment.Path)
327308
excludes = append(excludes, setting.LogRootPath)
309+
excludes = append(excludes, absFileName)
328310
if err := addRecursiveExclude(w, "data", setting.AppDataPath, excludes, verbose); err != nil {
329311
fatal("Failed to include data directory: %v", err)
330312
}
@@ -358,7 +340,7 @@ func runDump(ctx *cli.Context) error {
358340
log.Error("Unable to check if %s exists. Error: %v", setting.LogRootPath, err)
359341
}
360342
if isExist {
361-
if err := addRecursive(w, "log", setting.LogRootPath, verbose); err != nil {
343+
if err := addRecursiveExclude(w, "log", setting.LogRootPath, []string{absFileName}, verbose); err != nil {
362344
fatal("Failed to include log: %v", err)
363345
}
364346
}

docs/content/doc/usage/command-line.en-us.md

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ in the current directory.
253253
- `--file name`, `-f name`: Name of the dump file with will be created. Optional. (default: gitea-dump-[timestamp].zip).
254254
- `--tempdir path`, `-t path`: Path to the temporary directory used. Optional. (default: /tmp).
255255
- `--skip-repository`, `-R`: Skip the repository dumping. Optional.
256+
- `--skip-custom-dir`: Skip dumping of the custom dir. Optional.
256257
- `--database`, `-d`: Specify the database SQL syntax. Optional.
257258
- `--verbose`, `-V`: If provided, shows additional details. Optional.
258259
- Examples:

0 commit comments

Comments
 (0)