Пример #1
0
    def _read_backup_info_from_dict(self, backup_variables):
        """
        This method reads the backup information from the given dictionary and
        sets the needed class variables.
        """
        # Setting the oldest backup date. This will be used as start of
        # the new backup procedure.
        #
        # If the oldest backup date is not set, then find the oldest
        # creation timestamp and set it as the oldest backup date.
        if backup_variables.get(self.OLDEST_OBJECT_BK_KEY) is None:

            # qb = QueryBuilder()
            # qb.append(
            #     Node,
            # )
            # qb.order_by({Node: {'ctime': 'asc'}})
            # query_node_res = qb.first()

            query_node_res = self._query_first_node()
            query_workflow_res = self._query_first_workflow()

            if (not query_node_res) and (not query_workflow_res):
                self._logger.error("The oldest modification date "
                                   "was not found.")
                raise BackupError("The oldest modification date "
                                  "was not found.")

            oldest_timestamps = []
            if query_node_res:
                oldest_timestamps.append(query_node_res[0].ctime)
            if query_workflow_res:
                oldest_timestamps.append(query_workflow_res[0].ctime)

            self._oldest_object_bk = min(oldest_timestamps)
            self._logger.info("Setting the oldest modification date to the "
                              "creation date of the oldest object "
                              "({})".format(self._oldest_object_bk))

        # If the oldest backup date is not None then try to parse it
        else:
            try:
                self._oldest_object_bk = parse(
                    backup_variables.get(self.OLDEST_OBJECT_BK_KEY))
                if self._oldest_object_bk.tzinfo is None:
                    curr_timezone = str(dtimezone.get_current_timezone())
                    self._oldest_object_bk = ptimezone(
                        str(curr_timezone)).localize(self._oldest_object_bk)
                    self._logger.info(
                        "No timezone defined in the oldest "
                        "modification date timestamp. Setting "
                        "current timezone ({}).".format(curr_timezone))
            # If it is not parsable...
            except ValueError:
                self._logger.error(
                    "We did not manage to parse the start timestamp "
                    "of the last backup.")
                raise

        # Setting the backup directory & normalizing it
        self._backup_dir = os.path.normpath(
            backup_variables.get(self.BACKUP_DIR_KEY))
        if (not self._ignore_backup_dir_existence_check
                and not os.path.isdir(self._backup_dir)):
            self._logger.error("The given backup directory doesn't exist.")
            raise BackupError("The given backup directory doesn't exist.")

        # You can not set an end-of-backup date and end days from the backup
        # that you should stop.
        if (backup_variables.get(self.DAYS_TO_BACKUP_KEY) is not None and
                backup_variables.get(self.END_DATE_OF_BACKUP_KEY) is not None):
            self._logger.error("Only one end of backup date can be set.")
            raise BackupError("Only one backup end can be set (date or "
                              "days from backup start.")

        # Check if there is an end-of-backup date
        elif backup_variables.get(self.END_DATE_OF_BACKUP_KEY) is not None:
            try:
                self._end_date_of_backup = parse(
                    backup_variables.get(self.END_DATE_OF_BACKUP_KEY))

                if self._end_date_of_backup.tzinfo is None:
                    curr_timezone = str(dtimezone.get_current_timezone())
                    self._end_date_of_backup = \
                        ptimezone(str(curr_timezone)).localize(
                            self._end_date_of_backup)
                    self._logger.info("No timezone defined in the end date of "
                                      "backup timestamp. Setting current "
                                      "timezone ({}).".format(curr_timezone))

                self._internal_end_date_of_backup = self._end_date_of_backup
            except ValueError:
                self._logger.error("The end date of the backup could not be "
                                   "parsed correctly")
                raise

        # Check if there is defined a days to backup
        elif backup_variables.get(self.DAYS_TO_BACKUP_KEY) is not None:
            try:
                self._days_to_backup = int(
                    backup_variables.get(self.DAYS_TO_BACKUP_KEY))
                self._internal_end_date_of_backup = (
                    self._oldest_object_bk +
                    datetime.timedelta(days=self._days_to_backup))
            except ValueError:
                self._logger.error("The days to backup should be an integer")
                raise
        # If the backup end is not set, then the ending date remains open

        # Parse the backup periodicity.
        try:
            self._periodicity = int(backup_variables.get(self.PERIODICITY_KEY))
        except ValueError:
            self._logger.error("The backup _periodicity should be an integer")
            raise

        # Parse the backup length threshold
        try:
            hours_th = int(
                backup_variables.get(self.BACKUP_LENGTH_THRESHOLD_KEY))
            self._backup_length_threshold = datetime.timedelta(hours=hours_th)
        except ValueError:
            self._logger.error("The backup length threshold should be "
                               "an integer")
            raise
