From 958a8e97e9e8b1c32e18f9b915249e53bf2ce9f3 Mon Sep 17 00:00:00 2001 From: Dakota Walsh <101994734+dakota-portainer@users.noreply.github.com> Date: Fri, 8 Jul 2022 21:05:04 +1200 Subject: [PATCH] fix(migration): close the database before running backups EE-3627 (#7218) * fix(migration): close the database before running backups On certain filesystems, particuarly NTFS when a network mounted windows file server is used to store portainer's database, you are unable to copy the database while it is open. To fix this we simply close the database and then re-open it after a backup. * handle close and open errors * dont return error on nil --- api/datastore/backup.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/api/datastore/backup.go b/api/datastore/backup.go index 1ecf8f05e..d0d54e3b8 100644 --- a/api/datastore/backup.go +++ b/api/datastore/backup.go @@ -103,8 +103,26 @@ func (store *Store) backupWithOptions(options *BackupOptions) (string, error) { store.createBackupFolders() options = store.setupOptions(options) + dbPath := store.databasePath() - return options.BackupPath, store.copyDBFile(store.databasePath(), options.BackupPath) + if err := store.Close(); err != nil { + return options.BackupPath, fmt.Errorf( + "error closing datastore before creating backup: %v", + err, + ) + } + + if err := store.copyDBFile(dbPath, options.BackupPath); err != nil { + return options.BackupPath, err + } + + if _, err := store.Open(); err != nil { + return options.BackupPath, fmt.Errorf( + "error opening datastore after creating backup: %v", + err, + ) + } + return options.BackupPath, nil } // RestoreWithOptions previously saved backup for the current Edition with options