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 2.0KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2014 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. "log"
  8. "os"
  9. "path"
  10. "time"
  11. "github.com/Unknwon/cae/zip"
  12. "github.com/codegangsta/cli"
  13. "github.com/gogits/gogs/models"
  14. "github.com/gogits/gogs/modules/setting"
  15. )
  16. var CmdDump = cli.Command{
  17. Name: "dump",
  18. Usage: "Dump Gogs files and database",
  19. Description: `Dump compresses all related files and database into zip file.
  20. It can be used for backup and capture Gogs server image to send to maintainer`,
  21. Action: runDump,
  22. Flags: []cli.Flag{
  23. cli.StringFlag{"config, c", "custom/conf/app.ini", "Custom configuration file path", ""},
  24. cli.BoolFlag{"verbose, v", "show process details", ""},
  25. },
  26. }
  27. func runDump(ctx *cli.Context) {
  28. if ctx.IsSet("config") {
  29. setting.CustomConf = ctx.String("config")
  30. }
  31. setting.NewConfigContext()
  32. models.LoadModelsConfig()
  33. models.SetEngine()
  34. log.Printf("Dumping local repositories...%s", setting.RepoRootPath)
  35. zip.Verbose = ctx.Bool("verbose")
  36. defer os.Remove("gogs-repo.zip")
  37. if err := zip.PackTo(setting.RepoRootPath, "gogs-repo.zip", true); err != nil {
  38. log.Fatalf("Fail to dump local repositories: %v", err)
  39. }
  40. log.Printf("Dumping database...")
  41. defer os.Remove("gogs-db.sql")
  42. if err := models.DumpDatabase("gogs-db.sql"); err != nil {
  43. log.Fatalf("Fail to dump database: %v", err)
  44. }
  45. fileName := fmt.Sprintf("gogs-dump-%d.zip", time.Now().Unix())
  46. log.Printf("Packing dump files...")
  47. z, err := zip.Create(fileName)
  48. if err != nil {
  49. os.Remove(fileName)
  50. log.Fatalf("Fail to create %s: %v", fileName, err)
  51. }
  52. workDir, _ := setting.WorkDir()
  53. z.AddFile("gogs-repo.zip", path.Join(workDir, "gogs-repo.zip"))
  54. z.AddFile("gogs-db.sql", path.Join(workDir, "gogs-db.sql"))
  55. z.AddDir("custom", path.Join(workDir, "custom"))
  56. z.AddDir("log", path.Join(workDir, "log"))
  57. // FIXME: SSH key file.
  58. if err = z.Close(); err != nil {
  59. os.Remove(fileName)
  60. log.Fatalf("Fail to save %s: %v", fileName, err)
  61. }
  62. log.Println("Finish dumping!")
  63. }