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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package cmd
  6. import (
  7. "fmt"
  8. "io/ioutil"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strings"
  13. "time"
  14. "code.gitea.io/gitea/models"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/modules/storage"
  18. "code.gitea.io/gitea/modules/util"
  19. "gitea.com/go-chi/session"
  20. jsoniter "github.com/json-iterator/go"
  21. archiver "github.com/mholt/archiver/v3"
  22. "github.com/urfave/cli"
  23. )
  24. func addFile(w archiver.Writer, filePath string, absPath string, verbose bool) error {
  25. if verbose {
  26. log.Info("Adding file %s\n", filePath)
  27. }
  28. file, err := os.Open(absPath)
  29. if err != nil {
  30. return err
  31. }
  32. defer file.Close()
  33. fileInfo, err := file.Stat()
  34. if err != nil {
  35. return err
  36. }
  37. return w.Write(archiver.File{
  38. FileInfo: archiver.FileInfo{
  39. FileInfo: fileInfo,
  40. CustomName: filePath,
  41. },
  42. ReadCloser: file,
  43. })
  44. }
  45. func isSubdir(upper string, lower string) (bool, error) {
  46. if relPath, err := filepath.Rel(upper, lower); err != nil {
  47. return false, err
  48. } else if relPath == "." || !strings.HasPrefix(relPath, ".") {
  49. return true, nil
  50. }
  51. return false, nil
  52. }
  53. type outputType struct {
  54. Enum []string
  55. Default string
  56. selected string
  57. }
  58. func (o outputType) Join() string {
  59. return strings.Join(o.Enum, ", ")
  60. }
  61. func (o *outputType) Set(value string) error {
  62. for _, enum := range o.Enum {
  63. if enum == value {
  64. o.selected = value
  65. return nil
  66. }
  67. }
  68. return fmt.Errorf("allowed values are %s", o.Join())
  69. }
  70. func (o outputType) String() string {
  71. if o.selected == "" {
  72. return o.Default
  73. }
  74. return o.selected
  75. }
  76. var outputTypeEnum = &outputType{
  77. Enum: []string{"zip", "tar", "tar.gz", "tar.xz", "tar.bz2"},
  78. Default: "zip",
  79. }
  80. // CmdDump represents the available dump sub-command.
  81. var CmdDump = cli.Command{
  82. Name: "dump",
  83. Usage: "Dump Gitea files and database",
  84. Description: `Dump compresses all related files and database into zip file.
  85. It can be used for backup and capture Gitea server image to send to maintainer`,
  86. Action: runDump,
  87. Flags: []cli.Flag{
  88. cli.StringFlag{
  89. Name: "file, f",
  90. Value: fmt.Sprintf("gitea-dump-%d.zip", time.Now().Unix()),
  91. Usage: "Name of the dump file which will be created. Supply '-' for stdout. See type for available types.",
  92. },
  93. cli.BoolFlag{
  94. Name: "verbose, V",
  95. Usage: "Show process details",
  96. },
  97. cli.StringFlag{
  98. Name: "tempdir, t",
  99. Value: os.TempDir(),
  100. Usage: "Temporary dir path",
  101. },
  102. cli.StringFlag{
  103. Name: "database, d",
  104. Usage: "Specify the database SQL syntax",
  105. },
  106. cli.BoolFlag{
  107. Name: "skip-repository, R",
  108. Usage: "Skip the repository dumping",
  109. },
  110. cli.BoolFlag{
  111. Name: "skip-log, L",
  112. Usage: "Skip the log dumping",
  113. },
  114. cli.BoolFlag{
  115. Name: "skip-custom-dir",
  116. Usage: "Skip custom directory",
  117. },
  118. cli.GenericFlag{
  119. Name: "type",
  120. Value: outputTypeEnum,
  121. Usage: fmt.Sprintf("Dump output format: %s", outputTypeEnum.Join()),
  122. },
  123. },
  124. }
  125. func fatal(format string, args ...interface{}) {
  126. fmt.Fprintf(os.Stderr, format+"\n", args...)
  127. log.Fatal(format, args...)
  128. }
  129. func runDump(ctx *cli.Context) error {
  130. var file *os.File
  131. fileName := ctx.String("file")
  132. if fileName == "-" {
  133. file = os.Stdout
  134. err := log.DelLogger("console")
  135. if err != nil {
  136. fatal("Deleting default logger failed. Can not write to stdout: %v", err)
  137. }
  138. }
  139. setting.NewContext()
  140. // make sure we are logging to the console no matter what the configuration tells us do to
  141. if _, err := setting.Cfg.Section("log").NewKey("MODE", "console"); err != nil {
  142. fatal("Setting logging mode to console failed: %v", err)
  143. }
  144. if _, err := setting.Cfg.Section("log.console").NewKey("STDERR", "true"); err != nil {
  145. fatal("Setting console logger to stderr failed: %v", err)
  146. }
  147. if !setting.InstallLock {
  148. log.Error("Is '%s' really the right config path?\n", setting.CustomConf)
  149. return fmt.Errorf("gitea is not initialized")
  150. }
  151. setting.NewServices() // cannot access session settings otherwise
  152. err := models.SetEngine()
  153. if err != nil {
  154. return err
  155. }
  156. if err := storage.Init(); err != nil {
  157. return err
  158. }
  159. if file == nil {
  160. file, err = os.Create(fileName)
  161. if err != nil {
  162. fatal("Unable to open %s: %v", fileName, err)
  163. }
  164. }
  165. defer file.Close()
  166. absFileName, err := filepath.Abs(fileName)
  167. if err != nil {
  168. return err
  169. }
  170. verbose := ctx.Bool("verbose")
  171. outType := ctx.String("type")
  172. var iface interface{}
  173. if fileName == "-" {
  174. iface, err = archiver.ByExtension(fmt.Sprintf(".%s", outType))
  175. } else {
  176. iface, err = archiver.ByExtension(fileName)
  177. }
  178. if err != nil {
  179. fatal("Unable to get archiver for extension: %v", err)
  180. }
  181. w, _ := iface.(archiver.Writer)
  182. if err := w.Create(file); err != nil {
  183. fatal("Creating archiver.Writer failed: %v", err)
  184. }
  185. defer w.Close()
  186. if ctx.IsSet("skip-repository") && ctx.Bool("skip-repository") {
  187. log.Info("Skip dumping local repositories")
  188. } else {
  189. log.Info("Dumping local repositories... %s", setting.RepoRootPath)
  190. if err := addRecursiveExclude(w, "repos", setting.RepoRootPath, []string{absFileName}, verbose); err != nil {
  191. fatal("Failed to include repositories: %v", err)
  192. }
  193. if err := storage.LFS.IterateObjects(func(objPath string, object storage.Object) error {
  194. info, err := object.Stat()
  195. if err != nil {
  196. return err
  197. }
  198. return w.Write(archiver.File{
  199. FileInfo: archiver.FileInfo{
  200. FileInfo: info,
  201. CustomName: path.Join("data", "lfs", objPath),
  202. },
  203. ReadCloser: object,
  204. })
  205. }); err != nil {
  206. fatal("Failed to dump LFS objects: %v", err)
  207. }
  208. }
  209. tmpDir := ctx.String("tempdir")
  210. if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
  211. fatal("Path does not exist: %s", tmpDir)
  212. }
  213. dbDump, err := ioutil.TempFile(tmpDir, "gitea-db.sql")
  214. if err != nil {
  215. fatal("Failed to create tmp file: %v", err)
  216. }
  217. defer func() {
  218. if err := util.Remove(dbDump.Name()); err != nil {
  219. log.Warn("Unable to remove temporary file: %s: Error: %v", dbDump.Name(), err)
  220. }
  221. }()
  222. targetDBType := ctx.String("database")
  223. if len(targetDBType) > 0 && targetDBType != setting.Database.Type {
  224. log.Info("Dumping database %s => %s...", setting.Database.Type, targetDBType)
  225. } else {
  226. log.Info("Dumping database...")
  227. }
  228. if err := models.DumpDatabase(dbDump.Name(), targetDBType); err != nil {
  229. fatal("Failed to dump database: %v", err)
  230. }
  231. if err := addFile(w, "gitea-db.sql", dbDump.Name(), verbose); err != nil {
  232. fatal("Failed to include gitea-db.sql: %v", err)
  233. }
  234. if len(setting.CustomConf) > 0 {
  235. log.Info("Adding custom configuration file from %s", setting.CustomConf)
  236. if err := addFile(w, "app.ini", setting.CustomConf, verbose); err != nil {
  237. fatal("Failed to include specified app.ini: %v", err)
  238. }
  239. }
  240. if ctx.IsSet("skip-custom-dir") && ctx.Bool("skip-custom-dir") {
  241. log.Info("Skiping custom directory")
  242. } else {
  243. customDir, err := os.Stat(setting.CustomPath)
  244. if err == nil && customDir.IsDir() {
  245. if is, _ := isSubdir(setting.AppDataPath, setting.CustomPath); !is {
  246. if err := addRecursiveExclude(w, "custom", setting.CustomPath, []string{absFileName}, verbose); err != nil {
  247. fatal("Failed to include custom: %v", err)
  248. }
  249. } else {
  250. log.Info("Custom dir %s is inside data dir %s, skipped", setting.CustomPath, setting.AppDataPath)
  251. }
  252. } else {
  253. log.Info("Custom dir %s doesn't exist, skipped", setting.CustomPath)
  254. }
  255. }
  256. isExist, err := util.IsExist(setting.AppDataPath)
  257. if err != nil {
  258. log.Error("Unable to check if %s exists. Error: %v", setting.AppDataPath, err)
  259. }
  260. if isExist {
  261. log.Info("Packing data directory...%s", setting.AppDataPath)
  262. var excludes []string
  263. if setting.Cfg.Section("session").Key("PROVIDER").Value() == "file" {
  264. var opts session.Options
  265. json := jsoniter.ConfigCompatibleWithStandardLibrary
  266. if err = json.Unmarshal([]byte(setting.SessionConfig.ProviderConfig), &opts); err != nil {
  267. return err
  268. }
  269. excludes = append(excludes, opts.ProviderConfig)
  270. }
  271. excludes = append(excludes, setting.RepoRootPath)
  272. excludes = append(excludes, setting.LFS.Path)
  273. excludes = append(excludes, setting.Attachment.Path)
  274. excludes = append(excludes, setting.LogRootPath)
  275. excludes = append(excludes, absFileName)
  276. if err := addRecursiveExclude(w, "data", setting.AppDataPath, excludes, verbose); err != nil {
  277. fatal("Failed to include data directory: %v", err)
  278. }
  279. }
  280. if err := storage.Attachments.IterateObjects(func(objPath string, object storage.Object) error {
  281. info, err := object.Stat()
  282. if err != nil {
  283. return err
  284. }
  285. return w.Write(archiver.File{
  286. FileInfo: archiver.FileInfo{
  287. FileInfo: info,
  288. CustomName: path.Join("data", "attachments", objPath),
  289. },
  290. ReadCloser: object,
  291. })
  292. }); err != nil {
  293. fatal("Failed to dump attachments: %v", err)
  294. }
  295. // Doesn't check if LogRootPath exists before processing --skip-log intentionally,
  296. // ensuring that it's clear the dump is skipped whether the directory's initialized
  297. // yet or not.
  298. if ctx.IsSet("skip-log") && ctx.Bool("skip-log") {
  299. log.Info("Skip dumping log files")
  300. } else {
  301. isExist, err := util.IsExist(setting.LogRootPath)
  302. if err != nil {
  303. log.Error("Unable to check if %s exists. Error: %v", setting.LogRootPath, err)
  304. }
  305. if isExist {
  306. if err := addRecursiveExclude(w, "log", setting.LogRootPath, []string{absFileName}, verbose); err != nil {
  307. fatal("Failed to include log: %v", err)
  308. }
  309. }
  310. }
  311. if fileName != "-" {
  312. if err = w.Close(); err != nil {
  313. _ = util.Remove(fileName)
  314. fatal("Failed to save %s: %v", fileName, err)
  315. }
  316. if err := os.Chmod(fileName, 0600); err != nil {
  317. log.Info("Can't change file access permissions mask to 0600: %v", err)
  318. }
  319. }
  320. if fileName != "-" {
  321. log.Info("Finish dumping in file %s", fileName)
  322. } else {
  323. log.Info("Finish dumping to stdout")
  324. }
  325. return nil
  326. }
  327. func contains(slice []string, s string) bool {
  328. for _, v := range slice {
  329. if v == s {
  330. return true
  331. }
  332. }
  333. return false
  334. }
  335. // addRecursiveExclude zips absPath to specified insidePath inside writer excluding excludeAbsPath
  336. func addRecursiveExclude(w archiver.Writer, insidePath, absPath string, excludeAbsPath []string, verbose bool) error {
  337. absPath, err := filepath.Abs(absPath)
  338. if err != nil {
  339. return err
  340. }
  341. dir, err := os.Open(absPath)
  342. if err != nil {
  343. return err
  344. }
  345. defer dir.Close()
  346. files, err := dir.Readdir(0)
  347. if err != nil {
  348. return err
  349. }
  350. for _, file := range files {
  351. currentAbsPath := path.Join(absPath, file.Name())
  352. currentInsidePath := path.Join(insidePath, file.Name())
  353. if file.IsDir() {
  354. if !contains(excludeAbsPath, currentAbsPath) {
  355. if err := addFile(w, currentInsidePath, currentAbsPath, false); err != nil {
  356. return err
  357. }
  358. if err = addRecursiveExclude(w, currentInsidePath, currentAbsPath, excludeAbsPath, verbose); err != nil {
  359. return err
  360. }
  361. }
  362. } else {
  363. if err = addFile(w, currentInsidePath, currentAbsPath, verbose); err != nil {
  364. return err
  365. }
  366. }
  367. }
  368. return nil
  369. }