Skip to content

Commit 5dbe749

Browse files
GiteaBotkemzeb
andauthored
Add --skip-db option to dump command (#30613) (#30630)
Backport #30613 by @kemzeb Attempts to resolve #28720. Co-authored-by: Kemal Zebari <[email protected]>
1 parent fc376c8 commit 5dbe749

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

cmd/dump.go

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ var CmdDump = &cli.Command{
8787
Name: "skip-index",
8888
Usage: "Skip bleve index data",
8989
},
90+
&cli.BoolFlag{
91+
Name: "skip-db",
92+
Usage: "Skip database",
93+
},
9094
&cli.StringFlag{
9195
Name: "type",
9296
Usage: fmt.Sprintf(`Dump output format, default to "zip", supported types: %s`, strings.Join(dump.SupportedOutputTypes, ", ")),
@@ -185,35 +189,41 @@ func runDump(ctx *cli.Context) error {
185189
}
186190
}
187191

188-
tmpDir := ctx.String("tempdir")
189-
if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
190-
fatal("Path does not exist: %s", tmpDir)
191-
}
192+
if ctx.Bool("skip-db") {
193+
// Ensure that we don't dump the database file that may reside in setting.AppDataPath or elsewhere.
194+
dumper.GlobalExcludeAbsPath(setting.Database.Path)
195+
log.Info("Skipping database")
196+
} else {
197+
tmpDir := ctx.String("tempdir")
198+
if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
199+
fatal("Path does not exist: %s", tmpDir)
200+
}
192201

193-
dbDump, err := os.CreateTemp(tmpDir, "gitea-db.sql")
194-
if err != nil {
195-
fatal("Failed to create tmp file: %v", err)
196-
}
197-
defer func() {
198-
_ = dbDump.Close()
199-
if err := util.Remove(dbDump.Name()); err != nil {
200-
log.Warn("Unable to remove temporary file: %s: Error: %v", dbDump.Name(), err)
202+
dbDump, err := os.CreateTemp(tmpDir, "gitea-db.sql")
203+
if err != nil {
204+
fatal("Failed to create tmp file: %v", err)
201205
}
202-
}()
206+
defer func() {
207+
_ = dbDump.Close()
208+
if err := util.Remove(dbDump.Name()); err != nil {
209+
log.Warn("Unable to remove temporary file: %s: Error: %v", dbDump.Name(), err)
210+
}
211+
}()
203212

204-
targetDBType := ctx.String("database")
205-
if len(targetDBType) > 0 && targetDBType != setting.Database.Type.String() {
206-
log.Info("Dumping database %s => %s...", setting.Database.Type, targetDBType)
207-
} else {
208-
log.Info("Dumping database...")
209-
}
213+
targetDBType := ctx.String("database")
214+
if len(targetDBType) > 0 && targetDBType != setting.Database.Type.String() {
215+
log.Info("Dumping database %s => %s...", setting.Database.Type, targetDBType)
216+
} else {
217+
log.Info("Dumping database...")
218+
}
210219

211-
if err := db.DumpDatabase(dbDump.Name(), targetDBType); err != nil {
212-
fatal("Failed to dump database: %v", err)
213-
}
220+
if err := db.DumpDatabase(dbDump.Name(), targetDBType); err != nil {
221+
fatal("Failed to dump database: %v", err)
222+
}
214223

215-
if err = dumper.AddFile("gitea-db.sql", dbDump.Name()); err != nil {
216-
fatal("Failed to include gitea-db.sql: %v", err)
224+
if err = dumper.AddFile("gitea-db.sql", dbDump.Name()); err != nil {
225+
fatal("Failed to include gitea-db.sql: %v", err)
226+
}
217227
}
218228

219229
log.Info("Adding custom configuration file from %s", setting.CustomConf)

0 commit comments

Comments
 (0)