Skip to content

Commit 5fcf218

Browse files
authored
Add data directory excluding sessions to dump (#587) (#959)
1 parent 9183661 commit 5fcf218

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

cmd/dump.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"log"
1212
"os"
1313
"path"
14+
"path/filepath"
1415
"time"
1516

1617
"code.gitea.io/gitea/models"
@@ -49,6 +50,7 @@ func runDump(ctx *cli.Context) error {
4950
setting.CustomConf = ctx.String("config")
5051
}
5152
setting.NewContext()
53+
setting.NewServices() // cannot access session settings otherwise
5254
models.LoadConfigs()
5355
models.SetEngine()
5456

@@ -97,6 +99,20 @@ func runDump(ctx *cli.Context) error {
9799
} else {
98100
log.Printf("Custom dir %s doesn't exist, skipped", setting.CustomPath)
99101
}
102+
103+
log.Printf("Packing data directory...%s", setting.AppDataPath)
104+
var sessionAbsPath string
105+
if setting.SessionConfig.Provider == "file" {
106+
if len(setting.SessionConfig.ProviderConfig) == 0 {
107+
setting.SessionConfig.ProviderConfig = "data/sessions"
108+
}
109+
sessionAbsPath, _ = filepath.Abs(setting.SessionConfig.ProviderConfig)
110+
}
111+
112+
if err := zipAddDirectoryExclude(z, "data", setting.AppDataPath, sessionAbsPath); err != nil {
113+
log.Fatalf("Fail to include data directory: %v", err)
114+
}
115+
100116
if err := z.AddDir("log", setting.LogRootPath); err != nil {
101117
log.Fatalf("Fail to include log: %v", err)
102118
}
@@ -119,3 +135,40 @@ func runDump(ctx *cli.Context) error {
119135

120136
return nil
121137
}
138+
139+
// zipAddDirectoryExclude zips absPath to specified zipPath inside z excluding excludeAbsPath
140+
func zipAddDirectoryExclude(zip *zip.ZipArchive, zipPath, absPath string, excludeAbsPath string) error {
141+
absPath, err := filepath.Abs(absPath)
142+
if err != nil {
143+
return err
144+
}
145+
dir, err := os.Open(absPath)
146+
if err != nil {
147+
return err
148+
}
149+
defer dir.Close()
150+
151+
zip.AddEmptyDir(zipPath)
152+
153+
files, err := dir.Readdir(0)
154+
if err != nil {
155+
return err
156+
}
157+
for _, file := range files {
158+
currentAbsPath := path.Join(absPath, file.Name())
159+
currentZipPath := path.Join(zipPath, file.Name())
160+
if file.IsDir() {
161+
if currentAbsPath != excludeAbsPath {
162+
if err = zipAddDirectoryExclude(zip, currentZipPath, currentAbsPath, excludeAbsPath); err != nil {
163+
return err
164+
}
165+
}
166+
167+
} else {
168+
if err = zip.AddFile(currentZipPath, currentAbsPath); err != nil {
169+
return err
170+
}
171+
}
172+
}
173+
return nil
174+
}

0 commit comments

Comments
 (0)