def backupDatabases(self):
        try:
            logging.statusWriter(self.statusPath, 'Backing up databases..', 1)

            databases = self.website.databases_set.all()

            for items in databases:
                if mysqlUtilities.mysqlUtilities.createDatabaseBackup(
                        items.dbName, '/home/cyberpanel') == 0:
                    return 0

                dbPath = '/home/cyberpanel/%s.sql' % (items.dbName)

                if self.backupDestinations == 'local':
                    self.localFunction(dbPath, 'database')
                elif self.backupDestinations[:4] == 'sftp':
                    self.sftpFunction(dbPath, 'database')
                else:
                    self.awsFunction('backup', dbPath, '', 'database')

                try:
                    os.remove('/home/cyberpanel/%s.sql' % (items.dbName))
                except BaseException, msg:
                    logging.statusWriter(
                        self.statusPath,
                        'Failed to delete database: %s. [IncJobs.backupDatabases.456]'
                        % str(msg), 1)
            return 1
    def reconstructWithMeta(self):
        try:

            if self.reconstruct == 'remote':
                if self.backupDestinations[:4] == 'sftp':
                    self.sftpFunction('none', 'none', 1)
                else:
                    self.awsFunction('restore', '', self.jobid)
            else:
                if self.jobid.destination == 'local':
                    self.localFunction('none', 'none', 1)
                elif self.jobid.destination[:4] == 'sftp':
                    self.sftpFunction('none', 'none', 1)
                else:
                    self.awsFunction('restore', '', self.jobid.snapshotid)

            metaPathNew = '/home/%s/meta.xml' % (self.website)
            execPath = "nice -n 10 /usr/local/CyberCP/bin/python2 " + virtualHostUtilities.cyberPanel + "/IncBackups/restoreMeta.py"
            execPath = execPath + " submitRestore --metaPath %s --statusFile %s" % (
                metaPathNew, self.statusPath)
            result = ProcessUtilities.outputExecutioner(execPath)
            logging.statusWriter(self.statusPath, result, 1)

            try:
                os.remove(metaPathNew)
            except:
                pass

        except BaseException, msg:
            logging.statusWriter(self.statusPath, "%s [46][5009]" % (str(msg)),
                                 1)
            return 0
Exemple #3
0
    def MainController(self):

        if self.ExtractBackup():
            pass
        else:
            return 0

        if self.CreateMainWebsite():
            pass
        else:
            return 0

        if self.CreateChildDomains():
            pass
        else:
            return 0

        if self.CreateDNSRecords():
            pass
        else:
            return 0

        if self.RestoreDatabases():
            pass
        else:
            return 0

        self.RestoreEmails()
        self.FixPermissions()

        message = 'Backup file %s successfully restored.' % (self.backupFile)
        logging.statusWriter(self.logFile, message, 1)

        return 1
    def localFunction(self, backupPath, type, restore=None):
        if restore == None:
            command = 'restic -r %s backup %s --password-file %s --exclude %s' % (
                self.repoPath, backupPath, self.passwordFile, self.repoPath)
            result = ProcessUtilities.outputExecutioner(command)
            logging.statusWriter(self.statusPath, result, 1)
            snapShotid = result.split(' ')[-2]

            if type == 'database':
                newSnapshot = JobSnapshots(
                    job=self.jobid,
                    type='%s:%s' %
                    (type, backupPath.split('/')[-1].strip('.sql')),
                    snapshotid=snapShotid,
                    destination=self.backupDestinations)
            else:
                newSnapshot = JobSnapshots(job=self.jobid,
                                           type='%s:%s' % (type, backupPath),
                                           snapshotid=snapShotid,
                                           destination=self.backupDestinations)
            newSnapshot.save()
        else:
            repoLocation = '/home/%s/incbackup' % (self.website)
            command = 'restic -r %s restore %s --target / --password-file %s' % (
                repoLocation, self.jobid.snapshotid, self.passwordFile)
            result = ProcessUtilities.outputExecutioner(command)
            logging.statusWriter(self.statusPath, result, 1)
Exemple #5
0
    def createBackup(self):
        self.statusPath = self.extraArgs['tempPath']
        website = self.extraArgs['website']
        self.backupDestinations = self.extraArgs['backupDestinations']
        websiteData = self.extraArgs['websiteData']
        websiteEmails = self.extraArgs['websiteEmails']
        websiteSSLs = self.extraArgs['websiteSSLs']
        websiteDatabases = self.extraArgs['websiteDatabases']

        self.website = Websites.objects.get(domain=website)

        newJob = IncJob(website=self.website)
        newJob.save()

        self.jobid = newJob

        self.passwordFile = '/home/%s/%s' % (self.website.domain,
                                             self.website.domain)
        password = randomPassword.generate_pass()

        self.repoPath = '/home/%s/incbackup' % (self.website.domain)

        if not os.path.exists(self.passwordFile):
            command = 'echo "%s" > %s' % (password, self.passwordFile)
            ProcessUtilities.executioner(command, self.website.externalApp)

        if self.initiateRepo() == 0:
            return

        if self.prepareBackupMeta() == 0:
            return

        if websiteData:
            if self.backupData() == 0:
                return

        if websiteDatabases:
            if self.backupDatabases() == 0:
                return

        if websiteEmails:
            if self.emailBackup() == 0:
                return

        logging.statusWriter(self.statusPath, 'Completed', 1)
Exemple #6
0
    def emailBackup(self):
        try:
            logging.statusWriter(self.statusPath, 'Backing up emails..', 1)

            backupPath = '/home/vmail/%s' % (self.website.domain)

            if os.path.exists(backupPath):
                if self.backupDestinations == 'local':
                    logging.statusWriter(self.statusPath, 'hello world', 1)
                    command = 'restic -r %s backup %s --password-file %s' % (
                        self.repoPath, backupPath, self.passwordFile)
                    snapShotid = ProcessUtilities.outputExecutioner(
                        command).split(' ')[-2]

                    newSnapshot = JobSnapshots(
                        job=self.jobid,
                        type='email:%s' % (backupPath),
                        snapshotid=snapShotid,
                        destination=self.backupDestinations)
                    newSnapshot.save()
                    logging.statusWriter(self.statusPath, 'hello world 2', 1)

                elif self.backupDestinations[:4] == 'sftp':
                    remotePath = '/home/backup/%s' % (self.website.domain)
                    command = 'export PATH=${PATH}:/usr/bin && restic -r %s:%s backup %s --password-file %s --exclude %s' % (
                        self.backupDestinations, remotePath, backupPath,
                        self.passwordFile, self.repoPath)
                    snapShotid = ProcessUtilities.outputExecutioner(
                        command).split(' ')[-2]
                    newSnapshot = JobSnapshots(
                        job=self.jobid,
                        type='email:%s' % (backupPath),
                        snapshotid=snapShotid,
                        destination=self.backupDestinations)
                    newSnapshot.save()

            logging.statusWriter(
                self.statusPath, 'Emails for %s backed to %s.' %
                (self.website.domain, self.backupDestinations), 1)
            return 1
        except BaseException, msg:
            logging.statusWriter(
                self.statusPath,
                '%s. [IncJobs.backupDatabases.269][5009]' % str(msg), 1)
            return 0
