From e552f4b09743683f49afddf6779b934c0f321f23 Mon Sep 17 00:00:00 2001 From: Usman Nasir Date: Tue, 14 Jun 2022 16:32:29 +0500 Subject: [PATCH] fix issues in backups wp --- plogical/applicationInstaller.py | 92 +++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 25 deletions(-) diff --git a/plogical/applicationInstaller.py b/plogical/applicationInstaller.py index a742302e6..d54d70557 100755 --- a/plogical/applicationInstaller.py +++ b/plogical/applicationInstaller.py @@ -6,8 +6,6 @@ import time from loginSystem.models import Administrator from plogical.acl import ACLManager - - sys.path.append('/usr/local/CyberCP') import django @@ -2285,14 +2283,22 @@ $parameters = array( logging.statusWriter(self.tempStatusPath, 'Getting database...,20') command = f'{FinalPHPPath} -d error_reporting=0 /usr/bin/wp config get DB_NAME --skip-plugins --skip-themes --path={WPsitepath}' - stdoutput = ProcessUtilities.outputExecutioner(command, VHuser) - DataBaseName = stdoutput.rstrip("\n") + + retStatus, stdoutput = ProcessUtilities.outputExecutioner(command, VHuser, None, None, 1) + + if stdoutput.find('Error:') == -1: + DataBaseName = stdoutput.rstrip("\n") + else: + raise BaseException(stdoutput) command = f'{FinalPHPPath} -d error_reporting=0 /usr/bin/wp config get DB_USER --skip-plugins --skip-themes --path={WPsitepath}' - stdoutput = ProcessUtilities.outputExecutioner(command,VHuser) - DataBaseUser = stdoutput.rstrip("\n") + retStatus, stdoutput = ProcessUtilities.outputExecutioner(command,VHuser, None, None, 1) + if stdoutput.find('Error:') == -1: + DataBaseUser = stdoutput.rstrip("\n") + else: + raise BaseException(stdoutput) ### Create secure folder @@ -2346,40 +2352,52 @@ $parameters = array( os.chmod(configPath, 0o600) command = f"cp -R {configPath} {self.tempPath}" - ProcessUtilities.executioner(command, VHuser) + if ProcessUtilities.executioner(command, VHuser) == 0: + raise BaseException('Failed to config to temp path.') + command = f"rm -r {configPath}" ProcessUtilities.executioner(command) + if ProcessUtilities.executioner(command, VHuser) == 0: + raise BaseException('Failed to remove config.') logging.statusWriter(self.tempStatusPath, 'Copying website data.....,50') ############## Copy Public_htnl to backup command = "sudo -u %s cp -R %s* %s/public_html" % (VHuser, WPsitepath, self.tempPath) - result = ProcessUtilities.outputExecutioner(command) + retStatus, result = ProcessUtilities.outputExecutioner(command, None, None, None, 1) + + if retStatus == 0: + raise BaseException(result) if os.path.exists(ProcessUtilities.debugPath): logging.writeToFile(result) command = "sudo -u %s cp -R %s.[^.]* %s/public_html/" % (VHuser, WPsitepath, self.tempPath) - result = ProcessUtilities.outputExecutioner(command) + retStatus, result = ProcessUtilities.outputExecutioner(command, None, None, None, 1) + + if retStatus == 0: + raise BaseException(result) if os.path.exists(ProcessUtilities.debugPath): logging.writeToFile(result) - logging.statusWriter(self.tempStatusPath, 'Copying DataBase.....,70') + logging.statusWriter(self.tempStatusPath, 'Copying database.....,70') ##### SQLDUMP database into new directory command = "mysqldump %s --result-file %s/%s.sql" % (DataBaseName, self.tempPath, DataBaseName) - result = ProcessUtilities.outputExecutioner(command) + retStatus, result = ProcessUtilities.outputExecutioner(command, None, None, None, 1) + + if retStatus == 0: + raise BaseException(result) if os.path.exists(ProcessUtilities.debugPath): logging.writeToFile(result) - ######## Zip backup directory logging.statusWriter(self.tempStatusPath, 'Compressing backup files.....,80') @@ -2391,13 +2409,17 @@ $parameters = array( ProcessUtilities.executioner(command) command = f"tar -czvf /home/backup/{config['name']}.tar.gz -P {self.tempPath}" - result = ProcessUtilities.outputExecutioner(command) + retStatus, result = ProcessUtilities.outputExecutioner(command, None, None, None, 1) + + if retStatus == 0: + raise BaseException(result) + + if os.path.exists(ProcessUtilities.debugPath): + logging.writeToFile(result) backupobj = WPSitesBackup(owner=Adminobj, WPSiteID=wpsite.id, WebsiteID=website.id, config=json_object) backupobj.save() - if os.path.exists(ProcessUtilities.debugPath): - logging.writeToFile(result) command = f'rm -rf {self.tempPath}' ProcessUtilities.executioner(command) @@ -2449,6 +2471,7 @@ $parameters = array( # command = "sudo -u %s touch /home/cyberpanel/config.json" % (VHuser) # ProcessUtilities.executioner(command) ###### write into config + json_object = json.dumps(config, indent=4) configPath = "/home/cyberpanel/" + str(randint(1000, 9999)) file = open(configPath, "w") @@ -2458,22 +2481,33 @@ $parameters = array( os.chmod(configPath, 0o600) command = f"cp -R {configPath} {self.tempPath}" - ProcessUtilities.executioner(command, VHuser) + + if ProcessUtilities.executioner(command, VHuser) == 0: + raise BaseException('Failed to copy config to temp path.') command = f"rm -r {configPath}" ProcessUtilities.executioner(command) + if ProcessUtilities.executioner(command, VHuser) == 0: + raise BaseException('Failed to remove config temp file.') + logging.statusWriter(self.tempStatusPath, 'Copying website data.....,50') ############## Copy Public_htnl to backup command = "sudo -u %s cp -R %s* %s/public_html" % (VHuser, WPsitepath, self.tempPath) - result = ProcessUtilities.outputExecutioner(command) + retStatus, result = ProcessUtilities.outputExecutioner(command, None, None, None, 1) + + if retStatus == 0: + raise BaseException(result) if os.path.exists(ProcessUtilities.debugPath): logging.writeToFile(result) command = "sudo -u %s cp -R %s.[^.]* %s/public_html/" % (VHuser, WPsitepath, self.tempPath) - result = ProcessUtilities.outputExecutioner(command) + retStatus, result = ProcessUtilities.outputExecutioner(command, None, None, None, 1) + + if retStatus == 0: + raise BaseException(result) if os.path.exists(ProcessUtilities.debugPath): logging.writeToFile(result) @@ -2489,7 +2523,10 @@ $parameters = array( ProcessUtilities.executioner(command) command = f"tar -czvf /home/backup/{config['name']}.tar.gz -P {self.tempPath}" - result = ProcessUtilities.outputExecutioner(command) + retStatus, result = ProcessUtilities.outputExecutioner(command, None, None, None, 1) + + if retStatus == 0: + raise BaseException(result) backupobj = WPSitesBackup(owner=Adminobj, WPSiteID=wpsite.id, WebsiteID=website.id, config=json_object) @@ -2605,9 +2642,12 @@ $parameters = array( except BaseException as msg: logging.writeToFile("Error WPCreateBackup ....... %s" % str(msg)) - command = f'rm -rf {self.tempPath}' - ProcessUtilities.executioner(command) - logging.statusWriter(self.tempStatusPath, str(msg)) + try: + command = f'rm -rf {self.tempPath}' + ProcessUtilities.executioner(command) + except: + pass + logging.statusWriter(self.tempStatusPath, f'{str(msg)}. [404]') return 0, str(msg) def RestoreWPbackupNow(self): @@ -2625,7 +2665,6 @@ $parameters = array( userID = self.extraArgs['adminID'] - #### First get BAckup file from backupobj backupobj = WPSitesBackup.objects.get(pk=backupid) @@ -3268,8 +3307,11 @@ $parameters = array( logging.statusWriter(self.tempStatusPath, 'Completed.[200]') except BaseException as msg: logging.writeToFile("Error RestoreWPbackupNow ....... %s" % str(msg)) - command = f'rm -rf {self.tempPath}' - ProcessUtilities.executioner(command) + try: + command = f'rm -rf {self.tempPath}' + ProcessUtilities.executioner(command) + except: + pass logging.statusWriter(self.tempStatusPath, str(msg)) return 0, str(msg)