Skip to content

Commit 13e93e8

Browse files
committed
add backup frequency support for kopia backups
1 parent 70b470b commit 13e93e8

1 file changed

Lines changed: 36 additions & 15 deletions

File tree

hasadna_k8s/ceph/pvc_backup.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,42 @@ def main_shared(namespace, pvc_name, pv):
8383
print(f"ERROR! Failed to remove snapshot {backup_name} for namespace '{namespace}', pvc '{pvc_name}'")
8484

8585

86-
def main_pvc(namespace, pvc_name, pvc):
87-
print(f'Creating backup for PVC {pvc_name} in namespace {namespace}...')
88-
storage_class_name = pvc['spec']['storageClassName']
89-
volume_name = pvc['spec']['volumeName']
90-
print(f'Volume name: {volume_name}')
91-
pv = json.loads(subprocess.check_output(['kubectl', 'get', 'pv', volume_name, '-o', 'json']))
92-
if storage_class_name == 'rook-cephfs-shared':
93-
main_shared(namespace, pvc_name, pv)
94-
elif storage_class_name == 'rook-ceph-block':
95-
main_block(namespace, pvc_name, pv)
86+
def is_pvc_eligible_for_backup(namespace, pvc_name, pvc):
87+
not_eligible_msg = None
88+
if pvc['metadata']['labels'].get('hasadna/no-backup') == 'yes':
89+
not_eligible_msg = 'has label hasadna/no-backup=yes'
90+
if not_eligible_msg is None:
91+
return True
9692
else:
97-
raise Exception(f'Unexpected storage class name: {storage_class_name}')
93+
print(f'PVC {namespace}/{pvc_name} not eligible for backup: {not_eligible_msg}')
94+
return False
9895

9996

100-
def main_all():
97+
def main_pvc(namespace, pvc_name, pvc, with_weekly):
98+
backup_freq = pvc.get('metadata', {}).get('labels', {}).get('hasadna/iac-storage-backup-freq') or "weekly"
99+
assert backup_freq in ["none", "daily", "weekly"], f'Invalid backup frequency "{backup_freq}" for PVC {namespace}/{pvc_name}. Must be one of "none", "daily", "weekly".'
100+
if backup_freq == "daily" or (backup_freq == "weekly" and with_weekly):
101+
print(f'Creating backup for PVC {pvc_name} in namespace {namespace}...')
102+
storage_class_name = pvc['spec']['storageClassName']
103+
volume_name = pvc['spec']['volumeName']
104+
print(f'Volume name: {volume_name}')
105+
pv = json.loads(subprocess.check_output(['kubectl', 'get', 'pv', volume_name, '-o', 'json']))
106+
if storage_class_name == 'rook-cephfs-shared':
107+
main_shared(namespace, pvc_name, pv)
108+
elif storage_class_name == 'rook-ceph-block':
109+
main_block(namespace, pvc_name, pv)
110+
else:
111+
raise Exception(f'Unexpected storage class name: {storage_class_name}')
112+
return True
113+
else:
114+
print(f"Skipping backup for PVC {namespace}/{pvc_name} due to backup frequency '{backup_freq}' (with_weekly={with_weekly})")
115+
return False
116+
117+
118+
def main_all(with_weekly=False, with_weekly_on_saturday=False):
119+
if with_weekly_on_saturday and datetime.datetime.now().weekday() == 5:
120+
print("Saturday - setting with_weekly = True")
121+
with_weekly = True
101122
print('Fetching all PVCs eligible for backup in all namespaces...')
102123
backup_log = []
103124
for pvc in json.loads(subprocess.check_output([
@@ -109,7 +130,7 @@ def main_all():
109130
storage_class_name = pvc['spec']['storageClassName']
110131
if phase == 'Bound':
111132
if storage_class_name in ['rook-cephfs-shared', 'rook-ceph-block']:
112-
main_pvc(namespace, pvc_name, pvc)
133+
main_pvc(namespace, pvc_name, pvc, with_weekly)
113134
backup_log.append(f'{namespace}/{pvc_name}: Backup completed successfully.')
114135
else:
115136
print(f'Skipping PVC {pvc_name} in namespace {namespace} with storage class {storage_class_name}. Only rook-cephfs-shared and rook-ceph-block are eligible for backup.')
@@ -126,9 +147,9 @@ def main_all():
126147
raise Exception('CEPH_BACKUPS_HEARTBEAT_URL is not set, cannot send heartbeat.')
127148

128149

129-
def main(namespace, pvc_name):
150+
def main(namespace, pvc_name, with_weekly=False):
130151
print(f'Fetching PVC {pvc_name} in namespace {namespace}...')
131152
pvc = json.loads(subprocess.check_output([
132153
'kubectl', '-n', namespace, 'get', 'pvc', pvc_name, '-o', 'json'
133154
]))
134-
main_pvc(namespace, pvc_name, pvc)
155+
assert main_pvc(namespace, pvc_name, pvc, with_weekly)

0 commit comments

Comments
 (0)