Exemple #7
0
    def backupDatabases(self):
        try:
            logging.statusWriter(self.statusPath, 'Backing up databases..', 1)

            databases = self.website.databases_set.all()

            for items in databases:
                if mysqlUtilities.mysqlUtilities.createDatabaseBackup(
                        items.dbName, '/home/cyberpanel') == 0:
                    return 0

                dbPath = '/home/cyberpanel/%s.sql' % (items.dbName)

                if self.backupDestinations == 'local':
                    command = 'restic -r %s backup %s --password-file %s' % (
                        self.repoPath, dbPath, self.passwordFile)
                    snapShotid = ProcessUtilities.outputExecutioner(
                        command).split(' ')[-2]

                    newSnapshot = JobSnapshots(
                        job=self.jobid,
                        type='database:%s' % (items.dbName),
                        snapshotid=snapShotid,
                        destination=self.backupDestinations)
                    newSnapshot.save()

                elif self.backupDestinations[:4] == 'sftp':
                    remotePath = '/home/backup/%s' % (self.website.domain)
                    command = 'export PATH=${PATH}:/usr/bin && restic -r %s:%s backup %s --password-file %s --exclude %s' % (
                        self.backupDestinations, remotePath, dbPath,
                        self.passwordFile, self.repoPath)
                    snapShotid = ProcessUtilities.outputExecutioner(
                        command).split(' ')[-2]
                    newSnapshot = JobSnapshots(
                        job=self.jobid,
                        type='database:%s' % (items.dbName),
                        snapshotid=snapShotid,
                        destination=self.backupDestinations)
                    newSnapshot.save()
            return 1
        except BaseException, msg:
            logging.statusWriter(
                self.statusPath,
                '%s. [IncJobs.backupDatabases.269][5009]' % str(msg), 1)
            return 0
    def restoreEmail(self):
        try:

            if self.reconstruct == 'remote':
                if self.backupDestinations[:4] == 'sftp':
                    self.sftpFunction('none', 'none', 1)
                else:
                    self.awsFunction('restore', '', self.jobid)
            else:
                if self.jobid.destination == 'local':
                    self.localFunction('none', 'none', 1)
                elif self.jobid.destination[:4] == 'sftp':
                    self.sftpFunction('none', 'none', 1)
                else:
                    self.awsFunction('restore', '', self.jobid.snapshotid)

        except BaseException, msg:
            logging.statusWriter(self.statusPath, "%s [46][5009]" % (str(msg)),
                                 1)
            return 0
    def restoreDatabase(self):
        try:

            if self.reconstruct == 'remote':
                if self.backupDestinations[:4] == 'sftp':
                    self.sftpFunction('none', 'none', 1)
                else:
                    self.awsFunction('restore', '', self.jobid)

                if mysqlUtilities.mysqlUtilities.restoreDatabaseBackup(
                        self.path.split('/')[-1].rstrip('.sql'),
                        '/home/cyberpanel', 'dummy', 'dummy') == 0:
                    raise BaseException
            else:

                if self.jobid.destination == 'local':
                    self.localFunction('none', 'none', 1)
                elif self.jobid.destination[:4] == 'sftp':
                    self.sftpFunction('none', 'none', 1)
                else:
                    self.awsFunction('restore', '', self.jobid.snapshotid)

                if mysqlUtilities.mysqlUtilities.restoreDatabaseBackup(
                        self.jobid.type.split(':')[1].rstrip('.sql'),
                        '/home/cyberpanel', 'dummy', 'dummy') == 0:
                    raise BaseException

            try:
                if self.reconstruct == 'remote':
                    os.remove('/home/cyberpanel/%s' %
                              (self.path.split('/')[-1]))
                else:
                    os.remove('/home/cyberpanel/%s.sql' %
                              (self.jobid.type.split(':')[1]))
            except BaseException, msg:
                logging.writeToFile(str(msg))

        except BaseException, msg:
            logging.statusWriter(self.statusPath,
                                 "%s [160][5009]" % (str(msg)), 1)
            return 0
Exemple #10
0
    def setupConnection(self, db=None):
        try:
            passFile = "/etc/cyberpanel/mysqlPassword"

            f = open(passFile)
            data = f.read()
            password = data.split('\n', 1)[0]
            password = password.replace('\n', '').replace('\r', '')

            conn = mysql.connect(user='******',
                                 passwd=password,
                                 cursorclass=cursors.SSCursor)
            cursor = conn.cursor()

            return conn, cursor

        except BaseException, msg:
            message = 'Failed to connect to database, error message: %s. [ExtractBackup]' % (
                str(msg))
            logging.statusWriter(self.logFile, message, 1)
            return 0, 0
Exemple #11
0
    def ExtractBackup(self):
        try:

            message = 'Extracting main cPanel archive file: %s' % (
                self.backupFile)
            logging.statusWriter(self.logFile, message, 1)

            if not os.path.exists(cPanelImporter.mainBackupPath):
                os.mkdir(cPanelImporter.mainBackupPath)

            os.chdir(cPanelImporter.mainBackupPath)

            command = 'tar -xf %s --directory %s' % (
                self.backupFile, cPanelImporter.mainBackupPath)
            ProcessUtilities.normalExecutioner(command)

            message = '%s successfully extracted.' % (self.backupFile)
            logging.statusWriter(self.logFile, message, 1)

            return 1

        except BaseException, msg:
            message = 'Failed to extract backup for file %s, error message: %s. [ExtractBackup]' % (
                self.backupFile, str(msg))
            logging.statusWriter(self.logFile, message, 1)
            return 0
Exemple #12
0
    def initiateRepo(self):
        try:
            logging.statusWriter(self.statusPath,
                                 'Will first initiate backup repo..', 1)

            if self.backupDestinations == 'local':
                command = 'restic init --repo %s --password-file %s' % (
                    self.repoPath, self.passwordFile)
                ProcessUtilities.executioner(command, self.website.externalApp)

            elif self.backupDestinations[:4] == 'sftp':
                remotePath = '/home/backup/%s' % (self.website.domain)
                command = 'export PATH=${PATH}:/usr/bin && restic init --repo %s:%s --password-file %s' % (
                    self.backupDestinations, remotePath, self.passwordFile)
                ProcessUtilities.executioner(command)

            logging.statusWriter(
                self.statusPath, 'Repo %s initiated for %s.' %
                (self.backupDestinations, self.website.domain), 1)
            return 1
        except BaseException, msg:
            logging.statusWriter(
                self.statusPath,
                '%s. [IncJobs.initiateRepo.47][5009]' % str(msg), 1)
            return 0
Exemple #13
0
    def awsFunction(self, fType, backupPath=None, snapshotID=None, bType=None):
        try:
            if fType == 'backup':
                key, secret = self.getAWSData()
                command = 'export AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s  && restic -r s3:s3.amazonaws.com/%s backup %s --password-file %s' % (
                    key, secret, self.website.domain, backupPath,
                    self.passwordFile)
                result = ProcessUtilities.outputExecutioner(command)
                logging.statusWriter(self.statusPath, result, 1)
                snapShotid = result.split(' ')[-2]
                if bType == 'database':
                    newSnapshot = JobSnapshots(
                        job=self.jobid,
                        type='%s:%s' %
                        (bType, backupPath.split('/')[-1].strip('.sql')),
                        snapshotid=snapShotid,
                        destination=self.backupDestinations)
                else:
                    newSnapshot = JobSnapshots(
                        job=self.jobid,
                        type='%s:%s' % (bType, backupPath),
                        snapshotid=snapShotid,
                        destination=self.backupDestinations)
                newSnapshot.save()
            else:
                if self.reconstruct == 'remote':
                    self.backupDestinations = self.backupDestinations
                    key, secret = self.getAWSData()
                    command = 'export RESTIC_PASSWORD=%s AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s  && restic -r s3:s3.amazonaws.com/%s restore %s --target /' % (
                        self.passwordFile, key, secret, self.website,
                        snapshotID)
                    result = ProcessUtilities.outputExecutioner(command)
                    logging.statusWriter(self.statusPath, result, 1)
                else:
                    self.backupDestinations = self.jobid.destination
                    key, secret = self.getAWSData()
                    command = 'export AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s  && restic -r s3:s3.amazonaws.com/%s restore %s --password-file %s --target /' % (
                        key, secret, self.website, snapshotID,
                        self.passwordFile)
                    result = ProcessUtilities.outputExecutioner(command)
                    logging.statusWriter(self.statusPath, result, 1)

        except BaseException, msg:
            logging.statusWriter(self.statusPath, "%s [88][5009]" % (str(msg)),
                                 1)
            return 0
