From 6d30614b557044e5c2b741480145096e55f396bf Mon Sep 17 00:00:00 2001 From: Hassan Hashmi <75794688+habbi-hb@users.noreply.github.com> Date: Mon, 27 Jun 2022 16:28:36 +0500 Subject: [PATCH] cron0.1 --- plogical/IncScheduler.py | 205 ++++++++++++++++++++----------- plogical/applicationInstaller.py | 11 +- plogical/processUtilities.py | 7 +- 3 files changed, 145 insertions(+), 78 deletions(-) diff --git a/plogical/IncScheduler.py b/plogical/IncScheduler.py index d507cefbd..724ec186e 100644 --- a/plogical/IncScheduler.py +++ b/plogical/IncScheduler.py @@ -962,74 +962,137 @@ Automatic backup failed for %s on %s. @staticmethod def RemoteBackup(): - from websiteFunctions.models import RemoteBackupSchedule, RemoteBackupsites, WPSites - from loginSystem.models import Administrator + try: + from websiteFunctions.models import RemoteBackupSchedule, RemoteBackupsites, WPSites + from loginSystem.models import Administrator + import json + import time + from plogical.applicationInstaller import ApplicationInstaller + for config in RemoteBackupSchedule.objects.all(): + try: + configbakup=json.loads(config.config) + backuptype = configbakup['BackupType'] + if backuptype == 'Only DataBase': + Backuptype = "3" + elif backuptype == 'Only Website': + Backuptype = "2" + else: + Backuptype = "1" + except: + continue + try: + allRemoteBackupsiteobj = RemoteBackupsites.objects.filter(owner=config.pk) + for i in allRemoteBackupsiteobj: + backupsiteID=i.WPsites + wpsite=WPSites.objects.get(pk=backupsiteID) + AdminID=wpsite.owner.admin_id + Admin = Administrator.objects.get(pk=AdminID) + extraArgs = {} + extraArgs['adminID'] = Admin.pk + extraArgs['WPid'] = wpsite.pk + extraArgs['Backuptype'] = Backuptype + extraArgs['tempStatusPath'] = "/home/cyberpanel/" + str(randint(1000, 9999)) + Lastrun = config.lastrun + Currenttime = float(time.time()) + if config.timeintervel == "30 Minutes": + al = float(Currenttime) - float(1800) + if float(al) >= float(Lastrun): + # if 1 == 1: + background = ApplicationInstaller('WPCreateBackup', extraArgs) + status, msg = background.WPCreateBackup() + if status == 1: + filename = msg + IncScheduler.SendTORemote(filename, config.RemoteBackupConfig_id) + elif config.timeintervel == "1 Hour": + al = float(Currenttime) - float(3600) + if float(al) >= float(Lastrun): + background = ApplicationInstaller('WPCreateBackup', extraArgs) + status, msg = background.WPCreateBackup() + if status == 1: + filename = msg + IncScheduler.SendTORemote(filename, config.RemoteBackupConfig_id) + elif config.timeintervel == "6 Hours": + al = float(Currenttime) - float(21600) + if float(al) >= float(Lastrun): + background = ApplicationInstaller('WPCreateBackup', extraArgs) + status, msg = background.WPCreateBackup() + if status == 1: + filename = msg + IncScheduler.SendTORemote(filename, config.RemoteBackupConfig_id) + elif config.timeintervel == "12 Hours": + al = float(Currenttime) - float(43200) + if float(al) >= float(Lastrun): + background = ApplicationInstaller('WPCreateBackup', extraArgs) + status, msg = background.WPCreateBackup() + if status == 1: + filename = msg + IncScheduler.SendTORemote(filename, config.RemoteBackupConfig_id) + elif config.timeintervel == "1 Day": + al = float(Currenttime) - float(86400) + if float(al) >= float(Lastrun): + background = ApplicationInstaller('WPCreateBackup', extraArgs) + status, msg = background.WPCreateBackup() + if status == 1: + filename = msg + IncScheduler.SendTORemote(filename, config.RemoteBackupConfig_id) + elif config.timeintervel == "3 Days": + al = float(Currenttime) - float(259200) + if float(al) >= float(Lastrun): + background = ApplicationInstaller('WPCreateBackup', extraArgs) + status, msg = background.WPCreateBackup() + if status == 1: + filename = msg + IncScheduler.SendTORemote(filename, config.RemoteBackupConfig_id) + elif config.timeintervel == "1 Week": + al = float(Currenttime) - float(604800) + if float(al) >= float(Lastrun): + background = ApplicationInstaller('WPCreateBackup', extraArgs) + status, msg = background.WPCreateBackup() + if status == 1: + filename = msg + IncScheduler.SendTORemote(filename, config.RemoteBackupConfig_id) + except BaseException as msg: + print("Error Wpsite: %s"%str(msg)) + continue + except BaseException as msg: + print("Error: [RemoteBackup]: %s"%str(msg)) + logging.writeToFile('%s. [RemoteBackup]' % (str(msg))) + + + @staticmethod + def SendTORemote(FileName, RemoteBackupID): + import pysftp import json - for config in RemoteBackupSchedule.objects.all(): - try: - RemoteBackupsiteobj = RemoteBackupsites.objects.filter(owner=config.pk) - backupsiteID=RemoteBackupsiteobj.WPsites - wpsite=WPSites.objects.get(pk=backupsiteID) - AdminID=wpsite.owner.admin_id - Admin = Administrator.objects.get(pk=AdminID) - - except: - continue - try: - configbakup=json.loads(config.config) - backuptype = configbakup['BackupType'] - if backuptype == 'Only DataBase': - Backuptype = "3" - elif backuptype == 'Only Website': - Backuptype = "2" - else: - Backuptype = "1" - except: - pass + import pysftp as sftp + from websiteFunctions.models import RemoteBackupConfig - extraArgs = {} - extraArgs['adminID'] = Admin.pk - extraArgs['WPid'] = wpsite.pk - extraArgs['Backuptype'] = Backuptype - extraArgs['tempStatusPath'] = "/home/cyberpanel/" + str(randint(1000, 9999)) + try: + RemoteBackupOBJ = RemoteBackupConfig.objects.get(pk=RemoteBackupID) + config = json.loads(RemoteBackupOBJ.config) + HostName = config['Hostname'] + Username = config['Username'] + Password = config['Password'] + Path = config['Path'] - Lastrun = config.lastrun - Currenttime = float(time.time()) - if config.timeintervel == "30 Minutes": - al = float(Currenttime) - float(1800) - if float(al) >= float(Lastrun): - background = ApplicationInstaller('WPCreateBackup', extraArgs) - background.start() - elif config.timeintervel =="1 Hour": - al = float(Currenttime) - float(3600) - if float(al) >= float(Lastrun): - background = ApplicationInstaller('WPCreateBackup', extraArgs) - background.start() - elif config.timeintervel =="6 Hours": - al = float(Currenttime) - float(21600) - if float(al) >= float(Lastrun): - background = ApplicationInstaller('WPCreateBackup', extraArgs) - background.start() - elif config.timeintervel =="12 Hours": - al = float(Currenttime) - float(43200) - if float(al) >= float(Lastrun): - background = ApplicationInstaller('WPCreateBackup', extraArgs) - background.start() - elif config.timeintervel =="1 Day": - al = float(Currenttime) - float(86400) - if float(al) >= float(Lastrun): - background = ApplicationInstaller('WPCreateBackup', extraArgs) - background.start() - elif config.timeintervel =="3 Days": - al = float(Currenttime) - float(259200) - if float(al) >= float(Lastrun): - background = ApplicationInstaller('WPCreateBackup', extraArgs) - background.start() - elif config.timeintervel =="1 Week": - al = float(Currenttime) - float(604800) - if float(al) >= float(Lastrun): - background = ApplicationInstaller('WPCreateBackup', extraArgs) - background.start() + cnopts = sftp.CnOpts() + cnopts.hostkeys = None + + with pysftp.Connection(HostName, username=Username, password=Password, cnopts=cnopts) as sftp: + print("Connection succesfully stablished ... ") + + + try: + with sftp.cd(Path): + sftp.put(FileName) + except: + sftp.mkdir(Path) + with sftp.cd(Path): + sftp.put(FileName) + + + + except BaseException as msg: + logging.writeToFile('%s. [SendTORemote]' % (str(msg))) @@ -1055,12 +1118,12 @@ def main(): ### IncScheduler.startBackup(args.function) - IncScheduler.RemoteBackup(args) - IncScheduler.runGoogleDriveBackups(args.function) - IncScheduler.git(args.function) - IncScheduler.checkDiskUsage() - IncScheduler.startNormalBackups(args.function) - IncScheduler.runAWSBackups(args.function) + IncScheduler.RemoteBackup() + # IncScheduler.runGoogleDriveBackups(args.function) + # IncScheduler.git(args.function) + # IncScheduler.checkDiskUsage() + # IncScheduler.startNormalBackups(args.function) + # IncScheduler.runAWSBackups(args.function) ib.join() diff --git a/plogical/applicationInstaller.py b/plogical/applicationInstaller.py index a6eefeca1..128d9bc66 100755 --- a/plogical/applicationInstaller.py +++ b/plogical/applicationInstaller.py @@ -2262,7 +2262,6 @@ $parameters = array( Adminobj = Administrator.objects.get(pk=self.extraArgs['adminID']) Backuptype = self.extraArgs['Backuptype'] - website = wpsite.owner PhpVersion = website.phpSelection VHuser = website.externalApp @@ -2278,8 +2277,10 @@ $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}' + command = f'sudo -u {VHuser} {FinalPHPPath} -d error_reporting=0 /usr/bin/wp config get DB_NAME --skip-plugins --skip-themes --path={WPsitepath}' + print(command) retStatus, stdoutput = ProcessUtilities.outputExecutioner(command, VHuser, None, None, 1) + print(stdoutput) if stdoutput.find('Error:') == -1: DataBaseName = stdoutput.rstrip("\n") @@ -2287,7 +2288,7 @@ $parameters = array( raise BaseException(stdoutput) - command = f'{FinalPHPPath} -d error_reporting=0 /usr/bin/wp config get DB_USER --skip-plugins --skip-themes --path={WPsitepath}' + command = f'sudo -u {VHuser} {FinalPHPPath} -d error_reporting=0 /usr/bin/wp config get DB_USER --skip-plugins --skip-themes --path={WPsitepath}' retStatus, stdoutput = ProcessUtilities.outputExecutioner(command,VHuser, None, None, 1) if stdoutput.find('Error:') == -1: @@ -2316,7 +2317,7 @@ $parameters = array( ### Make directory for backup logging.statusWriter(self.tempStatusPath, 'Creating Backup Directory...,40') - command = f"mkdir -p {self.tempPath}/public_html" + command = f"sudo -u {VHuser} mkdir -p {self.tempPath}/public_html" result, stdout = ProcessUtilities.outputExecutioner(command, VHuser, None, None, 1) if result == 0: @@ -2357,7 +2358,7 @@ $parameters = array( os.chmod(configPath, 0o600) - command = f"cp -R {configPath} {self.tempPath}" + command = f"sudo -u {VHuser} cp -R {configPath} {self.tempPath}" retStatus, stdoutput = ProcessUtilities.outputExecutioner(command, VHuser, None, None, 1) if retStatus == 0: raise BaseException(stdoutput) diff --git a/plogical/processUtilities.py b/plogical/processUtilities.py index 8d7467736..731e93841 100755 --- a/plogical/processUtilities.py +++ b/plogical/processUtilities.py @@ -291,7 +291,6 @@ class ProcessUtilities(multi.Thread): @staticmethod def outputExecutioner(command, user=None, shell = None, dir = None, retRequired = None): try: - if getpass.getuser() == 'root': if os.path.exists(ProcessUtilities.debugPath): logging.writeToFile(command) @@ -299,7 +298,11 @@ class ProcessUtilities(multi.Thread): p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) else: p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - return p.communicate()[0].decode("utf-8") + + if retRequired: + return 1, p.communicate()[0].decode("utf-8") + else: + return p.communicate()[0].decode("utf-8") if type(command) == list: command = " ".join(command)