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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. sessionAbsPath = setting.SessionConfig.ProviderConfig
  113. }
  114. if err := zipAddDirectoryExclude(z, "data", setting.AppDataPath, sessionAbsPath); err != nil {
  115. log.Fatalf("Failed to include data directory: %v", err)
  116. }
  117. }
  118. if err := z.AddDir("log", setting.LogRootPath); err != nil {
  119. log.Fatalf("Failed to include log: %v", err)
  120. }
  121. if err = z.Close(); err != nil {
  122. _ = os.Remove(fileName)
  123. log.Fatalf("Failed to save %s: %v", fileName, err)
  124. }
  125. if err := os.Chmod(fileName, 0600); err != nil {
  126. log.Printf("Can't change file access permissions mask to 0600: %v", err)
  127. }
  128. log.Printf("Removing tmp work dir: %s", tmpWorkDir)
  129. if err := os.RemoveAll(tmpWorkDir); err != nil {
  130. log.Fatalf("Failed to remove %s: %v", tmpWorkDir, err)
  131. }
  132. log.Printf("Finish dumping in file %s", fileName)
  133. return nil
  134. }
  135. // zipAddDirectoryExclude zips absPath to specified zipPath inside z excluding excludeAbsPath
  136. func zipAddDirectoryExclude(zip *zip.ZipArchive, zipPath, absPath string, excludeAbsPath string) error {
  137. absPath, err := filepath.Abs(absPath)
  138. if err != nil {
  139. return err
  140. }
  141. dir, err := os.Open(absPath)
  142. if err != nil {
  143. return err
  144. }
  145. defer dir.Close()
  146. zip.AddEmptyDir(zipPath)
  147. files, err := dir.Readdir(0)
  148. if err != nil {
  149. return err
  150. }
  151. for _, file := range files {
  152. currentAbsPath := path.Join(absPath, file.Name())
  153. currentZipPath := path.Join(zipPath, file.Name())
  154. if file.IsDir() {
  155. if currentAbsPath != excludeAbsPath {
  156. if err = zipAddDirectoryExclude(zip, currentZipPath, currentAbsPath, excludeAbsPath); err != nil {
  157. return err
  158. }
  159. }
  160. } else {
  161. if err = zip.AddFile(currentZipPath, currentAbsPath); err != nil {
  162. return err
  163. }
  164. }
  165. }
  166. return nil
  167. }