def check_if_needed(self):
        last_date = utils.get_last_date_in_dir(self.dest_dir)
        report.log_msg("Last backup date: {0}".format(last_date))

        if last_date is None or args.force:
            return True

        if self.check == 'exact':
            diff = base_date - last_date
            report.log_msg("Checking exact: {0}".format(ago_format(diff)))
        elif self.check == 'daily':
            diff = base_date.date() - last_date.date()
            report.log_msg("Checking daily: {0}".format(ago_format(diff)))

        interval = timedelta(seconds=self.interval)
        return diff >= interval
 def process_vars(self, rpl):
     self.rpl = sorted(rpl, key=lambda x: -len(x[1]))
     report.log_msg("Variables:")
     for var in self.rpl:
         report.log_state("{0:>16s} = {1}".format("<{0}>".format(var[0]), var[1]))
    def perform(self):
        self.rpl = [
            ['DEST_DIR', self.dest_dir],
        ]
        self.rpl = sorted(self.rpl, key=lambda x: -len(x[1]))

        if self.delete_older_than is not None:
            interval = utils.get_interval_from_str(self.delete_older_than)

            for filename in os.listdir(self.dest_dir):
                if filename.endswith(".tmp"):
                    continue
                date = utils.get_date_from_filename(filename)
                if date is None:
                    continue

                if utils.get_date_diff_in_seconds(date, base_date) >= interval:
                    self.delete_backup_file(filename)

        elif self.clean_day_parts:
            parts = []
            # extract parts from string
            cur_date = base_date.date()
            for p in self.clean_day_parts.split(","):
                days = int(p.strip())
                start_date = cur_date - timedelta(days - 1)
                end_date = cur_date
                cur_date = start_date - timedelta(1)
                parts.append({"from": start_date, "to": end_date, "file_to_keep": None})

            # assign files to parts
            for filename in os.listdir(self.dest_dir):
                if filename.endswith(".tmp"):
                    continue
                date = utils.get_date_from_filename(filename)
                if date is None:
                    continue
                date = date.date()

                for p in parts:
                    if date >= p["from"] and date <= p["to"]:
                        if p["file_to_keep"] is None or utils.get_date_from_filename(p["file_to_keep"]).date() > date:
                            p["file_to_keep"] = filename

            # gather all files to keep
            report.log_msg("Current backups state")
            files_to_keep = []
            for p in parts:
                if p["file_to_keep"] is not None:
                    files_to_keep.append(p["file_to_keep"])

                    file_date = utils.get_date_from_filename(p["file_to_keep"])
                    file_str = "{0[file_to_keep]} ({1})".format(p, ago_format(base_date - file_date))
                else:
                    file_str = 'no file'

                report.log_state("[{0[from]}, {0[to]}] file: {1}".format(p, file_str))

            for filename in os.listdir(self.dest_dir):
                if filename.endswith(".tmp"):
                    continue
                date = utils.get_date_from_filename(filename)
                if date is None:
                    continue
                date = date.date()

                if not filename in files_to_keep:
                    self.delete_backup_file(filename)