From 890c943643dac02bc4ef36e6508dbe2f6668ffc8 Mon Sep 17 00:00:00 2001 From: tmgordeeva Date: Fri, 14 Feb 2020 09:19:58 -0800 Subject: [PATCH] fix(backup): handle backup with no credentials file (#16877) * fix(backup): handle backup with no credentials file Backups and restores should work whether or not the original installation uses a credentials file and whether or not the backup contains a credentials file. --- cmd/influxd/restore/command.go | 12 ++++++++++++ http/backup_service.go | 35 ++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/cmd/influxd/restore/command.go b/cmd/influxd/restore/command.go index 2707a88922..fb9385e020 100644 --- a/cmd/influxd/restore/command.go +++ b/cmd/influxd/restore/command.go @@ -136,6 +136,10 @@ func restoreE(cmd *cobra.Command, args []string) error { return fmt.Errorf("restore completed, but failed to cleanup temporary bolt file: %v", err) } + if err := removeTmpCred(); err != nil { + return fmt.Errorf("restore completed, but failed to cleanup temporary credentials file: %v", err) + } + if err := removeTmpEngine(); err != nil { return fmt.Errorf("restore completed, but failed to cleanup temporary engine data: %v", err) } @@ -284,6 +288,14 @@ func restoreFile(backup string, target string, filetype string) error { func restoreCred() error { backupCred := filepath.Join(flags.backupPath, http.DefaultTokenFile) + _, err := os.Stat(backupCred) + if os.IsNotExist(err) { + fmt.Printf("No credentials file found in backup, skipping.\n") + return nil + } else if err != nil { + return err + } + if err := restoreFile(backupCred, flags.credPath, "credentials"); err != nil { return err } diff --git a/http/backup_service.go b/http/backup_service.go index 947902ee7c..22b4184366 100644 --- a/http/backup_service.go +++ b/http/backup_service.go @@ -117,26 +117,17 @@ func (h *BackupHandler) handleCreate(w http.ResponseWriter, r *http.Request) { files = append(files, bolt.DefaultFilename) - credBackupPath := filepath.Join(internalBackupPath, DefaultTokenFile) + credsExist, err := h.backupCredentials(internalBackupPath) - credPath, err := defaultTokenPath() - if err != nil { - h.HandleHTTPError(ctx, err, w) - return - } - token, err := ioutil.ReadFile(credPath) if err != nil { h.HandleHTTPError(ctx, err, w) return } - if err := ioutil.WriteFile(credBackupPath, []byte(token), 0600); err != nil { - h.HandleHTTPError(ctx, err, w) - return + if credsExist { + files = append(files, DefaultTokenFile) } - files = append(files, DefaultTokenFile) - b := backup{ ID: id, Files: files, @@ -148,6 +139,26 @@ func (h *BackupHandler) handleCreate(w http.ResponseWriter, r *http.Request) { } } +func (h *BackupHandler) backupCredentials(internalBackupPath string) (bool, error) { + credBackupPath := filepath.Join(internalBackupPath, DefaultTokenFile) + + credPath, err := defaultTokenPath() + if err != nil { + return false, err + } + token, err := ioutil.ReadFile(credPath) + if err != nil && !os.IsNotExist(err) { + return false, err + } else if os.IsNotExist(err) { + return false, nil + } + + if err := ioutil.WriteFile(credBackupPath, []byte(token), 0600); err != nil { + return false, err + } + return true, nil +} + func (h *BackupHandler) handleFetchFile(w http.ResponseWriter, r *http.Request) { span, r := tracing.ExtractFromHTTPRequest(r, "BackupHandler.handleFetchFile") defer span.Finish()