Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed errors logging in dump.go #9218

Merged
merged 6 commits into from
Dec 17, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ package cmd
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/log"

"github.com/unknwon/cae/zip"
"github.com/unknwon/com"
Expand Down Expand Up @@ -55,6 +55,12 @@ It can be used for backup and capture Gitea server image to send to maintainer`,
},
}

func fatal(format string, args ...interface{}) {
lafriks marked this conversation as resolved.
Show resolved Hide resolved
logMessage := fmt.Sprintf(format, args...)
fmt.Fprintln(os.Stderr, logMessage)
log.Fatal(logMessage)
}

func runDump(ctx *cli.Context) error {
setting.NewContext()
setting.NewServices() // cannot access session settings otherwise
Expand All @@ -66,13 +72,13 @@ func runDump(ctx *cli.Context) error {

tmpDir := ctx.String("tempdir")
if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
log.Fatalf("Path does not exist: %s", tmpDir)
fatal("Path does not exist: %s", tmpDir)
}
tmpWorkDir, err := ioutil.TempDir(tmpDir, "gitea-dump-")
if err != nil {
log.Fatalf("Failed to create tmp work directory: %v", err)
fatal("Failed to create tmp work directory: %v", err)
}
log.Printf("Creating tmp work dir: %s", tmpWorkDir)
log.Info("Creating tmp work dir: %s", tmpWorkDir)

// work-around #1103
if os.Getenv("TMPDIR") == "" {
Expand All @@ -82,88 +88,89 @@ func runDump(ctx *cli.Context) error {
dbDump := path.Join(tmpWorkDir, "gitea-db.sql")

fileName := ctx.String("file")
log.Printf("Packing dump files...")
log.Info("Packing dump files...")
z, err := zip.Create(fileName)
if err != nil {
log.Fatalf("Failed to create %s: %v", fileName, err)
fatal("Failed to create %s: %v", fileName, err)
}

zip.Verbose = ctx.Bool("verbose")

if ctx.IsSet("skip-repository") {
log.Printf("Skip dumping local repositories")
log.Info("Skip dumping local repositories")
} else {
log.Printf("Dumping local repositories...%s", setting.RepoRootPath)
log.Info("Dumping local repositories...%s", setting.RepoRootPath)
reposDump := path.Join(tmpWorkDir, "gitea-repo.zip")
if err := zip.PackTo(setting.RepoRootPath, reposDump, true); err != nil {
log.Fatalf("Failed to dump local repositories: %v", err)
log.Fatal("Failed to dump local repositories: %v", err)
}
if err := z.AddFile("gitea-repo.zip", reposDump); err != nil {
log.Fatalf("Failed to include gitea-repo.zip: %v", err)
log.Fatal("Failed to include gitea-repo.zip: %v", err)
}
}

targetDBType := ctx.String("database")
if len(targetDBType) > 0 && targetDBType != setting.Database.Type {
log.Printf("Dumping database %s => %s...", setting.Database.Type, targetDBType)
log.Info("Dumping database %s => %s...", setting.Database.Type, targetDBType)
} else {
log.Printf("Dumping database...")
log.Info("Dumping database...")
}

if err := models.DumpDatabase(dbDump, targetDBType); err != nil {
log.Fatalf("Failed to dump database: %v", err)
log.Fatal("Failed to dump database: %v", err)
}

if err := z.AddFile("gitea-db.sql", dbDump); err != nil {
log.Fatalf("Failed to include gitea-db.sql: %v", err)
log.Fatal("Failed to include gitea-db.sql: %v", err)
}

if len(setting.CustomConf) > 0 {
log.Printf("Adding custom configuration file from %s", setting.CustomConf)
log.Info("Adding custom configuration file from %s", setting.CustomConf)
if err := z.AddFile("app.ini", setting.CustomConf); err != nil {
log.Fatalf("Failed to include specified app.ini: %v", err)
log.Fatal("Failed to include specified app.ini: %v", err)
}
}

customDir, err := os.Stat(setting.CustomPath)
if err == nil && customDir.IsDir() {
if err := z.AddDir("custom", setting.CustomPath); err != nil {
log.Fatalf("Failed to include custom: %v", err)
log.Fatal("Failed to include custom: %v", err)
}
} else {
log.Printf("Custom dir %s doesn't exist, skipped", setting.CustomPath)
log.Info("Custom dir %s doesn't exist, skipped", setting.CustomPath)
}

if com.IsExist(setting.AppDataPath) {
log.Printf("Packing data directory...%s", setting.AppDataPath)
log.Info("Packing data directory...%s", setting.AppDataPath)

var sessionAbsPath string
if setting.SessionConfig.Provider == "file" {
sessionAbsPath = setting.SessionConfig.ProviderConfig
}
if err := zipAddDirectoryExclude(z, "data", setting.AppDataPath, sessionAbsPath); err != nil {
log.Fatalf("Failed to include data directory: %v", err)
log.Fatal("Failed to include data directory: %v", err)
}
}

if err := z.AddDir("log", setting.LogRootPath); err != nil {
log.Fatalf("Failed to include log: %v", err)
log.Fatal("Failed to include log: %v", err)
}

if err = z.Close(); err != nil {
_ = os.Remove(fileName)
log.Fatalf("Failed to save %s: %v", fileName, err)
log.Fatal("Failed to save %s: %v", fileName, err)
}

if err := os.Chmod(fileName, 0600); err != nil {
log.Printf("Can't change file access permissions mask to 0600: %v", err)
log.Info("Can't change file access permissions mask to 0600: %v", err)
}

log.Printf("Removing tmp work dir: %s", tmpWorkDir)
log.Info("Removing tmp work dir: %s", tmpWorkDir)

if err := os.RemoveAll(tmpWorkDir); err != nil {
log.Fatalf("Failed to remove %s: %v", tmpWorkDir, err)
log.Fatal("Failed to remove %s: %v", tmpWorkDir, err)
}
log.Printf("Finish dumping in file %s", fileName)
log.Info("Finish dumping in file %s", fileName)

return nil
}
Expand Down