Aggiunge backup no-GUI per scheduler
This commit is contained in:
52
src/bakrest/backup_nogui.py
Normal file
52
src/bakrest/backup_nogui.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
|
||||
from .backup import run_backup, shutdown_after_backup
|
||||
from .config import load_config
|
||||
from .logging_setup import close_logger, configure_logging
|
||||
|
||||
|
||||
def run_nogui_backup(shutdown: bool = True) -> int:
|
||||
config = load_config()
|
||||
logger = configure_logging(config.storage.logs_dir, "backup-nogui")
|
||||
logger.info("BakRest no-GUI backup started")
|
||||
try:
|
||||
result = run_backup(config, logger)
|
||||
if not result.success:
|
||||
logger.error("BakRest no-GUI backup failed: %s", result.message)
|
||||
return _exit_code_for_error(result.error_code)
|
||||
|
||||
logger.info("BakRest no-GUI backup completed: %s", result.message)
|
||||
if shutdown:
|
||||
shutdown_after_backup(config, logger)
|
||||
else:
|
||||
logger.info("Shutdown skipped by command line")
|
||||
return 0
|
||||
finally:
|
||||
close_logger(logger)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description="Run Bak&Rest backup without GUI")
|
||||
parser.add_argument(
|
||||
"--no-shutdown",
|
||||
action="store_true",
|
||||
help="Run the backup but do not shut down the computer",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
return run_nogui_backup(shutdown=not args.no_shutdown)
|
||||
|
||||
|
||||
def _exit_code_for_error(error_code: str | None) -> int:
|
||||
if error_code == "server_unreachable":
|
||||
return 20
|
||||
if error_code == "rsync_start_failed":
|
||||
return 30
|
||||
if error_code == "rsync_failed":
|
||||
return 31
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import logging
|
||||
import threading
|
||||
@@ -11,6 +12,7 @@ from PIL import Image, ImageDraw
|
||||
import pystray
|
||||
|
||||
from .backup import run_backup, shutdown_after_backup
|
||||
from .backup_nogui import run_nogui_backup
|
||||
from .config import AppConfig, load_config
|
||||
from .logging_setup import configure_logging
|
||||
from .registry import ChangeRegistry
|
||||
@@ -101,7 +103,22 @@ def _show_error_message(title: str, message: str) -> None:
|
||||
root.destroy()
|
||||
|
||||
|
||||
def main() -> int:
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(description="Bak&Rest tray app")
|
||||
parser.add_argument(
|
||||
"--nogui",
|
||||
action="store_true",
|
||||
help="Run backup and shutdown without tray icon",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--no-shutdown",
|
||||
action="store_true",
|
||||
help="With --nogui, run the backup but do not shut down the computer",
|
||||
)
|
||||
args = parser.parse_args(argv)
|
||||
if args.nogui:
|
||||
return run_nogui_backup(shutdown=not args.no_shutdown)
|
||||
|
||||
config = load_config()
|
||||
logger = configure_logging(config.storage.logs_dir, "tray")
|
||||
app = BakRestTrayApp(config, logger)
|
||||
|
||||
Reference in New Issue
Block a user