mirror of https://github.com/gmbrax/Pilgrim.git
Merge pull request #80 from gmbrax/feat/backup-database
add the backup feature
This commit is contained in:
commit
48b3ec5fd1
|
|
@ -0,0 +1,36 @@
|
||||||
|
import sqlite3
|
||||||
|
import zipfile
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
from pilgrim.utils.directory_manager import DirectoryManager
|
||||||
|
|
||||||
|
|
||||||
|
class BackupService:
|
||||||
|
def __init__(self, session):
|
||||||
|
self.session = session
|
||||||
|
|
||||||
|
def create_backup(self):
|
||||||
|
db_path = DirectoryManager.get_database_path()
|
||||||
|
if not db_path.exists():
|
||||||
|
raise FileNotFoundError("No Database Found")
|
||||||
|
|
||||||
|
with self.session.connection() as conn:
|
||||||
|
raw_conn = conn.connection
|
||||||
|
dump = "\n".join(line for line in raw_conn.iterdump())
|
||||||
|
|
||||||
|
filename = DirectoryManager.get_config_directory() / "backup.zip"
|
||||||
|
diaries_root_path = DirectoryManager.get_diaries_root()
|
||||||
|
|
||||||
|
try:
|
||||||
|
with zipfile.ZipFile(filename, "w",zipfile.ZIP_DEFLATED) as zipf:
|
||||||
|
zipf.writestr("database.sql", dump)
|
||||||
|
if diaries_root_path.exists():
|
||||||
|
for file_path in diaries_root_path.rglob('*'):
|
||||||
|
if file_path.is_file():
|
||||||
|
arcname = file_path.relative_to(diaries_root_path.parent)
|
||||||
|
zipf.write(file_path, arcname=arcname)
|
||||||
|
return True, filename
|
||||||
|
except Exception as e:
|
||||||
|
return False, str(e)
|
||||||
|
|
||||||
|
|
@ -14,6 +14,8 @@ from pilgrim.ui.screens.edit_diary_modal import EditDiaryModal
|
||||||
from pilgrim.ui.screens.new_diary_modal import NewDiaryModal
|
from pilgrim.ui.screens.new_diary_modal import NewDiaryModal
|
||||||
from pilgrim.ui.screens.edit_entry_screen import EditEntryScreen
|
from pilgrim.ui.screens.edit_entry_screen import EditEntryScreen
|
||||||
|
|
||||||
|
from pilgrim.service.backup_service import BackupService
|
||||||
|
|
||||||
|
|
||||||
class DiaryListScreen(Screen):
|
class DiaryListScreen(Screen):
|
||||||
TITLE = "Pilgrim - Main"
|
TITLE = "Pilgrim - Main"
|
||||||
|
|
@ -298,3 +300,18 @@ class DiaryListScreen(Screen):
|
||||||
self.notify("Invalid diary ID")
|
self.notify("Invalid diary ID")
|
||||||
else:
|
else:
|
||||||
self.notify("Select a diary to open the settings")
|
self.notify("Select a diary to open the settings")
|
||||||
|
|
||||||
|
|
||||||
|
def action_backup(self):
|
||||||
|
session = self.app.service_manager.get_session()
|
||||||
|
if session:
|
||||||
|
backup_service = BackupService(session)
|
||||||
|
result_operation, result_data = backup_service.create_backup()
|
||||||
|
if result_operation:
|
||||||
|
self.notify(f"Backup result: {result_data}")
|
||||||
|
else:
|
||||||
|
self.notify(f"Error performing backup: {result_data}")
|
||||||
|
else:
|
||||||
|
self.notify("Error: Session not found",severity="error")
|
||||||
|
self.app.exit()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,11 @@ class UIApp(App):
|
||||||
"Open About Pilgrim",
|
"Open About Pilgrim",
|
||||||
screen.action_about_cmd
|
screen.action_about_cmd
|
||||||
)
|
)
|
||||||
|
yield SystemCommand(
|
||||||
|
"Backup Database",
|
||||||
|
"Backup the Database",
|
||||||
|
screen.action_backup
|
||||||
|
)
|
||||||
|
|
||||||
elif isinstance(screen, AboutScreen):
|
elif isinstance(screen, AboutScreen):
|
||||||
yield SystemCommand(
|
yield SystemCommand(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue