backup.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2017 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package cmd
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "time"
  12. "github.com/pkg/errors"
  13. "github.com/unknwon/cae/zip"
  14. "github.com/unknwon/com"
  15. "github.com/urfave/cli"
  16. "gopkg.in/ini.v1"
  17. log "unknwon.dev/clog/v2"
  18. "gogs.io/gogs/internal/conf"
  19. "gogs.io/gogs/internal/db"
  20. )
  21. var Backup = cli.Command{
  22. Name: "backup",
  23. Usage: "Backup files and database",
  24. Description: `Backup dumps and compresses all related files and database into zip file,
  25. which can be used for migrating Gogs to another server. The output format is meant to be
  26. portable among all supported database engines.`,
  27. Action: runBackup,
  28. Flags: []cli.Flag{
  29. stringFlag("config, c", "", "Custom configuration file path"),
  30. boolFlag("verbose, v", "Show process details"),
  31. stringFlag("tempdir, t", os.TempDir(), "Temporary directory path"),
  32. stringFlag("target", "./", "Target directory path to save backup archive"),
  33. stringFlag("archive-name", fmt.Sprintf("gogs-backup-%s.zip", time.Now().Format("20060102150405")), "Name of backup archive"),
  34. boolFlag("database-only", "Only dump database"),
  35. boolFlag("exclude-mirror-repos", "Exclude mirror repositories"),
  36. boolFlag("exclude-repos", "Exclude repositories"),
  37. },
  38. }
  39. const _CURRENT_BACKUP_FORMAT_VERSION = 1
  40. const _ARCHIVE_ROOT_DIR = "gogs-backup"
  41. func runBackup(c *cli.Context) error {
  42. zip.Verbose = c.Bool("verbose")
  43. err := conf.Init(c.String("config"))
  44. if err != nil {
  45. return errors.Wrap(err, "init configuration")
  46. }
  47. conf.InitLogging(true)
  48. if err = db.SetEngine(); err != nil {
  49. return errors.Wrap(err, "set engine")
  50. }
  51. tmpDir := c.String("tempdir")
  52. if !com.IsExist(tmpDir) {
  53. log.Fatal("'--tempdir' does not exist: %s", tmpDir)
  54. }
  55. rootDir, err := ioutil.TempDir(tmpDir, "gogs-backup-")
  56. if err != nil {
  57. log.Fatal("Failed to create backup root directory '%s': %v", rootDir, err)
  58. }
  59. log.Info("Backup root directory: %s", rootDir)
  60. // Metadata
  61. metaFile := path.Join(rootDir, "metadata.ini")
  62. metadata := ini.Empty()
  63. metadata.Section("").Key("VERSION").SetValue(com.ToStr(_CURRENT_BACKUP_FORMAT_VERSION))
  64. metadata.Section("").Key("DATE_TIME").SetValue(time.Now().String())
  65. metadata.Section("").Key("GOGS_VERSION").SetValue(conf.App.Version)
  66. if err = metadata.SaveTo(metaFile); err != nil {
  67. log.Fatal("Failed to save metadata '%s': %v", metaFile, err)
  68. }
  69. archiveName := filepath.Join(c.String("target"), c.String("archive-name"))
  70. log.Info("Packing backup files to: %s", archiveName)
  71. z, err := zip.Create(archiveName)
  72. if err != nil {
  73. log.Fatal("Failed to create backup archive '%s': %v", archiveName, err)
  74. }
  75. if err = z.AddFile(_ARCHIVE_ROOT_DIR+"/metadata.ini", metaFile); err != nil {
  76. log.Fatal("Failed to include 'metadata.ini': %v", err)
  77. }
  78. // Database
  79. dbDir := filepath.Join(rootDir, "db")
  80. if err = db.DumpDatabase(dbDir); err != nil {
  81. log.Fatal("Failed to dump database: %v", err)
  82. }
  83. if err = z.AddDir(_ARCHIVE_ROOT_DIR+"/db", dbDir); err != nil {
  84. log.Fatal("Failed to include 'db': %v", err)
  85. }
  86. // Custom files
  87. if !c.Bool("database-only") {
  88. if err = z.AddDir(_ARCHIVE_ROOT_DIR+"/custom", conf.CustomDir()); err != nil {
  89. log.Fatal("Failed to include 'custom': %v", err)
  90. }
  91. }
  92. // Data files
  93. if !c.Bool("database-only") {
  94. for _, dir := range []string{"attachments", "avatars", "repo-avatars"} {
  95. dirPath := filepath.Join(conf.Server.AppDataPath, dir)
  96. if !com.IsDir(dirPath) {
  97. continue
  98. }
  99. if err = z.AddDir(path.Join(_ARCHIVE_ROOT_DIR+"/data", dir), dirPath); err != nil {
  100. log.Fatal("Failed to include 'data': %v", err)
  101. }
  102. }
  103. }
  104. // Repositories
  105. if !c.Bool("exclude-repos") && !c.Bool("database-only") {
  106. reposDump := filepath.Join(rootDir, "repositories.zip")
  107. log.Info("Dumping repositories in %q", conf.Repository.Root)
  108. if c.Bool("exclude-mirror-repos") {
  109. repos, err := db.GetNonMirrorRepositories()
  110. if err != nil {
  111. log.Fatal("Failed to get non-mirror repositories: %v", err)
  112. }
  113. reposZip, err := zip.Create(reposDump)
  114. if err != nil {
  115. log.Fatal("Failed to create %q: %v", reposDump, err)
  116. }
  117. baseDir := filepath.Base(conf.Repository.Root)
  118. for _, r := range repos {
  119. name := r.FullName() + ".git"
  120. if err := reposZip.AddDir(filepath.Join(baseDir, name), filepath.Join(conf.Repository.Root, name)); err != nil {
  121. log.Fatal("Failed to add %q: %v", name, err)
  122. }
  123. }
  124. if err = reposZip.Close(); err != nil {
  125. log.Fatal("Failed to save %q: %v", reposDump, err)
  126. }
  127. } else {
  128. if err = zip.PackTo(conf.Repository.Root, reposDump, true); err != nil {
  129. log.Fatal("Failed to dump repositories: %v", err)
  130. }
  131. }
  132. log.Info("Repositories dumped to: %s", reposDump)
  133. if err = z.AddFile(_ARCHIVE_ROOT_DIR+"/repositories.zip", reposDump); err != nil {
  134. log.Fatal("Failed to include %q: %v", reposDump, err)
  135. }
  136. }
  137. if err = z.Close(); err != nil {
  138. log.Fatal("Failed to save backup archive '%s': %v", archiveName, err)
  139. }
  140. os.RemoveAll(rootDir)
  141. log.Info("Backup succeed! Archive is located at: %s", archiveName)
  142. log.Stop()
  143. return nil
  144. }