From 4b1ebdadff7278b7ff45a429d5017db7f2e650f6 Mon Sep 17 00:00:00 2001 From: Usman Nasir Date: Sun, 27 Sep 2020 18:23:44 +0500 Subject: [PATCH] complete local scheduled backups --- backup/backupManager.py | 19 ++-- backup/templates/backup/backupSchedule.html | 6 +- plogical/IncScheduler.py | 103 ++++++++++++++++++++ 3 files changed, 115 insertions(+), 13 deletions(-) diff --git a/backup/backupManager.py b/backup/backupManager.py index 45ec81431..6d0a93d50 100755 --- a/backup/backupManager.py +++ b/backup/backupManager.py @@ -27,7 +27,7 @@ import google.oauth2.credentials import googleapiclient.discovery from googleapiclient.discovery import build from websiteFunctions.models import NormalBackupDests, NormalBackupJobs, NormalBackupSites - +from plogical.IncScheduler import IncScheduler class BackupManager: localBackupPath = '/home/cyberpanel/localBackupPath' @@ -1512,22 +1512,22 @@ class BackupManager: config = json.loads(nbd.config) try: - lastRun = config['lastRun'] + lastRun = config[IncScheduler.lastRun] except: lastRun = 'Never' try: - allSites = config['allSites'] + allSites = config[IncScheduler.allSites] except: allSites = 'Selected Only' try: - frequency = config['frequency'] + frequency = config[IncScheduler.frequency] except: frequency = 'Never' try: - currentStatus = config['currentStatus'] + currentStatus = config[IncScheduler.currentStatus] except: currentStatus = 'Not running' @@ -1574,7 +1574,6 @@ class BackupManager: json_data = json.dumps(data_ret) return HttpResponse(json_data) - except BaseException as msg: data_ret = {'status': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) @@ -1600,8 +1599,8 @@ class BackupManager: config = json.loads(nbj.config) try: - if config['allSites'] == 'all': - config['allSites'] = 'Selected Only' + if config[IncScheduler.allSites] == 'all': + config[IncScheduler.allSites] = 'Selected Only' nbj.config = json.dumps(config) nbj.save() data_ret = {'status': 1} @@ -1609,7 +1608,7 @@ class BackupManager: return HttpResponse(json_data) except: pass - config['allSites'] = type + config[IncScheduler.allSites] = type nbj.config = json.dumps(config) nbj.save() @@ -1684,7 +1683,7 @@ class BackupManager: return ACLManager.loadErrorJson('scheduleStatus', 0) config = json.loads(nbj.config) - config['frequency'] = backupFrequency + config[IncScheduler.frequency] = backupFrequency nbj.config = json.dumps(config) nbj.save() diff --git a/backup/templates/backup/backupSchedule.html b/backup/templates/backup/backupSchedule.html index 5e0ef45e3..183e4cea4 100755 --- a/backup/templates/backup/backupSchedule.html +++ b/backup/templates/backup/backupSchedule.html @@ -126,7 +126,7 @@
-
+
@@ -156,7 +156,7 @@ -
+
- diff --git a/plogical/IncScheduler.py b/plogical/IncScheduler.py index ebe73de22..0eadd8382 100644 --- a/plogical/IncScheduler.py +++ b/plogical/IncScheduler.py @@ -18,6 +18,7 @@ from googleapiclient.discovery import build from googleapiclient.http import MediaFileUpload from plogical.backupSchedule import backupSchedule import requests +from websiteFunctions.models import NormalBackupJobs, NormalBackupSites, NormalBackupDests, NormalBackupJobLogs try: from plogical.virtualHostUtilities import virtualHostUtilities from plogical.mailUtilities import mailUtilities @@ -29,6 +30,15 @@ class IncScheduler(): logPath = '/home/cyberpanel/incbackuplogs' gitFolder = '/home/cyberpanel/git' + timeFormat = time.strftime("%m.%d.%Y_%H-%M-%S") + + ### Normal scheduled backups constants + + frequency = 'frequency' + allSites = 'allSites' + currentStatus = 'currentStatus' + lastRun = 'lastRun' + @staticmethod def startBackup(type): try: @@ -332,6 +342,99 @@ class IncScheduler(): GDriveJobLogs(owner=items, status=backupSchedule.ERROR, message='[Completely] Job failed, Error message: %s.' % (str(msg))).save() + @staticmethod + def startNormalBackups(type): + from plogical.processUtilities import ProcessUtilities + from plogical.backupSchedule import backupSchedule + import socket + + ## SFTP Destination Config sample + ## {"type": "SFTP", "ip": "ip", "username": "root", "port": "22", "path": "/home/backup"} + + ## Local Destination config sample + ## {"type": "local", "path": "/home/backup"} + + ## Backup jobs config + + ## {"frequency": "Daily", "allSites": "Selected Only"} + ## {"frequency": "Daily"} + + + for backupjob in NormalBackupJobs.objects.all(): + jobConfig = json.loads(backupjob.config) + destinationConfig = json.loads(backupjob.owner.config) + + currentTime = time.strftime("%m.%d.%Y_%H-%M-%S") + + if destinationConfig['type'] == 'local': + finalPath = '%s/%s' % (destinationConfig['path'].rstrip('/'), currentTime) + command = 'mkdir %s' % (finalPath) + ProcessUtilities.executioner(command) + + + if jobConfig[IncScheduler.frequency] == type: + NormalBackupJobLogs.objects.filter(owner=backupjob).delete() + NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO, + message='Starting %s backup on %s..' % (type, time.strftime("%m.%d.%Y_%H-%M-%S"))).save() + + if jobConfig[IncScheduler.allSites] == 'all': + websites = Websites.objects.all() + + else: + websites = backupjob.normalbackupsites_set.all() + + + for site in websites: + try: + domain = site.domain + except: + domain = site.domain.domain + + retValues = backupSchedule.createLocalBackup(domain, '/dev/null') + + if retValues[0] == 0: + NormalBackupJobLogs(owner=backupjob, status=backupSchedule.ERROR, + message='Backup failed for %s on %s.' % ( + domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save() + + SUBJECT = "Automatic backup failed for %s on %s." % (domain, currentTime) + adminEmailPath = '/home/cyberpanel/adminEmail' + adminEmail = open(adminEmailPath, 'r').read().rstrip('\n') + sender = 'root@%s' % (socket.gethostname()) + TO = [adminEmail] + message = """\ +From: %s +To: %s +Subject: %s + +Automatic backup failed for %s on %s. +""" % (sender, ", ".join(TO), SUBJECT, domain, currentTime) + + logging.SendEmail(sender, TO, message) + else: + backupPath = retValues[1] + ".tar.gz" + + command = 'mv %s %s' % (backupPath, finalPath) + ProcessUtilities.executioner(command) + + NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO, + message='Backup completed for %s on %s.' % ( + domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save() + + + + + + + + + + + + + + + def main():