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.

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