Пример #2
0
    def _read_backup_info_from_dict(self, backup_variables):  # pylint: disable=too-many-branches,too-many-statements
        """
        This method reads the backup information from the given dictionary and
        sets the needed class variables.
        """
        # Setting the oldest backup date. This will be used as start of
        # the new backup procedure.
        #
        # If the oldest backup date is not set, then find the oldest
        # creation timestamp and set it as the oldest backup date.
        if backup_variables.get(self.OLDEST_OBJECT_BK_KEY) is None:
            query_node_res = self._query_first_node()

            if not query_node_res:
                self._logger.error(
                    'The oldest modification date was not found.')
                raise BackupError(
                    'The oldest modification date was not found.')

            oldest_timestamps = []
            if query_node_res:
                oldest_timestamps.append(query_node_res[0].ctime)

            self._oldest_object_bk = min(oldest_timestamps)
            self._logger.info(
                'Setting the oldest modification date to the creation date of the oldest object '
                '(%s)', self._oldest_object_bk)

        # If the oldest backup date is not None then try to parse it
        else:
            try:
                self._oldest_object_bk = parse(
                    backup_variables.get(self.OLDEST_OBJECT_BK_KEY))
                if self._oldest_object_bk.tzinfo is None:
                    curr_timezone = str(dtimezone.get_current_timezone())
                    self._oldest_object_bk = ptimezone(curr_timezone).localize(
                        self._oldest_object_bk)
                    self._logger.info(
                        'No timezone defined in the oldest modification date timestamp. Setting current timezone (%s).',
                        curr_timezone)
            # If it is not parsable...
            except ValueError:
                self._logger.error(
                    'We did not manage to parse the start timestamp of the last backup.'
                )
                raise

        # Setting the backup directory & normalizing it
        self._backup_dir = os.path.normpath(
            backup_variables.get(self.BACKUP_DIR_KEY))
        if (not self._ignore_backup_dir_existence_check
                and not os.path.isdir(self._backup_dir)):
            self._logger.error('The given backup directory does not exist.')
            raise BackupError('The given backup directory does not exist.')

        # You can not set an end-of-backup date and end days from the backup
        # that you should stop.
        if (backup_variables.get(self.DAYS_TO_BACKUP_KEY) is not None and
                backup_variables.get(self.END_DATE_OF_BACKUP_KEY) is not None):
            self._logger.error('Only one end of backup date can be set.')
            raise BackupError(
                'Only one backup end can be set (date or days from backup start.'
            )

        # Check if there is an end-of-backup date
        elif backup_variables.get(self.END_DATE_OF_BACKUP_KEY) is not None:
            try:
                self._end_date_of_backup = parse(
                    backup_variables.get(self.END_DATE_OF_BACKUP_KEY))

                if self._end_date_of_backup.tzinfo is None:
                    curr_timezone = str(dtimezone.get_current_timezone())
                    self._end_date_of_backup = \
                        ptimezone(curr_timezone).localize(
                            self._end_date_of_backup)
                    self._logger.info(
                        'No timezone defined in the end date of backup timestamp. Setting current timezone (%s).',
                        curr_timezone)

                self._internal_end_date_of_backup = self._end_date_of_backup
            except ValueError:
                self._logger.error(
                    'The end date of the backup could not be parsed correctly')
                raise

        # Check if there is defined a days to backup
        elif backup_variables.get(self.DAYS_TO_BACKUP_KEY) is not None:
            try:
                self._days_to_backup = int(
                    backup_variables.get(self.DAYS_TO_BACKUP_KEY))
                self._internal_end_date_of_backup = (
                    self._oldest_object_bk +
                    datetime.timedelta(days=self._days_to_backup))
            except ValueError:
                self._logger.error('The days to backup should be an integer')
                raise
        # If the backup end is not set, then the ending date remains open

        # Parse the backup periodicity.
        try:
            self._periodicity = int(backup_variables.get(self.PERIODICITY_KEY))
        except ValueError:
            self._logger.error('The backup _periodicity should be an integer')
            raise

        # Parse the backup length threshold
        try:
            hours_th = int(
                backup_variables.get(self.BACKUP_LENGTH_THRESHOLD_KEY))
            self._backup_length_threshold = datetime.timedelta(hours=hours_th)
        except ValueError:
            self._logger.error(
                'The backup length threshold should be an integer')
            raise