def test_time_diff(self): """Test time_diff.""" self.assertEqual( Timing.time_diff("20180106_120000", "20180106_120010"), datetime.timedelta(0, 10)) self.assertEqual( Timing.time_diff("20180106_110000", "20180106_120010"), datetime.timedelta(0, 3610))
def should_run_tran_backup(now_time, force, latest_tran_backup_timestamp, log_backup_interval_min): """Determine if a 'tran' backup can be performed according to backup window rules.""" if force: return True age_of_latest_backup_in_storage = Timing.time_diff( latest_tran_backup_timestamp, now_time) min_interval_allows_backup = age_of_latest_backup_in_storage > log_backup_interval_min perform_tran_backup = min_interval_allows_backup return perform_tran_backup
def prune_old_backups(self, older_than, filesets): """ Delete (prune) old backups from Azure storage. """ minimum_deletable_age = datetime.timedelta(7, 0) logging.warn("Deleting files older than %s", older_than) if older_than < minimum_deletable_age: msg = "Will not delete files younger than {}, ignoring".format( minimum_deletable_age) logging.warn(msg) return marker = None while True: results = self.backup_configuration.storage_client.list_blobs( container_name=self.backup_configuration. azure_storage_container_name, marker=marker) for blob in results: parts = Naming.parse_blobname(blob.name) if parts is None: continue (fileset, _is_full, start_timestamp, _vmname) = parts if (fileset != None) and not fileset in filesets: continue diff = Timing.time_diff(start_timestamp, Timing.now_localtime()) delete = diff > older_than if delete: logging.warn("Deleting %s", blob.name) self.backup_configuration.storage_client.delete_blob( container_name=self.backup_configuration. azure_storage_container_name, blob_name=blob.name) else: logging.warn("Keeping %s", blob.name) if results.next_marker: marker = results.next_marker else: break
def should_run_full_backup(now_time, force, latest_full_backup_timestamp, business_hours, db_backup_interval_min, db_backup_interval_max): """ Determine whether a backup should be executed. """ allowed_by_business = business_hours.is_backup_allowed_time(now_time) age_of_latest_backup_in_storage = Timing.time_diff( latest_full_backup_timestamp, now_time) min_interval_allows_backup = age_of_latest_backup_in_storage > db_backup_interval_min max_interval_requires_backup = age_of_latest_backup_in_storage > db_backup_interval_max perform_full_backup = (allowed_by_business and min_interval_allows_backup or max_interval_requires_backup or force) # logging.info("Full backup requested. Current time: {now}. Last backup in storage: {last}. Age of backup {age}".format(now=now_time, last=latest_full_backup_timestamp, age=age_of_latest_backup_in_storage)) # logging.info("Backup requirements: min=\"{min}\" max=\"{max}\"".format(min=db_backup_interval_min,max=db_backup_interval_max)) # logging.info("Forced by user: {force}. Backup allowed by business hours: {allowed_by_business}. min_interval_allows_backup={min_interval_allows_backup}. max_interval_requires_backup={max_interval_requires_backup}".format(force=force, allowed_by_business=allowed_by_business, min_interval_allows_backup=min_interval_allows_backup, max_interval_requires_backup=max_interval_requires_backup)) # logging.info("Decision to backup: {perform_full_backup}.".format(perform_full_backup=perform_full_backup)) return perform_full_backup