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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. "os"
  10. "path"
  11. "path/filepath"
  12. "time"
  13. "code.gitea.io/gitea/models"
  14. "code.gitea.io/gitea/modules/log"
  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. cli.BoolFlag{
  51. Name: "skip-log, L",
  52. Usage: "Skip the log dumping",
  53. },
  54. },
  55. }
  56. func fatal(format string, args ...interface{}) {
  57. fmt.Fprintf(os.Stderr, format+"\n", args...)
  58. log.Fatal(format, args...)
  59. }
  60. func runDump(ctx *cli.Context) error {
  61. setting.NewContext()
  62. setting.NewServices() // cannot access session settings otherwise
  63. err := models.SetEngine()
  64. if err != nil {
  65. return err
  66. }
  67. tmpDir := ctx.String("tempdir")
  68. if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
  69. fatal("Path does not exist: %s", tmpDir)
  70. }
  71. tmpWorkDir, err := ioutil.TempDir(tmpDir, "gitea-dump-")
  72. if err != nil {
  73. fatal("Failed to create tmp work directory: %v", err)
  74. }
  75. log.Info("Creating tmp work dir: %s", tmpWorkDir)
  76. // work-around #1103
  77. if os.Getenv("TMPDIR") == "" {
  78. os.Setenv("TMPDIR", tmpWorkDir)
  79. }
  80. dbDump := path.Join(tmpWorkDir, "gitea-db.sql")
  81. fileName := ctx.String("file")
  82. log.Info("Packing dump files...")
  83. z, err := zip.Create(fileName)
  84. if err != nil {
  85. fatal("Failed to create %s: %v", fileName, err)
  86. }
  87. zip.Verbose = ctx.Bool("verbose")
  88. if ctx.IsSet("skip-repository") && ctx.Bool("skip-repository") {
  89. log.Info("Skip dumping local repositories")
  90. } else {
  91. log.Info("Dumping local repositories...%s", setting.RepoRootPath)
  92. reposDump := path.Join(tmpWorkDir, "gitea-repo.zip")
  93. if err := zip.PackTo(setting.RepoRootPath, reposDump, true); err != nil {
  94. fatal("Failed to dump local repositories: %v", err)
  95. }
  96. if err := z.AddFile("gitea-repo.zip", reposDump); err != nil {
  97. fatal("Failed to include gitea-repo.zip: %v", err)
  98. }
  99. }
  100. targetDBType := ctx.String("database")
  101. if len(targetDBType) > 0 && targetDBType != setting.Database.Type {
  102. log.Info("Dumping database %s => %s...", setting.Database.Type, targetDBType)
  103. } else {
  104. log.Info("Dumping database...")
  105. }
  106. if err := models.DumpDatabase(dbDump, targetDBType); err != nil {
  107. fatal("Failed to dump database: %v", err)
  108. }
  109. if err := z.AddFile("gitea-db.sql", dbDump); err != nil {
  110. fatal("Failed to include gitea-db.sql: %v", err)
  111. }
  112. if len(setting.CustomConf) > 0 {
  113. log.Info("Adding custom configuration file from %s", setting.CustomConf)
  114. if err := z.AddFile("app.ini", setting.CustomConf); err != nil {
  115. fatal("Failed to include specified app.ini: %v", err)
  116. }
  117. }
  118. customDir, err := os.Stat(setting.CustomPath)
  119. if err == nil && customDir.IsDir() {
  120. if err := z.AddDir("custom", setting.CustomPath); err != nil {
  121. fatal("Failed to include custom: %v", err)
  122. }
  123. } else {
  124. log.Info("Custom dir %s doesn't exist, skipped", setting.CustomPath)
  125. }
  126. if com.IsExist(setting.AppDataPath) {
  127. log.Info("Packing data directory...%s", setting.AppDataPath)
  128. var sessionAbsPath string
  129. if setting.SessionConfig.Provider == "file" {
  130. sessionAbsPath = setting.SessionConfig.ProviderConfig
  131. }
  132. if err := zipAddDirectoryExclude(z, "data", setting.AppDataPath, sessionAbsPath); err != nil {
  133. fatal("Failed to include data directory: %v", err)
  134. }
  135. }
  136. // Doesn't check if LogRootPath exists before processing --skip-log intentionally,
  137. // ensuring that it's clear the dump is skipped whether the directory's initialized
  138. // yet or not.
  139. if ctx.IsSet("skip-log") && ctx.Bool("skip-log") {
  140. log.Info("Skip dumping log files")
  141. } else if com.IsExist(setting.LogRootPath) {
  142. if err := z.AddDir("log", setting.LogRootPath); err != nil {
  143. fatal("Failed to include log: %v", err)
  144. }
  145. }
  146. if err = z.Close(); err != nil {
  147. _ = os.Remove(fileName)
  148. fatal("Failed to save %s: %v", fileName, err)
  149. }
  150. if err := os.Chmod(fileName, 0600); err != nil {
  151. log.Info("Can't change file access permissions mask to 0600: %v", err)
  152. }
  153. log.Info("Removing tmp work dir: %s", tmpWorkDir)
  154. if err := os.RemoveAll(tmpWorkDir); err != nil {
  155. fatal("Failed to remove %s: %v", tmpWorkDir, err)
  156. }
  157. log.Info("Finish dumping in file %s", fileName)
  158. return nil
  159. }
  160. // zipAddDirectoryExclude zips absPath to specified zipPath inside z excluding excludeAbsPath
  161. func zipAddDirectoryExclude(zip *zip.ZipArchive, zipPath, absPath string, excludeAbsPath string) error {
  162. absPath, err := filepath.Abs(absPath)
  163. if err != nil {
  164. return err
  165. }
  166. dir, err := os.Open(absPath)
  167. if err != nil {
  168. return err
  169. }
  170. defer dir.Close()
  171. zip.AddEmptyDir(zipPath)
  172. files, err := dir.Readdir(0)
  173. if err != nil {
  174. return err
  175. }
  176. for _, file := range files {
  177. currentAbsPath := path.Join(absPath, file.Name())
  178. currentZipPath := path.Join(zipPath, file.Name())
  179. if file.IsDir() {
  180. if currentAbsPath != excludeAbsPath {
  181. if err = zipAddDirectoryExclude(zip, currentZipPath, currentAbsPath, excludeAbsPath); err != nil {
  182. return err
  183. }
  184. }
  185. } else {
  186. if err = zip.AddFile(currentZipPath, currentAbsPath); err != nil {
  187. return err
  188. }
  189. }
  190. }
  191. return nil
  192. }