diff options
author | Manuel Kuhlmann <mkuhlmann@users.noreply.github.com> | 2017-01-12 05:47:20 +0100 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-01-12 12:47:20 +0800 |
commit | e698654902a101e616880a4a6aa0a6f9ca2b2ce7 (patch) | |
tree | d3fd4f58a939d1f9c46634f9be9ea3f06e56691d /cmd | |
parent | b7eae783b5312b01ebd6e23ffebfc49ecfeee071 (diff) | |
download | gitea-e698654902a101e616880a4a6aa0a6f9ca2b2ce7.tar.gz gitea-e698654902a101e616880a4a6aa0a6f9ca2b2ce7.zip |
Add data directory excluding sessions to dump (#587)
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/dump.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/cmd/dump.go b/cmd/dump.go index 5c31ac66de..d0f2be6b4b 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -11,6 +11,7 @@ import ( "log" "os" "path" + "path/filepath" "time" "code.gitea.io/gitea/models" @@ -53,6 +54,7 @@ func runDump(ctx *cli.Context) error { setting.CustomConf = ctx.String("config") } setting.NewContext() + setting.NewServices() // cannot access session settings otherwise models.LoadConfigs() models.SetEngine() @@ -107,6 +109,20 @@ func runDump(ctx *cli.Context) error { } else { log.Printf("Custom dir %s doesn't exist, skipped", setting.CustomPath) } + + log.Printf("Packing data directory...%s", setting.AppDataPath) + var sessionAbsPath string + if setting.SessionConfig.Provider == "file" { + if len(setting.SessionConfig.ProviderConfig) == 0 { + setting.SessionConfig.ProviderConfig = "data/sessions" + } + sessionAbsPath, _ = filepath.Abs(setting.SessionConfig.ProviderConfig) + } + + if err := zipAddDirectoryExclude(z, "data", setting.AppDataPath, sessionAbsPath); err != nil { + log.Fatalf("Fail to include data directory: %v", err) + } + if err := z.AddDir("log", setting.LogRootPath); err != nil { log.Fatalf("Fail to include log: %v", err) } @@ -129,3 +145,40 @@ func runDump(ctx *cli.Context) error { return nil } + +// zipAddDirectoryExclude zips absPath to specified zipPath inside z excluding excludeAbsPath +func zipAddDirectoryExclude(zip *zip.ZipArchive, zipPath, absPath string, excludeAbsPath string) error { + absPath, err := filepath.Abs(absPath) + if err != nil { + return err + } + dir, err := os.Open(absPath) + if err != nil { + return err + } + defer dir.Close() + + zip.AddEmptyDir(zipPath) + + files, err := dir.Readdir(0) + if err != nil { + return err + } + for _, file := range files { + currentAbsPath := path.Join(absPath, file.Name()) + currentZipPath := path.Join(zipPath, file.Name()) + if file.IsDir() { + if currentAbsPath != excludeAbsPath { + if err = zipAddDirectoryExclude(zip, currentZipPath, currentAbsPath, excludeAbsPath); err != nil { + return err + } + } + + } else { + if err = zip.AddFile(currentZipPath, currentAbsPath); err != nil { + return err + } + } + } + return nil +} |