mirror of https://github.com/gmbrax/Pilgrim.git
add the backup feature
This commit is contained in:
parent
4aca171ce3
commit
f62ecc18f7
|
|
@ -0,0 +1,33 @@
|
|||
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")
|
||||
conn = self.session.connection().connection
|
||||
dump = "\n".join(line for line in conn.iterdump())
|
||||
filename = str(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, None
|
||||
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.edit_entry_screen import EditEntryScreen
|
||||
|
||||
from pilgrim.service.backup_service import BackupService
|
||||
|
||||
|
||||
class DiaryListScreen(Screen):
|
||||
TITLE = "Pilgrim - Main"
|
||||
|
|
@ -298,3 +300,13 @@ class DiaryListScreen(Screen):
|
|||
self.notify("Invalid diary ID")
|
||||
else:
|
||||
self.notify("Select a diary to open the settings")
|
||||
|
||||
|
||||
def action_backup(self):
|
||||
session = self.app.service_manager.get_session()
|
||||
backup_service = BackupService(session)
|
||||
if session:
|
||||
result = backup_service.create_backup()
|
||||
else:
|
||||
self.notify("You must be logged in to perform this action")
|
||||
self.notify(f"Backup result: {result}")
|
||||
|
|
|
|||
|
|
@ -37,6 +37,11 @@ class UIApp(App):
|
|||
"Open About Pilgrim",
|
||||
screen.action_about_cmd
|
||||
)
|
||||
yield SystemCommand(
|
||||
"Backup Database",
|
||||
"Backup the Database",
|
||||
screen.action_backup
|
||||
)
|
||||
|
||||
elif isinstance(screen, AboutScreen):
|
||||
yield SystemCommand(
|
||||
|
|
|
|||
Loading…
Reference in New Issue