Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to create backups from command line using existing profiles #556

Merged
merged 30 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0fcef99
Add command line backup creation
samuel-w Jul 20, 2020
3bad83b
Exit check against valid profiles only
samuel-w Jul 20, 2020
1cfefb9
Simplify command line syntax
samuel-w Jul 20, 2020
cff01e0
Change syntax, use logger instead of print
samuel-w Jul 21, 2020
80a2b4a
Lint
samuel-w Jul 21, 2020
cd8a35d
Print profile name when completed
samuel-w Jul 22, 2020
ee57fda
Remove unneeded variable
samuel-w Jul 26, 2020
35ad8a2
Make args not self
samuel-w Jul 26, 2020
da34c30
Prevent backing up with no repo
samuel-w Jul 26, 2020
5e8b4ac
Merge branch 'master' into CLICreate
samuel-w Aug 9, 2020
93540e3
Merge branch 'master' into CLICreate
m3nu Aug 10, 2020
b6c9ec6
Merge remote-tracking branch 'upstream/master' into CLICreate
samuel-w Aug 17, 2020
33a45c9
Merge branch 'master' into CLICreate
samuel-w Aug 29, 2020
2080f8f
Merge branch 'master' into CLICreate
samuel-w Aug 30, 2020
04f7d1c
Merge branch 'master' into CLICreate
samuel-w Aug 30, 2020
22f3ae2
Merge branch 'master' into CLICreate
samuel-w Sep 4, 2020
3cc2875
Merge branch 'master' into CLICreate
samuel-w Sep 4, 2020
c22330e
Merge branch 'master' into CLICreate
samuel-w Sep 5, 2020
2c48416
Merge branch 'master' into CLICreate
samuel-w Sep 6, 2020
ea412f4
Merge branch 'master' into CLICreate
samuel-w Sep 6, 2020
28a2cea
Merge branch 'master' into CLICreate
samuel-w Sep 6, 2020
29bb1a3
Merge branch 'master' into CLICreate
samuel-w Sep 7, 2020
971f0ce
Merge branch 'master' into CLICreate
samuel-w Sep 8, 2020
803b4d5
Merge branch 'master' into CLICreate
samuel-w Sep 27, 2020
36e4f7f
Grammar
samuel-w Oct 12, 2020
2273c16
Merge branch 'master' into CLICreate
samuel-w Oct 20, 2020
cfa24b1
Merge branch 'master' into CLICreate
samuel-w Oct 30, 2020
d864a4f
Run backups on exisiting thread.
samuel-w Nov 21, 2020
b4fd9c4
Make it require running instance
samuel-w Nov 24, 2020
05579b2
Merge branch 'master' into CLICreate
samuel-w Dec 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/vorta/application.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import logging
import os
import sys
import sip
import time

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
Expand All @@ -16,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")


Expand Down Expand Up @@ -49,20 +55,45 @@ def __init__(self, args_raw, single_app=False):
self.tray = TrayMenu(self)

args = parse_args()
if getattr(args, 'daemonize', False):
if getattr(args, 'daemonize', False) or args.profile:
pass
elif SettingsModel.get(key='foreground').value:
self.open_main_window_action()

if args.profile:
m3nu marked this conversation as resolved.
Show resolved Hide resolved
self.completedProfiles = []
self.validProfiles = []
for profile_name in args.profile:
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, from_cmdline=True)
else:
logger.warning(f"Invalid profile name {profile_name}")

self.backup_started_event.connect(self.backup_started_event_response)
self.backup_finished_event.connect(self.backup_finished_event_response)
self.backup_cancelled_event.connect(self.backup_cancelled_event_response)
self.message_received_event.connect(self.message_received_event_response)
self.set_borg_details_action()
self.installEventFilter(self)

def exit_checker(self, result):
"""Exit when all profiles have been run"""
profile_name = result['params']['profile_name']
logger.info(f"Backup complete for {profile_name}")
self.completedProfiles.append(profile_name)
if self.validProfiles == self.completedProfiles:
os._exit(0)

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()
Expand All @@ -71,14 +102,16 @@ def eventFilter(self, source, event):
self.tray.set_tray_icon()
return False

def create_backup_action(self, profile_id=None):
def create_backup_action(self, profile_id=None, from_cmdline=False):
if not profile_id:
profile_id = self.main_window.current_profile.id

profile = BackupProfileModel.get(id=profile_id)
msg = BorgCreateThread.prepare(profile)
if msg['ok']:
thread = BorgCreateThread(msg['cmd'], msg, parent=self)
if from_cmdline:
thread.result.connect(self.exit_checker)
thread.start()
else:
notifier = VortaNotifications.pick()
Expand Down
1 change: 1 addition & 0 deletions src/vorta/borg/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/vorta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def get_private_keys():
def pretty_bytes(size):
"""from https://stackoverflow.com/questions/12523586/
python-format-size-application-converting-b-to-kb-mb-gb-tb/37423778"""
if type(size) != int:
if not isinstance(size, int):
return ''
power = 1000 # GiB is base 2**10, GB is base 10**3.
n = 0
Expand Down Expand Up @@ -192,6 +192,11 @@ 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). Will automatically close once complete')

return parser.parse_known_args()[0]

Expand Down