Exemple #14
0
    def backupData(self):
        try:
            logging.statusWriter(self.statusPath, 'Backing up data..', 1)
            backupPath = '/home/%s' % (self.website.domain)

            if self.backupDestinations == 'local':
                self.localFunction(backupPath, 'data')
            elif self.backupDestinations[:4] == 'sftp':
                self.sftpFunction(backupPath, 'data')
            else:
                self.awsFunction('backup', backupPath, '', 'data')

            logging.statusWriter(
                self.statusPath, 'Data for %s backed to %s.' %
                (self.website.domain, self.backupDestinations), 1)
            return 1
        except BaseException, msg:
            logging.statusWriter(
                self.statusPath,
                '%s. [IncJobs.backupData.223][5009]' % str(msg), 1)
            return 0
Exemple #15
0
    def emailBackup(self):
        try:
            logging.statusWriter(self.statusPath, 'Backing up emails..', 1)

            backupPath = '/home/vmail/%s' % (self.website.domain)

            if self.backupDestinations == 'local':
                self.localFunction(backupPath, 'email')
            elif self.backupDestinations[:4] == 'sftp':
                self.sftpFunction(backupPath, 'email')
            else:
                self.awsFunction('backup', backupPath, '', 'email')

            logging.statusWriter(
                self.statusPath, 'Emails for %s backed to %s.' %
                (self.website.domain, self.backupDestinations), 1)
            return 1
        except BaseException, msg:
            logging.statusWriter(
                self.statusPath,
                '%s. [IncJobs.emailBackup.269][5009]' % str(msg), 1)
            return 0
Exemple #16
0
    def sftpFunction(self, backupPath, type, restore=None):
        if restore == None:
            remotePath = '/home/backup/%s' % (self.website.domain)
            command = 'export PATH=${PATH}:/usr/bin && restic -r %s:%s backup %s --password-file %s --exclude %s' % (
                self.backupDestinations, remotePath, backupPath,
                self.passwordFile, self.repoPath)
            result = ProcessUtilities.outputExecutioner(command)
            logging.statusWriter(self.statusPath, result, 1)
            snapShotid = result.split(' ')[-2]

            if type == 'database':
                newSnapshot = JobSnapshots(
                    job=self.jobid,
                    type='%s:%s' %
                    (type, backupPath.split('/')[-1].strip('.sql')),
                    snapshotid=snapShotid,
                    destination=self.backupDestinations)
            else:
                newSnapshot = JobSnapshots(job=self.jobid,
                                           type='%s:%s' % (type, backupPath),
                                           snapshotid=snapShotid,
                                           destination=self.backupDestinations)
            newSnapshot.save()
        else:
            if self.reconstruct == 'remote':
                repoLocation = '/home/backup/%s' % (self.website)
                command = 'export RESTIC_PASSWORD=%s PATH=${PATH}:/usr/bin && restic -r %s:%s restore %s --target /' % (
                    self.passwordFile, self.backupDestinations, repoLocation,
                    self.jobid)
                result = ProcessUtilities.outputExecutioner(command)
                logging.statusWriter(self.statusPath, result, 1)
            else:
                repoLocation = '/home/backup/%s' % (self.website)
                command = 'export PATH=${PATH}:/usr/bin && restic -r %s:%s restore %s --target / --password-file %s' % (
                    self.jobid.destination, repoLocation,
                    self.jobid.snapshotid, self.passwordFile)
                result = ProcessUtilities.outputExecutioner(command)
                logging.statusWriter(self.statusPath, result, 1)
Exemple #17
0
    def startBackup(type):
        try:
            logging.statusWriter(IncScheduler.logPath,
                                 'Starting Incremental Backup job..', 1)
            tempPath = "/home/cyberpanel/" + str(randint(1000, 9999))
            for job in BackupJob.objects.all():
                logging.statusWriter(
                    IncScheduler.logPath,
                    'Job Description:\n\n Destination: %s, Frequency: %s.\n ' %
                    (job.destination, job.frequency), 1)
                if job.frequency == type:
                    for web in job.jobsites_set.all():
                        logging.statusWriter(IncScheduler.logPath,
                                             'Backing up %s.' % (web.website),
                                             1)
                        extraArgs = {}
                        extraArgs['website'] = web.website
                        extraArgs['tempPath'] = tempPath
                        extraArgs['backupDestinations'] = job.destination

                        if job.websiteData == 1:
                            extraArgs['websiteData'] = True
                        else:
                            extraArgs['websiteData'] = False

                        if job.websiteDatabases == 1:
                            extraArgs['websiteDatabases'] = True
                        else:
                            extraArgs['websiteDatabases'] = False

                        if job.websiteDataEmails == 1:
                            extraArgs['websiteEmails'] = True
                        else:
                            extraArgs['websiteEmails'] = False

                        extraArgs['websiteSSLs'] = False

                        startJob = IncJobs('createBackup', extraArgs)
                        startJob.start()

                        ### Checking status

                        while True:
                            if os.path.exists(tempPath):
                                result = open(tempPath, 'r').read()

                                if result.find("Completed") > -1:

                                    ### Removing Files

                                    os.remove(tempPath)

                                    logging.statusWriter(
                                        IncScheduler.logPath,
                                        'Backed up %s.' % (web.website), 1)
                                    break
                                elif result.find("[5009]") > -1:
                                    ## removing status file, so that backup can re-runn
                                    try:
                                        os.remove(tempPath)
                                    except:
                                        pass

                                    logging.statusWriter(
                                        IncScheduler.logPath,
                                        'Failed backup for %s, error: %s.' %
                                        (web.website, result), 1)
                                    break
        except BaseException, msg:
            logging.writeToFile(str(msg))
Exemple #18
0
    def RestoreEmails(self):
        try:

            message = 'Restoring emails from archive file: %s' % (
                self.backupFile)
            logging.statusWriter(self.logFile, message, 1)

            CompletPathToExtractedArchive = cPanelImporter.mainBackupPath + self.fileName

            ### Find Mail Format
            UserData = '%s/homedir/mail' % (CompletPathToExtractedArchive)
            FormatPath = '%s/mailbox_format.cpanel' % (UserData)

            message = 'Detecting email format from %s.' % (self.backupFile)
            logging.statusWriter(self.logFile, message, 1)

            try:

                Format = open(FormatPath, 'r').read()

                if Format.find('mdbox') > -1:
                    self.mailFormat = cPanelImporter.MdBox
                    message = 'Mdbox format detected from %s.' % (
                        self.backupFile)
                    logging.statusWriter(self.logFile, message, 1)
                else:
                    self.mailFormat = cPanelImporter.MailDir
                    message = 'Maildir format detected from %s.' % (
                        self.backupFile)
                    logging.statusWriter(self.logFile, message, 1)
            except:
                self.mailFormat = cPanelImporter.MailDir

            ####

            for items in os.listdir(UserData):
                FinalMailDomainPath = '%s/%s' % (UserData, items)
                if os.path.isdir(FinalMailDomainPath):
                    if items[0] == '.':
                        continue
                    if items.find('.') > -1:
                        for it in os.listdir(FinalMailDomainPath):
                            try:
                                if self.checkIfExists(items) == 0:
                                    self.createDummyChild(items)

                                mailUtilities.createEmailAccount(
                                    items, it, 'cyberpanel')
                                finalEmailUsername = it + "@" + items
                                message = 'Starting restore for %s.' % (
                                    finalEmailUsername)
                                logging.statusWriter(self.logFile, message, 1)
                                eUser = EUsers.objects.get(
                                    email=finalEmailUsername)

                                if self.mailFormat == cPanelImporter.MailDir:
                                    eUser.mail = 'maildir:/home/vmail/%s/%s/Maildir' % (
                                        items, it)
                                    MailPath = '/home/vmail/%s/%s' % (items,
                                                                      it)

                                    command = 'mkdir -p %s' % (MailPath)
                                    ProcessUtilities.normalExecutioner(command)

                                    command = 'rm -rf %s/Maildir' % (MailPath)
                                    ProcessUtilities.normalExecutioner(command)

                                    MailPathInBackup = '%s/%s' % (
                                        FinalMailDomainPath, it)

                                    command = 'mv %s %s/Maildir' % (
                                        MailPathInBackup, MailPath)
                                    subprocess.call(command, shell=True)

                                else:
                                    eUser.mail = 'mdbox:/home/vmail/%s/%s/Mdbox' % (
                                        items, it)
                                    MailPath = '/home/vmail/%s/%s' % (items,
                                                                      it)

                                    command = 'mkdir -p %s' % (MailPath)
                                    ProcessUtilities.normalExecutioner(command)

                                    command = 'rm -rf %s/Mdbox' % (MailPath)
                                    ProcessUtilities.normalExecutioner(command)

                                    MailPathInBackup = '%s/%s' % (
                                        FinalMailDomainPath, it)

                                    command = 'mv %s %s/Mdbox' % (
                                        MailPathInBackup, MailPath)
                                    subprocess.call(command, shell=True)

                                ## Also update password

                                PasswordPath = '%s/homedir/etc/%s/shadow' % (
                                    CompletPathToExtractedArchive, items)
                                PasswordData = open(PasswordPath,
                                                    'r').readlines()

                                for i in PasswordData:
                                    if i.find(it) > -1:
                                        finalPassword = '******' % (
                                            '{CRYPT}', i.split(':')[1])
                                        eUser.password = finalPassword

                                eUser.save()

                                message = 'Restore completed for %s.' % (
                                    finalEmailUsername)
                                logging.statusWriter(self.logFile, message, 1)
                            except BaseException, msg:
                                message = 'Failed to restore emails from archive file %s, For domain: %s. error message: %s. [ExtractBackup]' % (
                                    self.backupFile, items, str(msg))
                                logging.statusWriter(self.logFile, message, 1)

            command = 'chown -R vmail:vmail /home/vmail'
            ProcessUtilities.normalExecutioner(command)

            message = 'Emails successfully restored'
            logging.statusWriter(self.logFile, message, 1)

            return 1
