Skip to content

Commit 0c0bc03

Browse files
kemzebGiteaBot
andcommitted
Add --skip-db option to dump command (go-gitea#30613)
Attempts to resolve go-gitea#28720. --- Note that I am not a Gitea administrator so I don't normally use the gitea CLI. Just saw this issue and wanted an opportunity to understand how this subcommand works and see if I can add this feature :^) I tested both with `--skip-db` and without and it appears to not add any database-specific files to the generated archive i.e. I don't see a `gitea-db.sql` or `gitea.db` file: ```console $ TAGS="bindata sqlite sqlite_unlock_notify" make backend Running go generate... bindata for migration already up-to-date bindata for options already up-to-date bindata for public already up-to-date bindata for templates already up-to-date $ ./gitea dump --skip-db 2024/04/20 01:16:11 ...s/setting/session.go:77:loadSessionFrom() [I] Session Service Enabled 2024/04/20 01:16:11 ...s/storage/storage.go:176:initAttachments() [I] Initialising Attachment storage with type: local 2024/04/20 01:16:11 ...les/storage/local.go:33:NewLocalStorage() [I] Creating new Local Storage at /workspaces/gitea/data/attachments 2024/04/20 01:16:11 ...s/storage/storage.go:166:initAvatars() [I] Initialising Avatar storage with type: local 2024/04/20 01:16:11 ...les/storage/local.go:33:NewLocalStorage() [I] Creating new Local Storage at /workspaces/gitea/data/avatars 2024/04/20 01:16:11 ...s/storage/storage.go:192:initRepoAvatars() [I] Initialising Repository Avatar storage with type: local 2024/04/20 01:16:11 ...les/storage/local.go:33:NewLocalStorage() [I] Creating new Local Storage at /workspaces/gitea/data/repo-avatars 2024/04/20 01:16:11 ...s/storage/storage.go:186:initLFS() [I] Initialising LFS storage with type: local 2024/04/20 01:16:11 ...les/storage/local.go:33:NewLocalStorage() [I] Creating new Local Storage at /workspaces/gitea/data/lfs 2024/04/20 01:16:11 ...s/storage/storage.go:198:initRepoArchives() [I] Initialising Repository Archive storage with type: local 2024/04/20 01:16:11 ...les/storage/local.go:33:NewLocalStorage() [I] Creating new Local Storage at /workspaces/gitea/data/repo-archive 2024/04/20 01:16:11 ...s/storage/storage.go:208:initPackages() [I] Initialising Packages storage with type: local 2024/04/20 01:16:11 ...les/storage/local.go:33:NewLocalStorage() [I] Creating new Local Storage at /workspaces/gitea/data/packages 2024/04/20 01:16:11 ...s/storage/storage.go:219:initActions() [I] Initialising Actions storage with type: local 2024/04/20 01:16:11 ...les/storage/local.go:33:NewLocalStorage() [I] Creating new Local Storage at /workspaces/gitea/data/actions_log 2024/04/20 01:16:11 ...s/storage/storage.go:223:initActions() [I] Initialising ActionsArtifacts storage with type: local 2024/04/20 01:16:11 ...les/storage/local.go:33:NewLocalStorage() [I] Creating new Local Storage at /workspaces/gitea/data/actions_artifacts 2024/04/20 01:16:11 cmd/dump.go:172:runDump() [I] Dumping local repositories... /workspaces/gitea/data/gitea-repositories 2024/04/20 01:16:11 cmd/dump.go:195:runDump() [I] Skipping database 2024/04/20 01:16:11 cmd/dump.go:229:runDump() [I] Adding custom configuration file from /workspaces/gitea/custom/conf/app.ini 2024/04/20 01:16:11 cmd/dump.go:256:runDump() [I] Packing data directory.../workspaces/gitea/data 2024/04/20 01:16:11 cmd/dump.go:335:runDump() [I] Finish dumping in file /workspaces/gitea/gitea-dump-1713575771.zip $ unzip /workspaces/gitea/gitea-dump-1713575771.zip -d example Archive: /workspaces/gitea/gitea-dump-1713575771.zip . . . $ ls example/ app.ini custom data repos $ ls example/data/ actions_artifacts actions_log avatars home indexers jwt queues repo-archive repo-avatars tmp ``` Co-authored-by: Giteabot <teabot@gitea.io>
1 parent fc376c8 commit 0c0bc03

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

cmd/dump.go

+34-24
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)