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.

192 lines
5.2 KiB

10 years ago
10 years ago
10 years ago
9 years ago
9 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. "path/filepath"
  13. "time"
  14. "code.gitea.io/gitea/models"
  15. "code.gitea.io/gitea/modules/setting"
  16. "github.com/Unknwon/cae/zip"
  17. "github.com/Unknwon/com"
  18. "github.com/urfave/cli"
  19. )
  20. // CmdDump represents the available dump sub-command.
  21. var CmdDump = cli.Command{
  22. Name: "dump",
  23. Usage: "Dump Gitea files and database",
  24. Description: `Dump compresses all related files and database into zip file.
  25. It can be used for backup and capture Gitea server image to send to maintainer`,
  26. Action: runDump,
  27. Flags: []cli.Flag{
  28. cli.StringFlag{
  29. Name: "config, c",
  30. Value: "custom/conf/app.ini",
  31. Usage: "Custom configuration file path",
  32. },
  33. cli.BoolFlag{
  34. Name: "verbose, v",
  35. Usage: "Show process details",
  36. },
  37. cli.StringFlag{
  38. Name: "tempdir, t",
  39. Value: os.TempDir(),
  40. Usage: "Temporary dir path",
  41. },
  42. cli.StringFlag{
  43. Name: "database, d",
  44. Usage: "Specify the database SQL syntax",
  45. },
  46. },
  47. }
  48. func runDump(ctx *cli.Context) error {
  49. if ctx.IsSet("config") {
  50. setting.CustomConf = ctx.String("config")
  51. }
  52. setting.NewContext()
  53. setting.NewServices() // cannot access session settings otherwise
  54. models.LoadConfigs()
  55. err := models.SetEngine()
  56. if err != nil {
  57. return err
  58. }
  59. tmpDir := ctx.String("tempdir")
  60. if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
  61. log.Fatalf("Path does not exist: %s", tmpDir)
  62. }
  63. TmpWorkDir, err := ioutil.TempDir(tmpDir, "gitea-dump-")
  64. if err != nil {
  65. log.Fatalf("Failed to create tmp work directory: %v", err)
  66. }
  67. log.Printf("Creating tmp work dir: %s", TmpWorkDir)
  68. reposDump := path.Join(TmpWorkDir, "gitea-repo.zip")
  69. dbDump := path.Join(TmpWorkDir, "gitea-db.sql")
  70. log.Printf("Dumping local repositories...%s", setting.RepoRootPath)
  71. zip.Verbose = ctx.Bool("verbose")
  72. if err := zip.PackTo(setting.RepoRootPath, reposDump, true); err != nil {
  73. log.Fatalf("Failed to dump local repositories: %v", err)
  74. }
  75. targetDBType := ctx.String("database")
  76. if len(targetDBType) > 0 && targetDBType != models.DbCfg.Type {
  77. log.Printf("Dumping database %s => %s...", models.DbCfg.Type, targetDBType)
  78. } else {
  79. log.Printf("Dumping database...")
  80. }
  81. if err := models.DumpDatabase(dbDump, targetDBType); err != nil {
  82. log.Fatalf("Failed to dump database: %v", err)
  83. }
  84. fileName := fmt.Sprintf("gitea-dump-%d.zip", time.Now().Unix())
  85. log.Printf("Packing dump files...")
  86. z, err := zip.Create(fileName)
  87. if err != nil {
  88. log.Fatalf("Failed to create %s: %v", fileName, err)
  89. }
  90. if err := z.AddFile("gitea-repo.zip", reposDump); err != nil {
  91. log.Fatalf("Failed to include gitea-repo.zip: %v", err)
  92. }
  93. if err := z.AddFile("gitea-db.sql", dbDump); err != nil {
  94. log.Fatalf("Failed to include gitea-db.sql: %v", err)
  95. }
  96. customDir, err := os.Stat(setting.CustomPath)
  97. if err == nil && customDir.IsDir() {
  98. if err := z.AddDir("custom", setting.CustomPath); err != nil {
  99. log.Fatalf("Failed to include custom: %v", err)
  100. }
  101. } else {
  102. log.Printf("Custom dir %s doesn't exist, skipped", setting.CustomPath)
  103. }
  104. if com.IsExist(setting.AppDataPath) {
  105. log.Printf("Packing data directory...%s", setting.AppDataPath)
  106. var sessionAbsPath string
  107. if setting.SessionConfig.Provider == "file" {
  108. if len(setting.SessionConfig.ProviderConfig) == 0 {
  109. setting.SessionConfig.ProviderConfig = "data/sessions"
  110. }
  111. sessionAbsPath, _ = filepath.Abs(setting.SessionConfig.ProviderConfig)
  112. }
  113. if err := zipAddDirectoryExclude(z, "data", setting.AppDataPath, sessionAbsPath); err != nil {
  114. log.Fatalf("Failed to include data directory: %v", err)
  115. }
  116. }
  117. if err := z.AddDir("log", setting.LogRootPath); err != nil {
  118. log.Fatalf("Failed to include log: %v", err)
  119. }
  120. if err = z.Close(); err != nil {
  121. _ = os.Remove(fileName)
  122. log.Fatalf("Failed to save %s: %v", fileName, err)
  123. }
  124. if err := os.Chmod(fileName, 0600); err != nil {
  125. log.Printf("Can't change file access permissions mask to 0600: %v", err)
  126. }
  127. log.Printf("Removing tmp work dir: %s", TmpWorkDir)
  128. if err := os.RemoveAll(TmpWorkDir); err != nil {
  129. log.Fatalf("Failed to remove %s: %v", TmpWorkDir, err)
  130. }
  131. log.Printf("Finish dumping in file %s", fileName)
  132. return nil
  133. }
  134. // zipAddDirectoryExclude zips absPath to specified zipPath inside z excluding excludeAbsPath
  135. func zipAddDirectoryExclude(zip *zip.ZipArchive, zipPath, absPath string, excludeAbsPath string) error {
  136. absPath, err := filepath.Abs(absPath)
  137. if err != nil {
  138. return err
  139. }
  140. dir, err := os.Open(absPath)
  141. if err != nil {
  142. return err
  143. }
  144. defer dir.Close()
  145. zip.AddEmptyDir(zipPath)
  146. files, err := dir.Readdir(0)
  147. if err != nil {
  148. return err
  149. }
  150. for _, file := range files {
  151. currentAbsPath := path.Join(absPath, file.Name())
  152. currentZipPath := path.Join(zipPath, file.Name())
  153. if file.IsDir() {
  154. if currentAbsPath != excludeAbsPath {
  155. if err = zipAddDirectoryExclude(zip, currentZipPath, currentAbsPath, excludeAbsPath); err != nil {
  156. return err
  157. }
  158. }
  159. } else {
  160. if err = zip.AddFile(currentZipPath, currentAbsPath); err != nil {
  161. return err
  162. }
  163. }
  164. }
  165. return nil
  166. }