dump.go 3.3 KB

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