Exemple #19
0
    def startCloning(self):
        try:
            tempStatusPath = self.extraArgs['tempStatusPath']
            masterDomain = self.extraArgs['masterDomain']
            domain = self.extraArgs['domain']
            admin = self.extraArgs['admin']

            website = Websites.objects.get(domain=masterDomain)

            ## Creating Child Domain

            path = "/home/" + masterDomain + "/public_html/" + domain

            logging.statusWriter(
                tempStatusPath, 'Creating domain for staging environment..,5')
            phpSelection = 'PHP 7.1'
            execPath = "/usr/local/CyberCP/bin/python2 " + virtualHostUtilities.cyberPanel + "/plogical/virtualHostUtilities.py"

            execPath = execPath + " createDomain --masterDomain " + masterDomain + " --virtualHostName " + domain + \
                       " --phpVersion '" + phpSelection + "' --ssl 0 --dkimCheck 0 --openBasedir 0 --path " + path + ' --websiteOwner ' \
                       + admin.userName + ' --tempStatusPath  %s' % (tempStatusPath + '1') + " --apache 0"

            ProcessUtilities.executioner(execPath)

            domainCreationStatusPath = tempStatusPath + '1'

            data = open(domainCreationStatusPath, 'r').read()

            if data.find('[200]') > -1:
                pass
            else:
                logging.statusWriter(
                    tempStatusPath,
                    'Failed to create child-domain for staging enviroment. [404]'
                )
                return 0

            logging.statusWriter(tempStatusPath,
                                 'Domain successfully created..,15')

            ## Copying Data

            masterPath = '/home/%s/public_html' % (masterDomain)

            command = 'rsync -avzh --exclude "%s" --exclude "wp-content/backups" --exclude "wp-content/updraft" --exclude "wp-content/cache" --exclude "wp-content/plugins/litespeed-cache" %s/ %s' % (
                domain, masterPath, path)
            ProcessUtilities.executioner(command, website.externalApp)

            logging.statusWriter(tempStatusPath, 'Data copied..,50')

            ## Creating Database

            logging.statusWriter(tempStatusPath,
                                 'Creating and copying database..,50')

            dbNameRestore, dbUser, dbPassword = ApplicationInstaller(
                None, None).dbCreation(tempStatusPath, website)

            # Create dump of existing database

            configPath = '%s/wp-config.php' % (masterPath)

            if not os.path.exists(configPath):
                logging.statusWriter(tempStatusPath,
                                     'WordPress is not detected. [404]')
                return 0

            data = open(configPath, 'r').readlines()

            for items in data:
                if items.find('DB_NAME') > -1:
                    try:
                        dbName = items.split("'")[3]
                        if mysqlUtilities.createDatabaseBackup(
                                dbName, '/home/cyberpanel'):
                            break
                        else:
                            raise BaseException(
                                'Failed to create database backup.')
                    except:
                        dbName = items.split('"')[1]
                        if mysqlUtilities.createDatabaseBackup(
                                dbName, '/home/cyberpanel'):
                            break
                        else:
                            raise BaseException(
                                'Failed to create database backup.')

            databasePath = '%s/%s.sql' % ('/home/cyberpanel', dbName)

            command = "sed -i 's/%s/%s/g' %s" % (masterDomain, domain,
                                                 databasePath)
            ProcessUtilities.executioner(command, 'cyberpanel')
            command = "sed -i 's/%s/%s/g' %s" % ('https', 'http', databasePath)
            ProcessUtilities.executioner(command, 'cyberpanel')

            if not mysqlUtilities.restoreDatabaseBackup(
                    dbNameRestore, '/home/cyberpanel', None, 1, dbName):
                try:
                    os.remove(databasePath)
                except:
                    pass
                raise BaseException('Failed to restore database backup.')

            try:
                os.remove(databasePath)
            except:
                pass

            ## Update final config file

            pathFinalConfig = '%s/wp-config.php' % (path)
            data = open(pathFinalConfig, 'r').readlines()

            tmp = "/tmp/" + str(randint(1000, 9999))
            writeToFile = open(tmp, 'w')

            for items in data:
                if items.find('DB_NAME') > -1:
                    writeToFile.write("define( 'DB_NAME', '%s' );\n" %
                                      (dbNameRestore))
                elif items.find('DB_USER') > -1:
                    writeToFile.write("define( 'DB_USER', '%s' );\n" %
                                      (dbUser))
                elif items.find('DB_PASSWORD') > -1:
                    writeToFile.write("define( 'DB_PASSWORD', '%s' );\n" %
                                      (dbPassword))
                elif items.find('WP_SITEURL') > -1:
                    continue
                else:
                    writeToFile.write(items)

            writeToFile.close()

            command = 'mv %s %s' % (tmp, pathFinalConfig)
            ProcessUtilities.executioner(command, website.externalApp)

            logging.statusWriter(tempStatusPath, 'Database synced..,100')

            try:
                os.path.remove(databasePath)
            except:
                pass

            logging.statusWriter(tempStatusPath, 'Data copied..,[200]')

            return 0
        except BaseException, msg:
            mesg = '%s. [404]' % (str(msg))
            logging.statusWriter(tempStatusPath, mesg)
