backup.go 4.3 KB

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