-
Notifications
You must be signed in to change notification settings - Fork 10
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
Databases: add support for zip archiving #97
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8be7645
Zip: add support for zip archiving
Akarys42 6962f32
Localstorage: rename zip to localstorage
Akarys42 6d9b1b4
Localstorage: make sure we don't compress twice
Akarys42 793b538
Localstorage: validate arguments early and add comments
Akarys42 f98a9b5
README: fix spelling of local storage
Akarys42 b4e0f92
Localstorage: file -> subpath
Akarys42 97385eb
Merge remote-tracking branch 'upstream/main' into zip-files
Akarys42 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 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 |
---|---|---|
|
@@ -277,7 +277,7 @@ databases: | |
|
||
## Databases | ||
|
||
Right now, this app supports **MongoDB**, **PostgreSQL 13**, **MariaDB** and **Redis**. If | ||
Right now, this app supports **MongoDB**, **PostgreSQL 13**, **MariaDB**, **Redis** and **LocalStorage archiving**. If | ||
you need support for an additional database, consider opening a pull request to | ||
add a new database handler. | ||
|
||
|
@@ -371,6 +371,21 @@ Identifiers can be any string of your choosing. | |
port: "6379" | ||
``` | ||
|
||
### Local Storage | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's do |
||
|
||
- **Database type**: `localstorage` | ||
- **Required field**: `path` | ||
- **Optional field**: `compression_level` | ||
- The compression level must be an integer between 0 and 9. | ||
- The archive will contain the full structure, starting from the root folder. | ||
|
||
```yaml | ||
localstorage: | ||
main_localstorage: | ||
path: /path/to/folder | ||
compression_level: 7 | ||
``` | ||
|
||
#### To restore from the backup | ||
|
||
- Stop Redis server. | ||
|
This file contains 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 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,42 @@ | ||
import datetime | ||
from collections import deque | ||
from pathlib import Path | ||
from zipfile import ZIP_DEFLATED | ||
from zipfile import ZipFile | ||
|
||
from blackbox.exceptions import ImproperlyConfigured | ||
from blackbox.handlers.databases._base import BlackboxDatabase | ||
|
||
|
||
class LocalStorage(BlackboxDatabase): | ||
"""A Database handler that will zip a local folder.""" | ||
|
||
required_fields = ("path",) | ||
|
||
def backup(self) -> Path: | ||
date = datetime.date.today().strftime("%d_%m_%Y") | ||
|
||
path = self.config["path"] | ||
compression_level = self.config.get("compression_level", 5) | ||
|
||
if compression_level < 0 or compression_level > 9: | ||
raise ImproperlyConfigured( | ||
f"Invalid compression level. Must be an integer between 0 and 9, got {compression_level}." | ||
) | ||
|
||
backup_path = Path.home() / f"{self.config['id']}_blackbox_{date}.zip" | ||
|
||
with ZipFile(backup_path, "w", ZIP_DEFLATED, compresslevel=compression_level) as zipfile: | ||
directories = deque((Path(path),)) | ||
|
||
while not len(directories) == 0: | ||
path = directories.pop() | ||
|
||
for file in path.iterdir(): | ||
if file.is_file(): | ||
zipfile.write(file) | ||
elif file.is_dir(): | ||
directories.append(file) | ||
|
||
self.success = True | ||
return backup_path |
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.
this feels weird, and may be confused with
localStorage
, the HTML5 feature.Let's just make it local storage archiving without capital letters.