Exemple #20
0
    def RestoreDatabases(self):
        try:

            message = 'Restoring databases from %s.' % (self.backupFile)
            logging.statusWriter(self.logFile, message, 1)

            ##
            passFile = "/etc/cyberpanel/mysqlPassword"

            f = open(passFile)
            data = f.read()
            password = data.split('\n', 1)[0]
            ##

            connection, cursor = self.setupConnection()

            CompletPathToExtractedArchive = cPanelImporter.mainBackupPath + self.fileName

            DatabasesPath = '%s/mysql' % (CompletPathToExtractedArchive)

            for items in os.listdir(DatabasesPath):
                if items.find('roundcube') > -1:
                    continue
                if items.endswith('.sql'):
                    message = 'Restoring MySQL dump for %s.' % (items.replace(
                        '.sql', ''))
                    logging.statusWriter(self.logFile, message, 1)

                    try:
                        cursor.execute("CREATE DATABASE `%s`" %
                                       (items.replace('.sql', '')))
                    except BaseException, msg:
                        message = 'Failed while restoring database %s from backup file %s, error message: %s' % (
                            items.replace('.sql',
                                          ''), self.backupFile, str(msg))
                        logging.statusWriter(self.logFile, message, 1)

                    command = 'sudo mysql -u root -p' + password + ' ' + items.replace(
                        '.sql', '')

                    cmd = shlex.split(command)

                    DBPath = "%s/%s" % (DatabasesPath, items)

                    with open(DBPath, 'r') as f:
                        res = subprocess.call(cmd, stdin=f)

                    website = Websites.objects.get(domain=self.mainDomain)

                    ## Trying to figure out dbname

                    CommandsPath = '%s/mysql.sql' % (
                        CompletPathToExtractedArchive)

                    data = open(CommandsPath, 'r').readlines()

                    for inItems in data:
                        if inItems.find(
                                'GRANT ALL PRIVILEGES') > -1 and inItems.find(
                                    'localhost') > -1 and inItems.find(
                                        '_test') == -1:
                            cDBName = inItems.split('`')[1].replace('\\', '')
                            logging.statusWriter(self.logFile, inItems, 1)
                            if cDBName == items.replace('.sql', ''):
                                cDBUser = inItems.split("'")[1]
                                message = 'Database user for %s is %s.' % (
                                    cDBName, cDBUser)
                                logging.statusWriter(self.logFile, message, 1)
                                if Databases.objects.filter(
                                        dbUser=cDBUser).count() > 0:
                                    continue
                                break

                    db = Databases(website=website,
                                   dbName=items.replace('.sql', ''),
                                   dbUser=cDBUser)
                    db.save()

                    message = 'MySQL dump successfully restored for %s.' % (
                        items.replace('.sql', ''))
                    logging.statusWriter(self.logFile, message, 1)

            message = 'Creating Database users from backup file %s.' % (
                self.backupFile)
            logging.statusWriter(self.logFile, message, 1)

            CommandsPath = '%s/mysql.sql' % (CompletPathToExtractedArchive)

            data = open(CommandsPath, 'r').readlines()

            for items in data:
                if items.find("--") > -1 or items.find("'cyberpanel'@") > -1:
                    continue
                try:
                    cursor.execute(items)
                except BaseException, msg:
                    message = 'Failed while restoring database %s from backup file %s, error message: %s' % (
                        items.replace('.sql', ''), self.backupFile, str(msg))
                    logging.statusWriter(self.logFile, message, 1)
Exemple #21
0
    def setupNode(self):
        try:

            if ProcessUtilities.decideDistro() == ProcessUtilities.centos:
                mesg = 'Clusters are only supported on Ubuntu 18.04. [404]'
                logging.statusWriter(self.data['tempStatusPath'], mesg)
                return 0

            userID = self.request.session['userID']
            currentACL = ACLManager.loadedACL(userID)

            if currentACL['admin'] == 0:
                mesg = 'Only administrators can create clusters. [404]'
                logging.statusWriter(self.data['tempStatusPath'], mesg)
                return 0

            logging.statusWriter(self.data['tempStatusPath'],
                                 'Setting up node in progress..')

            commands = self.data['commands']

            for command in commands:
                try:
                    result = subprocess.call(command, shell=True)
                    if result != 0:
                        logging.writeToFile(command + ' Failed.')
                except BaseException:
                    logging.statusWriter(self.data['tempStatusPath'],
                                         command + ' Failed. [404]')
                    return 0

            try:
                FirewallUtilities.addRule('tcp', '2377', "0.0.0.0/0")
                fwRule = FirewallRules(name="Docker", port='2377', proto="tcp")
                fwRule.save()
            except:
                pass

            mesg = 'Node successfully configured. [200]'
            logging.statusWriter(self.data['tempStatusPath'], mesg)

        except BaseException, msg:
            logging.writeToFile(str(msg))
            logging.statusWriter(self.data['tempStatusPath'],
                                 str(msg) + '. [404]')
Exemple #22
0
    def CreateDNSRecords(self):
        try:

            message = 'We are going to create DNS records now, please note we will not create DKIM records. Make sure to create them from CyberPanel interface using our DKIM manager.'
            logging.statusWriter(self.logFile, message, 1)

            ipFile = "/etc/cyberpanel/machineIP"
            f = open(ipFile)
            ipData = f.read()
            ipAddress = ipData.split('\n', 1)[0]
            admin = Administrator.objects.get(pk=1)

            CompletPathToExtractedArchive = cPanelImporter.mainBackupPath + self.fileName
            DNSZonesPath = '%s/dnszones' % (CompletPathToExtractedArchive)

            for items in os.listdir(DNSZonesPath):
                topLevelDomain = items.replace('.db', '', 1)

                message = 'Creating DNS records for %s' % (topLevelDomain)
                logging.statusWriter(self.logFile, message, 1)

                try:
                    Domains.objects.get(name=topLevelDomain).delete()
                except:
                    pass

                try:
                    pdns = PDNSStatus.objects.get(pk=1)
                    if pdns.type == 'MASTER':
                        zone = Domains(admin=admin,
                                       name=topLevelDomain,
                                       type="MASTER")
                        zone.save()
                    else:
                        zone = Domains(admin=admin,
                                       name=topLevelDomain,
                                       type="NATIVE")
                        zone.save()
                except:
                    pass

                content = "ns1." + topLevelDomain + " hostmaster." + topLevelDomain + " 1 10800 3600 604800 3600"

                soaRecord = Records(domainOwner=zone,
                                    domain_id=zone.id,
                                    name=topLevelDomain,
                                    type="SOA",
                                    content=content,
                                    ttl=3600,
                                    prio=0,
                                    disabled=0,
                                    auth=1)
                soaRecord.save()

                CurrentZonePath = '%s/%s' % (DNSZonesPath, items)

                data = open(CurrentZonePath, 'r').readlines()

                SOACheck = 0
                start = 0

                for items in data:
                    try:
                        if items.find('SOA') > -1:
                            SOACheck = 1
                            continue

                        if SOACheck == 1 and items.find(')') > -1:
                            SOACheck = 0
                            start = 1
                            continue
                        else:
                            pass

                        if start == 1:
                            if len(items) > 3:
                                if items.find("DKIM1") > -1:
                                    continue
                                RecordsData = items.split('\t')

                                if RecordsData[3] == 'A':
                                    RecordsData[4] = ipAddress

                                if RecordsData[0].find(topLevelDomain) > -1:
                                    if RecordsData[3] == 'MX':
                                        DNS.createDNSRecord(
                                            zone, RecordsData[0].rstrip('.'),
                                            RecordsData[3],
                                            RecordsData[5].rstrip('.').rstrip(
                                                '.\n'), int(RecordsData[4]),
                                            RecordsData[1])
                                    else:
                                        DNS.createDNSRecord(
                                            zone, RecordsData[0].rstrip('.'),
                                            RecordsData[3],
                                            RecordsData[4].rstrip('.').rstrip(
                                                '.\n'), 0, RecordsData[1])
                                else:
                                    if RecordsData[3] == 'MX':
                                        DNS.createDNSRecord(
                                            zone, RecordsData[0] + '.' +
                                            topLevelDomain, RecordsData[3],
                                            RecordsData[5].rstrip('.').rstrip(
                                                '.\n'), RecordsData[4],
                                            RecordsData[1])
                                    else:
                                        DNS.createDNSRecord(
                                            zone, RecordsData[0] + '.' +
                                            topLevelDomain, RecordsData[3],
                                            RecordsData[4].rstrip('.').rstrip(
                                                '.\n'), 0, RecordsData[1])
                    except BaseException, msg:
                        message = 'Failed while creating DNS entry for %s, error message: %s.' % (
                            topLevelDomain, str(msg))
                        logging.statusWriter(self.logFile, message, 1)

                message = 'DNS records successfully created for %s.' % (
                    topLevelDomain)
                logging.statusWriter(self.logFile, message, 1)

            return 1
