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.

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