-
Notifications
You must be signed in to change notification settings - Fork 256
feat(backup)_: local user data backups #6722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jrainville
wants to merge
5
commits into
develop
Choose a base branch
from
feat/local-backup-implement-controller
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,894
−48
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a0d2b6
feat(backup)_: local user data backups
jrainville f253df9
chore_: some code review comments addressed
jrainville fb10068
chore_: add backwards compatibility test
jrainville 2e40422
chore_: pass logger to backup controller
jrainville 70419e2
chore_: fix tests missing settings initialization
jrainville File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
appdatabase/migrations/sql/1750356404_add_add_backup_path_setting.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE settings ADD COLUMN backup_path VARCHAR DEFAULT ''; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package backup | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/status-im/status-go/common" | ||
) | ||
|
||
type BackupConfig struct { | ||
PrivateKey []byte | ||
FileNameGetter func() (string, error) | ||
BackupEnabled bool | ||
Interval time.Duration | ||
} | ||
|
||
type Controller struct { | ||
config BackupConfig | ||
core *core | ||
logger *zap.Logger | ||
quit chan struct{} | ||
mutex sync.Mutex | ||
} | ||
|
||
func NewController(config BackupConfig, logger *zap.Logger) (*Controller, error) { | ||
if len(config.PrivateKey) == 0 { | ||
return nil, errors.New("private key must be provided") | ||
} | ||
if config.FileNameGetter == nil { | ||
return nil, errors.New("filename getter must be provided") | ||
} | ||
|
||
return &Controller{ | ||
config: config, | ||
core: newCore(), | ||
logger: logger, | ||
quit: make(chan struct{}), | ||
}, nil | ||
} | ||
|
||
func (c *Controller) Register(componentName string, dumpFunc func() ([]byte, error), loadFunc func([]byte) error) { | ||
c.mutex.Lock() | ||
defer c.mutex.Unlock() | ||
|
||
c.core.Register(componentName, dumpFunc, loadFunc) | ||
} | ||
|
||
func (c *Controller) Start() { | ||
if !c.config.BackupEnabled { | ||
return | ||
} | ||
|
||
go func() { | ||
defer common.LogOnPanic() | ||
ticker := time.NewTicker(c.config.Interval) | ||
defer ticker.Stop() | ||
for { | ||
select { | ||
case <-ticker.C: | ||
_, err := c.PerformBackup() | ||
if err != nil { | ||
c.logger.Error("Error performing backup: %v\n", zap.Error(err)) | ||
} | ||
case <-c.quit: | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (c *Controller) Stop() { | ||
close(c.quit) | ||
} | ||
|
||
func (c *Controller) PerformBackup() (string, error) { | ||
c.mutex.Lock() | ||
defer c.mutex.Unlock() | ||
|
||
backupData, err := c.core.Create(c.config.PrivateKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
fileName, err := c.config.FileNameGetter() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if err := os.MkdirAll(filepath.Dir(fileName), 0700); err != nil { | ||
return "", err | ||
} | ||
|
||
file, err := os.Create(fileName) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer file.Close() | ||
|
||
_, err = file.Write(backupData) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return fileName, nil | ||
} | ||
|
||
func (c *Controller) LoadBackup(filePath string) error { | ||
c.mutex.Lock() | ||
defer c.mutex.Unlock() | ||
|
||
file, err := os.Open(filePath) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
fileInfo, err := file.Stat() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
backupData := make([]byte, fileInfo.Size()) | ||
_, err = file.Read(backupData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.core.Restore(c.config.PrivateKey, backupData) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Bug" we found with @osmaczko . If the settings are not created at the start (of a test for example), then
saveSetting
still "works", ie doesn't send an error. It makes debugging the failing test very annoying.