Exemple #23
0
    def CreateChildDomains(self):
        try:

            message = 'Creating child domains from archive file: %s' % (
                self.backupFile)
            logging.statusWriter(self.logFile, message, 1)

            CompletPathToExtractedArchive = cPanelImporter.mainBackupPath + self.fileName

            ### Find Possible Child Domains

            message = 'Finding Addon/Subdomains from backup file %s. Account main domain was %s.' % (
                self.backupFile, self.mainDomain)
            logging.statusWriter(self.logFile, message, 1)

            UserData = '%s/userdata/main' % (CompletPathToExtractedArchive)

            data = open(UserData, 'r').readlines()
            Domains = []
            addonStatus = 0
            subDomainsStatus = 0

            for items in data:
                if items.find('addon_domains') > -1:
                    addonStatus = 1
                    continue

                if addonStatus == 1:
                    if items.find('main_domain') > -1:
                        addonStatus = 0
                        continue
                    else:
                        cDomain = items.split(':')[0].replace(' ', '')
                        if len(cDomain) < 2:
                            continue
                        Domains.append(ChildDomains(cDomain, 1))
                        continue

                ##

                if items.find('sub_domains') > -1:
                    subDomainsStatus = 1
                    continue

                existCheck = 0
                if subDomainsStatus == 1:
                    cDomain = items.split(' ')[-1].replace('\n', '')
                    for items in Domains:
                        if cDomain.find(items.domain) > -1:
                            existCheck = 1
                    if existCheck == 0:
                        if len(cDomain) > 2:
                            Domains.append(ChildDomains(cDomain, 0))

            message = 'Following Addon/Subdomains found for backup file %s. Account main domain was %s.' % (
                self.backupFile, self.mainDomain)
            logging.statusWriter(self.logFile, message, 1)

            for items in Domains:
                print items.domain

            ## Starting Child-domains creation

            message = 'Starting Addon/Subdomains creation from backup file %s. Account main domain was %s.' % (
                self.backupFile, self.mainDomain)
            logging.statusWriter(self.logFile, message, 1)

            for items in Domains:

                try:

                    message = 'Creating %s.' % (items.domain)
                    logging.statusWriter(self.logFile, message, 1)

                    path = '/home/' + self.mainDomain + '/public_html/' + items.domain

                    ## Find PHP Version

                    if items.addon == 1:
                        DomainMeta = '%s/userdata/%s.%s' % (
                            CompletPathToExtractedArchive, items.domain,
                            self.mainDomain)
                    else:
                        DomainMeta = '%s/userdata/%s' % (
                            CompletPathToExtractedArchive, items.domain)

                    data = open(DomainMeta, 'r').readlines()
                    phpChecker = 1
                    for it in data:
                        if it.find('phpversion') > -1:
                            self.PHPVersion = it.split(' ')[-1].replace(
                                '\n', '')
                            self.PHPDecider()
                            phpChecker = 0
                            break

                    if phpChecker:
                        self.PHPDecider()

                    message = 'Calling core to create %s.' % (items.domain)
                    logging.statusWriter(self.logFile, message, 1)

                    result = virtualHostUtilities.createDomain(
                        self.mainDomain, items.domain, self.PHPVersion, path,
                        0, 0, 0, 'admin', 0)

                    if result[0] == 1:
                        message = 'Child domain %s created from archive file: %s' % (
                            items.domain, self.backupFile)
                        logging.statusWriter(self.logFile, message, 1)
                    else:
                        message = 'Failed to create Child domain %s from archive file: %s' % (
                            items.domain, self.backupFile)
                        logging.statusWriter(self.logFile, message, 1)

                    ## Setup SSL

                    message = 'Detecting SSL for %s.' % (items.domain)
                    logging.statusWriter(self.logFile, message, 1)

                    SSLPath = '%s/apache_tls/%s' % (
                        CompletPathToExtractedArchive, items.domain)

                    if os.path.exists(SSLPath):
                        message = 'SSL found for %s, setting up.' % (
                            items.domain)
                        logging.statusWriter(self.logFile, message, 1)
                        self.SetupSSL(SSLPath, items.domain)
                        message = 'SSL set up OK for %s.' % (items.domain)
                        logging.statusWriter(self.logFile, message, 1)
                    else:
                        SSLPath = '%s/apache_tls/%s.%s' % (
                            CompletPathToExtractedArchive, items.domain,
                            self.mainDomain)
                        if os.path.exists(SSLPath):
                            message = 'SSL found for %s, setting up.' % (
                                items.domain)
                            logging.statusWriter(self.logFile, message, 1)
                            self.SetupSSL(SSLPath, items.domain)
                            message = 'SSL set up OK for %s.' % (items.domain)
                            logging.statusWriter(self.logFile, message, 1)
                        else:
                            message = 'SSL not detected for %s, you can later issue SSL from Manage SSL in CyberPanel.' % (
                                items.domain)
                            logging.statusWriter(self.logFile, message, 1)

                    ## Creating Document root for childs

                    message = 'Restoring document root files for %s.' % (
                        items.domain)
                    logging.statusWriter(self.logFile, message, 1)

                    externalApp = "".join(
                        re.findall("[a-zA-Z]+", self.mainDomain))[:7]

                    data = open(DomainMeta, 'r').readlines()

                    for items in data:
                        if items.find('documentroot') > -1:
                            ChildDocRoot = items.split(' ')[-1].replace(
                                '\n', '')
                            break

                    if os.path.exists(path):
                        shutil.rmtree(path)

                    movePath = '%s/homedir/public_html/%s' % (
                        CompletPathToExtractedArchive,
                        ChildDocRoot.replace(self.documentRoot, '', 1).replace(
                            '/', ''))

                    if os.path.exists(movePath):
                        shutil.move(movePath, path)
                    else:
                        movePath = '%s/homedir/%s' % (
                            CompletPathToExtractedArchive,
                            ChildDocRoot.split('/')[-1].replace(
                                self.documentRoot, '', 1).replace('/', ''))
                        if os.path.exists(movePath):
                            shutil.move(movePath, path)
                        else:
                            movePath = '%s/homedir/%s' % (
                                CompletPathToExtractedArchive, items.domain)
                            shutil.move(movePath, path)

                    command = 'chown -R %s:%s %s' % (externalApp, externalApp,
                                                     path)
                    ProcessUtilities.normalExecutioner(command)

                    message = 'Successfully created child domain.'
                    logging.statusWriter(self.logFile, message, 1)
                except BaseException, msg:
                    message = 'Failed to create child domain from backup file %s, error message: %s. Moving on..' % (
                        self.backupFile, str(msg))

            return 1
