dump.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package cmd
  5. import (
  6. "fmt"
  7. "log"
  8. "os"
  9. "path"
  10. "time"
  11. "io/ioutil"
  12. "github.com/Unknwon/cae/zip"
  13. "github.com/urfave/cli"
  14. "github.com/gogits/gogs/models"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. var CmdDump = cli.Command{
  18. Name: "dump",
  19. Usage: "Dump Gogs files and database",
  20. Description: `Dump compresses all related files and database into zip file.
  21. It can be used for backup and capture Gogs server image to send to maintainer`,
  22. Action: runDump,
  23. Flags: []cli.Flag{
  24. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  25. boolFlag("verbose, v", "Show process details"),
  26. stringFlag("tempdir, t", os.TempDir(), "Temporary dir path"),
  27. },
  28. }
  29. func runDump(ctx *cli.Context) error {
  30. if ctx.IsSet("config") {
  31. setting.CustomConf = ctx.String("config")
  32. }
  33. setting.NewContext()
  34. models.LoadConfigs()
  35. models.SetEngine()
  36. tmpDir := ctx.String("tempdir")
  37. if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
  38. log.Fatalf("Path does not exist: %s", tmpDir)
  39. }
  40. TmpWorkDir, err := ioutil.TempDir(tmpDir, "gogs-dump-")
  41. if err != nil {
  42. log.Fatalf("Fail to create tmp work directory: %v", err)
  43. }
  44. log.Printf("Creating tmp work dir: %s", TmpWorkDir)
  45. reposDump := path.Join(TmpWorkDir, "gogs-repo.zip")
  46. dbDump := path.Join(TmpWorkDir, "gogs-db.sql")
  47. log.Printf("Dumping local repositories...%s", setting.RepoRootPath)
  48. zip.Verbose = ctx.Bool("verbose")
  49. if err := zip.PackTo(setting.RepoRootPath, reposDump, true); err != nil {
  50. log.Fatalf("Fail to dump local repositories: %v", err)
  51. }
  52. log.Printf("Dumping database...")
  53. if err := models.DumpDatabase(dbDump); err != nil {
  54. log.Fatalf("Fail to dump database: %v", err)
  55. }
  56. fileName := fmt.Sprintf("gogs-dump-%d.zip", time.Now().Unix())
  57. log.Printf("Packing dump files...")
  58. z, err := zip.Create(fileName)
  59. if err != nil {
  60. os.Remove(fileName)
  61. log.Fatalf("Fail to create %s: %v", fileName, err)
  62. }
  63. if err := z.AddFile("gogs-repo.zip", reposDump); err != nil {
  64. log.Fatalf("Fail to include gogs-repo.zip: %v", err)
  65. }
  66. if err := z.AddFile("gogs-db.sql", dbDump); err != nil {
  67. log.Fatalf("Fail to include gogs-db.sql: %v", err)
  68. }
  69. customDir, err := os.Stat(setting.CustomPath)
  70. if err == nil && customDir.IsDir() {
  71. if err := z.AddDir("custom", setting.CustomPath); err != nil {
  72. log.Fatalf("Fail to include custom: %v", err)
  73. }
  74. } else {
  75. log.Printf("Custom dir %s doesn't exist, skipped", setting.CustomPath)
  76. }
  77. if err := z.AddDir("log", setting.LogRootPath); err != nil {
  78. log.Fatalf("Fail to include log: %v", err)
  79. }
  80. for _, dir := range []string{"attachments", "avatars"} {
  81. if err := z.AddDir(path.Join("data", dir), path.Join(setting.AppDataPath, dir)); err != nil {
  82. log.Fatalf("Fail to include data/%s: %v", dir, err)
  83. }
  84. }
  85. // FIXME: SSH key file.
  86. if err = z.Close(); err != nil {
  87. os.Remove(fileName)
  88. log.Fatalf("Fail to save %s: %v", fileName, err)
  89. }
  90. if err := os.Chmod(fileName, 0600); err != nil {
  91. log.Printf("Can't change file access permissions mask to 0600: %v", err)
  92. }
  93. log.Printf("Removing tmp work dir: %s", TmpWorkDir)
  94. os.RemoveAll(TmpWorkDir)
  95. log.Printf("Finish dumping in file %s", fileName)
  96. return nil
  97. }