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.

dump.go 5.7KB

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