@@ -11,6 +11,7 @@ import (
11
11
"log"
12
12
"os"
13
13
"path"
14
+ "path/filepath"
14
15
"time"
15
16
16
17
"code.gitea.io/gitea/models"
@@ -49,6 +50,7 @@ func runDump(ctx *cli.Context) error {
49
50
setting .CustomConf = ctx .String ("config" )
50
51
}
51
52
setting .NewContext ()
53
+ setting .NewServices () // cannot access session settings otherwise
52
54
models .LoadConfigs ()
53
55
models .SetEngine ()
54
56
@@ -97,6 +99,20 @@ func runDump(ctx *cli.Context) error {
97
99
} else {
98
100
log .Printf ("Custom dir %s doesn't exist, skipped" , setting .CustomPath )
99
101
}
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
+
100
116
if err := z .AddDir ("log" , setting .LogRootPath ); err != nil {
101
117
log .Fatalf ("Fail to include log: %v" , err )
102
118
}
@@ -119,3 +135,40 @@ func runDump(ctx *cli.Context) error {
119
135
120
136
return nil
121
137
}
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