Get general concept for backup resotration using a unified interface implemented

This commit is contained in:
Dane Everitt
2021-01-18 21:20:58 -08:00
parent 66b6f40b61
commit 6ef0bd7496
7 changed files with 179 additions and 49 deletions

View File

@@ -1,6 +1,7 @@
package server
import (
"io"
"io/ioutil"
"os"
@@ -50,9 +51,9 @@ func (s *Server) getServerwideIgnoredFiles() (string, error) {
return string(b), nil
}
// Performs a server backup and then emits the event over the server websocket. We
// let the actual backup system handle notifying the panel of the status, but that
// won't emit a websocket event.
// Backup performs a server backup and then emits the event over the server
// websocket. We let the actual backup system handle notifying the panel of the
// status, but that won't emit a websocket event.
func (s *Server) Backup(b backup.BackupInterface) error {
ignored := b.Ignored()
if b.Ignored() == "" {
@@ -108,3 +109,43 @@ func (s *Server) Backup(b backup.BackupInterface) error {
return nil
}
// RestoreBackup calls the Restore function on the provided backup. Once this
// restoration is completed an event is emitted to the websocket to notify the
// Panel that is has been completed.
//
// In addition to the websocket event an API call is triggered to notify the
// Panel of the new state.
func (s *Server) RestoreBackup(b backup.BackupInterface, reader io.ReadCloser) (err error) {
s.Config().SetSuspended(true)
// Local backups will not pass a reader through to this function, so check first
// to make sure it is a valid reader before trying to close it.
defer func() {
s.Config().SetSuspended(false)
if reader != nil {
reader.Close()
}
}()
// Don't try to restore the server until we have completely stopped the running
// instance, otherwise you'll likely hit all types of write errors due to the
// server being suspended.
err = s.Environment.WaitForStop(120, false)
if err != nil {
return err
}
// Send an API call to the Panel as soon as this function is done running so that
// the Panel is informed of the restoration status of this backup.
defer func() {
if err := api.New().SendRestorationStatus(b.Identifier(), err == nil); err != nil {
s.Log().WithField("error", err).WithField("backup", b.Identifier()).Error("failed to notify Panel of backup restoration status")
}
}()
// Attempt to restore the backup to the server by running through each entry
// in the file one at a time and writing them to the disk.
err = b.Restore(reader, func(file string, r io.Reader) error {
return s.Filesystem().Writefile(file, r)
})
return err
}