backup.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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-repos", "Exclude repositories"),
  36. },
  37. }
  38. const _CURRENT_BACKUP_FORMAT_VERSION = 1
  39. const _ARCHIVE_ROOT_DIR = "gogs-backup"
  40. func runBackup(c *cli.Context) error {
  41. zip.Verbose = c.Bool("verbose")
  42. err := conf.Init(c.String("config"))
  43. if err != nil {
  44. return errors.Wrap(err, "init configuration")
  45. }
  46. conf.InitLogging(true)
  47. if err = db.SetEngine(); err != nil {
  48. return errors.Wrap(err, "set engine")
  49. }
  50. tmpDir := c.String("tempdir")
  51. if !com.IsExist(tmpDir) {
  52. log.Fatal("'--tempdir' does not exist: %s", tmpDir)
  53. }
  54. rootDir, err := ioutil.TempDir(tmpDir, "gogs-backup-")
  55. if err != nil {
  56. log.Fatal("Failed to create backup root directory '%s': %v", rootDir, err)
  57. }
  58. log.Info("Backup root directory: %s", rootDir)
  59. // Metadata
  60. metaFile := path.Join(rootDir, "metadata.ini")
  61. metadata := ini.Empty()
  62. metadata.Section("").Key("VERSION").SetValue(com.ToStr(_CURRENT_BACKUP_FORMAT_VERSION))
  63. metadata.Section("").Key("DATE_TIME").SetValue(time.Now().String())
  64. metadata.Section("").Key("GOGS_VERSION").SetValue(conf.App.Version)
  65. if err = metadata.SaveTo(metaFile); err != nil {
  66. log.Fatal("Failed to save metadata '%s': %v", metaFile, err)
  67. }
  68. archiveName := filepath.Join(c.String("target"), c.String("archive-name"))
  69. log.Info("Packing backup files to: %s", archiveName)
  70. z, err := zip.Create(archiveName)
  71. if err != nil {
  72. log.Fatal("Failed to create backup archive '%s': %v", archiveName, err)
  73. }
  74. if err = z.AddFile(_ARCHIVE_ROOT_DIR+"/metadata.ini", metaFile); err != nil {
  75. log.Fatal("Failed to include 'metadata.ini': %v", err)
  76. }
  77. // Database
  78. dbDir := filepath.Join(rootDir, "db")
  79. if err = db.DumpDatabase(dbDir); err != nil {
  80. log.Fatal("Failed to dump database: %v", err)
  81. }
  82. if err = z.AddDir(_ARCHIVE_ROOT_DIR+"/db", dbDir); err != nil {
  83. log.Fatal("Failed to include 'db': %v", err)
  84. }
  85. // Custom files
  86. if !c.Bool("database-only") {
  87. if err = z.AddDir(_ARCHIVE_ROOT_DIR+"/custom", conf.CustomDir()); err != nil {
  88. log.Fatal("Failed to include 'custom': %v", err)
  89. }
  90. }
  91. // Data files
  92. if !c.Bool("database-only") {
  93. for _, dir := range []string{"attachments", "avatars", "repo-avatars"} {
  94. dirPath := filepath.Join(conf.Server.AppDataPath, dir)
  95. if !com.IsDir(dirPath) {
  96. continue
  97. }
  98. if err = z.AddDir(path.Join(_ARCHIVE_ROOT_DIR+"/data", dir), dirPath); err != nil {
  99. log.Fatal("Failed to include 'data': %v", err)
  100. }
  101. }
  102. }
  103. // Repositories
  104. if !c.Bool("exclude-repos") && !c.Bool("database-only") {
  105. reposDump := filepath.Join(rootDir, "repositories.zip")
  106. log.Info("Dumping repositories in %q", conf.Repository.Root)
  107. if err = zip.PackTo(conf.Repository.Root, reposDump, true); err != nil {
  108. log.Fatal("Failed to dump repositories: %v", err)
  109. }
  110. log.Info("Repositories dumped to: %s", reposDump)
  111. if err = z.AddFile(_ARCHIVE_ROOT_DIR+"/repositories.zip", reposDump); err != nil {
  112. log.Fatal("Failed to include 'repositories.zip': %v", err)
  113. }
  114. }
  115. if err = z.Close(); err != nil {
  116. log.Fatal("Failed to save backup archive '%s': %v", archiveName, err)
  117. }
  118. os.RemoveAll(rootDir)
  119. log.Info("Backup succeed! Archive is located at: %s", archiveName)
  120. log.Stop()
  121. return nil
  122. }