mirror of
https://github.com/gmbrax/Pilgrim.git
synced 2026-11-10 04:50:18 +00:00
Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c3397ab982 | ||
|
|
bb9457978c | ||
|
|
c09425cd48 | ||
|
|
7c950b93ab | ||
|
|
17371088da | ||
|
|
209099cc5e | ||
|
|
890fc470bb | ||
|
|
8cf5f7e465 | ||
|
|
f62ecc18f7 | ||
|
|
4aca171ce3 | ||
|
|
49b6917291 |
@@ -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.edit_entry_screen import EditEntryScreen
|
||||
|
||||
from pilgrim.service.backup_service import BackupService
|
||||
|
||||
|
||||
class DiaryListScreen(Screen):
|
||||
TITLE = "Pilgrim - Main"
|
||||
@@ -298,3 +300,18 @@ 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()
|
||||
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()
|
||||
|
||||
|
||||
@@ -100,7 +100,6 @@ class SettingsScreen(Screen):
|
||||
)
|
||||
|
||||
self.diary_denger_zone_container = Container(
|
||||
self.backup_diary_button_container,
|
||||
self.delete_diary_button_container,
|
||||
self.delete_all_entries_button_container,
|
||||
self.delete_all_photos_button_container,
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user