Exemple #24
0
    def CreateMainWebsite(self):
        try:

            message = 'Creating main account from archive file: %s' % (
                self.backupFile)
            logging.statusWriter(self.logFile, message, 1)

            CompletPathToExtractedArchive = cPanelImporter.mainBackupPath + self.fileName

            ### Find Domain Name
            UserData = '%s/userdata/main' % (CompletPathToExtractedArchive)

            data = open(UserData, 'r').readlines()
            DomainName = ''

            for items in data:
                if items.find('main_domain') > -1:
                    DomainName = items.split(' ')[-1].replace('\n', '')
                    self.mainDomain = DomainName
                    break

            message = 'Detected main domain for this file is: %s.' % (
                DomainName)
            logging.statusWriter(self.logFile, message, 1)

            ## Find PHP Version

            message = 'Finding PHP version for %s.' % (DomainName)
            logging.statusWriter(self.logFile, message, 1)

            DomainMeta = '%s/userdata/%s' % (CompletPathToExtractedArchive,
                                             DomainName)

            data = open(DomainMeta, 'r').readlines()
            phpChecker = 1

            for items in data:
                if items.find('phpversion') > -1:
                    self.PHPVersion = items.split(' ')[-1].replace('\n', '')
                    self.PHPDecider()
                    phpChecker = 0
                    break

            if phpChecker:
                self.PHPDecider()

            message = 'PHP version of %s is %s.' % (DomainName,
                                                    self.PHPVersion)
            logging.statusWriter(self.logFile, message, 1)

            ## Find Email

            message = 'Finding Server Admin email for %s.' % (DomainName)
            logging.statusWriter(self.logFile, message, 1)

            data = open(DomainMeta, 'r').readlines()

            for items in data:
                if items.find('serveradmin') > -1:
                    self.email = items.split(' ')[-1].replace('\n', '')
                    break

            message = 'Server Admin email for %s is %s.' % (DomainName,
                                                            self.email)
            logging.statusWriter(self.logFile, message, 1)

            ## Create Site

            message = 'Calling core to create %s.' % (DomainName)
            logging.statusWriter(self.logFile, message, 1)

            self.externalApp = "".join(re.findall("[a-zA-Z]+", DomainName))[:7]

            try:
                counter = 0
                while True:
                    tWeb = Websites.objects.get(externalApp=self.externalApp)
                    self.externalApp = '%s%s' % (tWeb.externalApp,
                                                 str(counter))
                    counter = counter + 1
                    print self.externalApp
            except BaseException, msg:
                logging.statusWriter(self.logFile, str(msg), 1)
                time.sleep(2)

            result = virtualHostUtilities.createVirtualHost(
                DomainName, self.email, self.PHPVersion, self.externalApp, 0,
                0, 0, 'admin', 'Default', 0)

            if result[0] == 1:
                pass
            else:
                message = 'Failed to create main site %s from archive file: %s' % (
                    DomainName, self.backupFile)
                logging.statusWriter(self.logFile, message, 1)
                return 0

            message = 'Successfully created %s from core.' % (DomainName)
            logging.statusWriter(self.logFile, message, 1)

            ### Let see if there is SSL

            message = 'Detecting SSL for %s.' % (DomainName)
            logging.statusWriter(self.logFile, message, 1)

            SSLPath = '%s/apache_tls/%s' % (CompletPathToExtractedArchive,
                                            DomainName)

            if os.path.exists(SSLPath):
                message = 'SSL found for %s, setting up.' % (DomainName)
                logging.statusWriter(self.logFile, message, 1)
                self.SetupSSL(SSLPath, DomainName)
                message = 'SSL set up OK for %s.' % (DomainName)
                logging.statusWriter(self.logFile, message, 1)
            else:
                message = 'SSL not detected for %s, you can later issue SSL from Manage SSL in CyberPanel.' % (
                    DomainName)
                logging.statusWriter(self.logFile, message, 1)

            ## Document root

            message = 'Restoring document root files for %s.' % (DomainName)
            logging.statusWriter(self.logFile, message, 1)

            data = open(DomainMeta, 'r').readlines()

            for items in data:
                if items.find('homedir') > -1:
                    self.homeDir = items.split(' ')[-1].replace('\n', '')
                    break

            data = open(DomainMeta, 'r').readlines()

            for items in data:
                if items.find('documentroot') > -1:
                    self.documentRoot = items.split(' ')[-1].replace('\n', '')
                    break

            nowPath = '/home/%s/public_html' % (DomainName)
            if os.path.exists(nowPath):
                shutil.rmtree(nowPath)

            movePath = '%s/homedir/%s' % (CompletPathToExtractedArchive,
                                          self.documentRoot.replace(
                                              self.homeDir, '', 1).replace(
                                                  '/', ''))

            shutil.copytree(movePath, nowPath, symlinks=True)

            command = 'chown -R %s:%s %s' % (self.externalApp,
                                             self.externalApp, nowPath)
            ProcessUtilities.normalExecutioner(command)

            message = 'Main site %s created from archive file: %s' % (
                DomainName, self.backupFile)
            logging.statusWriter(self.logFile, message, 1)

            return 1
Exemple #25
0
                reparsed = minidom.parseString(rough_string)
                return reparsed.toprettyxml(indent="  ")

            ## /home/example.com/backup/backup-example-06-50-03-Thu-Feb-2018/meta.xml -- metaPath

            metaPath = '/home/cyberpanel/%s' % (str(randint(1000, 9999)))

            xmlpretty = prettify(metaFileXML).encode('ascii', 'ignore')
            metaFile = open(metaPath, 'w')
            metaFile.write(xmlpretty)
            metaFile.close()
            os.chmod(metaPath, 0640)

            ## meta generated

            logging.statusWriter(self.statusPath, 'Meta data is ready..', 1)

            metaPathNew = '/home/%s/meta.xml' % (self.website.domain)
            command = 'mv %s %s' % (metaPath, metaPathNew)
            ProcessUtilities.executioner(command)

            return 1

        except BaseException, msg:
            logging.statusWriter(self.statusPath,
                                 "%s [207][5009]" % (str(msg)), 1)
            return 0

    def backupData(self):
        try:
            logging.statusWriter(self.statusPath, 'Backing up data..', 1)
Exemple #26
0
            shutil.copytree(movePath, nowPath, symlinks=True)

            command = 'chown -R %s:%s %s' % (self.externalApp,
                                             self.externalApp, nowPath)
            ProcessUtilities.normalExecutioner(command)

            message = 'Main site %s created from archive file: %s' % (
                DomainName, self.backupFile)
            logging.statusWriter(self.logFile, message, 1)

            return 1

        except BaseException, msg:
            message = 'Failed to create main website from backup file %s, error message: %s.' % (
                self.backupFile, str(msg))
            logging.statusWriter(self.logFile, message, 1)
            return 0

    def CreateChildDomains(self):
        try:

            message = 'Creating child domains from archive file: %s' % (
                self.backupFile)
            logging.statusWriter(self.logFile, message, 1)

            CompletPathToExtractedArchive = cPanelImporter.mainBackupPath + self.fileName

            ### Find Possible Child Domains

            message = 'Finding Addon/Subdomains from backup file %s. Account main domain was %s.' % (
                self.backupFile, self.mainDomain)
Exemple #27
0
    def createBackup(self):
        self.statusPath = self.extraArgs['tempPath']
        website = self.extraArgs['website']
        self.backupDestinations = self.extraArgs['backupDestinations']
        websiteData = self.extraArgs['websiteData']
        websiteEmails = self.extraArgs['websiteEmails']
        websiteSSLs = self.extraArgs['websiteSSLs']
        websiteDatabases = self.extraArgs['websiteDatabases']

        self.website = Websites.objects.get(domain=website)

        newJob = IncJob(website=self.website)
        newJob.save()

        self.jobid = newJob

        self.passwordFile = '/home/%s/%s' % (self.website.domain,
                                             self.website.domain)

        self.repoPath = '/home/%s/incbackup' % (self.website.domain)

        if not os.path.exists(self.passwordFile):
            password = randomPassword.generate_pass()
            command = 'echo "%s" > %s' % (password, self.passwordFile)
            ProcessUtilities.executioner(command, self.website.externalApp)

            command = 'chmod 600 %s' % (self.passwordFile)
            ProcessUtilities.executioner(command)
            SUBJECT = "Backup Repository password for %s" % (self.website)
            text = """Password: %s
This is password for your incremental backup repository, please save it in safe place as it will be required when you want to restore backup for this site on remote server.
"""

            sender = 'cyberpanel@%s' % (self.website.domain)
            TO = [self.website.adminEmail]
            message = """\
From: %s
To: %s
Subject: %s

%s
""" % (sender, ", ".join(TO), SUBJECT, text)
            mailUtilities.SendEmail(sender, TO, message)

        if self.initiateRepo() == 0:
            return

        if self.prepareBackupMeta() == 0:
            return

        if websiteData:
            if self.backupData() == 0:
                return

        if websiteDatabases:
            if self.backupDatabases() == 0:
                return

        if websiteEmails:
            if self.emailBackup() == 0:
                return

        self.metaBackup()

        metaPathNew = '/home/%s/meta.xml' % (self.website.domain)

        try:
            command = 'rm -f %s' % (metaPathNew)
            ProcessUtilities.executioner(command)
        except BaseException, msg:
            logging.statusWriter(
                self.statusPath,
                'Failed to delete meta file: %s. [IncJobs.createBackup.591]' %
                str(msg), 1)
