You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dump.go 1.8 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. }
  24. func runDump(*cli.Context) {
  25. setting.NewConfigContext()
  26. models.LoadModelsConfig()
  27. models.SetEngine()
  28. log.Printf("Dumping local repositories...%s", setting.RepoRootPath)
  29. zip.Verbose = false
  30. defer os.Remove("gogs-repo.zip")
  31. if err := zip.PackTo(setting.RepoRootPath, "gogs-repo.zip", true); err != nil {
  32. log.Fatalf("Fail to dump local repositories: %v", err)
  33. }
  34. log.Printf("Dumping database...")
  35. defer os.Remove("gogs-db.sql")
  36. if err := models.DumpDatabase("gogs-db.sql"); err != nil {
  37. log.Fatalf("Fail to dump database: %v", err)
  38. }
  39. fileName := fmt.Sprintf("gogs-dump-%d.zip", time.Now().Unix())
  40. log.Printf("Packing dump files...")
  41. z, err := zip.Create(fileName)
  42. if err != nil {
  43. os.Remove(fileName)
  44. log.Fatalf("Fail to create %s: %v", fileName, err)
  45. }
  46. workDir, _ := setting.WorkDir()
  47. z.AddFile("gogs-repo.zip", path.Join(workDir, "gogs-repo.zip"))
  48. z.AddFile("gogs-db.sql", path.Join(workDir, "gogs-db.sql"))
  49. z.AddFile("custom/conf/app.ini", path.Join(workDir, "custom/conf/app.ini"))
  50. z.AddDir("log", path.Join(workDir, "log"))
  51. if err = z.Close(); err != nil {
  52. os.Remove(fileName)
  53. log.Fatalf("Fail to save %s: %v", fileName, err)
  54. }
  55. log.Println("Finish dumping!")
  56. }