@@ -35,7 +35,7 @@ def main_block(namespace, pvc_name, pv):
3535 kopia_snapshot_source = f'ceph@{ namespace } :{ pvc_name } '
3636 print (f'Creating Kopia snapshot with source { kopia_snapshot_source } ...' )
3737 subprocess .check_call (['kopia' , 'snapshot' , 'create' , f'/tmp{ device } ' , '--override-source' , kopia_snapshot_source ])
38- print ( "Kopia snapshot created successfully." )
38+ return "Kopia snapshot created successfully."
3939 finally :
4040 if subprocess .call (['umount' , f'/tmp{ device } ' ]) != 0 :
4141 print (f"ERROR! Failed to unmount device { device } for namespace '{ namespace } ', pvc '{ pvc_name } '" )
@@ -74,7 +74,7 @@ def main_shared(namespace, pvc_name, pv):
7474 kopia_snapshot_source = f'ceph@{ namespace } :{ pvc_name } '
7575 print (f'Creating Kopia snapshot with source { kopia_snapshot_source } ...' )
7676 subprocess .check_call (['kopia' , 'snapshot' , 'create' , path , '--override-source' , kopia_snapshot_source ])
77- print ( "Kopia snapshot created successfully." )
77+ return "Kopia snapshot created successfully."
7878 finally :
7979 if subprocess .call (['umount' , '/mnt/ceph' ]) != 0 :
8080 print (f"ERROR! Failed to unmount /mnt/ceph after creating snapshot { backup_name } for namespace '{ namespace } ', pvc '{ pvc_name } '" )
@@ -83,17 +83,6 @@ 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 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
92- else :
93- print (f'PVC { namespace } /{ pvc_name } not eligible for backup: { not_eligible_msg } ' )
94- return False
95-
96-
9786def main_pvc (namespace , pvc_name , pvc , with_weekly ):
9887 backup_freq = pvc .get ('metadata' , {}).get ('labels' , {}).get ('hasadna/iac-storage-backup-freq' ) or "weekly"
9988 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".'
@@ -104,15 +93,13 @@ def main_pvc(namespace, pvc_name, pvc, with_weekly):
10493 print (f'Volume name: { volume_name } ' )
10594 pv = json .loads (subprocess .check_output (['kubectl' , 'get' , 'pv' , volume_name , '-o' , 'json' ]))
10695 if storage_class_name == 'rook-cephfs-shared' :
107- main_shared (namespace , pvc_name , pv )
96+ return main_shared (namespace , pvc_name , pv )
10897 elif storage_class_name == 'rook-ceph-block' :
109- main_block (namespace , pvc_name , pv )
98+ return main_block (namespace , pvc_name , pv )
11099 else :
111100 raise Exception (f'Unexpected storage class name: { storage_class_name } ' )
112- return True
113101 else :
114- print (f"Skipping backup for PVC { namespace } /{ pvc_name } due to backup frequency '{ backup_freq } ' (with_weekly={ with_weekly } )" )
115- return False
102+ return f"Skipping backup for PVC { namespace } /{ pvc_name } due to backup frequency '{ backup_freq } ' (with_weekly={ with_weekly } )"
116103
117104
118105def main_all (with_weekly = False , with_weekly_on_saturday = False ):
@@ -130,8 +117,9 @@ def main_all(with_weekly=False, with_weekly_on_saturday=False):
130117 storage_class_name = pvc ['spec' ]['storageClassName' ]
131118 if phase == 'Bound' :
132119 if storage_class_name in ['rook-cephfs-shared' , 'rook-ceph-block' ]:
133- main_pvc (namespace , pvc_name , pvc , with_weekly )
134- backup_log .append (f'{ namespace } /{ pvc_name } : Backup completed successfully.' )
120+ msg = main_pvc (namespace , pvc_name , pvc , with_weekly )
121+ print (msg )
122+ backup_log .append (f'{ namespace } /{ pvc_name } : { msg } ' )
135123 else :
136124 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.' )
137125 backup_log .append (f'{ namespace } /{ pvc_name } : Skipped due to unsupported storage class { storage_class_name } .' )
@@ -140,16 +128,11 @@ def main_all(with_weekly=False, with_weekly_on_saturday=False):
140128 backup_log .append (f'{ namespace } /{ pvc_name } : Skipped due to phase { phase } . Only Bound PVCs are eligible for backup.' )
141129 print ("Great Success! Backup log:" )
142130 print ('\n ' .join (backup_log ))
143- if CEPH_BACKUPS_HEARTBEAT_URL :
144- print (f'Sending heartbeat to { CEPH_BACKUPS_HEARTBEAT_URL } ...' )
145- subprocess .check_call (['curl' , CEPH_BACKUPS_HEARTBEAT_URL ])
146- else :
147- raise Exception ('CEPH_BACKUPS_HEARTBEAT_URL is not set, cannot send heartbeat.' )
148131
149132
150133def main (namespace , pvc_name , with_weekly = False ):
151134 print (f'Fetching PVC { pvc_name } in namespace { namespace } ...' )
152135 pvc = json .loads (subprocess .check_output ([
153136 'kubectl' , '-n' , namespace , 'get' , 'pvc' , pvc_name , '-o' , 'json'
154137 ]))
155- assert main_pvc (namespace , pvc_name , pvc , with_weekly )
138+ print ( main_pvc (namespace , pvc_name , pvc , with_weekly ) )
0 commit comments