Skip to content

Commit c254e8f

Browse files
committed
add code for remote backups on sft
1 parent 640ef44 commit c254e8f

File tree

2 files changed

+98
-33
lines changed

2 files changed

+98
-33
lines changed

plogical/IncScheduler.py

Lines changed: 96 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -368,58 +368,123 @@ def startNormalBackups(type):
368368

369369
if destinationConfig['type'] == 'local':
370370
finalPath = '%s/%s' % (destinationConfig['path'].rstrip('/'), currentTime)
371-
command = 'mkdir %s' % (finalPath)
371+
command = 'mkdir -p %s' % (finalPath)
372372
ProcessUtilities.executioner(command)
373373

374+
if jobConfig[IncScheduler.frequency] == type:
375+
NormalBackupJobLogs.objects.filter(owner=backupjob).delete()
376+
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
377+
message='Starting %s backup on %s..' % (type, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
374378

375-
if jobConfig[IncScheduler.frequency] == type:
376-
NormalBackupJobLogs.objects.filter(owner=backupjob).delete()
377-
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
378-
message='Starting %s backup on %s..' % (type, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
379+
actualDomain = 0
380+
try:
381+
if jobConfig[IncScheduler.allSites] == 'all':
382+
websites = Websites.objects.all()
383+
actualDomain = 1
384+
else:
385+
websites = backupjob.normalbackupsites_set.all()
386+
except:
387+
websites = backupjob.normalbackupsites_set.all()
379388

380-
if jobConfig[IncScheduler.allSites] == 'all':
381-
websites = Websites.objects.all()
382389

383-
else:
384-
websites = backupjob.normalbackupsites_set.all()
390+
for site in websites:
385391

392+
if actualDomain:
393+
domain = site.domain
394+
else:
395+
domain = site.domain.domain
396+
397+
retValues = backupSchedule.createLocalBackup(domain, '/dev/null')
386398

387-
for site in websites:
399+
if retValues[0] == 0:
400+
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.ERROR,
401+
message='Backup failed for %s on %s.' % (
402+
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
403+
404+
SUBJECT = "Automatic backup failed for %s on %s." % (domain, currentTime)
405+
adminEmailPath = '/home/cyberpanel/adminEmail'
406+
adminEmail = open(adminEmailPath, 'r').read().rstrip('\n')
407+
sender = 'root@%s' % (socket.gethostname())
408+
TO = [adminEmail]
409+
message = """\
410+
From: %s
411+
To: %s
412+
Subject: %s
413+
414+
Automatic backup failed for %s on %s.
415+
""" % (sender, ", ".join(TO), SUBJECT, domain, currentTime)
416+
417+
logging.SendEmail(sender, TO, message)
418+
else:
419+
backupPath = retValues[1] + ".tar.gz"
420+
421+
command = 'mv %s %s' % (backupPath, finalPath)
422+
ProcessUtilities.executioner(command)
423+
424+
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
425+
message='Backup completed for %s on %s.' % (
426+
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
427+
else:
428+
import subprocess
429+
import shlex
430+
finalPath = '%s/%s' % (destinationConfig['path'].rstrip('/'), currentTime)
431+
command = "ssh -o StrictHostKeyChecking=no -p " + destinationConfig['port'] + " -i /root/.ssh/cyberpanel " + destinationConfig['username'] + "@" + destinationConfig['ip'] + " mkdir -p %s" % (finalPath)
432+
subprocess.call(shlex.split(command))
433+
434+
if jobConfig[IncScheduler.frequency] == type:
435+
NormalBackupJobLogs.objects.filter(owner=backupjob).delete()
436+
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
437+
message='Starting %s backup on %s..' % (
438+
type, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
439+
440+
actualDomain = 0
388441
try:
389-
domain = site.domain
442+
if jobConfig[IncScheduler.allSites] == 'all':
443+
websites = Websites.objects.all()
444+
actualDomain = 1
445+
else:
446+
websites = backupjob.normalbackupsites_set.all()
390447
except:
391-
domain = site.domain.domain
448+
websites = backupjob.normalbackupsites_set.all()
449+
450+
for site in websites:
392451

393-
retValues = backupSchedule.createLocalBackup(domain, '/dev/null')
452+
if actualDomain:
453+
domain = site.domain
454+
else:
455+
domain = site.domain.domain
456+
457+
retValues = backupSchedule.createLocalBackup(domain, '/dev/null')
394458

395-
if retValues[0] == 0:
396-
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.ERROR,
397-
message='Backup failed for %s on %s.' % (
398-
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
459+
if retValues[0] == 0:
460+
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.ERROR,
461+
message='Backup failed for %s on %s.' % (
462+
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
399463

400-
SUBJECT = "Automatic backup failed for %s on %s." % (domain, currentTime)
401-
adminEmailPath = '/home/cyberpanel/adminEmail'
402-
adminEmail = open(adminEmailPath, 'r').read().rstrip('\n')
403-
sender = 'root@%s' % (socket.gethostname())
404-
TO = [adminEmail]
405-
message = """\
464+
SUBJECT = "Automatic backup failed for %s on %s." % (domain, currentTime)
465+
adminEmailPath = '/home/cyberpanel/adminEmail'
466+
adminEmail = open(adminEmailPath, 'r').read().rstrip('\n')
467+
sender = 'root@%s' % (socket.gethostname())
468+
TO = [adminEmail]
469+
message = """\
406470
From: %s
407471
To: %s
408472
Subject: %s
409473
410474
Automatic backup failed for %s on %s.
411475
""" % (sender, ", ".join(TO), SUBJECT, domain, currentTime)
412476

413-
logging.SendEmail(sender, TO, message)
414-
else:
415-
backupPath = retValues[1] + ".tar.gz"
477+
logging.SendEmail(sender, TO, message)
478+
else:
479+
backupPath = retValues[1] + ".tar.gz"
416480

417-
command = 'mv %s %s' % (backupPath, finalPath)
418-
ProcessUtilities.executioner(command)
481+
command = "scp -o StrictHostKeyChecking=no -P " + destinationConfig['port'] + " -i /root/.ssh/cyberpanel " + backupPath + " " + destinationConfig['username'] + "@" + destinationConfig['ip'] + ":%s" % (finalPath)
482+
ProcessUtilities.executioner(command)
483+
484+
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
485+
message='Backup completed for %s on %s.' % (
486+
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
419487

420-
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
421-
message='Backup completed for %s on %s.' % (
422-
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
423488

424489

425490
def main():

plogical/backupSchedule.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def sendBackup(backupPath, IPAddress, backupLogPath , port='22', user='root'):
277277
##
278278

279279
writeToFile = open(backupLogPath, "a")
280-
command = "sudo scp -o StrictHostKeyChecking=no -P "+port+" -i /root/.ssh/cyberpanel " + backupPath + " " + user + "@" + IPAddress+":~/backup/" + ipAddressLocal + "/" + time.strftime("%a-%b") + "/"
280+
command = "scp -o StrictHostKeyChecking=no -P "+port+" -i /root/.ssh/cyberpanel " + backupPath + " " + user + "@" + IPAddress+":~/backup/" + ipAddressLocal + "/" + time.strftime("%m.%d.%Y_%H-%M-%S") + "/"
281281
subprocess.call(shlex.split(command), stdout=writeToFile)
282282

283283
if os.path.exists(ProcessUtilities.debugPath):
@@ -366,7 +366,7 @@ def prepare():
366366
else:
367367
## Create backup dir on remote server in ~/backup
368368

369-
command = "sudo ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel " + user + "@" + ipAddress + " mkdir -p ~/backup/" + ipAddressLocal + "/" + time.strftime(
369+
command = "ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel " + user + "@" + ipAddress + " mkdir -p ~/backup/" + ipAddressLocal + "/" + time.strftime(
370370
"%a-%b")
371371
subprocess.call(shlex.split(command))
372372
pass

0 commit comments

Comments
 (0)