backup.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. "context"
  7. "fmt"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "strconv"
  12. "time"
  13. "github.com/pkg/errors"
  14. "github.com/unknwon/cae/zip"
  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/database"
  20. "gogs.io/gogs/internal/osutil"
  21. )
  22. var Backup = cli.Command{
  23. Name: "backup",
  24. Usage: "Backup files and database",
  25. Description: `Backup dumps and compresses all related files and database into zip file,
  26. which can be used for migrating Gogs to another server. The output format is meant to be
  27. portable among all supported database engines.`,
  28. Action: runBackup,
  29. Flags: []cli.Flag{
  30. stringFlag("config, c", "", "Custom configuration file path"),
  31. boolFlag("verbose, v", "Show process details"),
  32. stringFlag("tempdir, t", os.TempDir(), "Temporary directory path"),
  33. stringFlag("target", "./", "Target directory path to save backup archive"),
  34. stringFlag("archive-name", fmt.Sprintf("gogs-backup-%s.zip", time.Now().Format("20060102150405")), "Name of backup archive"),
  35. boolFlag("database-only", "Only dump database"),
  36. boolFlag("exclude-mirror-repos", "Exclude mirror repositories"),
  37. boolFlag("exclude-repos", "Exclude repositories"),
  38. },
  39. }
  40. const (
  41. currentBackupFormatVersion = 1
  42. archiveRootDir = "gogs-backup"
  43. )
  44. func runBackup(c *cli.Context) error {
  45. zip.Verbose = c.Bool("verbose")
  46. err := conf.Init(c.String("config"))
  47. if err != nil {
  48. return errors.Wrap(err, "init configuration")
  49. }
  50. conf.InitLogging(true)
  51. conn, err := database.SetEngine()
  52. if err != nil {
  53. return errors.Wrap(err, "set engine")
  54. }
  55. tmpDir := c.String("tempdir")
  56. if !osutil.IsExist(tmpDir) {
  57. log.Fatal("'--tempdir' does not exist: %s", tmpDir)
  58. }
  59. rootDir, err := os.MkdirTemp(tmpDir, "gogs-backup-")
  60. if err != nil {
  61. log.Fatal("Failed to create backup root directory '%s': %v", rootDir, err)
  62. }
  63. log.Info("Backup root directory: %s", rootDir)
  64. // Metadata
  65. metaFile := path.Join(rootDir, "metadata.ini")
  66. metadata := ini.Empty()
  67. metadata.Section("").Key("VERSION").SetValue(strconv.Itoa(currentBackupFormatVersion))
  68. metadata.Section("").Key("DATE_TIME").SetValue(time.Now().String())
  69. metadata.Section("").Key("GOGS_VERSION").SetValue(conf.App.Version)
  70. if err = metadata.SaveTo(metaFile); err != nil {
  71. log.Fatal("Failed to save metadata '%s': %v", metaFile, err)
  72. }
  73. archiveName := filepath.Join(c.String("target"), c.String("archive-name"))
  74. log.Info("Packing backup files to: %s", archiveName)
  75. z, err := zip.Create(archiveName)
  76. if err != nil {
  77. log.Fatal("Failed to create backup archive '%s': %v", archiveName, err)
  78. }
  79. if err = z.AddFile(archiveRootDir+"/metadata.ini", metaFile); err != nil {
  80. log.Fatal("Failed to include 'metadata.ini': %v", err)
  81. }
  82. // Database
  83. dbDir := filepath.Join(rootDir, "db")
  84. if err = database.DumpDatabase(context.Background(), conn, dbDir, c.Bool("verbose")); err != nil {
  85. log.Fatal("Failed to dump database: %v", err)
  86. }
  87. if err = z.AddDir(archiveRootDir+"/db", dbDir); err != nil {
  88. log.Fatal("Failed to include 'db': %v", err)
  89. }
  90. if !c.Bool("database-only") {
  91. // Custom files
  92. err = addCustomDirToBackup(z)
  93. if err != nil {
  94. log.Fatal("Failed to add custom directory to backup: %v", err)
  95. }
  96. // Data files
  97. for _, dir := range []string{"ssh", "attachments", "avatars", "repo-avatars"} {
  98. dirPath := filepath.Join(conf.Server.AppDataPath, dir)
  99. if !osutil.IsDir(dirPath) {
  100. continue
  101. }
  102. if err = z.AddDir(path.Join(archiveRootDir+"/data", dir), dirPath); err != nil {
  103. log.Fatal("Failed to include 'data': %v", err)
  104. }
  105. }
  106. }
  107. // Repositories
  108. if !c.Bool("exclude-repos") && !c.Bool("database-only") {
  109. reposDump := filepath.Join(rootDir, "repositories.zip")
  110. log.Info("Dumping repositories in %q", conf.Repository.Root)
  111. if c.Bool("exclude-mirror-repos") {
  112. repos, err := database.GetNonMirrorRepositories()
  113. if err != nil {
  114. log.Fatal("Failed to get non-mirror repositories: %v", err)
  115. }
  116. reposZip, err := zip.Create(reposDump)
  117. if err != nil {
  118. log.Fatal("Failed to create %q: %v", reposDump, err)
  119. }
  120. baseDir := filepath.Base(conf.Repository.Root)
  121. for _, r := range repos {
  122. name := r.FullName() + ".git"
  123. if err := reposZip.AddDir(filepath.Join(baseDir, name), filepath.Join(conf.Repository.Root, name)); err != nil {
  124. log.Fatal("Failed to add %q: %v", name, err)
  125. }
  126. }
  127. if err = reposZip.Close(); err != nil {
  128. log.Fatal("Failed to save %q: %v", reposDump, err)
  129. }
  130. } else {
  131. if err = zip.PackTo(conf.Repository.Root, reposDump, true); err != nil {
  132. log.Fatal("Failed to dump repositories: %v", err)
  133. }
  134. }
  135. log.Info("Repositories dumped to: %s", reposDump)
  136. if err = z.AddFile(archiveRootDir+"/repositories.zip", reposDump); err != nil {
  137. log.Fatal("Failed to include %q: %v", reposDump, err)
  138. }
  139. }
  140. if err = z.Close(); err != nil {
  141. log.Fatal("Failed to save backup archive '%s': %v", archiveName, err)
  142. }
  143. _ = os.RemoveAll(rootDir)
  144. log.Info("Backup succeed! Archive is located at: %s", archiveName)
  145. log.Stop()
  146. return nil
  147. }
  148. func addCustomDirToBackup(z *zip.ZipArchive) error {
  149. customDir := conf.CustomDir()
  150. entries, err := os.ReadDir(customDir)
  151. if err != nil {
  152. return errors.Wrap(err, "list custom directory entries")
  153. }
  154. for _, e := range entries {
  155. if e.Name() == "data" {
  156. // Skip the "data" directory because it lives under the "custom" directory in
  157. // the Docker setup and will be backed up separately.
  158. log.Trace(`Skipping "data" directory in custom directory`)
  159. continue
  160. }
  161. add := z.AddFile
  162. if e.IsDir() {
  163. add = z.AddDir
  164. }
  165. err = add(
  166. fmt.Sprintf("%s/custom/%s", archiveRootDir, e.Name()),
  167. filepath.Join(customDir, e.Name()),
  168. )
  169. if err != nil {
  170. return errors.Wrapf(err, "add %q", e.Name())
  171. }
  172. }
  173. return nil
  174. }