From e1f1f525d5d69a01a983ec8e1830b183b118d8a3 Mon Sep 17 00:00:00 2001 From: Usman Nasir Date: Sun, 6 Dec 2020 14:10:09 +0500 Subject: [PATCH] complete restore of cloudbackup --- cloudAPI/cloudManager.py | 44 ++++++++++- cloudAPI/views.py | 4 + plogical/backupUtilities.py | 144 +++++++++++++++++++++++++++++++++++- plogical/mysqlUtilities.py | 1 + 4 files changed, 188 insertions(+), 5 deletions(-) diff --git a/cloudAPI/cloudManager.py b/cloudAPI/cloudManager.py index 736fdba6f..587e63291 100755 --- a/cloudAPI/cloudManager.py +++ b/cloudAPI/cloudManager.py @@ -1664,8 +1664,11 @@ class CloudManager: backupDomain = self.data['domainName'] backupsPath = '/home/cyberpanel/backups/%s/' % (backupDomain) - backups = os.listdir(backupsPath) - backups.reverse() + try: + backups = os.listdir(backupsPath) + backups.reverse() + except: + backups = [] json_data = "[" checker = 0 @@ -1732,3 +1735,40 @@ class CloudManager: data_ret = {'status': 0, 'abort': 0, 'installationProgress': "0", 'errorMessage': str(msg)} json_data = json.dumps(data_ret) return HttpResponse(json_data) + + def deleteCloudBackup(self): + try: + + backupDomain = self.data['domainName'] + backupFile = self.data['backupFile'] + backupsPathComplete = '/home/cyberpanel/backups/%s/%s' % (backupDomain, backupFile) + + command = 'rm -f %s' % (backupsPathComplete) + ProcessUtilities.executioner(command) + + final_json = json.dumps({'status': 1, 'fetchStatus': 1, 'error_message': "None"}) + 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) + + def SubmitCloudBackupRestore(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 + " SubmitCloudBackupRestore --backupDomain %s --backupFile %s --tempStoragePath %s" % (self.data['domain'], self.data['backupFile'], 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)) diff --git a/cloudAPI/views.py b/cloudAPI/views.py index 9e21468ce..f88431d33 100755 --- a/cloudAPI/views.py +++ b/cloudAPI/views.py @@ -59,6 +59,10 @@ def router(request): return cm.fetchCloudBackupSettings() elif controller == 'saveCloudBackupSettings': return cm.saveCloudBackupSettings() + elif controller == 'deleteCloudBackup': + return cm.deleteCloudBackup() + elif controller == 'SubmitCloudBackupRestore': + return cm.SubmitCloudBackupRestore() elif controller == 'fetchWebsites': return cm.fetchWebsites() elif controller == 'fetchWebsiteDataJSON': diff --git a/plogical/backupUtilities.py b/plogical/backupUtilities.py index cd843cbca..b61344ceb 100755 --- a/plogical/backupUtilities.py +++ b/plogical/backupUtilities.py @@ -1279,7 +1279,7 @@ class backupUtilities: self.CheckIfSleepNeeded() - command = 'nice -n %s cp -R %s %s' % (self.nice, self.DataPath, self.BackupDataPath) + command = 'nice -n %s cp -Rp %s %s' % (self.nice, self.DataPath, self.BackupDataPath) ProcessUtilities.executioner(command) ## Store child domains if any in json format @@ -1289,7 +1289,7 @@ class backupUtilities: import json for child in self.website.childdomains_set.all(): - childs.append(child.domain) + childs.append({'domain': child.domain, 'path': child.path, 'php': child.phpSelection}) DataJson['ChildDomains'] = childs @@ -1322,7 +1322,7 @@ class backupUtilities: self.CheckIfSleepNeeded() - command = 'nice -n %s cp -R %s %s' % (self.nice, self.DataPath, self.BackupDataPath) + command = 'nice -n %s cp -Rp %s %s' % (self.nice, self.DataPath, self.BackupDataPath) ProcessUtilities.executioner(command) ## Store child domains if any in json format @@ -1476,6 +1476,137 @@ class backupUtilities: command = 'rm -rf %s' % (self.BackupPath) ProcessUtilities.executioner(command) + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], 'Completed [200].') + + ## Restore functions + + def SubmitCloudBackupRestore(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'] + self.time = int(result['time']) + else: + self.nice = backupUtilities.NiceDefault + self.cpu = backupUtilities.CPUDefault + self.time = int(backupUtilities.time) + + self.BackupPath = '/home/cyberpanel/backups/%s/%s' % (self.extraArgs['domain'], self.extraArgs['backupFile']) + self.website = Websites.objects.get(domain=self.extraArgs['domain']) + + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Extracting main archive..,0') + + + command = 'tar -xf %s -C %s' % (self.BackupPath, '/home/cyberpanel/backups/%s/' % (self.extraArgs['domain'])) + ProcessUtilities.executioner(command) + + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Main Archive extracted,20') + + self.extractedPath = '/home/cyberpanel/backups/%s/%s' % (self.extraArgs['domain'], self.extraArgs['backupFile'].rstrip('.tar.gz')) + + self.dataPath = '%s/data' % (self.extractedPath) + logging.CyberCPLogFileWriter.writeToFile('Data path: %s' % (self.dataPath)) + self.databasesPath = '%s/databases' % (self.extractedPath) + logging.CyberCPLogFileWriter.writeToFile('Databases path: %s' % (self.databasesPath)) + self.emailsPath = '%s/emails' % (self.extractedPath) + logging.CyberCPLogFileWriter.writeToFile('Emails path: %s' % (self.emailsPath)) + + ## Data + + if os.path.exists(self.dataPath): + try: + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Creating child domains if any..,20') + childDomains = json.loads(open('%s/data.json' % (self.extractedPath), 'r').read())['ChildDomains'] + + for child in childDomains: + try: + ch = ChildDomains.objects.get(domain=child['domain']) + except: + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Creating %s,20' % (child['domain'])) + virtualHostUtilities.createDomain(self.website.domain, child['domain'], child['php'], child['path'], 1, 0, 0, + self.website.admin.userName, 0, "/home/cyberpanel/" + str(randint(1000, 9999))) + + except BaseException as msg: + logging.CyberCPLogFileWriter.writeToFile('%s [SubmitCloudBackupRestore:1533]' % str(msg)) + + homePath = '/home/%s' % (self.website.domain) + command = 'rm -rf %s' % (homePath) + ProcessUtilities.executioner(command) + + command = 'mv %s/%s %s' % (self.dataPath, self.website.domain, '/home') + ProcessUtilities.executioner(command) + + from filemanager.filemanager import FileManager + + fm = FileManager(None, None) + fm.fixPermissions(self.website.domain) + + ## Emails + + if os.path.exists(self.emailsPath): + try: + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Creating emails if any..,40') + emails = json.loads(open('%s/emails.json' % (self.extractedPath), 'r').read())['emails'] + from mailServer.models import Domains, EUsers + emailDomain = Domains.objects.get(domain=self.website.domain) + + for email in emails: + try: + eu = EUsers.objects.get(emailOwner=emailDomain, email=email['email']) + except: + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Creating %s,40' % (email['email'])) + emailAcct = EUsers(emailOwner=emailDomain, email=email['email'], password=email['password']) + emailAcct.mail = 'maildir:/home/vmail/%s/%s/Maildir' % (self.website.domain, email['email'].split('@')[0]) + emailAcct.save() + + EmailsHome = '/home/vmail/%s' % (self.website.domain) + + command = 'rm -rf %s' % (EmailsHome) + ProcessUtilities.executioner(command) + + command = 'mv %s/%s /home/vmail' % (self.emailsPath, self.website.domain) + ProcessUtilities.executioner(command) + + command = 'chown -R vmail:vmail %s' % (EmailsHome) + ProcessUtilities.executioner(command) + + + + except BaseException as msg: + logging.CyberCPLogFileWriter.writeToFile('%s [SubmitCloudBackupRestore:1533]' % str(msg)) + + ## Databases + + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Restoring databases if any..,70') + + databases = json.loads(open('%s/databases.json' % (self.extractedPath), 'r').read())['databases'] + + for db in databases: + + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], + 'Restoring database %s..,70' % (db['databaseName'])) + + mysqlUtilities.mysqlUtilities.submitDBDeletion(db['databaseName']) + + if mysqlUtilities.mysqlUtilities.createDatabase(db['databaseName'], db['databaseUser'], "cyberpanel") == 0: + raise BaseException("Failed to create Databases!") + + newDB = Databases(website=self.website, dbName=db['databaseName'], dbUser=db['databaseUser']) + newDB.save() + + mysqlUtilities.mysqlUtilities.restoreDatabaseBackup(db['databaseName'], self.databasesPath, db['password']) + + + + logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], 'Completed [200].') ### Cloud Backup functions ends @@ -1746,6 +1877,13 @@ def main(): extraArgs['databases'] = int(args.databases) bu = backupUtilities(extraArgs) bu.CloudBackups() + elif args.function == 'SubmitCloudBackupRestore': + extraArgs = {} + extraArgs['domain'] = args.backupDomain + extraArgs['tempStatusPath'] = args.tempStoragePath + extraArgs['backupFile'] = args.backupFile + bu = backupUtilities(extraArgs) + bu.SubmitCloudBackupRestore() if __name__ == "__main__": diff --git a/plogical/mysqlUtilities.py b/plogical/mysqlUtilities.py index 392fc587f..81c8fb9ac 100755 --- a/plogical/mysqlUtilities.py +++ b/plogical/mysqlUtilities.py @@ -289,6 +289,7 @@ password=%s cnfContent = """[mysqldump] user=root password=%s +max_allowed_packet=1024M [mysql] user=root password=%s