Skip to content

Commit ea8db10

Browse files
committed
database backups
1 parent ff8e40f commit ea8db10

File tree

7 files changed

+63
-17
lines changed

7 files changed

+63
-17
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
__pycache__/
22
.vscode/
3-
library_db/logs/
3+
library_db/logs/
4+
library_db/backup/

TODO

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ Home Page (done)
66
Filter by status (done)
77
Improve footer positioning on mobile (done)
88
Split API Routes (done)
9-
Logging
10-
DB Backup (with Button in Server Dashboard)
9+
Logging (done)
10+
DB Backup (done)

library_db/database.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from pathlib import Path
33

44

5+
def get_db_path() -> Path:
6+
return Path(__file__).parent / "library.db"
7+
8+
59
def get_db_connection():
6-
db_path = Path(__file__).parent / "library.db"
7-
return sqlite3.connect(db_path.__str__(), check_same_thread=False)
10+
return sqlite3.connect(get_db_path().__str__(), check_same_thread=False)

library_db/routes/api/user/user.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ def remove_user():
157157

158158
delete_user(user.get("id"))
159159

160-
current_app.logger.info(f"{session['email']} Deleted user with email {session['email']}")
160+
current_app.logger.info(
161+
f"{session['email']} Deleted user with email {session['email']}"
162+
)
161163

162164
session.pop("email")
163165
session.pop("pwdhash")

library_db/routes/panel/admin/admin.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import shutil
12
from hashlib import md5
23
from pathlib import Path
4+
from datetime import datetime
35
from flask import (
46
Blueprint,
57
abort,
@@ -13,6 +15,7 @@
1315
)
1416

1517

18+
from library_db.database import get_db_path
1619
from library_db.utils.utils import is_admin, get_template_vars
1720
from library_db.utils.db_utils import delete_user, get_user_data, update_user
1821
from library_db.logger import (
@@ -148,3 +151,23 @@ def toggel_request_filter():
148151
time_file_handler.addFilter(RequestFilter())
149152

150153
return redirect(url_for("panel_bluep.admin_bluep.server_dashboard"))
154+
155+
156+
@admin_bluep.route("/server/backupdb", methods=["POST"])
157+
def backup_db():
158+
if not is_admin(session):
159+
return abort(403)
160+
161+
current_db = get_db_path()
162+
backup_path = current_db.parent / "backup"
163+
backup_file = backup_path / (
164+
"library_db_" + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + ".db"
165+
)
166+
167+
try:
168+
backup_path.mkdir(parents=True, exist_ok=True)
169+
shutil.copy(str(current_db), str(backup_file))
170+
except shutil.Error:
171+
return abort(500, "Failed Copying to Backup Folder")
172+
173+
return redirect(url_for("panel_bluep.admin_bluep.server_dashboard"))

library_db/routes/panel/admin/templates/admin/server_dash.html

+23-10
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,29 @@ <h5>Server Logs</h5>
159159
</li>
160160
</ul>
161161
<hr />
162-
<form
163-
action="{{url_for('panel_bluep.admin_bluep.toggel_request_filter')}}"
164-
method="post"
165-
class="mt-2"
166-
>
167-
<button type="submit" class="btn btn-primary">
168-
{% if has_filter %} Deactivate {%else %} Activate {%endif%}
169-
Debug Log Request Filter
170-
</button>
171-
</form>
162+
<h5>Server Actions</h5>
163+
<div class="d-flex gap-2">
164+
<form
165+
action="{{url_for('panel_bluep.admin_bluep.toggel_request_filter')}}"
166+
method="post"
167+
class="mt-2"
168+
>
169+
<button type="submit" class="btn btn-primary">
170+
{% if has_filter %} Deactivate {%else %} Activate
171+
{%endif%} Debug Log Request Filter
172+
</button>
173+
</form>
174+
175+
<form
176+
action="{{url_for('panel_bluep.admin_bluep.backup_db')}}"
177+
method="post"
178+
class="mt-2"
179+
>
180+
<button type="submit" class="btn btn-primary btn-success">
181+
Backup Database
182+
</button>
183+
</form>
184+
</div>
172185
</div>
173186
</div>
174187
</div>

library_db/utils/db_utils.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,11 @@ def author_exsists(name: str) -> bool:
351351

352352

353353
def add_media_item(
354-
title: str, author_id: int, age_limit: int, media_type_id: int, isbn: int | None = None
354+
title: str,
355+
author_id: int,
356+
age_limit: int,
357+
media_type_id: int,
358+
isbn: int | None = None,
355359
) -> int | None:
356360
statement = """
357361
INSERT INTO media (

0 commit comments

Comments
 (0)