THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

207 líneas
5.5KB

  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: "file, f",
  30. Value: fmt.Sprintf("gitea-dump-%d.zip", time.Now().Unix()),
  31. Usage: "Name of the dump file which will be created.",
  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. cli.BoolFlag{
  47. Name: "skip-repository, R",
  48. Usage: "Skip the repository dumping",
  49. },
  50. },
  51. }
  52. func runDump(ctx *cli.Context) error {
  53. setting.NewContext()
  54. setting.NewServices() // cannot access session settings otherwise
  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. // work-around #1103
  69. if os.Getenv("TMPDIR") == "" {
  70. os.Setenv("TMPDIR", tmpWorkDir)
  71. }
  72. dbDump := path.Join(tmpWorkDir, "gitea-db.sql")
  73. fileName := ctx.String("file")
  74. log.Printf("Packing dump files...")
  75. z, err := zip.Create(fileName)
  76. if err != nil {
  77. log.Fatalf("Failed to create %s: %v", fileName, err)
  78. }
  79. zip.Verbose = ctx.Bool("verbose")
  80. if ctx.IsSet("skip-repository") {
  81. log.Printf("Skip dumping local repositories")
  82. } else {
  83. log.Printf("Dumping local repositories...%s", setting.RepoRootPath)
  84. reposDump := path.Join(tmpWorkDir, "gitea-repo.zip")
  85. if err := zip.PackTo(setting.RepoRootPath, reposDump, true); err != nil {
  86. log.Fatalf("Failed to dump local repositories: %v", err)
  87. }
  88. if err := z.AddFile("gitea-repo.zip", reposDump); err != nil {
  89. log.Fatalf("Failed to include gitea-repo.zip: %v", err)
  90. }
  91. }
  92. targetDBType := ctx.String("database")
  93. if len(targetDBType) > 0 && targetDBType != setting.Database.Type {
  94. log.Printf("Dumping database %s => %s...", setting.Database.Type, targetDBType)
  95. } else {
  96. log.Printf("Dumping database...")
  97. }
  98. if err := models.DumpDatabase(dbDump, targetDBType); err != nil {
  99. log.Fatalf("Failed to dump database: %v", err)
  100. }
  101. if err := z.AddFile("gitea-db.sql", dbDump); err != nil {
  102. log.Fatalf("Failed to include gitea-db.sql: %v", err)
  103. }
  104. if len(setting.CustomConf) > 0 {
  105. log.Printf("Adding custom configuration file from %s", setting.CustomConf)
  106. if err := z.AddFile("app.ini", setting.CustomConf); err != nil {
  107. log.Fatalf("Failed to include specified app.ini: %v", err)
  108. }
  109. }
  110. customDir, err := os.Stat(setting.CustomPath)
  111. if err == nil && customDir.IsDir() {
  112. if err := z.AddDir("custom", setting.CustomPath); err != nil {
  113. log.Fatalf("Failed to include custom: %v", err)
  114. }
  115. } else {
  116. log.Printf("Custom dir %s doesn't exist, skipped", setting.CustomPath)
  117. }
  118. if com.IsExist(setting.AppDataPath) {
  119. log.Printf("Packing data directory...%s", setting.AppDataPath)
  120. var sessionAbsPath string
  121. if setting.SessionConfig.Provider == "file" {
  122. sessionAbsPath = setting.SessionConfig.ProviderConfig
  123. }
  124. if err := zipAddDirectoryExclude(z, "data", setting.AppDataPath, sessionAbsPath); err != nil {
  125. log.Fatalf("Failed to include data directory: %v", err)
  126. }
  127. }
  128. if err := z.AddDir("log", setting.LogRootPath); err != nil {
  129. log.Fatalf("Failed to include log: %v", err)
  130. }
  131. if err = z.Close(); err != nil {
  132. _ = os.Remove(fileName)
  133. log.Fatalf("Failed to save %s: %v", fileName, err)
  134. }
  135. if err := os.Chmod(fileName, 0600); err != nil {
  136. log.Printf("Can't change file access permissions mask to 0600: %v", err)
  137. }
  138. log.Printf("Removing tmp work dir: %s", tmpWorkDir)
  139. if err := os.RemoveAll(tmpWorkDir); err != nil {
  140. log.Fatalf("Failed to remove %s: %v", tmpWorkDir, err)
  141. }
  142. log.Printf("Finish dumping in file %s", fileName)
  143. return nil
  144. }
  145. // zipAddDirectoryExclude zips absPath to specified zipPath inside z excluding excludeAbsPath
  146. func zipAddDirectoryExclude(zip *zip.ZipArchive, zipPath, absPath string, excludeAbsPath string) error {
  147. absPath, err := filepath.Abs(absPath)
  148. if err != nil {
  149. return err
  150. }
  151. dir, err := os.Open(absPath)
  152. if err != nil {
  153. return err
  154. }
  155. defer dir.Close()
  156. zip.AddEmptyDir(zipPath)
  157. files, err := dir.Readdir(0)
  158. if err != nil {
  159. return err
  160. }
  161. for _, file := range files {
  162. currentAbsPath := path.Join(absPath, file.Name())
  163. currentZipPath := path.Join(zipPath, file.Name())
  164. if file.IsDir() {
  165. if currentAbsPath != excludeAbsPath {
  166. if err = zipAddDirectoryExclude(zip, currentZipPath, currentAbsPath, excludeAbsPath); err != nil {
  167. return err
  168. }
  169. }
  170. } else {
  171. if err = zip.AddFile(currentZipPath, currentAbsPath); err != nil {
  172. return err
  173. }
  174. }
  175. }
  176. return nil
  177. }