Skip to content

Commit e698654

Browse files
mkuhlmannlunny
authored andcommitted
Add data directory excluding sessions to dump (#587)
1 parent b7eae78 commit e698654

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"
@@ -53,6 +54,7 @@ func runDump(ctx *cli.Context) error {
5354
setting.CustomConf = ctx.String("config")
5455
}
5556
setting.NewContext()
57+
setting.NewServices() // cannot access session settings otherwise
5658
models.LoadConfigs()
5759
models.SetEngine()
5860

@@ -107,6 +109,20 @@ func runDump(ctx *cli.Context) error {
107109
} else {
108110
log.Printf("Custom dir %s doesn't exist, skipped", setting.CustomPath)
109111
}
112+
113+
log.Printf("Packing data directory...%s", setting.AppDataPath)
114+
var sessionAbsPath string
115+
if setting.SessionConfig.Provider == "file" {
116+
if len(setting.SessionConfig.ProviderConfig) == 0 {
117+
setting.SessionConfig.ProviderConfig = "data/sessions"
118+
}
119+
sessionAbsPath, _ = filepath.Abs(setting.SessionConfig.ProviderConfig)
120+
}
121+
122+
if err := zipAddDirectoryExclude(z, "data", setting.AppDataPath, sessionAbsPath); err != nil {
123+
log.Fatalf("Fail to include data directory: %v", err)
124+
}
125+
110126
if err := z.AddDir("log", setting.LogRootPath); err != nil {
111127
log.Fatalf("Fail to include log: %v", err)
112128
}
@@ -129,3 +145,40 @@ func runDump(ctx *cli.Context) error {
129145

130146
return nil
131147
}
148+
149+
// zipAddDirectoryExclude zips absPath to specified zipPath inside z excluding excludeAbsPath
150+
func zipAddDirectoryExclude(zip *zip.ZipArchive, zipPath, absPath string, excludeAbsPath string) error {
151+
absPath, err := filepath.Abs(absPath)
152+
if err != nil {
153+
return err
154+
}
155+
dir, err := os.Open(absPath)
156+
if err != nil {
157+
return err
158+
}
159+
defer dir.Close()
160+
161+
zip.AddEmptyDir(zipPath)
162+
163+
files, err := dir.Readdir(0)
164+
if err != nil {
165+
return err
166+
}
167+
for _, file := range files {
168+
currentAbsPath := path.Join(absPath, file.Name())
169+
currentZipPath := path.Join(zipPath, file.Name())
170+
if file.IsDir() {
171+
if currentAbsPath != excludeAbsPath {
172+
if err = zipAddDirectoryExclude(zip, currentZipPath, currentAbsPath, excludeAbsPath); err != nil {
173+
return err
174+
}
175+
}
176+
177+
} else {
178+
if err = zip.AddFile(currentZipPath, currentAbsPath); err != nil {
179+
return err
180+
}
181+
}
182+
}
183+
return nil
184+
}

0 commit comments

Comments
 (0)