Skip to content
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 7 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

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.

you need support for an additional database, consider opening a pull request to
add a new database handler.

Expand Down Expand Up @@ -371,6 +371,21 @@ Identifiers can be any string of your choosing.
port: "6379"
```

### Local Storage
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's do Local storage here.


- **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.
Expand Down
1 change: 1 addition & 0 deletions blackbox/handlers/databases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from .mysql import MySQL
from .postgres import Postgres
from .redis import Redis
from .localstorage import LocalStorage
42 changes: 42 additions & 0 deletions blackbox/handlers/databases/localstorage.py
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