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.

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