You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.2 KiB

10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. "io/ioutil"
  8. "log"
  9. "os"
  10. "path"
  11. "time"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/setting"
  14. "github.com/Unknwon/cae/zip"
  15. "github.com/urfave/cli"
  16. )
  17. // CmdDump represents the available dump sub-command.
  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. cli.StringFlag{
  26. Name: "config, c",
  27. Value: "custom/conf/app.ini",
  28. Usage: "Custom configuration file path",
  29. },
  30. cli.BoolFlag{
  31. Name: "verbose, v",
  32. Usage: "Show process details",
  33. },
  34. cli.StringFlag{
  35. Name: "tempdir, t",
  36. Value: os.TempDir(),
  37. Usage: "Temporary dir path",
  38. },
  39. },
  40. }
  41. func runDump(ctx *cli.Context) error {
  42. if ctx.IsSet("config") {
  43. setting.CustomConf = ctx.String("config")
  44. }
  45. setting.NewContext()
  46. models.LoadConfigs()
  47. models.SetEngine()
  48. tmpDir := ctx.String("tempdir")
  49. if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
  50. log.Fatalf("Path does not exist: %s", tmpDir)
  51. }
  52. TmpWorkDir, err := ioutil.TempDir(tmpDir, "gogs-dump-")
  53. if err != nil {
  54. log.Fatalf("Fail to create tmp work directory: %v", err)
  55. }
  56. log.Printf("Creating tmp work dir: %s", TmpWorkDir)
  57. reposDump := path.Join(TmpWorkDir, "gogs-repo.zip")
  58. dbDump := path.Join(TmpWorkDir, "gogs-db.sql")
  59. log.Printf("Dumping local repositories...%s", setting.RepoRootPath)
  60. zip.Verbose = ctx.Bool("verbose")
  61. if err := zip.PackTo(setting.RepoRootPath, reposDump, true); err != nil {
  62. log.Fatalf("Fail to dump local repositories: %v", err)
  63. }
  64. log.Printf("Dumping database...")
  65. if err := models.DumpDatabase(dbDump); err != nil {
  66. log.Fatalf("Fail to dump database: %v", err)
  67. }
  68. fileName := fmt.Sprintf("gogs-dump-%d.zip", time.Now().Unix())
  69. log.Printf("Packing dump files...")
  70. z, err := zip.Create(fileName)
  71. if err != nil {
  72. log.Fatalf("Fail to create %s: %v", fileName, err)
  73. }
  74. if err := z.AddFile("gogs-repo.zip", reposDump); err != nil {
  75. log.Fatalf("Fail to include gogs-repo.zip: %v", err)
  76. }
  77. if err := z.AddFile("gogs-db.sql", dbDump); err != nil {
  78. log.Fatalf("Fail to include gogs-db.sql: %v", err)
  79. }
  80. customDir, err := os.Stat(setting.CustomPath)
  81. if err == nil && customDir.IsDir() {
  82. if err := z.AddDir("custom", setting.CustomPath); err != nil {
  83. log.Fatalf("Fail to include custom: %v", err)
  84. }
  85. } else {
  86. log.Printf("Custom dir %s doesn't exist, skipped", setting.CustomPath)
  87. }
  88. if err := z.AddDir("log", setting.LogRootPath); err != nil {
  89. log.Fatalf("Fail to include log: %v", err)
  90. }
  91. // FIXME: SSH key file.
  92. if err = z.Close(); err != nil {
  93. _ = os.Remove(fileName)
  94. log.Fatalf("Fail to save %s: %v", fileName, err)
  95. }
  96. if err := os.Chmod(fileName, 0600); err != nil {
  97. log.Printf("Can't change file access permissions mask to 0600: %v", err)
  98. }
  99. log.Printf("Removing tmp work dir: %s", TmpWorkDir)
  100. if err := os.RemoveAll(TmpWorkDir); err != nil {
  101. log.Fatalf("Fail to remove %s: %v", TmpWorkDir, err)
  102. }
  103. log.Printf("Finish dumping in file %s", fileName)
  104. return nil
  105. }