Skip to content

Commit 1659f68

Browse files
Tanya GordeevaJacob Marble
Tanya Gordeeva
authored and
Jacob Marble
committed
fix(backup): actually copy the credentials file for the backup
1 parent 7fb200a commit 1659f68

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

http/backup_service.go

+40-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8+
"io/ioutil"
89
"net/http"
910
"os"
1011
"path"
@@ -15,6 +16,7 @@ import (
1516
"github.com/influxdata/httprouter"
1617
"github.com/influxdata/influxdb"
1718
"github.com/influxdata/influxdb/bolt"
19+
"github.com/influxdata/influxdb/internal/fs"
1820
"github.com/influxdata/influxdb/kit/tracing"
1921
"go.uber.org/multierr"
2022
"go.uber.org/zap"
@@ -98,32 +100,48 @@ func (h *BackupHandler) handleCreate(w http.ResponseWriter, r *http.Request) {
98100
}
99101

100102
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)
105103

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
109110
}
110-
if err = json.NewEncoder(w).Encode(&b); err != nil {
111+
112+
if err = h.KVBackupService.Backup(ctx, boltFile); err != nil {
111113
err = multierr.Append(err, os.RemoveAll(internalBackupPath))
112114
h.HandleHTTPError(ctx, err, w)
113115
return
114116
}
115-
}
116117

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)
120128
if err != nil {
121-
err = multierr.Append(err, os.RemoveAll(internalBackupPath))
122129
h.HandleHTTPError(ctx, err, w)
123130
return
124131
}
125132

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 {
127145
err = multierr.Append(err, os.RemoveAll(internalBackupPath))
128146
h.HandleHTTPError(ctx, err, w)
129147
return
@@ -229,6 +247,14 @@ func (s *BackupService) FetchBackupFile(ctx context.Context, backupID int, backu
229247
return nil
230248
}
231249

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+
232258
func (s *BackupService) InternalBackupPath(backupID int) string {
233259
panic("internal method not implemented here")
234260
}

0 commit comments

Comments
 (0)