mirror of
https://github.com/borgbase/vorta
synced 2025-01-03 05:36:19 +00:00
Create backups from command line using existing profiles. By @samuel-w (#556)
This commit is contained in:
parent
19b4946b45
commit
a4b49e7e0b
4 changed files with 52 additions and 7 deletions
|
@ -34,7 +34,7 @@ def main():
|
|||
|
||||
# Init app after database is available
|
||||
from vorta.application import VortaApp
|
||||
app = VortaApp(sys.argv, single_app=True)
|
||||
app = VortaApp(sys.argv, single_app=args.profile is None)
|
||||
app.updater = get_updater()
|
||||
|
||||
sys.exit(app.exec())
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
import logging
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import ast
|
||||
|
||||
from PyQt5 import QtCore
|
||||
from PyQt5.QtWidgets import QMessageBox
|
||||
|
||||
from vorta.borg.borg_thread import BorgThread
|
||||
from vorta.borg.create import BorgCreateThread
|
||||
from vorta.borg.version import BorgVersionThread
|
||||
from vorta.config import TEMP_DIR
|
||||
|
@ -15,6 +20,8 @@
|
|||
from vorta.views.main_window import MainWindow
|
||||
from vorta.notifications import VortaNotifications
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
APP_ID = os.path.join(TEMP_DIR, "socket")
|
||||
|
||||
|
||||
|
@ -35,10 +42,18 @@ class VortaApp(QtSingleApplication):
|
|||
def __init__(self, args_raw, single_app=False):
|
||||
|
||||
super().__init__(APP_ID, args_raw)
|
||||
if self.isRunning() and single_app:
|
||||
self.sendMessage("open main window")
|
||||
print('An instance of Vorta is already running. Opening main window.')
|
||||
sys.exit()
|
||||
args = parse_args()
|
||||
if self.isRunning():
|
||||
if single_app:
|
||||
self.sendMessage("open main window")
|
||||
print('An instance of Vorta is already running. Opening main window.')
|
||||
sys.exit()
|
||||
elif args.profile:
|
||||
self.sendMessage(f"create {args.profile}")
|
||||
print('Creating backups using existing Vorta instance.')
|
||||
sys.exit()
|
||||
elif args.profile:
|
||||
sys.exit('Vorta must already be running for --create to work')
|
||||
|
||||
init_translations(self)
|
||||
|
||||
|
@ -50,7 +65,6 @@ def __init__(self, args_raw, single_app=False):
|
|||
self.tray = TrayMenu(self)
|
||||
self.main_window = MainWindow(self)
|
||||
|
||||
args = parse_args()
|
||||
if getattr(args, 'daemonize', False):
|
||||
pass
|
||||
elif SettingsModel.get(key='foreground').value:
|
||||
|
@ -63,8 +77,25 @@ def __init__(self, args_raw, single_app=False):
|
|||
self.set_borg_details_action()
|
||||
self.installEventFilter(self)
|
||||
|
||||
def create_backups_cmdline(self, profiles):
|
||||
self.completedProfiles = []
|
||||
self.validProfiles = []
|
||||
for profile_name in profiles:
|
||||
profile = BackupProfileModel.get_or_none(name=profile_name)
|
||||
if profile is not None:
|
||||
if profile.repo is None:
|
||||
logger.warning(f"Add a repository to {profile_name}")
|
||||
continue
|
||||
self.validProfiles.append(profile_name)
|
||||
# Wait a bit in case something is running
|
||||
while BorgThread.is_running():
|
||||
time.sleep(0.1)
|
||||
self.create_backup_action(profile_id=profile.id)
|
||||
else:
|
||||
logger.warning(f"Invalid profile name {profile_name}")
|
||||
|
||||
def eventFilter(self, source, event):
|
||||
if event.type() == QtCore.QEvent.ApplicationPaletteChange and type(source) == MainWindow:
|
||||
if event.type() == QtCore.QEvent.ApplicationPaletteChange and isinstance(source, MainWindow):
|
||||
self.main_window.set_icons()
|
||||
self.main_window.repoTab.set_icons()
|
||||
self.main_window.archiveTab.set_icons()
|
||||
|
@ -110,6 +141,13 @@ def backup_cancelled_event_response(self):
|
|||
def message_received_event_response(self, message):
|
||||
if message == "open main window":
|
||||
self.open_main_window_action()
|
||||
elif message.startswith("create"):
|
||||
message = message[7:] # Remove create
|
||||
profiles = ast.literal_eval(message) # Safely parse string array
|
||||
if BorgThread.is_running():
|
||||
logger.warning("Cannot run while backups are already running")
|
||||
else:
|
||||
self.create_backups_cmdline(profiles)
|
||||
|
||||
def set_borg_details_action(self):
|
||||
params = BorgVersionThread.prepare()
|
||||
|
|
|
@ -43,6 +43,7 @@ def started_event(self):
|
|||
|
||||
def finished_event(self, result):
|
||||
self.app.backup_finished_event.emit(result)
|
||||
self.result.emit(result)
|
||||
self.pre_post_backup_cmd(self.params, cmd='post_backup_cmd', returncode=result['returncode'])
|
||||
|
||||
@classmethod
|
||||
|
|
|
@ -237,6 +237,12 @@ def parse_args():
|
|||
parser.add_argument('--daemonize', '-d',
|
||||
action='store_true',
|
||||
help="Fork to background and don't open window on startup.")
|
||||
parser.add_argument(
|
||||
'--create',
|
||||
nargs='+',
|
||||
dest='profile',
|
||||
help='Create a backup in the background using the given profile(s). '
|
||||
'Vorta must already be running for this to work.')
|
||||
|
||||
return parser.parse_known_args()[0]
|
||||
|
||||
|
|
Loading…
Reference in a new issue