Beispiel #1
0
 def bootstrap_profile(self, bootstrap_file=PROFILE_BOOTSTRAP_FILE):
     """
     Make sure there is at least one profile when first starting Vorta.
     Will either import a profile placed in ~/.vorta-init.json
     or add an empty "Default" profile.
     """
     if bootstrap_file.is_file():
         try:
             profile_export = ProfileExport.from_json(bootstrap_file)
             profile = profile_export.to_db(overwrite_profile=True,
                                            overwrite_settings=True)
         except Exception as exception:
             double_newline = os.linesep + os.linesep
             QMessageBox.critical(
                 None, self.tr('Failed to import profile'),
                 "{}{}\"{}\"{}{}".format(
                     self.tr('Failed to import a profile from {}:').format(
                         bootstrap_file),
                     double_newline,
                     str(exception),
                     double_newline,
                     self.tr('Consider removing or repairing this file to '
                             'get rid of this message.'),
                 ))
             return
         bootstrap_file.unlink()
         notifier = VortaNotifications.pick()
         notifier.deliver(self.tr('Profile import successful!'),
                          self.tr('Profile {} imported.').format(
                              profile.name),
                          level='info')
         logger.info('Profile {} imported.'.format(profile.name))
     if BackupProfileModel.select().count() == 0:
         default_profile = BackupProfileModel(name='Default')
         default_profile.save()
Beispiel #2
0
    def create_backup(self, profile_id):
        notifier = VortaNotifications.pick()
        profile = BackupProfileModel.get(id=profile_id)

        logger.info('Starting background backup for %s', profile.name)
        notifier.deliver(self.tr('Vorta Backup'),
                         self.tr('Starting background backup for %s.') % profile.name,
                         level='info')

        msg = BorgCreateThread.prepare(profile)
        if msg['ok']:
            logger.info('Preparation for backup successful.')
            thread = BorgCreateThread(msg['cmd'], msg)
            thread.start()
            thread.wait()
            if thread.process.returncode in [0, 1]:
                notifier.deliver(self.tr('Vorta Backup'),
                                 self.tr('Backup successful for %s.') % profile.name,
                                 level='info')
                logger.info('Backup creation successful.')
                self.post_backup_tasks(profile_id)
            else:
                notifier.deliver(self.tr('Vorta Backup'), self.tr('Error during backup creation.'), level='error')
                logger.error('Error during backup creation.')
        else:
            logger.error('Conditions for backup not met. Aborting.')
            logger.error(msg['message'])
            notifier.deliver(self.tr('Vorta Backup'), translate('messages', msg['message']), level='error')
Beispiel #3
0
    def create_backup(self, profile_id):
        notifier = VortaNotifications.pick()
        profile = BackupProfileModel.get_or_none(id=profile_id)

        if profile is None:
            logger.info('Profile not found. Maybe deleted?')
            return

        # Skip if a job for this profile (repo) is already in progress
        if self.app.jobs_manager.is_worker_running(site=profile.repo.id):
            logger.debug('A job for repo %s is already active.',
                         profile.repo.id)
            return

        self.lock.acquire()
        logger.info('Starting background backup for %s', profile.name)
        notifier.deliver(self.tr('Vorta Backup'),
                         self.tr('Starting background backup for %s.') %
                         profile.name,
                         level='info')
        msg = BorgCreateJob.prepare(profile)
        if msg['ok']:
            logger.info('Preparation for backup successful.')
            msg['category'] = 'scheduled'
            job = BorgCreateJob(msg['cmd'], msg, profile.repo.id)
            job.result.connect(self.notify)
            self.app.jobs_manager.add_job(job)
        else:
            logger.error('Conditions for backup not met. Aborting.')
            logger.error(msg['message'])
            notifier.deliver(self.tr('Vorta Backup'),
                             translate('messages', msg['message']),
                             level='error')
        self.lock.release()
Beispiel #4
0
    def create_backup_action(self, profile_id=None):
        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)
            thread.start()
        else:
            notifier = VortaNotifications.pick()
            notifier.deliver(self.tr('Vorta Backup'), translate('messages', msg['message']), level='error')
            self.backup_log_event.emit(translate('messages', msg['message']))
