Skip to content

Commit a4feafe

Browse files
committed
restart backup if killed prematurely
1 parent c254e8f commit a4feafe

1 file changed

Lines changed: 56 additions & 12 deletions

File tree

plogical/IncScheduler.py

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -361,39 +361,77 @@ def startNormalBackups(type):
361361

362362

363363
for backupjob in NormalBackupJobs.objects.all():
364+
364365
jobConfig = json.loads(backupjob.config)
365366
destinationConfig = json.loads(backupjob.owner.config)
366367

367368
currentTime = time.strftime("%m.%d.%Y_%H-%M-%S")
368369

369370
if destinationConfig['type'] == 'local':
371+
370372
finalPath = '%s/%s' % (destinationConfig['path'].rstrip('/'), currentTime)
371373
command = 'mkdir -p %s' % (finalPath)
372374
ProcessUtilities.executioner(command)
373375

374376
if jobConfig[IncScheduler.frequency] == type:
377+
378+
### Check if an old job prematurely killed, then start from there.
379+
try:
380+
oldJobContinue = 1
381+
pid = jobConfig['pid']
382+
stuckDomain = jobConfig['website']
383+
finalPath = jobConfig['finalPath']
384+
jobConfig['pid'] = os.getpid()
385+
386+
command = 'ps aux'
387+
result = ProcessUtilities.outputExecutioner(command)
388+
389+
if result.find(pid) > -1 and result.find('IncScheduler.py') > -1:
390+
quit(1)
391+
392+
393+
except:
394+
### Save some important info in backup config
395+
oldJobContinue = 0
396+
jobConfig['pid'] = str(os.getpid())
397+
jobConfig['finalPath'] = finalPath
398+
375399
NormalBackupJobLogs.objects.filter(owner=backupjob).delete()
376400
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
377401
message='Starting %s backup on %s..' % (type, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
378402

379403
actualDomain = 0
380404
try:
381405
if jobConfig[IncScheduler.allSites] == 'all':
382-
websites = Websites.objects.all()
406+
websites = Websites.objects.all().order_by('domain')
383407
actualDomain = 1
384408
else:
385-
websites = backupjob.normalbackupsites_set.all()
409+
websites = backupjob.normalbackupsites_set.all().order_by('domain__domain')
386410
except:
387-
websites = backupjob.normalbackupsites_set.all()
411+
websites = backupjob.normalbackupsites_set.all().order_by('domain__domain')
388412

413+
doit = 0
389414

390415
for site in websites:
391-
392416
if actualDomain:
393417
domain = site.domain
394418
else:
395419
domain = site.domain.domain
396420

421+
422+
## Save currently backing domain in db, so that i can restart from here when prematurely killed
423+
424+
jobConfig['website'] = domain
425+
backupjob.config = json.dumps(jobConfig)
426+
backupjob.save()
427+
428+
if oldJobContinue and not doit:
429+
if domain == stuckDomain:
430+
doit = 1
431+
pass
432+
else:
433+
continue
434+
397435
retValues = backupSchedule.createLocalBackup(domain, '/dev/null')
398436

399437
if retValues[0] == 0:
@@ -407,12 +445,12 @@ def startNormalBackups(type):
407445
sender = 'root@%s' % (socket.gethostname())
408446
TO = [adminEmail]
409447
message = """\
410-
From: %s
411-
To: %s
412-
Subject: %s
448+
From: %s
449+
To: %s
450+
Subject: %s
413451
414-
Automatic backup failed for %s on %s.
415-
""" % (sender, ", ".join(TO), SUBJECT, domain, currentTime)
452+
Automatic backup failed for %s on %s.
453+
""" % (sender, ", ".join(TO), SUBJECT, domain, currentTime)
416454

417455
logging.SendEmail(sender, TO, message)
418456
else:
@@ -424,6 +462,12 @@ def startNormalBackups(type):
424462
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
425463
message='Backup completed for %s on %s.' % (
426464
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
465+
466+
jobConfig = json.loads(backupjob.config)
467+
if jobConfig['pid']:
468+
del jobConfig['pid']
469+
backupjob.config = json.dumps(jobConfig)
470+
backupjob.save()
427471
else:
428472
import subprocess
429473
import shlex
@@ -440,12 +484,12 @@ def startNormalBackups(type):
440484
actualDomain = 0
441485
try:
442486
if jobConfig[IncScheduler.allSites] == 'all':
443-
websites = Websites.objects.all()
487+
websites = Websites.objects.all().order_by('domain')
444488
actualDomain = 1
445489
else:
446-
websites = backupjob.normalbackupsites_set.all()
490+
websites = backupjob.normalbackupsites_set.all().order_by('domain__domain')
447491
except:
448-
websites = backupjob.normalbackupsites_set.all()
492+
websites = backupjob.normalbackupsites_set.all().order_by('domain__domain')
449493

450494
for site in websites:
451495

0 commit comments

Comments
 (0)