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.2 kB

11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/urfave/cli"
  18. )
  19. // CmdDump represents the available dump sub-command.
  20. var CmdDump = cli.Command{
  21. Name: "dump",
  22. Usage: "Dump Gitea files and database",
  23. Description: `Dump compresses all related files and database into zip file.
  24. It can be used for backup and capture Gitea server image to send to maintainer`,
  25. Action: runDump,
  26. Flags: []cli.Flag{
  27. cli.StringFlag{
  28. Name: "config, c",
  29. Value: "custom/conf/app.ini",
  30. Usage: "Custom configuration file path",
  31. },
  32. cli.BoolFlag{
  33. Name: "verbose, v",
  34. Usage: "Show process details",
  35. },
  36. cli.StringFlag{
  37. Name: "tempdir, t",
  38. Value: os.TempDir(),
  39. Usage: "Temporary dir path",
  40. },
  41. cli.StringFlag{
  42. Name: "database, d",
  43. Usage: "Specify the database SQL syntax",
  44. },
  45. },
  46. }
  47. func runDump(ctx *cli.Context) error {
  48. if ctx.IsSet("config") {
  49. setting.CustomConf = ctx.String("config")
  50. }
  51. setting.NewContext()
  52. setting.NewServices() // cannot access session settings otherwise
  53. models.LoadConfigs()
  54. err := models.SetEngine()
  55. if err != nil {
  56. return err
  57. }
  58. tmpDir := ctx.String("tempdir")
  59. if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
  60. log.Fatalf("Path does not exist: %s", tmpDir)
  61. }
  62. TmpWorkDir, err := ioutil.TempDir(tmpDir, "gitea-dump-")
  63. if err != nil {
  64. log.Fatalf("Fail to create tmp work directory: %v", err)
  65. }
  66. log.Printf("Creating tmp work dir: %s", TmpWorkDir)
  67. reposDump := path.Join(TmpWorkDir, "gitea-repo.zip")
  68. dbDump := path.Join(TmpWorkDir, "gitea-db.sql")
  69. log.Printf("Dumping local repositories...%s", setting.RepoRootPath)
  70. zip.Verbose = ctx.Bool("verbose")
  71. if err := zip.PackTo(setting.RepoRootPath, reposDump, true); err != nil {
  72. log.Fatalf("Fail to dump local repositories: %v", err)
  73. }
  74. targetDBType := ctx.String("database")
  75. if len(targetDBType) > 0 && targetDBType != models.DbCfg.Type {
  76. log.Printf("Dumping database %s => %s...", models.DbCfg.Type, targetDBType)
  77. } else {
  78. log.Printf("Dumping database...")
  79. }
  80. if err := models.DumpDatabase(dbDump, targetDBType); err != nil {
  81. log.Fatalf("Fail to dump database: %v", err)
  82. }
  83. fileName := fmt.Sprintf("gitea-dump-%d.zip", time.Now().Unix())
  84. log.Printf("Packing dump files...")
  85. z, err := zip.Create(fileName)
  86. if err != nil {
  87. log.Fatalf("Fail to create %s: %v", fileName, err)
  88. }
  89. if err := z.AddFile("gitea-repo.zip", reposDump); err != nil {
  90. log.Fatalf("Fail to include gitea-repo.zip: %v", err)
  91. }
  92. if err := z.AddFile("gitea-db.sql", dbDump); err != nil {
  93. log.Fatalf("Fail to include gitea-db.sql: %v", err)
  94. }
  95. customDir, err := os.Stat(setting.CustomPath)
  96. if err == nil && customDir.IsDir() {
  97. if err := z.AddDir("custom", setting.CustomPath); err != nil {
  98. log.Fatalf("Fail to include custom: %v", err)
  99. }
  100. } else {
  101. log.Printf("Custom dir %s doesn't exist, skipped", setting.CustomPath)
  102. }
  103. log.Printf("Packing data directory...%s", setting.AppDataPath)
  104. var sessionAbsPath string
  105. if setting.SessionConfig.Provider == "file" {
  106. if len(setting.SessionConfig.ProviderConfig) == 0 {
  107. setting.SessionConfig.ProviderConfig = "data/sessions"
  108. }
  109. sessionAbsPath, _ = filepath.Abs(setting.SessionConfig.ProviderConfig)
  110. }
  111. if err := zipAddDirectoryExclude(z, "data", setting.AppDataPath, sessionAbsPath); err != nil {
  112. log.Fatalf("Fail to include data directory: %v", err)
  113. }
  114. if err := z.AddDir("log", setting.LogRootPath); err != nil {
  115. log.Fatalf("Fail to include log: %v", err)
  116. }
  117. // FIXME: SSH key file.
  118. if err = z.Close(); err != nil {
  119. _ = os.Remove(fileName)
  120. log.Fatalf("Fail to save %s: %v", fileName, err)
  121. }
  122. if err := os.Chmod(fileName, 0600); err != nil {
  123. log.Printf("Can't change file access permissions mask to 0600: %v", err)
  124. }
  125. log.Printf("Removing tmp work dir: %s", TmpWorkDir)
  126. if err := os.RemoveAll(TmpWorkDir); err != nil {
  127. log.Fatalf("Fail to remove %s: %v", TmpWorkDir, err)
  128. }
  129. log.Printf("Finish dumping in file %s", fileName)
  130. return nil
  131. }
  132. // zipAddDirectoryExclude zips absPath to specified zipPath inside z excluding excludeAbsPath
  133. func zipAddDirectoryExclude(zip *zip.ZipArchive, zipPath, absPath string, excludeAbsPath string) error {
  134. absPath, err := filepath.Abs(absPath)
  135. if err != nil {
  136. return err
  137. }
  138. dir, err := os.Open(absPath)
  139. if err != nil {
  140. return err
  141. }
  142. defer dir.Close()
  143. zip.AddEmptyDir(zipPath)
  144. files, err := dir.Readdir(0)
  145. if err != nil {
  146. return err
  147. }
  148. for _, file := range files {
  149. currentAbsPath := path.Join(absPath, file.Name())
  150. currentZipPath := path.Join(zipPath, file.Name())
  151. if file.IsDir() {
  152. if currentAbsPath != excludeAbsPath {
  153. if err = zipAddDirectoryExclude(zip, currentZipPath, currentAbsPath, excludeAbsPath); err != nil {
  154. return err
  155. }
  156. }
  157. } else {
  158. if err = zip.AddFile(currentZipPath, currentAbsPath); err != nil {
  159. return err
  160. }
  161. }
  162. }
  163. return nil
  164. }