You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dump.go 5.7 kB

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