Beispiel #5
0
    def create_backup_action(self, profile_id=None):
        if not profile_id:
            profile_id = self.main_window.current_profile.id

        profile = BackupProfileModel.get(id=profile_id)
        msg = BorgCreateJob.prepare(profile)
        if msg['ok']:
            job = BorgCreateJob(msg['cmd'], msg, profile.repo.id)
            self.jobs_manager.add_job(job)
        else:
            notifier = VortaNotifications.pick()
            notifier.deliver(self.tr('Vorta Backup'),
                             translate('messages', msg['message']),
                             level='error')
            self.backup_progress_event.emit(
                translate('messages', msg['message']))
            return None
Beispiel #6
0
    def notify(self, result):
        notifier = VortaNotifications.pick()
        profile_name = result['params']['profile_name']
        profile_id = result['params']['profile'].id

        if result['returncode'] in [0, 1]:
            notifier.deliver(self.tr('Vorta Backup'),
                             self.tr('Backup successful for %s.') %
                             profile_name,
                             level='info')
            logger.info('Backup creation successful.')
            self.post_backup_tasks(profile_id)
        else:
            notifier.deliver(self.tr('Vorta Backup'),
                             self.tr('Error during backup creation.'),
                             level='error')
            logger.error('Error during backup creation.')

        self.set_timer_for_profile(profile_id)
Beispiel #7
0
 def bootstrap_profile(self, bootstrap_file=PROFILE_BOOTSTRAP_FILE):
     """
     Make sure there is at least one profile when first starting Vorta.
     Will either import a profile placed in ~/.vorta-init.json
     or add an empty "Default" profile.
     """
     if bootstrap_file.is_file():
         profile_export = ProfileExport.from_json(bootstrap_file)
         profile = profile_export.to_db(overwrite_profile=True,
                                        overwrite_settings=True)
         bootstrap_file.unlink()
         notifier = VortaNotifications.pick()
         notifier.deliver(self.tr('Profile import successful!'),
                          self.tr('Profile {} imported.').format(
                              profile.name),
                          level='info')
         logger.info('Profile {} imported.'.format(profile.name))
     if BackupProfileModel.select().count() == 0:
         default_profile = BackupProfileModel(name='Default')
         default_profile.save()
Beispiel #8
0
    def reload(self):
        for profile in BackupProfileModel.select():
            trigger = None
            job_id = f'{profile.id}'
            if profile.schedule_mode == 'interval':
                if profile.schedule_interval_hours >= 24:
                    days = profile.schedule_interval_hours // 24
                    leftover_hours = profile.schedule_interval_hours % 24

                    if leftover_hours == 0:
                        cron_hours = '1'
                    else:
                        cron_hours = f'*/{leftover_hours}'

                    trigger = cron.CronTrigger(day=f'*/{days}',
                                               hour=cron_hours,
                                               minute=profile.schedule_interval_minutes)
                else:
                    trigger = cron.CronTrigger(hour=f'*/{profile.schedule_interval_hours}',
                                               minute=profile.schedule_interval_minutes)
            elif profile.schedule_mode == 'fixed':
                trigger = cron.CronTrigger(hour=profile.schedule_fixed_hour,
                                           minute=profile.schedule_fixed_minute)
            if self.get_job(job_id) is not None and trigger is not None:
                self.reschedule_job(job_id, trigger=trigger)
                notifier = VortaNotifications.pick()
                notifier.deliver(self.tr('Vorta Scheduler'), self.tr('Background scheduler was changed.'))
                logger.debug('Job for profile %s was rescheduled.', profile.name)
            elif trigger is not None:
                self.add_job(
                    func=self.create_backup,
                    args=[profile.id],
                    trigger=trigger,
                    id=job_id,
                    misfire_grace_time=180
                )
                logger.debug('New job for profile %s was added.', profile.name)
            elif self.get_job(job_id) is not None and trigger is None:
                self.remove_job(job_id)
                logger.debug('Job for profile %s was removed.', profile.name)