5
5
"encoding/json"
6
6
"fmt"
7
7
"io"
8
+ "io/ioutil"
8
9
"net/http"
9
10
"os"
10
11
"path"
@@ -15,6 +16,7 @@ import (
15
16
"github.com/influxdata/httprouter"
16
17
"github.com/influxdata/influxdb"
17
18
"github.com/influxdata/influxdb/bolt"
19
+ "github.com/influxdata/influxdb/internal/fs"
18
20
"github.com/influxdata/influxdb/kit/tracing"
19
21
"go.uber.org/multierr"
20
22
"go.uber.org/zap"
@@ -98,32 +100,48 @@ func (h *BackupHandler) handleCreate(w http.ResponseWriter, r *http.Request) {
98
100
}
99
101
100
102
internalBackupPath := h .BackupService .InternalBackupPath (id )
101
- h .addFile (ctx , w , internalBackupPath , bolt .DefaultFilename )
102
- h .addFile (ctx , w , internalBackupPath , DefaultTokenFile )
103
- files = append (files , bolt .DefaultFilename )
104
- files = append (files , DefaultTokenFile )
105
103
106
- b := backup {
107
- ID : id ,
108
- Files : files ,
104
+ boltPath := filepath .Join (internalBackupPath , bolt .DefaultFilename )
105
+ boltFile , err := os .OpenFile (boltPath , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , 0660 )
106
+ if err != nil {
107
+ err = multierr .Append (err , os .RemoveAll (internalBackupPath ))
108
+ h .HandleHTTPError (ctx , err , w )
109
+ return
109
110
}
110
- if err = json .NewEncoder (w ).Encode (& b ); err != nil {
111
+
112
+ if err = h .KVBackupService .Backup (ctx , boltFile ); err != nil {
111
113
err = multierr .Append (err , os .RemoveAll (internalBackupPath ))
112
114
h .HandleHTTPError (ctx , err , w )
113
115
return
114
116
}
115
- }
116
117
117
- func (h * BackupHandler ) addFile (ctx context.Context , w http.ResponseWriter , internalBackupPath string , filename string ) {
118
- path := filepath .Join (internalBackupPath , filename )
119
- file , err := os .OpenFile (path , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , 0660 )
118
+ files = append (files , bolt .DefaultFilename )
119
+
120
+ credBackupPath := filepath .Join (internalBackupPath , DefaultTokenFile )
121
+
122
+ credPath , err := defaultTokenPath ()
123
+ if err != nil {
124
+ h .HandleHTTPError (ctx , err , w )
125
+ return
126
+ }
127
+ token , err := ioutil .ReadFile (credPath )
120
128
if err != nil {
121
- err = multierr .Append (err , os .RemoveAll (internalBackupPath ))
122
129
h .HandleHTTPError (ctx , err , w )
123
130
return
124
131
}
125
132
126
- if err = h .KVBackupService .Backup (ctx , file ); err != nil {
133
+ if err := ioutil .WriteFile (credBackupPath , []byte (token ), 0600 ); err != nil {
134
+ h .HandleHTTPError (ctx , err , w )
135
+ return
136
+ }
137
+
138
+ files = append (files , DefaultTokenFile )
139
+
140
+ b := backup {
141
+ ID : id ,
142
+ Files : files ,
143
+ }
144
+ if err = json .NewEncoder (w ).Encode (& b ); err != nil {
127
145
err = multierr .Append (err , os .RemoveAll (internalBackupPath ))
128
146
h .HandleHTTPError (ctx , err , w )
129
147
return
@@ -229,6 +247,14 @@ func (s *BackupService) FetchBackupFile(ctx context.Context, backupID int, backu
229
247
return nil
230
248
}
231
249
250
+ func defaultTokenPath () (string , error ) {
251
+ dir , err := fs .InfluxDir ()
252
+ if err != nil {
253
+ return "" , err
254
+ }
255
+ return filepath .Join (dir , DefaultTokenFile ), nil
256
+ }
257
+
232
258
func (s * BackupService ) InternalBackupPath (backupID int ) string {
233
259
panic ("internal method not implemented here" )
234
260
}
0 commit comments