From 9b81679bad103b63320be13500f753d08c5f9a05 Mon Sep 17 00:00:00 2001 From: Usman Nasir Date: Thu, 26 Nov 2020 18:13:05 +0500 Subject: [PATCH] initiate cloudbackup --- cloudAPI/cloudManager.py | 56 +++++++++++++ cloudAPI/views.py | 4 + plogical/backupUtilities.py | 159 ++++++++++++++++++++++++++++++++++-- 3 files changed, 212 insertions(+), 7 deletions(-) diff --git a/cloudAPI/cloudManager.py b/cloudAPI/cloudManager.py index b36e2a2df..b6a2d8c60 100755 --- a/cloudAPI/cloudManager.py +++ b/cloudAPI/cloudManager.py @@ -1638,3 +1638,59 @@ class CloudManager: except BaseException as msg: return self.ajaxPre(0, str(msg)) + + def SubmitCloudBackup(self): + try: + + tempStatusPath = "/home/cyberpanel/" + str(randint(1000, 9999)) + + writeToFile = open(tempStatusPath, 'w') + writeToFile.write('Starting..,0') + writeToFile.close() + + execPath = "/usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/backupUtilities.py" + execPath = execPath + " CloudBackup --backupDomain %s --data 1 --emails 1 --databases 1 --tempStoragePath %s" % (self.data['domain'], tempStatusPath) + ProcessUtilities.popenExecutioner(execPath) + + final_dic = {'status': 1, 'tempStatusPath': tempStatusPath} + final_json = json.dumps(final_dic) + return HttpResponse(final_json) + + except BaseException as msg: + return self.ajaxPre(0, str(msg)) + + def getCurrentCloudBackups(self): + try: + + backupDomain = self.data['domainName'] + backupsPath = '/home/cyberpanel/backups/%s/' % (backupDomain) + backups = os.listdir(backupsPath) + backups.reverse() + + json_data = "[" + checker = 0 + + counter = 1 + for items in backups: + + size = str(int(int(os.path.getsize('%s/%s' % (backupsPath, items)))/int(1048576))) + + dic = {'id': counter, + 'file': items, + 'size': '%s MBs' % (size), + } + counter = counter + 1 + + if checker == 0: + json_data = json_data + json.dumps(dic) + checker = 1 + else: + json_data = json_data + ',' + json.dumps(dic) + + json_data = json_data + ']' + final_json = json.dumps({'status': 1, 'fetchStatus': 1, 'error_message': "None", "data": json_data}) + return HttpResponse(final_json) + except BaseException as msg: + final_dic = {'status': 0, 'fetchStatus': 0, 'error_message': str(msg)} + final_json = json.dumps(final_dic) + return HttpResponse(final_json) diff --git a/cloudAPI/views.py b/cloudAPI/views.py index ee7807470..18f132bea 100755 --- a/cloudAPI/views.py +++ b/cloudAPI/views.py @@ -51,6 +51,10 @@ def router(request): return cm.ReadReportDNS() elif controller == 'ResetDNSConfigurations': return cm.ResetDNSConfigurations() + elif controller == 'SubmitCloudBackup': + return cm.SubmitCloudBackup() + elif controller == 'getCurrentCloudBackups': + return cm.getCurrentCloudBackups() elif controller == 'fetchWebsites': return cm.fetchWebsites() elif controller == 'fetchWebsiteDataJSON': diff --git a/plogical/backupUtilities.py b/plogical/backupUtilities.py index c4b79db1f..a14c4e0cb 100755 --- a/plogical/backupUtilities.py +++ b/plogical/backupUtilities.py @@ -54,10 +54,15 @@ BUILD = 3 class backupUtilities: Server_root = "/usr/local/lsws" - completeKeyPath = "/home/cyberpanel/.ssh" destinationsPath = "/home/cyberpanel/destinations" licenseKey = '/usr/local/lsws/conf/license.key' + NiceDefault = '10' + CPUDefault = '40' + CloudBackupConfigPath = '/home/cyberpanel/CloudBackup.json' + + def __init__(self, extraArgs): + self.extraArgs = extraArgs @staticmethod def prepareBackupMeta(backupDomain, backupName, tempStoragePath, backupPath): @@ -1246,6 +1251,134 @@ class backupUtilities: logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [getAliases]") print(0) + ### Cloud Backup functions + + def CheckIfSleepNeeded(self): + import psutil + while (1): + if int(psutil.cpu_percent()) > int(self.cpu): + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Current CPU usage exceeds %s percent. Backup process will sleep for 10 seconds..,0' % (self.cpu)) + import time + time.sleep(10) + else: + break + + def BackupData(self): + + ### Creating the dir to store backups + self.BackupDataPath = '%s/data' % (self.BackupPath) + command = 'mkdir -p %s' % (self.BackupDataPath) + ProcessUtilities.executioner(command) + self.DataPath = '/home/%s' % (self.extraArgs['domain']) + + ## Backing up data + + self.CheckIfSleepNeeded() + + command = 'nice -n %s cp -R %s %s' % (self.nice, self.DataPath, self.BackupDataPath) + ProcessUtilities.executioner(command) + + ## Store child domains if any in json format + + DataJson = {} + childs = [] + import json + + for child in self.website.childdomains_set.all(): + childs.append(child.domain) + + DataJson['ChildDomains'] = childs + + DataJsonPath = '%s/%s' % (self.BackupPath, 'data.json') + + writeToFile = open(DataJsonPath, 'w') + writeToFile.write(json.dumps(DataJson)) + writeToFile.close() + + return 1 + + def BackupEmails(self): + + ### Creating the dir to store backups + self.BackupDataPath = '%s/emails' % (self.BackupPath) + command = 'mkdir -p %s' % (self.BackupDataPath) + ProcessUtilities.executioner(command) + self.DataPath = '/home/vmail/%s' % (self.extraArgs['domain']) + + ## Backing up data + + self.CheckIfSleepNeeded() + + command = 'nice -n %s cp -R %s %s' % (self.nice, self.DataPath, self.BackupDataPath) + ProcessUtilities.executioner(command) + + ## Store child domains if any in json format + + DataJson = {} + emailsList = [] + import json + + from mailServer.models import Domains, EUsers + emailDomain = Domains.objects.get(domainOwner=self.website) + + for emails in emailDomain.eusers_set.all(): + emailsList.append({'email': emails.email, 'password': emails.password}) + + DataJson['emails'] = emailsList + DataJsonPath = '%s/%s' % (self.BackupPath, 'emails.json') + writeToFile = open(DataJsonPath, 'w') + writeToFile.write(json.dumps(DataJson)) + writeToFile.close() + + return 1 + + def CloudBackups(self): + import json + if os.path.exists(backupUtilities.CloudBackupConfigPath): + result = json.loads(open(backupUtilities.CloudBackupConfigPath, 'r').read()) + self.nice = result['nice'] + self.cpu = result['cpu'] + else: + self.nice = backupUtilities.NiceDefault + self.cpu = backupUtilities.CPUDefault + + self.BackupPath = '/home/cyberpanel/backups/%s/backup-' % (self.extraArgs['domain']) + self.extraArgs['domain'] + "-" + time.strftime("%m.%d.%Y_%H-%M-%S") + self.website = Websites.objects.get(domain=self.extraArgs['domain']) + + command = 'mkdir -p %s' % (self.BackupPath) + ProcessUtilities.executioner(command) + + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Starting backup generation..,0') + if self.extraArgs['data']: + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Generating backup for your data,5') + if self.BackupData() == 0: + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Failed to generate backups for data. [404], 0') + return 0 + + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Data backup successfully generated,30') + + if self.extraArgs['emails']: + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Generating backup for your emails,5') + if self.BackupEmails() == 0: + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Failed to generate backups for emails. [404], 0') + return 0 + + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Emails backup successfully generated,30') + + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], 'Completed [200].') + + + + ### Cloud Backup functions ends + def submitBackupCreation(tempStoragePath, backupName, backupPath, backupDomain): try: @@ -1386,7 +1519,6 @@ def submitBackupCreation(tempStoragePath, backupName, backupPath, backupDomain): logging.CyberCPLogFileWriter.writeToFile( str(msg) + " [submitBackupCreation]") - def cancelBackupCreation(backupCancellationDomain, fileName): try: @@ -1421,7 +1553,6 @@ def cancelBackupCreation(backupCancellationDomain, fileName): str(msg) + " [cancelBackupCreation]") print("0," + str(msg)) - def submitRestore(backupFile, dir): try: @@ -1435,7 +1566,6 @@ def submitRestore(backupFile, dir): str(msg) + " [cancelBackupCreation]") print("0," + str(msg)) - def submitDestinationCreation(ipAddress, password, port='22', user='root'): setupKeys = backupUtilities.setupSSHKeys(ipAddress, password, port, user) @@ -1445,7 +1575,6 @@ def submitDestinationCreation(ipAddress, password, port='22', user='root'): else: print(setupKeys[1]) - def getConnectionStatus(ipAddress): try: checkCon = backupUtilities.checkConnection(ipAddress) @@ -1460,8 +1589,8 @@ def getConnectionStatus(ipAddress): def main(): - parser = argparse.ArgumentParser(description='CyberPanel Installer') - parser.add_argument('function', help='Specific a function to call!') + parser = argparse.ArgumentParser(description='CyberPanel Backup Generator') + parser.add_argument('function', help='Specify a function to call!') parser.add_argument('--tempStoragePath', help='') parser.add_argument('--backupName', help='!') parser.add_argument('--backupPath', help='') @@ -1485,6 +1614,13 @@ def main(): parser.add_argument('--backupFile', help='') parser.add_argument('--dir', help='') + ### For Cloud Backups + + parser.add_argument('--data', help='') + parser.add_argument('--emails', help='') + parser.add_argument('--databases', help='') + + args = parser.parse_args() if args.function == "submitBackupCreation": @@ -1501,6 +1637,15 @@ def main(): backupUtilities.startBackup(args.tempStoragePath, args.backupName, args.backupPath, args.metaPath) elif args.function == "BackupRoot": backupUtilities.BackupRoot(args.tempStoragePath, args.backupName, args.backupPath, args.metaPath) + elif args.function == 'CloudBackup': + extraArgs = {} + extraArgs['domain'] = args.backupDomain + extraArgs['tempStatusPath'] = args.tempStoragePath + extraArgs['data'] = int(args.data) + extraArgs['emails'] = int(args.emails) + extraArgs['databases'] = int(args.databases) + bu = backupUtilities(extraArgs) + bu.CloudBackups() if __name__ == "__main__":