Exemple #28
0
    def initiateRepo(self):
        try:
            logging.statusWriter(self.statusPath,
                                 'Will first initiate backup repo..', 1)

            if self.backupDestinations == 'local':
                command = 'restic init --repo %s --password-file %s' % (
                    self.repoPath, self.passwordFile)
                result = ProcessUtilities.outputExecutioner(command)
                logging.statusWriter(self.statusPath, result, 1)

            elif self.backupDestinations[:4] == 'sftp':
                remotePath = '/home/backup/%s' % (self.website.domain)
                command = 'export PATH=${PATH}:/usr/bin && restic init --repo %s:%s --password-file %s' % (
                    self.backupDestinations, remotePath, self.passwordFile)
                result = ProcessUtilities.outputExecutioner(command)
                logging.statusWriter(self.statusPath, result, 1)
            else:
                key, secret = self.getAWSData()
                command = 'export AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s  && restic -r s3:s3.amazonaws.com/%s init --password-file %s' % (
                    key, secret, self.website.domain, self.passwordFile)
                result = ProcessUtilities.outputExecutioner(command)
                logging.statusWriter(self.statusPath, result, 1)
                return 1

            logging.statusWriter(
                self.statusPath, 'Repo %s initiated for %s.' %
                (self.backupDestinations, self.website.domain), 1)
            return 1
        except BaseException, msg:
            logging.statusWriter(
                self.statusPath,
                '%s. [IncJobs.initiateRepo.47][5009]' % str(msg), 1)
            return 0
Exemple #29
0
    def restorePoint(self):
        try:
            self.statusPath = self.extraArgs['tempPath']
            self.website = self.extraArgs['website']
            jobid = self.extraArgs['jobid']
            self.reconstruct = self.extraArgs['reconstruct']

            if self.reconstruct == 'remote':
                self.jobid = self.extraArgs['jobid']
                self.backupDestinations = self.extraArgs['backupDestinations']
                self.passwordFile = self.extraArgs['password']
                self.path = self.extraArgs['path']

                if self.path.find('.sql') > -1:
                    message = 'Restoring database..'
                    logging.statusWriter(self.statusPath, message, 1)
                    self.restoreDatabase()
                    message = 'Database restored.'
                    logging.statusWriter(self.statusPath, message, 1)
                elif self.path == '/home/%s' % (self.website):
                    message = 'Restoring data..'
                    logging.statusWriter(self.statusPath, message, 1)
                    self.restoreData()
                    message = 'Data restored..'
                    logging.statusWriter(self.statusPath, message, 1)
                elif self.path.find('vmail') > -1:
                    message = 'Restoring email..'
                    logging.statusWriter(self.statusPath, message, 1)
                    self.restoreEmail()
                    message = 'Emails restored.'
                    logging.statusWriter(self.statusPath, message, 1)
                elif self.path.find('meta.xml') > -1:
                    message = 'Reconstructing with meta..'
                    logging.statusWriter(self.statusPath, message, 1)
                    self.reconstructWithMeta()
                    message = 'Reconstructed'
                    logging.statusWriter(self.statusPath, message, 1)
            else:
                self.jobid = JobSnapshots.objects.get(pk=jobid)

                message = 'Starting restore of %s for %s.' % (
                    self.jobid.snapshotid, self.website)
                logging.statusWriter(self.statusPath, message, 1)
                self.passwordFile = '/home/%s/%s' % (self.website,
                                                     self.website)

                ##

                if self.jobid.type[:8] == 'database':
                    message = 'Restoring database..'
                    logging.statusWriter(self.statusPath, message, 1)
                    self.restoreDatabase()
                    message = 'Database restored.'
                    logging.statusWriter(self.statusPath, message, 1)
                elif self.jobid.type[:4] == 'data':
                    self.restoreData()
                elif self.jobid.type[:5] == 'email':
                    message = 'Restoring email..'
                    logging.statusWriter(self.statusPath, message, 1)
                    self.restoreEmail()
                    message = 'Emails restored.'
                    logging.statusWriter(self.statusPath, message, 1)
                elif self.jobid.type[:4] == 'meta':
                    message = 'Reconstructing with meta..'
                    logging.statusWriter(self.statusPath, message, 1)
                    self.reconstructWithMeta()
                    message = 'Reconstructed'
                    logging.statusWriter(self.statusPath, message, 1)

            logging.statusWriter(self.statusPath, 'Completed', 1)
        except BaseException, msg:
            logging.statusWriter(self.extraArgs['tempPath'], str(msg), 1)
Exemple #30
0
    def prepareBackupMeta(self):
        try:

            ######### Generating meta

            ## XML Generation

            metaFileXML = Element('metaFile')

            child = SubElement(metaFileXML, 'masterDomain')
            child.text = self.website.domain

            child = SubElement(metaFileXML, 'phpSelection')
            child.text = self.website.phpSelection

            child = SubElement(metaFileXML, 'externalApp')
            child.text = self.website.externalApp

            childDomains = self.website.childdomains_set.all()

            databases = self.website.databases_set.all()

            ## Child domains XML

            childDomainsXML = Element('ChildDomains')

            for items in childDomains:
                childDomainXML = Element('domain')

                child = SubElement(childDomainXML, 'domain')
                child.text = items.domain
                child = SubElement(childDomainXML, 'phpSelection')
                child.text = items.phpSelection
                child = SubElement(childDomainXML, 'path')
                child.text = items.path

                childDomainsXML.append(childDomainXML)

            metaFileXML.append(childDomainsXML)

            ## Databases XML

            databasesXML = Element('Databases')

            for items in databases:
                try:
                    dbuser = DBUsers.objects.get(user=items.dbUser)
                    userToTry = items.dbUser
                except:
                    dbusers = DBUsers.objects.all().filter(user=items.dbUser)
                    userToTry = items.dbUser
                    for it in dbusers:
                        dbuser = it
                        break

                    userToTry = mysqlUtilities.mysqlUtilities.fetchuser(
                        items.dbUser)

                    try:
                        dbuser = DBUsers.objects.get(user=userToTry)
                    except:
                        dbusers = DBUsers.objects.all().filter(user=userToTry)
                        for it in dbusers:
                            dbuser = it
                            break

                databaseXML = Element('database')

                child = SubElement(databaseXML, 'dbName')
                child.text = items.dbName
                child = SubElement(databaseXML, 'dbUser')
                child.text = userToTry
                child = SubElement(databaseXML, 'password')
                child.text = dbuser.password

                databasesXML.append(databaseXML)

            metaFileXML.append(databasesXML)

            ## Get Aliases

            aliasesXML = Element('Aliases')

            aliases = backupUtilities.getAliases(self.website.domain)

            for items in aliases:
                child = SubElement(aliasesXML, 'alias')
                child.text = items

            metaFileXML.append(aliasesXML)

            ## Finish Alias

            ## DNS Records XML

            try:

                dnsRecordsXML = Element("dnsrecords")
                dnsRecords = DNS.getDNSRecords(self.website.domain)

                for items in dnsRecords:
                    dnsRecordXML = Element('dnsrecord')

                    child = SubElement(dnsRecordXML, 'type')
                    child.text = items.type
                    child = SubElement(dnsRecordXML, 'name')
                    child.text = items.name
                    child = SubElement(dnsRecordXML, 'content')
                    child.text = items.content
                    child = SubElement(dnsRecordXML, 'priority')
                    child.text = str(items.prio)

                    dnsRecordsXML.append(dnsRecordXML)

                metaFileXML.append(dnsRecordsXML)

            except BaseException, msg:
                logging.statusWriter(self.statusPath,
                                     '%s. [158:prepMeta]' % (str(msg)), 1)

            ## Email accounts XML

            try:
                emailRecordsXML = Element('emails')
                eDomain = eDomains.objects.get(domain=self.website.domain)
                emailAccounts = eDomain.eusers_set.all()

                for items in emailAccounts:
                    emailRecordXML = Element('emailAccount')

                    child = SubElement(emailRecordXML, 'email')
                    child.text = items.email
                    child = SubElement(emailRecordXML, 'password')
                    child.text = items.password

                    emailRecordsXML.append(emailRecordXML)

                metaFileXML.append(emailRecordsXML)
            except BaseException, msg:
                logging.statusWriter(self.statusPath,
                                     '%s. [warning:179:prepMeta]' % (str(msg)),
                                     1)