@@ -86,7 +86,7 @@ def unlock_instance(driver, instance, client):
86
86
return False
87
87
88
88
89
- def make_instance_snapshot_backup (instance , error , group ,
89
+ def make_instance_old_snapshot_backup (instance , error , group ,
90
90
provider_class = VolumeProviderSnapshot ,
91
91
target_volume = None ,
92
92
current_hour = None ,
@@ -211,6 +211,144 @@ def make_instance_snapshot_backup(instance, error, group,
211
211
212
212
return snapshot
213
213
214
+ def make_instance_snapshot_backup (
215
+ instance , error , group , provider_class = VolumeProviderSnapshot , target_volume = None ,
216
+ current_hour = None , task = None , persist = 0
217
+ ):
218
+ LOG .info ("Make instance backup for {}" .format (instance ))
219
+ provider = provider_class (instance )
220
+ infra = instance .databaseinfra
221
+ database = infra .databases .first ()
222
+
223
+ backup_retry_attempts = Configuration .get_by_name_as_int ('backup_retry_attempts' , default = 3 )
224
+
225
+ snapshot = Snapshot .create (
226
+ instance , group , target_volume or provider .volume ,
227
+ environment = provider .environment , persistent = True if persist != 0 else False
228
+ )
229
+
230
+ snapshot_final_status = Snapshot .SUCCESS
231
+
232
+ locked = None
233
+ client = None
234
+ driver = infra .get_driver ()
235
+ try :
236
+ client = driver .get_client (instance )
237
+ locked = lock_instance (driver , instance , client )
238
+ if not locked :
239
+ snapshot_final_status = Snapshot .WARNING
240
+
241
+ if 'MySQL' in type (driver ).__name__ :
242
+ mysql_binlog_save (client , instance )
243
+
244
+ has_snapshot = Snapshot .objects .filter (
245
+ status = Snapshot .WARNING , instance = instance , end_at__year = datetime .now ().year ,
246
+ end_at__month = datetime .now ().month , end_at__day = datetime .now ().day
247
+ )
248
+ backup_hour_list = Configuration .get_by_name_as_list ('make_database_backup_hour' )
249
+ if not snapshot_final_status == Snapshot .WARNING and not has_snapshot :
250
+ cont = 0
251
+ for _ in range (backup_retry_attempts ):
252
+ cont += 1
253
+ try :
254
+ code = 201
255
+ response , data = provider .new_take_snapshot (persist = persist )
256
+
257
+ if response .status_code < 400 :
258
+ break
259
+
260
+ if cont >= 3 :
261
+ raise IndexError
262
+
263
+ except IndexError as e :
264
+ content , response = e
265
+ if response .status_code == 503 :
266
+ errormsg = "{} - 503 error creating snapshot for instance: {}. It will try again in 30 seconds. " .format (
267
+ strftime ("%d/%m/%Y %H:%M:%S" ), instance
268
+ )
269
+ LOG .error (errormsg )
270
+ if task :
271
+ task .add_detail (errormsg )
272
+ sleep (30 )
273
+ else :
274
+ raise e
275
+
276
+ if response .status_code < 400 :
277
+ while code != 200 :
278
+ sleep (20 )
279
+ snap_response , snap_status = provider .take_snapshot_status (data ['identifier' ])
280
+ if snap_response .status_code in [200 , 202 ]:
281
+ unlock_instance (driver , instance , client )
282
+ if snap_response .status_code == 200 :
283
+ break
284
+ if snap_response .status_code >= 400 :
285
+ raise error
286
+ code = snap_response .status_code
287
+
288
+ snapshot .done (snap_status )
289
+ snapshot .save ()
290
+ else :
291
+ errormsg = response ['message' ]
292
+ set_backup_error (infra , snapshot , errormsg )
293
+ else :
294
+ if str (current_hour ) in backup_hour_list :
295
+ raise Exception ("Backup with WARNING already created today." )
296
+
297
+ except Exception as e :
298
+ errormsg = "Error creating snapshot: {}" .format (e )
299
+ error ['errormsg' ] = errormsg
300
+ set_backup_error (infra , snapshot , errormsg )
301
+ return snapshot
302
+ finally :
303
+ unlock_instance (driver , instance , client )
304
+
305
+ if not snapshot .size :
306
+ command = "du -sb /data/.snapshot/%s | awk '{print $1}'" % (
307
+ snapshot .snapshot_name
308
+ )
309
+ try :
310
+ output = instance .hostname .ssh .run_script (command )
311
+ size = int (output ['stdout' ][0 ])
312
+ snapshot .size = size
313
+ except Exception as e :
314
+ snapshot .size = 0
315
+ LOG .error ("Error exec remote command {}" .format (e ))
316
+
317
+ backup_path = database .backup_path
318
+ if backup_path :
319
+ now = datetime .now ()
320
+ target_path = "{}/{}/{}/{}/{}" .format (
321
+ backup_path ,
322
+ now .strftime ("%Y_%m_%d" ),
323
+ instance .hostname .hostname .split ('.' )[0 ],
324
+ now .strftime ("%Y%m%d%H%M%S" ),
325
+ infra .name
326
+ )
327
+ snapshot_path = "/data/.snapshot/{}/data/" .format (
328
+ snapshot .snapshot_name
329
+ )
330
+ command = """
331
+ if [ -d "{backup_path}" ]
332
+ then
333
+ rm -rf {backup_path}/20[0-9][0-9]_[0-1][0-9]_[0-3][0-9] &
334
+ mkdir -p {target_path}
335
+ cp -r {snapshot_path} {target_path} &
336
+ fi
337
+ """ .format (backup_path = backup_path ,
338
+ target_path = target_path ,
339
+ snapshot_path = snapshot_path )
340
+ try :
341
+ instance .hostname .ssh .run_script (command )
342
+ except Exception as e :
343
+ LOG .error ("Error exec remote command {}" .format (e ))
344
+
345
+ snapshot .status = snapshot_final_status
346
+ snapshot .end_at = datetime .now ()
347
+ snapshot .save ()
348
+ register_backup_dbmonitor (infra , snapshot )
349
+
350
+ return snapshot
351
+
214
352
215
353
def make_instance_snapshot_backup_upgrade_disk (instance , error , group , provider_class = VolumeProviderSnapshot ,
216
354
target_volume = None ,
@@ -742,6 +880,7 @@ def _create_database_backup(instance, task, group, current_hour, persist):
742
880
743
881
error = {}
744
882
try :
883
+ LOG .info ('Starting make database snapshot' )
745
884
snapshot = make_instance_snapshot_backup (
746
885
instance = instance ,
747
886
error = error ,
0 commit comments