Files
CyberPanel/plogical/backupUtilities.py

2219 lines
89 KiB
Python
Raw Normal View History

2021-10-16 00:51:48 +06:00
import os
import sys
2018-06-05 00:53:45 +05:00
sys.path.append('/usr/local/CyberCP')
import django
2018-06-05 00:53:45 +05:00
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
try:
django.setup()
except:
pass
2017-10-24 19:16:36 +05:00
import pexpect
2019-12-15 13:30:40 +05:00
from plogical import CyberCPLogFileWriter as logging
2017-10-24 19:16:36 +05:00
import subprocess
import shlex
from shutil import make_archive, rmtree
2019-12-15 13:30:40 +05:00
from plogical import mysqlUtilities
2017-10-24 19:16:36 +05:00
import tarfile
from multiprocessing import Process
import signal
2019-12-15 13:30:40 +05:00
from plogical.installUtilities import installUtilities
2017-12-09 22:30:10 +05:00
import argparse
2019-07-18 14:08:00 +05:00
try:
2019-12-15 13:30:40 +05:00
from plogical.virtualHostUtilities import virtualHostUtilities
from plogical.sslUtilities import sslUtilities
2019-07-18 14:08:00 +05:00
from plogical.mailUtilities import mailUtilities
except:
pass
2018-06-05 00:53:45 +05:00
from xml.etree.ElementTree import Element, SubElement
from xml.etree import ElementTree
from xml.dom import minidom
import time
2020-04-06 15:59:05 +05:00
from shutil import copy
from random import randint
from plogical.processUtilities import ProcessUtilities
2019-07-18 14:08:00 +05:00
try:
from websiteFunctions.models import Websites, ChildDomains, Backups
from databases.models import Databases
from loginSystem.models import Administrator
2019-12-15 13:30:40 +05:00
from plogical.dnsUtilities import DNS
2019-07-18 14:08:00 +05:00
from mailServer.models import Domains as eDomains
from backup.models import DBUsers
except:
pass
2021-03-08 11:10:48 +05:00
VERSION = '2.1'
2021-10-20 12:52:02 +05:00
BUILD = 2
2020-04-06 14:24:52 +05:00
2018-05-09 23:10:57 +05:00
## I am not the monster that you think I am..
2017-10-24 19:16:36 +05:00
class backupUtilities:
2019-07-18 14:08:00 +05:00
Server_root = "/usr/local/lsws"
completeKeyPath = "/home/cyberpanel/.ssh"
2017-12-09 22:30:10 +05:00
destinationsPath = "/home/cyberpanel/destinations"
2018-08-28 01:19:34 +05:00
licenseKey = '/usr/local/lsws/conf/license.key'
2020-11-26 18:13:05 +05:00
NiceDefault = '10'
2020-11-27 22:04:29 +05:00
CPUDefault = '1000'
2020-11-26 18:13:05 +05:00
CloudBackupConfigPath = '/home/cyberpanel/CloudBackup.json'
time = 10
2020-11-26 18:13:05 +05:00
def __init__(self, extraArgs):
self.extraArgs = extraArgs
2017-11-05 03:02:51 +05:00
2018-06-05 00:53:45 +05:00
@staticmethod
2021-10-16 00:51:48 +06:00
def prepareBackupMeta(backupDomain, backupName, tempStoragePath, backupPath, FromInner=1):
2018-06-05 00:53:45 +05:00
try:
2021-08-12 14:37:30 +05:00
website = Websites.objects.get(domain=backupDomain)
2021-05-07 22:22:29 +05:00
connection, cursor = mysqlUtilities.mysqlUtilities.setupConnection()
2021-05-07 22:22:29 +05:00
if FromInner:
status = os.path.join(backupPath, 'status')
2021-08-12 14:37:30 +05:00
#logging.CyberCPLogFileWriter.statusWriter(status, 'Setting up meta data..')
2021-10-16 00:51:48 +06:00
command = f"echo 'Setting up meta data..' > {status}"
2021-08-12 14:37:30 +05:00
ProcessUtilities.executioner(command, website.externalApp)
2021-05-07 22:22:29 +05:00
else:
status = '/home/cyberpanel/dummy'
2019-07-18 14:08:00 +05:00
2021-05-07 22:22:29 +05:00
if os.path.exists(ProcessUtilities.debugPath):
2021-10-16 00:51:48 +06:00
logging.CyberCPLogFileWriter.writeToFile(f'Creating meta for {backupDomain}.')
2019-07-18 14:08:00 +05:00
2018-06-05 00:53:45 +05:00
######### Generating meta
## XML Generation
metaFileXML = Element('metaFile')
2020-04-06 14:24:52 +05:00
child = SubElement(metaFileXML, 'VERSION')
child.text = VERSION
child = SubElement(metaFileXML, 'BUILD')
child.text = str(BUILD)
2018-06-05 00:53:45 +05:00
child = SubElement(metaFileXML, 'masterDomain')
child.text = backupDomain
child = SubElement(metaFileXML, 'phpSelection')
child.text = website.phpSelection
child = SubElement(metaFileXML, 'externalApp')
child.text = website.externalApp
### Find user of site
siteUser = website.admin
child = SubElement(metaFileXML, 'userName')
child.text = siteUser.userName
child = SubElement(metaFileXML, 'userPassword')
child.text = siteUser.password
child = SubElement(metaFileXML, 'firstName')
child.text = siteUser.firstName
child = SubElement(metaFileXML, 'lastName')
child.text = siteUser.lastName
child = SubElement(metaFileXML, 'email')
child.text = siteUser.email
child = SubElement(metaFileXML, 'type')
child.text = str(siteUser.type)
child = SubElement(metaFileXML, 'owner')
child.text = str(siteUser.owner)
child = SubElement(metaFileXML, 'token')
child.text = siteUser.token
child = SubElement(metaFileXML, 'api')
child.text = str(siteUser.api)
child = SubElement(metaFileXML, 'securityLevel')
child.text = str(siteUser.securityLevel)
child = SubElement(metaFileXML, 'state')
child.text = siteUser.state
child = SubElement(metaFileXML, 'initWebsitesLimit')
child.text = str(siteUser.initWebsitesLimit)
child = SubElement(metaFileXML, 'aclName')
child.text = siteUser.acl.name
#####################
2018-06-05 00:53:45 +05:00
childDomains = website.childdomains_set.all()
databases = 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:
2019-12-05 22:36:17 +05:00
2018-06-05 00:53:45 +05:00
databaseXML = Element('database')
child = SubElement(databaseXML, 'dbName')
2019-12-05 22:36:17 +05:00
child.text = str(items.dbName)
2021-10-16 00:51:48 +06:00
cursor.execute(f"select user,host from mysql.db where db='{items.dbName}'")
databaseUsers = cursor.fetchall()
for databaseUser in databaseUsers:
databaseUserXML = Element('databaseUsers')
child = SubElement(databaseUserXML, 'dbUser')
child.text = databaseUser[0]
child = SubElement(databaseUserXML, 'dbHost')
child.text = databaseUser[1]
## Fetch user password
2021-04-12 16:02:17 +05:00
dbuser = DBUsers.objects.get(user=databaseUser[0], host=databaseUser[1])
child = SubElement(databaseUserXML, 'password')
child.text = str(dbuser.password)
databaseXML.append(databaseUserXML)
2018-06-05 00:53:45 +05:00
databasesXML.append(databaseXML)
metaFileXML.append(databasesXML)
## Get Aliases
2019-12-05 22:36:17 +05:00
try:
aliasesXML = Element('Aliases')
2018-06-05 00:53:45 +05:00
2019-12-05 22:36:17 +05:00
aliases = backupUtilities.getAliases(backupDomain)
2018-06-05 00:53:45 +05:00
2019-12-05 22:36:17 +05:00
for items in aliases:
child = SubElement(aliasesXML, 'alias')
child.text = items
2018-06-05 00:53:45 +05:00
2019-12-05 22:36:17 +05:00
metaFileXML.append(aliasesXML)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2021-08-30 23:56:54 +05:00
logging.CyberCPLogFileWriter.writeToFile('%s. [167:prepMeta]' % (str(msg)))
2018-06-05 00:53:45 +05:00
## Finish Alias
## DNS Records XML
try:
2019-03-13 18:22:12 +05:00
2018-06-05 00:53:45 +05:00
dnsRecordsXML = Element("dnsrecords")
dnsRecords = DNS.getDNSRecords(backupDomain)
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)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2021-08-30 23:56:54 +05:00
logging.CyberCPLogFileWriter.writeToFile('%s. [158:prepMeta]' % (str(msg)))
2018-06-05 00:53:45 +05:00
## Email accounts XML
try:
emailRecordsXML = Element('emails')
eDomain = eDomains.objects.get(domain=backupDomain)
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)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2021-08-30 23:56:54 +05:00
logging.CyberCPLogFileWriter.writeToFile('%s. [179:prepMeta]' % (str(msg)))
2018-06-05 00:53:45 +05:00
## Email meta generated!
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = ElementTree.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
## /home/example.com/backup/backup-example.com-02.13.2018_10-24-52/meta.xml -- metaPath
2021-11-16 19:12:34 +05:00
metaPath = '/tmp/%s' % (str(randint(1000, 9999)))
2018-06-05 00:53:45 +05:00
2021-05-07 22:22:29 +05:00
if os.path.exists(ProcessUtilities.debugPath):
2021-10-16 00:51:48 +06:00
logging.CyberCPLogFileWriter.writeToFile(f'Path to meta file {metaPath}')
2021-05-07 22:22:29 +05:00
2018-06-05 00:53:45 +05:00
xmlpretty = prettify(metaFileXML).encode('ascii', 'ignore')
2021-11-16 19:12:34 +05:00
metaFile = open(metaPath, 'w')
metaFile.write(xmlpretty.decode())
metaFile.close()
os.chmod(metaPath, 0o600)
2018-06-05 00:53:45 +05:00
## meta generated
2021-05-07 22:22:29 +05:00
if FromInner:
newBackup = Backups(website=website, fileName=backupName, date=time.strftime("%m.%d.%Y_%H-%M-%S"),
size=0, status=1)
newBackup.save()
2018-06-05 00:53:45 +05:00
2021-10-16 00:51:48 +06:00
command = f"echo 'Meta data is ready..' > {status}"
2021-08-12 14:37:30 +05:00
ProcessUtilities.executioner(command, website.externalApp)
2019-07-18 14:08:00 +05:00
return 1, 'None', metaPath
2018-06-05 00:53:45 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2021-10-16 00:51:48 +06:00
logging.CyberCPLogFileWriter.writeToFile(f"{str(msg)} [207][5009]")
2021-05-07 22:22:29 +05:00
if FromInner:
2021-08-12 14:37:30 +05:00
#logging.CyberCPLogFileWriter.statusWriter(status, "%s [207][5009]" % (str(msg)), status)
2021-10-16 00:51:48 +06:00
command = f"echo '{status} [207][5009]' > {status}"
2021-08-12 14:37:30 +05:00
ProcessUtilities.executioner(command, website.externalApp)
2021-05-07 22:22:29 +05:00
return 0, str(msg), 'None'
2017-10-24 19:16:36 +05:00
@staticmethod
def startBackup(tempStoragePath, backupName, backupPath, metaPath=None):
2017-10-24 19:16:36 +05:00
try:
## /home/example.com/backup/backup-example.com-02.13.2018_10-24-52 -- tempStoragePath
## /home/example.com/backup - backupPath
2018-05-11 00:03:26 +05:00
##### Writing the name of backup file.
2017-12-09 22:30:10 +05:00
2018-02-16 00:57:46 +05:00
## /home/example.com/backup/backupFileName
2021-10-16 00:51:48 +06:00
pidFile = f'{backupPath}startBackup'
writeToFile = open(pidFile, 'w')
writeToFile.writelines(str(os.getpid()))
writeToFile.close()
2018-08-23 15:39:28 +05:00
backupFileNamePath = os.path.join(backupPath, "backupFileName")
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.statusWriter(backupFileNamePath, backupName)
2017-12-09 22:30:10 +05:00
2018-05-11 00:03:26 +05:00
#####
2017-10-24 19:16:36 +05:00
status = os.path.join(backupPath, 'status')
2018-02-16 00:57:46 +05:00
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.statusWriter(status, "Making archive of home directory.\n")
2017-10-24 19:16:36 +05:00
2018-05-11 00:03:26 +05:00
##### Parsing XML Meta file!
2017-12-09 22:30:10 +05:00
## /home/example.com/backup/backup-example.com-02.13.2018_10-24-52 -- tempStoragePath
metaPathInBackup = os.path.join(tempStoragePath, 'meta.xml')
if metaPath != None:
writeToFile = open(metaPathInBackup, 'w')
writeToFile.write(open(metaPath, 'r').read())
writeToFile.close()
backupMetaData = ElementTree.parse(metaPathInBackup)
2018-05-11 00:03:26 +05:00
##### Making archive of home directory
2018-02-16 00:57:46 +05:00
domainName = backupMetaData.find('masterDomain').text
2018-08-28 01:19:34 +05:00
## Saving original vhost conf file
2021-10-16 00:51:48 +06:00
completPathToConf = f'{backupUtilities.Server_root}/conf/vhosts/{domainName}/vhost.conf'
2019-03-13 23:05:22 +05:00
2018-08-28 01:19:34 +05:00
if os.path.exists(backupUtilities.licenseKey):
copy(completPathToConf, tempStoragePath + '/vhost.conf')
## /home/example.com/backup/backup-example.com-02.13.2018_10-24-52 -- tempStoragePath
## shutil.make_archive
2018-05-11 00:03:26 +05:00
2020-04-06 14:24:52 +05:00
## Stop making archive of document_root and copy instead
2021-03-25 00:10:30 +05:00
# copy_tree('/home/%s/public_html' % domainName, '%s/%s' % (tempStoragePath, 'public_html'))
2021-10-16 00:51:48 +06:00
command = f'cp -R /home/{domainName}/public_html {tempStoragePath}/public_html'
2021-03-26 00:36:41 +05:00
if ProcessUtilities.normalExecutioner(command) == 0:
2021-10-16 00:51:48 +06:00
raise BaseException(f'Failed to run {command}.')
2021-03-25 00:10:30 +05:00
# make_archive(os.path.join(tempStoragePath,"public_html"), 'gztar', os.path.join("/home",domainName,"public_html"))
2020-04-06 14:24:52 +05:00
##
2018-02-16 00:57:46 +05:00
2019-07-18 14:08:00 +05:00
logging.CyberCPLogFileWriter.statusWriter(status, "Backing up databases..")
2019-12-10 15:09:10 +05:00
print('1,None')
2017-10-24 19:16:36 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
# try:
# os.remove(os.path.join(backupPath, backupName + ".tar.gz"))
# except:
# pass
#
# try:
# rmtree(tempStoragePath)
# except:
# pass
2017-10-24 19:16:36 +05:00
2019-03-31 02:47:35 +05:00
status = os.path.join(backupPath, 'status')
logging.CyberCPLogFileWriter.statusWriter(status, "Aborted, " + str(msg) + ".[365] [5009]")
2021-10-16 00:51:48 +06:00
print(f"Aborted, {str(msg)}.[365] [5009]")
2017-10-24 19:16:36 +05:00
os.remove(pidFile)
@staticmethod
2021-11-16 18:06:04 +05:00
def BackupRoot(tempStoragePath, backupName, backupPath, metaPath=None, externalApp = None):
pidFile = '%sBackupRoot' % (backupPath)
writeToFile = open(pidFile, 'w')
writeToFile.writelines(str(os.getpid()))
writeToFile.close()
status = os.path.join(backupPath, 'status')
metaPathInBackup = os.path.join(tempStoragePath, 'meta.xml')
backupMetaData = ElementTree.parse(metaPathInBackup)
2019-08-31 15:30:53 +05:00
domainName = backupMetaData.find('masterDomain').text
##### Saving SSL Certificates if any
sslStoragePath = '/etc/letsencrypt/live/' + domainName
if os.path.exists(sslStoragePath):
try:
copy(os.path.join(sslStoragePath, "cert.pem"), os.path.join(tempStoragePath, domainName + ".cert.pem"))
copy(os.path.join(sslStoragePath, "fullchain.pem"),
os.path.join(tempStoragePath, domainName + ".fullchain.pem"))
copy(os.path.join(sslStoragePath, "privkey.pem"),
os.path.join(tempStoragePath, domainName + ".privkey.pem"))
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2021-10-16 00:51:48 +06:00
logging.CyberCPLogFileWriter.writeToFile(f'{str(msg)}. [283:startBackup]')
2019-08-31 15:30:53 +05:00
## Child Domains SSL.
childDomains = backupMetaData.findall('ChildDomains/domain')
try:
for childDomain in childDomains:
actualChildDomain = childDomain.find('domain').text
2020-04-06 14:24:52 +05:00
childPath = childDomain.find('path').text
2019-08-31 15:30:53 +05:00
if os.path.exists(backupUtilities.licenseKey):
2021-10-16 00:51:48 +06:00
completPathToConf = f'{backupUtilities.Server_root}/conf/vhosts/{actualChildDomain}/vhost.conf'
copy(completPathToConf, f'{tempStoragePath}/{actualChildDomain}.vhost.conf')
2019-08-31 15:30:53 +05:00
### Storing SSL for child domainsa
2021-10-16 00:51:48 +06:00
sslStoragePath = f'/etc/letsencrypt/live/{actualChildDomain}'
2019-08-31 15:30:53 +05:00
if os.path.exists(sslStoragePath):
try:
copy(os.path.join(sslStoragePath, "cert.pem"),
os.path.join(tempStoragePath, actualChildDomain + ".cert.pem"))
copy(os.path.join(sslStoragePath, "fullchain.pem"),
os.path.join(tempStoragePath, actualChildDomain + ".fullchain.pem"))
copy(os.path.join(sslStoragePath, "privkey.pem"),
os.path.join(tempStoragePath, actualChildDomain + ".privkey.pem"))
make_archive(os.path.join(tempStoragePath, "sslData-" + domainName), 'gztar',
sslStoragePath)
except:
pass
2020-04-06 14:24:52 +05:00
2021-10-16 00:51:48 +06:00
if childPath.find(f'/home/{domainName}/public_html') == -1:
2021-03-25 00:10:30 +05:00
# copy_tree(childPath, '%s/%s-docroot' % (tempStoragePath, actualChildDomain))
2021-10-16 00:51:48 +06:00
command = f'cp -R {childPath} {tempStoragePath}/{actualChildDomain}-docroot'
2021-03-25 00:10:30 +05:00
ProcessUtilities.executioner(command)
2020-04-06 14:24:52 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2019-08-31 15:30:53 +05:00
pass
## backup emails
domainName = backupMetaData.find('masterDomain').text
if os.path.islink(status) or os.path.islink(tempStoragePath or os.path.islink(backupPath)) or os.path.islink(
metaPath):
logging.CyberCPLogFileWriter.writeToFile('symlinked.')
2021-11-16 18:06:04 +05:00
#logging.CyberCPLogFileWriter.statusWriter(status, 'Symlink attack. [365][5009]')
return 0
## backup email accounts
2021-11-16 18:06:04 +05:00
if externalApp == None:
logging.CyberCPLogFileWriter.statusWriter(status, "Backing up email accounts..\n")
else:
command = f"echo 'Backing up email accounts..' > {status}"
ProcessUtilities.executioner(command, externalApp)
2020-06-10 21:22:12 +05:00
try:
2021-10-16 00:51:48 +06:00
emailPath = f'/home/vmail/{domainName}'
2020-06-10 21:22:12 +05:00
if os.path.exists(emailPath):
2021-03-25 00:10:30 +05:00
# copy_tree(emailPath, '%s/vmail' % (tempStoragePath), preserve_symlinks=True)
2021-10-16 00:51:48 +06:00
command = f'cp -R {emailPath} {tempStoragePath}/vmail'
2021-03-25 00:10:30 +05:00
ProcessUtilities.executioner(command)
2020-06-10 21:22:12 +05:00
## shutil.make_archive. Creating final package.
2021-11-16 18:06:04 +05:00
if externalApp == None:
logging.CyberCPLogFileWriter.statusWriter(status, "Preparing final compressed package..\n")
else:
command = f"echo 'Preparing final compressed package..' > {status}"
ProcessUtilities.executioner(command, externalApp, True)
2020-06-10 21:22:12 +05:00
make_archive(os.path.join(backupPath, backupName), 'gztar', tempStoragePath)
rmtree(tempStoragePath)
2020-06-10 21:22:12 +05:00
###
2020-06-10 21:22:12 +05:00
backupObs = Backups.objects.filter(fileName=backupName)
2020-04-28 17:51:31 +05:00
2021-10-16 00:51:48 +06:00
filePath = f'{backupPath}/{backupName}.tar.gz'
2020-06-10 21:22:12 +05:00
totalSize = '%sMB' % (str(int(os.path.getsize(filePath) / 1048576)))
try:
for items in backupObs:
items.status = 1
items.size = totalSize
items.save()
except BaseException as msg:
logging.CyberCPLogFileWriter.writeToFile('%s. [backupRoot:499]' % str(msg))
for items in backupObs:
items.status = 1
items.size = totalSize
items.save()
2020-06-10 21:22:12 +05:00
command = 'chmod 600 %s' % (os.path.join(backupPath, backupName + ".tar.gz"))
ProcessUtilities.executioner(command)
2020-04-06 19:16:49 +05:00
2021-11-16 18:06:04 +05:00
if externalApp == None:
logging.CyberCPLogFileWriter.statusWriter(status, "Completed\n")
else:
command = f"echo 'Completed' > {status}"
ProcessUtilities.executioner(command, externalApp, True)
2020-06-10 21:22:12 +05:00
os.remove(pidFile)
except BaseException as msg:
logging.CyberCPLogFileWriter.statusWriter(status, '%s. [511:BackupRoot][[5009]]\n' % str(msg))
2021-11-16 18:06:04 +05:00
if externalApp == None:
logging.CyberCPLogFileWriter.statusWriter(status, '%s. [511:BackupRoot][[5009]]\n')
else:
command = f"echo '%s. [511:BackupRoot][[5009]]' > {status}"
ProcessUtilities.executioner(command, externalApp)
2017-10-24 19:16:36 +05:00
@staticmethod
def initiateBackup(tempStoragePath, backupName, backupPath):
2017-10-24 19:16:36 +05:00
try:
p = Process(target=backupUtilities.startBackup, args=(tempStoragePath, backupName, backupPath,))
2017-10-24 19:16:36 +05:00
p.start()
pid = open(backupPath + 'pid', "w")
pid.write(str(p.pid))
pid.close()
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2017-10-24 19:16:36 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [initiateBackup]")
2018-06-05 00:53:45 +05:00
@staticmethod
def createWebsiteFromBackup(backupFileOrig, dir):
try:
backupFile = backupFileOrig.strip(".tar.gz")
2021-10-16 00:51:48 +06:00
originalFile = f"/home/backup/{backupFileOrig}"
2018-06-05 00:53:45 +05:00
if os.path.exists(backupFileOrig):
path = backupFile
elif not os.path.exists(originalFile):
dir = dir
path = "/home/backup/transfer-" + str(dir) + "/" + backupFile
else:
2021-10-16 00:51:48 +06:00
path = f"/home/backup/{backupFile}"
2018-06-05 00:53:45 +05:00
2020-01-29 11:05:47 +05:00
admin = Administrator.objects.get(userName='admin')
2018-06-05 00:53:45 +05:00
## open meta file to read data
## Parsing XML Meta file!
backupMetaData = ElementTree.parse(os.path.join(path, 'meta.xml'))
domain = backupMetaData.find('masterDomain').text
phpSelection = backupMetaData.find('phpSelection').text
externalApp = backupMetaData.find('externalApp').text
VERSION = backupMetaData.find('VERSION').text
BUILD = backupMetaData.find('BUILD').text
2018-06-05 00:53:45 +05:00
### Fetch user details
try:
userName = backupMetaData.find('userName').text
try:
siteUser = Administrator.objects.get(userName=userName)
except:
userPassword = backupMetaData.find('userPassword').text
firstName = backupMetaData.find('firstName').text
lastName = backupMetaData.find('lastName').text
email = backupMetaData.find('email').text
type = int(backupMetaData.find('type').text)
owner = int(backupMetaData.find('owner').text)
token = backupMetaData.find('token').text
api = int(backupMetaData.find('api').text)
securityLevel = int(backupMetaData.find('securityLevel').text)
state = backupMetaData.find('state').text
initWebsitesLimit = int(backupMetaData.find('initWebsitesLimit').text)
from loginSystem.models import ACL
acl = ACL.objects.get(name=backupMetaData.find('aclName').text)
siteUser = Administrator(userName=userName, password=userPassword, firstName=firstName,
initWebsitesLimit=initWebsitesLimit, acl=acl,
lastName=lastName, email=email, type=type, owner=owner, token=token,
api=api, securityLevel=securityLevel, state=state)
siteUser.save()
except:
siteUser = Administrator.objects.get(userName='admin')
2018-06-05 00:53:45 +05:00
## Pre-creation checks
if Websites.objects.filter(domain=domain).count() > 0:
raise BaseException('This website already exists.')
if ChildDomains.objects.filter(domain=domain).count() > 0:
raise BaseException("This website already exists as child domain.")
####### Pre-creation checks ends
## Create Configurations
result = virtualHostUtilities.createVirtualHost(domain, siteUser.email, phpSelection, externalApp, 0, 1, 0,
siteUser.userName, 'Default', 0)
2018-06-05 00:53:45 +05:00
if result[0] == 0:
raise BaseException(result[1])
## Create Configurations ends here
## Create databases
### This code is just to create databases, database users will be created later
2018-06-05 00:53:45 +05:00
databases = backupMetaData.findall('Databases/database')
website = Websites.objects.get(domain=domain)
for database in databases:
2018-06-05 00:53:45 +05:00
dbName = database.find('dbName').text
if VERSION == '2.1' and int(BUILD) >= 1:
logging.CyberCPLogFileWriter.writeToFile('Backup version 2.1.1+ detected..')
databaseUsers = database.findall('databaseUsers')
for databaseUser in databaseUsers:
dbUser = databaseUser.find('dbUser').text
res = mysqlUtilities.mysqlUtilities.createDatabase(dbName, dbUser, 'cyberpanel')
if res == 0:
logging.CyberCPLogFileWriter.writeToFile(
'Failed to restore database %s. But it can be false positive, moving on..' % (dbName))
newDB = Databases(website=website, dbName=dbName, dbUser=dbUser)
newDB.save()
break
2018-06-05 00:53:45 +05:00
else:
dbUser = database.find('dbUser').text
if mysqlUtilities.mysqlUtilities.createDatabase(dbName, dbUser, "cyberpanel") == 0:
2021-04-12 16:02:17 +05:00
raise BaseException
newDB = Databases(website=website, dbName=dbName, dbUser=dbUser)
newDB.save()
2018-06-05 00:53:45 +05:00
## Create dns zone
dnsrecords = backupMetaData.findall('dnsrecords/dnsrecord')
DNS.createDNSZone(domain, admin)
zone = DNS.getZoneObject(domain)
for dnsrecord in dnsrecords:
recordType = dnsrecord.find('type').text
value = dnsrecord.find('name').text
content = dnsrecord.find('content').text
prio = int(dnsrecord.find('priority').text)
DNS.createDNSRecord(zone, value, recordType, content, prio, 3600)
return 1, 'None'
2018-06-05 00:53:45 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-05 00:53:45 +05:00
return 0, str(msg)
2017-10-24 19:16:36 +05:00
@staticmethod
2017-10-26 23:50:59 +05:00
def startRestore(backupName, dir):
2017-10-24 19:16:36 +05:00
try:
2017-12-09 22:30:10 +05:00
if dir == "CyberPanelRestore":
2017-10-26 23:50:59 +05:00
backupFileName = backupName.strip(".tar.gz")
completPath = os.path.join("/home", "backup", backupFileName) ## without extension
originalFile = os.path.join("/home", "backup", backupName) ## with extension
2018-06-05 00:53:45 +05:00
elif dir == 'CLI':
completPath = backupName.strip(".tar.gz") ## without extension
originalFile = backupName ## with extension
2017-10-26 23:50:59 +05:00
else:
backupFileName = backupName.strip(".tar.gz")
completPath = "/home/backup/transfer-" + str(dir) + "/" + backupFileName ## without extension
originalFile = "/home/backup/transfer-" + str(dir) + "/" + backupName ## with extension
2017-10-26 23:50:59 +05:00
pathToCompressedHome = os.path.join(completPath, "public_html.tar.gz")
2017-10-24 19:16:36 +05:00
if not os.path.exists(completPath):
os.mkdir(completPath)
2018-08-23 15:39:28 +05:00
## Writing pid of restore process
2017-10-24 19:16:36 +05:00
pid = os.path.join(completPath, 'pid')
2017-10-24 19:16:36 +05:00
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.statusWriter(pid, str(os.getpid()))
status = os.path.join(completPath, 'status')
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.statusWriter(status, "Extracting Main Archive!")
2017-10-24 19:16:36 +05:00
## Converting /home/backup/backup-example.com-02.13.2018_10-24-52.tar.gz -> /home/backup/backup-example.com-02.13.2018_10-24-52
2017-10-24 19:16:36 +05:00
tar = tarfile.open(originalFile)
tar.extractall(completPath)
tar.close()
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.statusWriter(status, "Creating Accounts,Databases and DNS records!")
2017-10-24 19:16:36 +05:00
2018-05-09 23:10:57 +05:00
########### Creating website and its dabases
2017-10-24 19:16:36 +05:00
2018-05-11 00:03:26 +05:00
## extracting master domain for later use
backupMetaData = ElementTree.parse(os.path.join(completPath, "meta.xml"))
masterDomain = backupMetaData.find('masterDomain').text
VERSION = backupMetaData.find('VERSION').text
BUILD = backupMetaData.find('BUILD').text
2018-05-11 00:03:26 +05:00
2020-04-06 15:59:05 +05:00
twoPointO = 0
2020-04-06 14:24:52 +05:00
try:
version = backupMetaData.find('VERSION').text
build = backupMetaData.find('BUILD').text
2020-04-06 15:59:05 +05:00
twoPointO = 1
2020-04-06 14:24:52 +05:00
except:
2020-04-06 15:59:05 +05:00
twoPointO = 0
2020-04-06 14:24:52 +05:00
2018-06-05 00:53:45 +05:00
result = backupUtilities.createWebsiteFromBackup(backupName, dir)
2017-10-24 19:16:36 +05:00
2018-06-05 00:53:45 +05:00
if result[0] == 1:
## Let us try to restore SSL.
2018-05-11 00:03:26 +05:00
2019-03-13 23:05:22 +05:00
sslStoragePath = completPath + "/" + masterDomain + ".cert.pem"
2018-05-11 00:03:26 +05:00
2018-08-22 00:37:43 +05:00
if os.path.exists(sslStoragePath):
sslHome = '/etc/letsencrypt/live/' + masterDomain
2019-03-13 23:05:22 +05:00
try:
if not os.path.exists(sslHome):
os.mkdir(sslHome)
copy(completPath + "/" + masterDomain + ".cert.pem", sslHome + "/cert.pem")
copy(completPath + "/" + masterDomain + ".privkey.pem", sslHome + "/privkey.pem")
copy(completPath + "/" + masterDomain + ".fullchain.pem", sslHome + "/fullchain.pem")
sslUtilities.installSSLForDomain(masterDomain)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2019-08-31 15:30:53 +05:00
logging.CyberCPLogFileWriter.writeToFile('%s. [555:startRestore]' % (str(msg)))
2018-05-11 00:03:26 +05:00
2018-06-05 00:53:45 +05:00
else:
logging.CyberCPLogFileWriter.statusWriter(status, "Error Message: " + result[
1] + ". Not able to create Account, Databases and DNS Records, aborting. [575][5009]")
2017-10-24 19:16:36 +05:00
return 0
2018-05-11 00:03:26 +05:00
########### Creating child/sub/addon/parked domains
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.statusWriter(status, "Creating Child Domains!")
2018-05-11 00:03:26 +05:00
## Reading meta file to create subdomains
2018-02-16 00:57:46 +05:00
externalApp = backupMetaData.find('externalApp').text
websiteHome = os.path.join("/home", masterDomain, "public_html")
2018-05-11 00:03:26 +05:00
### Restoring Child Domains if any.
2018-05-09 23:10:57 +05:00
2018-05-11 00:03:26 +05:00
childDomains = backupMetaData.findall('ChildDomains/domain')
2018-05-09 23:10:57 +05:00
try:
2018-02-16 00:57:46 +05:00
for childDomain in childDomains:
2018-02-16 00:57:46 +05:00
domain = childDomain.find('domain').text
## mail domain check
mailDomain = 'mail.%s' % (masterDomain)
if domain == mailDomain:
continue
## Mail domain check
2018-02-16 00:57:46 +05:00
phpSelection = childDomain.find('phpSelection').text
path = childDomain.find('path').text
retValues = virtualHostUtilities.createDomain(masterDomain, domain, phpSelection, path, 0, 0, 0,
'admin', 0)
2018-08-22 00:37:43 +05:00
if retValues[0] == 1:
2018-08-23 15:39:28 +05:00
if os.path.exists(websiteHome):
rmtree(websiteHome)
2018-05-11 00:03:26 +05:00
## Let us try to restore SSL for Child Domains.
2018-08-22 00:37:43 +05:00
try:
2018-08-28 01:19:34 +05:00
if os.path.exists(backupUtilities.licenseKey):
if os.path.exists(completPath + '/' + domain + '.vhost.conf'):
2019-07-18 14:08:00 +05:00
completPathToConf = backupUtilities.Server_root + '/conf/vhosts/' + domain + '/vhost.conf'
2018-08-28 01:19:34 +05:00
copy(completPath + '/' + domain + '.vhost.conf', completPathToConf)
2019-03-13 23:05:22 +05:00
sslStoragePath = completPath + "/" + domain + ".cert.pem"
2018-05-11 00:03:26 +05:00
2018-08-22 00:37:43 +05:00
if os.path.exists(sslStoragePath):
sslHome = '/etc/letsencrypt/live/' + domain
2019-03-13 23:20:15 +05:00
2019-03-13 23:05:22 +05:00
try:
if not os.path.exists(sslHome):
os.mkdir(sslHome)
copy(completPath + "/" + domain + ".cert.pem", sslHome + "/cert.pem")
copy(completPath + "/" + domain + ".privkey.pem", sslHome + "/privkey.pem")
copy(completPath + "/" + domain + ".fullchain.pem",
sslHome + "/fullchain.pem")
sslUtilities.installSSLForDomain(domain)
except:
pass
2018-08-22 00:37:43 +05:00
except:
logging.CyberCPLogFileWriter.writeToFile(
'While restoring backup we had minor issues for rebuilding vhost conf for: ' + domain + '. However this will be auto healed.')
2018-05-11 00:03:26 +05:00
2020-04-06 14:24:52 +05:00
if float(version) > 2.0 or float(build) > 0:
if path.find('/home/%s/public_html' % masterDomain) == -1:
2021-04-12 20:34:18 +05:00
2021-03-25 00:10:30 +05:00
#copy_tree('%s/%s-docroot' % (completPath, domain), path)
2021-04-12 16:02:17 +05:00
## First remove if already exists
command = 'rm -rf %s' % (path)
ProcessUtilities.executioner(command)
##
2021-03-25 00:10:30 +05:00
command = 'cp -R %s/%s-docroot %s' % (completPath, domain, path)
ProcessUtilities.executioner(command)
2020-04-06 14:24:52 +05:00
2018-02-16 00:57:46 +05:00
continue
else:
logging.CyberCPLogFileWriter.writeToFile('Error domain %s' % (domain))
logging.CyberCPLogFileWriter.statusWriter(status, "Error Message: " + retValues[
1] + ". Not able to create child domains, aborting. [635][5009]")
2018-02-16 00:57:46 +05:00
return 0
2019-12-10 15:09:10 +05:00
except BaseException as msg:
status = open(os.path.join(completPath, 'status'), "w")
status.write("Error Message: " + str(msg) + ". Not able to create child domains, aborting. [638][5009]")
status.close()
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startRestore]")
return 0
2018-05-11 00:03:26 +05:00
## Restore Aliases
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.statusWriter(status, "Restoring Domain Aliases!")
2018-05-09 23:10:57 +05:00
aliases = backupMetaData.findall('Aliases/alias')
for items in aliases:
2018-08-23 15:39:28 +05:00
virtualHostUtilities.createAlias(masterDomain, items.text, 0, "", "", "admin")
2018-05-09 23:10:57 +05:00
2018-05-11 00:03:26 +05:00
## Restoring email accounts
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.statusWriter(status, "Restoring email accounts!")
2018-05-11 00:03:26 +05:00
emailAccounts = backupMetaData.findall('emails/emailAccount')
try:
for emailAccount in emailAccounts:
email = emailAccount.find('email').text
username = email.split("@")[0]
password = emailAccount.find('password').text
2019-12-16 20:53:03 +05:00
result = mailUtilities.createEmailAccount(masterDomain, username, password, 'restore')
2018-07-05 15:22:48 +05:00
if result[0] == 0:
raise BaseException(result[1])
2019-12-10 15:09:10 +05:00
except BaseException as msg:
logging.CyberCPLogFileWriter.statusWriter(status, "Error Message: " + str(
msg) + ". Not able to create email accounts, aborting. [671][5009]")
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startRestore]")
return 0
## Emails restored
## restoring databases
2017-10-24 19:16:36 +05:00
### This will actually restore mysql dump and create mysql users
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.statusWriter(status, "Restoring Databases!")
2017-10-24 19:16:36 +05:00
2018-02-16 00:57:46 +05:00
databases = backupMetaData.findall('Databases/database')
for database in databases:
2018-02-16 00:57:46 +05:00
dbName = database.find('dbName').text
if VERSION == '2.1' and int(BUILD) >= 1:
2021-04-12 16:02:17 +05:00
logging.CyberCPLogFileWriter.writeToFile('Backup version 2.1.1+ detected..')
2021-04-12 16:02:17 +05:00
first = 1
databaseUsers = database.findall('databaseUsers')
for databaseUser in databaseUsers:
dbUser = databaseUser.find('dbUser').text
dbHost = databaseUser.find('dbHost').text
password = databaseUser.find('password').text
2021-04-12 16:02:17 +05:00
if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile('Database user: %s' % (dbUser))
logging.CyberCPLogFileWriter.writeToFile('Database host: %s' % (dbHost))
logging.CyberCPLogFileWriter.writeToFile('Database password: %s' % (password))
## Future ref, this logic can be further refactored to improve restore backup logic
if first:
first = 0
res = mysqlUtilities.mysqlUtilities.restoreDatabaseBackup(dbName, completPath, password, 1)
if res == 0:
logging.CyberCPLogFileWriter.writeToFile(
'Failed to restore database %s. But it can be false positive, moving on..' % (
dbName))
2021-04-12 20:34:18 +05:00
### This function will not create database, only database user is created as third value is 0 for createDB
mysqlUtilities.mysqlUtilities.createDatabase(dbName, dbUser, password, 0, dbHost)
2021-04-12 16:02:17 +05:00
mysqlUtilities.mysqlUtilities.changePassword(dbUser, password, 1, dbHost)
# UserInMySQLTable = DBUsers.objects.get(user=dbUser, host=dbHost)
# UserInMySQLTable.password = password
# UserInMySQLTable.save()
else:
password = database.find('password').text
if mysqlUtilities.mysqlUtilities.restoreDatabaseBackup(dbName, completPath, password) == 0:
raise BaseException
## Databases restored
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.statusWriter(status, "Extracting web home data!")
2017-10-24 19:16:36 +05:00
# /home/backup/backup-example.com-02.13.2018_10-24-52/public_html.tar.gz
2020-04-06 14:24:52 +05:00
## Moving above v2.0.0 extracting webhome data is not required, thus commenting below lines
2020-04-06 15:59:05 +05:00
if not twoPointO:
tar = tarfile.open(pathToCompressedHome)
tar.extractall(websiteHome)
tar.close()
else:
if float(version) > 2.0 or float(build) > 0:
2021-03-25 00:10:30 +05:00
#copy_tree('%s/public_html' % (completPath), websiteHome)
2021-04-12 16:02:17 +05:00
## First remove if already exists
command = 'rm -rf %s' % (websiteHome)
ProcessUtilities.executioner(command)
##
2021-03-25 00:10:30 +05:00
command = 'cp -R %s/public_html %s' % (completPath, websiteHome)
ProcessUtilities.executioner(command)
2017-10-24 19:16:36 +05:00
## extracting email accounts
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.statusWriter(status, "Extracting email accounts!")
if not twoPointO:
try:
pathToCompressedEmails = os.path.join(completPath, masterDomain + ".tar.gz")
emailHome = os.path.join("/home", "vmail", masterDomain)
2018-08-28 01:19:34 +05:00
tar = tarfile.open(pathToCompressedEmails)
tar.extractall(emailHome)
tar.close()
2018-08-28 01:19:34 +05:00
## Change permissions
2018-08-28 01:19:34 +05:00
command = "chown -R vmail:vmail " + emailHome
subprocess.call(shlex.split(command))
except:
pass
else:
emailsPath = '%s/vmail' % (completPath)
if os.path.exists(emailsPath):
2021-03-25 00:10:30 +05:00
#copy_tree(emailsPath, '/home/vmail/%s' % (masterDomain))
2021-04-12 16:02:17 +05:00
## First remove if already exists
command = 'rm -rf /home/vmail/%s' % (masterDomain)
ProcessUtilities.executioner(command)
##
2021-03-25 00:10:30 +05:00
command = 'cp -R %s /home/vmail/%s' % (emailsPath, masterDomain)
ProcessUtilities.executioner(command)
command = "chown -R vmail:vmail /home/vmail/%s" % (masterDomain)
ProcessUtilities.executioner(command)
## emails extracted
2018-08-28 01:19:34 +05:00
if os.path.exists(backupUtilities.licenseKey):
2019-07-18 14:08:00 +05:00
completPathToConf = backupUtilities.Server_root + '/conf/vhosts/' + masterDomain + '/vhost.conf'
2018-08-28 01:19:34 +05:00
if os.path.exists(completPath + '/vhost.conf'):
copy(completPath + '/vhost.conf', completPathToConf)
2018-02-19 23:42:57 +05:00
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.statusWriter(status, "Done")
2018-02-19 23:42:57 +05:00
2017-11-05 03:02:51 +05:00
installUtilities.reStartLiteSpeed()
2020-04-06 19:08:41 +05:00
## Fix permissions
2020-04-03 11:28:27 +05:00
2020-04-06 19:08:41 +05:00
from filemanager.filemanager import FileManager
2020-04-06 19:08:41 +05:00
fm = FileManager(None, None)
fm.fixPermissions(masterDomain)
2017-11-05 03:02:51 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2019-03-13 18:22:12 +05:00
status = os.path.join(completPath, 'status')
2019-12-05 22:36:17 +05:00
logging.CyberCPLogFileWriter.statusWriter(status, str(msg) + " [736][5009]")
2017-10-24 19:16:36 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startRestore]")
@staticmethod
def initiateRestore(backupName, dir):
2017-10-24 19:16:36 +05:00
try:
2017-10-26 23:50:59 +05:00
p = Process(target=backupUtilities.startRestore, args=(backupName, dir,))
2017-10-24 19:16:36 +05:00
p.start()
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2017-10-24 19:16:36 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [initiateRestore]")
@staticmethod
def sendKey(IPAddress, password, port='22', user='root'):
2017-10-24 19:16:36 +05:00
try:
2019-03-01 20:20:44 +05:00
expectation = []
expectation.append("password:")
expectation.append("Password:")
expectation.append("Permission denied")
expectation.append("100%")
2019-03-01 20:20:44 +05:00
2020-05-16 15:42:15 +05:00
## Temp changes
command = 'chmod 600 %s' % ('/root/.ssh/cyberpanel.pub')
ProcessUtilities.executioner(command)
command = "scp -o StrictHostKeyChecking=no -P " + port + " /root/.ssh/cyberpanel.pub " + user + "@" + IPAddress + ":~/.ssh/authorized_keys"
2019-03-01 20:20:44 +05:00
setupKeys = pexpect.spawn(command, timeout=3)
2020-04-27 16:18:46 +05:00
if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile(command)
2019-03-01 20:20:44 +05:00
index = setupKeys.expect(expectation)
2017-10-24 19:16:36 +05:00
2019-03-01 20:20:44 +05:00
## on first login attempt send password
2017-10-24 19:16:36 +05:00
2019-03-01 20:20:44 +05:00
if index == 0:
setupKeys.sendline(password)
2019-03-02 17:57:09 +05:00
setupKeys.expect("100%")
setupKeys.wait()
2019-03-01 20:20:44 +05:00
elif index == 1:
setupKeys.sendline(password)
2019-03-02 17:57:09 +05:00
setupKeys.expect("100%")
setupKeys.wait()
2019-03-01 20:20:44 +05:00
elif index == 2:
return [0, 'Please enable password authentication on your remote server.']
elif index == 3:
pass
2019-03-01 20:20:44 +05:00
else:
raise BaseException
2017-10-24 19:16:36 +05:00
2020-05-16 15:42:15 +05:00
## Temp changes
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
ProcessUtilities.executioner(command)
return [1, "None"]
2017-10-24 19:16:36 +05:00
2019-12-10 15:09:10 +05:00
except pexpect.TIMEOUT as msg:
2020-05-16 15:42:15 +05:00
2020-05-16 15:43:16 +05:00
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
2020-05-16 15:42:15 +05:00
ProcessUtilities.executioner(command)
2017-10-24 19:16:36 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
return [0, "TIMEOUT [sendKey]"]
2019-12-10 15:09:10 +05:00
except pexpect.EOF as msg:
2020-05-16 15:42:15 +05:00
2020-05-16 15:43:16 +05:00
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
2020-05-16 15:42:15 +05:00
ProcessUtilities.executioner(command)
2017-10-24 19:16:36 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
return [0, "EOF [sendKey]"]
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2020-05-16 15:42:15 +05:00
2020-05-16 15:43:16 +05:00
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
2020-05-16 15:42:15 +05:00
ProcessUtilities.executioner(command)
2017-10-24 19:16:36 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
return [0, str(msg) + " [sendKey]"]
2017-10-24 19:16:36 +05:00
@staticmethod
def setupSSHKeys(IPAddress, password, port='22', user='root'):
2017-10-24 19:16:36 +05:00
try:
## Checking for host verification
backupUtilities.host_key_verification(IPAddress)
if backupUtilities.checkIfHostIsUp(IPAddress) == 1:
pass
else:
2019-03-01 20:20:44 +05:00
logging.CyberCPLogFileWriter.writeToFile("Host is Down.")
# return [0,"Host is Down."]
2017-10-24 19:16:36 +05:00
expectation = []
expectation.append("password:")
2019-03-01 20:20:44 +05:00
expectation.append("Password:")
expectation.append("Permission denied")
expectation.append("File exists")
2017-10-24 19:16:36 +05:00
2020-05-16 15:42:15 +05:00
command = "ssh -o StrictHostKeyChecking=no -p " + port + ' ' + user + "@" + IPAddress + ' "mkdir ~/.ssh || rm -f ~/.ssh/temp && rm -f ~/.ssh/authorized_temp && cp ~/.ssh/authorized_keys ~/.ssh/temp || chmod 700 ~/.ssh || chmod g-w ~"'
2019-03-01 20:20:44 +05:00
setupKeys = pexpect.spawn(command, timeout=3)
2017-10-24 19:16:36 +05:00
2020-04-27 16:18:46 +05:00
if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile(command)
index = setupKeys.expect(expectation)
2017-10-24 19:16:36 +05:00
## on first login attempt send password
2017-10-24 19:16:36 +05:00
if index == 0:
setupKeys.sendline(password)
elif index == 1:
2019-03-01 20:20:44 +05:00
setupKeys.sendline(password)
elif index == 2:
return [0, 'Please enable password authentication on your remote server.']
elif index == 3:
pass
else:
raise BaseException
2017-10-24 19:16:36 +05:00
## if it again give you password, than provided password is wrong
2017-10-24 19:16:36 +05:00
expectation = []
expectation.append("please try again.")
2019-03-02 17:57:09 +05:00
expectation.append("Password:")
expectation.append(pexpect.EOF)
2017-10-24 19:16:36 +05:00
index = setupKeys.expect(expectation)
2017-10-24 19:16:36 +05:00
if index == 0:
return [0, "Wrong Password!"]
2017-10-24 19:16:36 +05:00
elif index == 1:
2019-03-02 17:57:09 +05:00
return [0, "Wrong Password!"]
elif index == 2:
setupKeys.wait()
2017-10-24 19:16:36 +05:00
sendKey = backupUtilities.sendKey(IPAddress, password, port, user)
2017-10-24 19:16:36 +05:00
if sendKey[0] == 1:
return [1, "None"]
else:
return [0, sendKey[1]]
2017-10-24 19:16:36 +05:00
2019-12-10 15:09:10 +05:00
except pexpect.TIMEOUT as msg:
return [0, str(msg) + " [TIMEOUT setupSSHKeys]"]
2019-12-10 15:09:10 +05:00
except BaseException as msg:
return [0, str(msg) + " [setupSSHKeys]"]
2017-10-24 19:16:36 +05:00
@staticmethod
def checkIfHostIsUp(IPAddress):
try:
2019-12-15 13:30:40 +05:00
if subprocess.check_output(['ping', IPAddress, '-c 1']).decode("utf-8").find("0% packet loss") > -1:
2017-10-24 19:16:36 +05:00
return 1
else:
return 0
2019-12-10 15:09:10 +05:00
except BaseException as msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[checkIfHostIsUp]")
2017-10-24 19:16:36 +05:00
@staticmethod
2020-04-27 21:01:28 +05:00
def checkConnection(IPAddress, port='22', user='root'):
2017-10-24 19:16:36 +05:00
try:
2017-12-09 22:30:10 +05:00
try:
2020-04-27 16:18:46 +05:00
import json
2017-12-09 22:30:10 +05:00
destinations = backupUtilities.destinationsPath
2020-04-27 16:18:46 +05:00
data = json.loads(open(destinations, 'r').read())
port = data['port']
user = data['user']
2017-12-09 22:30:10 +05:00
except:
port = "22"
2017-10-24 19:16:36 +05:00
expectation = []
expectation.append("password:")
2019-03-02 17:57:09 +05:00
expectation.append("Password:")
2017-10-24 19:16:36 +05:00
expectation.append("Last login")
expectation.append(pexpect.EOF)
expectation.append(pexpect.TIMEOUT)
2017-10-24 19:16:36 +05:00
2020-04-27 21:01:28 +05:00
command = "sudo ssh -i /root/.ssh/cyberpanel -o StrictHostKeyChecking=no -p " + port + ' ' + user + "@" + IPAddress
if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile(command)
checkConn = pexpect.spawn(command,timeout=3)
2017-10-24 19:16:36 +05:00
index = checkConn.expect(expectation)
if index == 0:
2019-03-26 16:19:03 +05:00
subprocess.call(['kill', str(checkConn.pid)])
logging.CyberCPLogFileWriter.writeToFile(
"Remote Server is not able to authenticate for transfer to initiate, IP Address:" + IPAddress)
return [0, "Remote Server is not able to authenticate for transfer to initiate."]
2017-10-24 19:16:36 +05:00
elif index == 1:
2019-03-26 16:19:03 +05:00
subprocess.call(['kill', str(checkConn.pid)])
2019-03-02 17:57:09 +05:00
logging.CyberCPLogFileWriter.writeToFile(
"Remote Server is not able to authenticate for transfer to initiate, IP Address:" + IPAddress)
return [0, "Remote Server is not able to authenticate for transfer to initiate."]
elif index == 2:
2019-03-26 16:19:03 +05:00
subprocess.call(['kill', str(checkConn.pid)])
return [1, "None"]
2019-03-02 17:57:09 +05:00
elif index == 4:
2019-03-26 16:19:03 +05:00
subprocess.call(['kill', str(checkConn.pid)])
return [1, "None"]
2017-10-24 19:16:36 +05:00
else:
2019-03-26 16:19:03 +05:00
subprocess.call(['kill', str(checkConn.pid)])
return [1, "None"]
2017-10-24 19:16:36 +05:00
2019-12-10 15:09:10 +05:00
except pexpect.TIMEOUT as msg:
logging.CyberCPLogFileWriter.writeToFile("Timeout " + IPAddress + " [checkConnection]")
return [0, "371 Timeout while making connection to this server [checkConnection]"]
2019-12-10 15:09:10 +05:00
except pexpect.EOF as msg:
logging.CyberCPLogFileWriter.writeToFile("EOF " + IPAddress + "[checkConnection]")
return [0, "374 Remote Server is not able to authenticate for transfer to initiate. [checkConnection]"]
2019-12-10 15:09:10 +05:00
except BaseException as msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " " + IPAddress + " [checkConnection]")
return [0, "377 Remote Server is not able to authenticate for transfer to initiate. [checkConnection]"]
2017-10-24 19:16:36 +05:00
@staticmethod
2020-04-25 07:24:26 -04:00
def verifyHostKey(IPAddress, port='22', user='root'):
2017-10-24 19:16:36 +05:00
try:
backupUtilities.host_key_verification(IPAddress)
2017-10-24 19:16:36 +05:00
password = "hello" ## dumb password, not used anywhere.
expectation = []
expectation.append("continue connecting (yes/no)?")
expectation.append("password:")
2020-04-25 07:24:26 -04:00
setupSSHKeys = pexpect.spawn("ssh -p " + port + user + "@" + IPAddress, timeout=3)
index = setupSSHKeys.expect(expectation)
if index == 0:
setupSSHKeys.sendline("yes")
setupSSHKeys.expect("password:")
setupSSHKeys.sendline(password)
expectation = []
expectation.append("password:")
expectation.append(pexpect.EOF)
innerIndex = setupSSHKeys.expect(expectation)
if innerIndex == 0:
setupSSHKeys.kill(signal.SIGTERM)
return [1, "None"]
elif innerIndex == 1:
setupSSHKeys.kill(signal.SIGTERM)
return [1, "None"]
elif index == 1:
setupSSHKeys.expect("password:")
setupSSHKeys.sendline(password)
expectation = []
expectation.append("password:")
expectation.append(pexpect.EOF)
innerIndex = setupSSHKeys.expect(expectation)
if innerIndex == 0:
setupSSHKeys.kill(signal.SIGTERM)
return [1, "None"]
elif innerIndex == 1:
setupSSHKeys.kill(signal.SIGTERM)
return [1, "None"]
2017-10-24 19:16:36 +05:00
2019-12-10 15:09:10 +05:00
except pexpect.TIMEOUT as msg:
2017-10-26 23:50:59 +05:00
logging.CyberCPLogFileWriter.writeToFile("Timeout [verifyHostKey]")
return [0, "Timeout [verifyHostKey]"]
2019-12-10 15:09:10 +05:00
except pexpect.EOF as msg:
2017-10-26 23:50:59 +05:00
logging.CyberCPLogFileWriter.writeToFile("EOF [verifyHostKey]")
return [0, "EOF [verifyHostKey]"]
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2017-10-24 19:16:36 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [verifyHostKey]")
return [0, str(msg) + " [verifyHostKey]"]
2017-10-24 19:16:36 +05:00
@staticmethod
def createBackupDir(IPAddress, port='22', user='root'):
2017-10-24 19:16:36 +05:00
try:
2020-04-25 06:48:08 -04:00
command = "sudo ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel " + user + "@" + IPAddress + " mkdir ~/backup"
2020-04-27 16:18:46 +05:00
if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile(command)
2019-03-26 16:19:03 +05:00
subprocess.call(shlex.split(command))
command = "sudo ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel " + user + "@" + IPAddress + ' "cat ~/.ssh/authorized_keys ~/.ssh/temp > ~/.ssh/authorized_temp"'
2020-04-27 16:18:46 +05:00
if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile(command)
2019-03-26 16:19:03 +05:00
subprocess.call(shlex.split(command))
2020-05-16 15:42:15 +05:00
command = "sudo ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel " + user + "@" + IPAddress + ' "cat ~/.ssh/authorized_temp > ~/.ssh/authorized_keys"'
2020-04-27 16:18:46 +05:00
if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile(command)
2019-03-26 16:19:03 +05:00
subprocess.call(shlex.split(command))
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2017-10-24 19:16:36 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [createBackupDir]")
return 0
@staticmethod
def host_key_verification(IPAddress):
try:
command = 'sudo ssh-keygen -R ' + IPAddress
2019-03-26 16:19:03 +05:00
subprocess.call(shlex.split(command))
return 1
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2017-10-24 19:16:36 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [host_key_verification]")
return 0
2018-05-09 23:10:57 +05:00
@staticmethod
def getAliases(masterDomain):
try:
aliases = []
2018-09-24 18:56:48 +05:00
master = Websites.objects.get(domain=masterDomain)
aliasDomains = master.aliasdomains_set.all()
2018-05-09 23:10:57 +05:00
2018-09-24 18:56:48 +05:00
for items in aliasDomains:
aliases.append(items.aliasDomain)
2018-05-09 23:10:57 +05:00
return aliases
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-05-09 23:10:57 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [getAliases]")
2019-12-10 15:09:10 +05:00
print(0)
2018-05-09 23:10:57 +05:00
2020-11-26 18:13:05 +05:00
### Cloud Backup functions
def CheckIfSleepNeeded(self):
import psutil
while (1):
2020-11-27 22:04:29 +05:00
logging.CyberCPLogFileWriter.writeToFile('Current CPU percent %s.' % (int(psutil.cpu_percent(interval=None))))
if int(psutil.cpu_percent(interval=None)) > int(self.cpu):
2020-11-26 18:13:05 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Current CPU usage exceeds %s percent. Backup process will sleep for %s seconds..,0' % (self.cpu, str(self.time)))
2020-11-26 18:13:05 +05:00
import time
time.sleep(self.time)
2020-11-26 18:13:05 +05:00
else:
break
def BackupData(self):
2020-11-27 22:04:29 +05:00
try:
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
### Creating the dir to store backups
self.BackupDataPath = '%s/data' % (self.BackupPath)
command = 'mkdir -p %s' % (self.BackupDataPath)
ProcessUtilities.executioner(command)
self.DataPath = '/home/%s' % (self.extraArgs['domain'])
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
## Backing up data
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
self.CheckIfSleepNeeded()
2020-11-26 18:13:05 +05:00
2020-12-06 14:10:09 +05:00
command = 'nice -n %s cp -Rp %s %s' % (self.nice, self.DataPath, self.BackupDataPath)
2020-11-27 22:04:29 +05:00
ProcessUtilities.executioner(command)
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
## Store child domains if any in json format
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
DataJson = {}
childs = []
import json
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
for child in self.website.childdomains_set.all():
2020-12-06 14:10:09 +05:00
childs.append({'domain': child.domain, 'path': child.path, 'php': child.phpSelection})
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
DataJson['ChildDomains'] = childs
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
DataJsonPath = '%s/%s' % (self.BackupPath, 'data.json')
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
writeToFile = open(DataJsonPath, 'w')
writeToFile.write(json.dumps(DataJson))
writeToFile.close()
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
return 1, None
except BaseException as msg:
return 0, str(msg)
2020-11-26 18:13:05 +05:00
def BackupEmails(self):
2020-11-27 22:04:29 +05:00
try:
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
from mailServer.models import Domains, EUsers
try:
emailDomain = Domains.objects.get(domainOwner=self.website)
except:
return 1, None
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
### Creating the dir to store backups
self.BackupDataPath = '%s/emails' % (self.BackupPath)
command = 'mkdir -p %s' % (self.BackupDataPath)
ProcessUtilities.executioner(command)
self.DataPath = '/home/vmail/%s' % (self.extraArgs['domain'])
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
## Backing up data
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
self.CheckIfSleepNeeded()
2020-11-26 18:13:05 +05:00
2020-12-06 14:10:09 +05:00
command = 'nice -n %s cp -Rp %s %s' % (self.nice, self.DataPath, self.BackupDataPath)
2020-11-27 22:04:29 +05:00
ProcessUtilities.executioner(command)
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
## Store child domains if any in json format
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
DataJson = {}
emailsList = []
import json
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
for emails in emailDomain.eusers_set.all():
emailsList.append({'email': emails.email, 'password': emails.password})
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
DataJson['emails'] = emailsList
DataJsonPath = '%s/%s' % (self.BackupPath, 'emails.json')
writeToFile = open(DataJsonPath, 'w')
writeToFile.write(json.dumps(DataJson))
writeToFile.close()
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
return 1, None
except BaseException as msg:
return 0, str(msg)
def BackupDatabases(self):
try:
### Creating the dir to store backups
self.BackupDataPath = '%s/databases' % (self.BackupPath)
command = 'mkdir -p %s' % (self.BackupDataPath)
ProcessUtilities.executioner(command)
## Backing up data
self.CheckIfSleepNeeded()
DataJson = {}
databases = []
import json
for items in self.website.databases_set.all():
try:
dbuser = DBUsers.objects.get(user=items.dbUser)
userToTry = items.dbUser
except:
try:
dbusers = DBUsers.objects.all().filter(user=items.dbUser)
userToTry = items.dbUser
for it in dbusers:
dbuser = it
break
userToTry = mysqlUtilities.mysqlUtilities.fetchuser(items.dbName)
if userToTry == 0 or userToTry == 1:
continue
try:
dbuser = DBUsers.objects.get(user=userToTry)
except:
try:
dbusers = DBUsers.objects.all().filter(user=userToTry)
for it in dbusers:
dbuser = it
break
except BaseException as msg:
logging.CyberCPLogFileWriter.writeToFile(
'While creating backup for %s, we failed to backup database %s. Error message: %s' % (
self.website.domain, items.dbName, str(msg)))
continue
except BaseException as msg:
logging.CyberCPLogFileWriter.writeToFile(
'While creating backup for %s, we failed to backup database %s. Error message: %s' % (
self.website.domain, items.dbName, str(msg)))
continue
databases.append({'databaseName': str(items.dbName), 'databaseUser': str(userToTry), 'password': str(dbuser.password)})
self.CheckIfSleepNeeded()
mysqlUtilities.mysqlUtilities.createDatabaseBackup(items.dbName, self.BackupDataPath)
DataJson['databases'] = databases
DataJsonPath = '%s/%s' % (self.BackupPath, 'databases.json')
writeToFile = open(DataJsonPath, 'w')
writeToFile.write(json.dumps(DataJson))
writeToFile.close()
return 1, None
except BaseException as msg:
return 0, str(msg)
2020-11-26 18:13:05 +05:00
def CloudBackups(self):
2020-11-26 18:13:05 +05:00
import json
if os.path.exists(backupUtilities.CloudBackupConfigPath):
result = json.loads(open(backupUtilities.CloudBackupConfigPath, 'r').read())
self.nice = result['nice']
self.cpu = result['cpu']
self.time = int(result['time'])
2020-11-26 18:13:05 +05:00
else:
self.nice = backupUtilities.NiceDefault
self.cpu = backupUtilities.CPUDefault
self.time = int(backupUtilities.time)
2020-11-26 18:13:05 +05:00
2020-12-31 22:54:07 +05:00
self.BackupPath = self.extraArgs['path']
2020-11-26 18:13:05 +05:00
self.website = Websites.objects.get(domain=self.extraArgs['domain'])
command = 'mkdir -p %s' % (self.BackupPath)
ProcessUtilities.executioner(command)
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Starting backup generation..,0')
if self.extraArgs['data']:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Generating backup for your data,5')
2020-11-27 22:04:29 +05:00
result = self.BackupData()
if result[0] == 0:
2020-11-26 18:13:05 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
2020-11-27 22:04:29 +05:00
'Failed to generate backups for data. Error: %s. [404], 0' % (result[1] ))
2020-12-08 07:35:30 +05:00
return 0, self.BackupPath
2020-11-26 18:13:05 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Data backup successfully generated,30')
if self.extraArgs['emails']:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
2020-11-28 10:43:19 +05:00
'Generating backup for your emails,40')
2020-11-27 22:04:29 +05:00
result = self.BackupEmails()
if result[0] == 0:
2020-11-26 18:13:05 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
2020-11-27 22:04:29 +05:00
'Failed to generate backups for emails. Error: %s. [404], 0' % (result[1] ))
2020-12-08 07:35:30 +05:00
return 0, self.BackupPath
2020-11-26 18:13:05 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
2020-11-28 10:43:19 +05:00
'Emails backup successfully generated,60')
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
if self.extraArgs['databases']:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
2020-11-28 10:43:19 +05:00
'Generating backup for your databases,60')
2020-11-27 22:04:29 +05:00
result = self.BackupDatabases()
if result[0] == 0:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Failed to generate backups for databases. Error: %s. [404], 0' % (result[1] ))
2020-12-08 07:35:30 +05:00
return 0, self.BackupPath
2020-11-26 18:13:05 +05:00
2020-11-27 22:04:29 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Databases backups successfully generated,30')
2020-11-26 18:13:05 +05:00
2020-11-28 10:43:19 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Creating final archive..,80')
command = 'nice -n %s tar czf %s.tar.gz -C %s %s' % (self.nice, self.BackupPath, '/home/cyberpanel/backups/%s' % (self.extraArgs['domain']), self.BackupPath.split('/')[-1])
ProcessUtilities.executioner(command)
command = 'rm -rf %s' % (self.BackupPath)
ProcessUtilities.executioner(command)
2020-12-08 07:35:30 +05:00
finalPath = '%s.tar.gz' % (self.BackupPath)
command = 'chown cyberpanel:cyberpanel %s' % (finalPath)
ProcessUtilities.executioner(command)
command = 'chmod 600:600 %s' % (finalPath)
ProcessUtilities.executioner(command)
2020-12-31 22:54:07 +05:00
if self.extraArgs['port'] != 0:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Sending file to destination server..,90')
command = "scp -o StrictHostKeyChecking=no -P %s -i /root/.ssh/cyberpanel %s root@%s:/home/cyberpanel/backups/%s/" % (self.extraArgs['port'], finalPath, self.extraArgs['ip'], self.extraArgs['destinationDomain'])
ProcessUtilities.outputExecutioner(command)
2020-12-08 15:01:17 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], 'Completed [200].')
2020-12-08 07:35:30 +05:00
return 1, self.BackupPath + '.tar.gz'
2020-12-06 14:10:09 +05:00
## Restore functions
def SubmitCloudBackupRestore(self):
2020-12-11 23:07:15 +05:00
try:
import json
if os.path.exists(backupUtilities.CloudBackupConfigPath):
result = json.loads(open(backupUtilities.CloudBackupConfigPath, 'r').read())
self.nice = result['nice']
self.cpu = result['cpu']
self.time = int(result['time'])
else:
self.nice = backupUtilities.NiceDefault
self.cpu = backupUtilities.CPUDefault
self.time = int(backupUtilities.time)
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
self.BackupPath = '/home/cyberpanel/backups/%s/%s' % (self.extraArgs['domain'], self.extraArgs['backupFile'])
self.website = Websites.objects.get(domain=self.extraArgs['domain'])
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Extracting main archive..,0')
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
command = 'tar -xf %s -C %s' % (self.BackupPath, '/home/cyberpanel/backups/%s/' % (self.extraArgs['domain']))
ProcessUtilities.executioner(command)
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Main Archive extracted,20')
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
self.extractedPath = '/home/cyberpanel/backups/%s/%s' % (self.extraArgs['domain'], self.extraArgs['backupFile'].rstrip('.tar.gz'))
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
self.dataPath = '%s/data' % (self.extractedPath)
self.databasesPath = '%s/databases' % (self.extractedPath)
self.emailsPath = '%s/emails' % (self.extractedPath)
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
## Data
if os.path.exists(self.dataPath):
try:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Creating child domains if any..,20')
childDomains = json.loads(open('%s/data.json' % (self.extractedPath), 'r').read())['ChildDomains']
for child in childDomains:
try:
ch = ChildDomains.objects.get(domain=child['domain'])
except:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Creating %s,20' % (child['domain']))
virtualHostUtilities.createDomain(self.website.domain, child['domain'], child['php'], child['path'], 1, 0, 0,
self.website.admin.userName, 0, "/home/cyberpanel/" + str(randint(1000, 9999)))
except BaseException as msg:
logging.CyberCPLogFileWriter.writeToFile('%s [SubmitCloudBackupRestore:1533]' % str(msg))
homePath = '/home/%s' % (self.website.domain)
command = 'rm -rf %s' % (homePath)
ProcessUtilities.executioner(command)
2020-12-31 22:54:07 +05:00
if self.extraArgs['sourceDomain'] == 'None':
command = 'mv %s/%s %s' % (self.dataPath, self.website.domain, '/home')
else:
command = 'mv %s/%s %s/%s' % (self.dataPath, self.extraArgs['sourceDomain'], '/home', self.extraArgs['domain'])
2020-12-11 23:07:15 +05:00
ProcessUtilities.executioner(command)
2020-12-31 22:54:07 +05:00
2020-12-11 23:07:15 +05:00
from filemanager.filemanager import FileManager
fm = FileManager(None, None)
fm.fixPermissions(self.website.domain)
## Emails
if os.path.exists(self.emailsPath):
try:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Creating emails if any..,40')
emails = json.loads(open('%s/emails.json' % (self.extractedPath), 'r').read())['emails']
from mailServer.models import Domains, EUsers
emailDomain = Domains.objects.get(domain=self.website.domain)
for email in emails:
try:
eu = EUsers.objects.get(emailOwner=emailDomain, email=email['email'])
except:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Creating %s,40' % (email['email']))
emailAcct = EUsers(emailOwner=emailDomain, email=email['email'], password=email['password'])
emailAcct.mail = 'maildir:/home/vmail/%s/%s/Maildir' % (self.website.domain, email['email'].split('@')[0])
emailAcct.save()
EmailsHome = '/home/vmail/%s' % (self.website.domain)
command = 'rm -rf %s' % (EmailsHome)
ProcessUtilities.executioner(command)
command = 'mv %s/%s /home/vmail' % (self.emailsPath, self.website.domain)
ProcessUtilities.executioner(command)
command = 'chown -R vmail:vmail %s' % (EmailsHome)
ProcessUtilities.executioner(command)
except BaseException as msg:
logging.CyberCPLogFileWriter.writeToFile('%s [SubmitCloudBackupRestore:1533]' % str(msg))
## Databases
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Restoring databases if any..,70')
databases = json.loads(open('%s/databases.json' % (self.extractedPath), 'r').read())['databases']
for db in databases:
2020-12-06 14:10:09 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
2020-12-11 23:07:15 +05:00
'Restoring database %s..,70' % (db['databaseName']))
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
mysqlUtilities.mysqlUtilities.submitDBDeletion(db['databaseName'])
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
if mysqlUtilities.mysqlUtilities.createDatabase(db['databaseName'], db['databaseUser'], "cyberpanel") == 0:
raise BaseException("Failed to create Databases!")
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
newDB = Databases(website=self.website, dbName=db['databaseName'], dbUser=db['databaseUser'])
newDB.save()
mysqlUtilities.mysqlUtilities.restoreDatabaseBackup(db['databaseName'], self.databasesPath, db['password'])
2020-12-31 22:54:07 +05:00
if self.extraArgs['sourceDomain'] != 'None':
if self.extraArgs['sourceDomain'] != self.extraArgs['domain']:
try:
command = 'wp --info'
outout = ProcessUtilities.outputExecutioner(command)
if not outout.find('WP-CLI root dir:') > -1:
from plogical.applicationInstaller import ApplicationInstaller
ai = ApplicationInstaller(None, None)
ai.installWPCLI()
except subprocess.CalledProcessError:
from plogical.applicationInstaller import ApplicationInstaller
ai = ApplicationInstaller(None, None)
ai.installWPCLI()
path = '/home/%s/public_html' % (self.extraArgs['domain'])
command = "wp search-replace '%s' '%s' --path=%s --allow-root" % (self.extraArgs['sourceDomain'], self.extraArgs['domain'], path)
ProcessUtilities.outputExecutioner(command)
2021-01-12 17:56:36 +05:00
command = "wp search-replace 'www.%s' '%s' --path=%s --allow-root" % (
self.extraArgs['sourceDomain'], self.extraArgs['domain'], path)
ProcessUtilities.outputExecutioner(command)
command = "wp search-replace 'www.%s' '%s' --path=%s --allow-root" % (
self.extraArgs['domain'], self.extraArgs['domain'], path)
ProcessUtilities.outputExecutioner(command)
2020-12-11 23:07:15 +05:00
command = 'rm -rf %s' % (self.extractedPath)
2020-12-06 14:10:09 +05:00
ProcessUtilities.executioner(command)
2020-12-11 23:07:15 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], 'Completed [200].')
except BaseException as msg:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], '%s [404].' % str(msg))
### Cloud Backup functions ends
def fetchAWSKeys(self):
path = '/home/cyberpanel/.aws'
credentials = path + '/credentials'
data = open(credentials, 'r').readlines()
aws_access_key_id = data[1].split(' ')[2].strip(' ').strip('\n')
aws_secret_access_key = data[2].split(' ')[2].strip(' ').strip('\n')
region = data[3].split(' ')[2].strip(' ').strip('\n')
return aws_access_key_id, aws_secret_access_key, region
def SubmitS3BackupRestore(self):
try:
import json
if os.path.exists(backupUtilities.CloudBackupConfigPath):
result = json.loads(open(backupUtilities.CloudBackupConfigPath, 'r').read())
self.nice = result['nice']
self.cpu = result['cpu']
self.time = int(result['time'])
else:
self.nice = backupUtilities.NiceDefault
self.cpu = backupUtilities.CPUDefault
self.time = int(backupUtilities.time)
### First Download file from S3
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Starting file download from S3..,0')
import boto3
from s3Backups.models import BackupPlan
plan = BackupPlan.objects.get(name=self.extraArgs['planName'])
aws_access_key_id, aws_secret_access_key, region = self.fetchAWSKeys()
2020-12-20 05:58:08 +05:00
if region.find('http') > -1:
s3 = boto3.resource(
2020-12-11 23:07:15 +05:00
's3',
aws_access_key_id=aws_access_key_id,
2020-12-20 05:58:08 +05:00
aws_secret_access_key=aws_secret_access_key,
endpoint_url=region
)
else:
s3 = boto3.resource(
's3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
2020-12-11 23:07:15 +05:00
)
self.BackupPath = '/home/cyberpanel/backups/%s/%s' % (self.extraArgs['domain'], self.extraArgs['backupFile'].split('/')[-1])
s3.Bucket(plan.bucket).download_file(self.extraArgs['backupFile'], self.BackupPath)
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'File download completed..,5')
self.website = Websites.objects.get(domain=self.extraArgs['domain'])
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Extracting main archive..,0')
command = 'tar -xf %s -C %s' % (self.BackupPath, '/home/cyberpanel/backups/%s/' % (self.extraArgs['domain']))
2020-12-06 14:10:09 +05:00
ProcessUtilities.executioner(command)
2020-12-11 23:07:15 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Main Archive extracted,20')
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
self.extractedPath = '/home/cyberpanel/backups/%s/%s' % (self.extraArgs['domain'], self.extraArgs['backupFile'].split('/')[-1].rstrip('.tar.gz'))
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
self.dataPath = '%s/data' % (self.extractedPath)
self.databasesPath = '%s/databases' % (self.extractedPath)
self.emailsPath = '%s/emails' % (self.extractedPath)
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
## Data
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
if os.path.exists(self.dataPath):
try:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Creating child domains if any..,20')
childDomains = json.loads(open('%s/data.json' % (self.extractedPath), 'r').read())['ChildDomains']
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
for child in childDomains:
try:
ch = ChildDomains.objects.get(domain=child['domain'])
except:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Creating %s,20' % (child['domain']))
virtualHostUtilities.createDomain(self.website.domain, child['domain'], child['php'], child['path'], 1, 0, 0,
self.website.admin.userName, 0, "/home/cyberpanel/" + str(randint(1000, 9999)))
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
except BaseException as msg:
logging.CyberCPLogFileWriter.writeToFile('%s [SubmitCloudBackupRestore:1533]' % str(msg))
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
homePath = '/home/%s' % (self.website.domain)
command = 'rm -rf %s' % (homePath)
ProcessUtilities.executioner(command)
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
command = 'mv %s/%s %s' % (self.dataPath, self.website.domain, '/home')
ProcessUtilities.executioner(command)
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
from filemanager.filemanager import FileManager
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
fm = FileManager(None, None)
fm.fixPermissions(self.website.domain)
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
## Emails
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
if os.path.exists(self.emailsPath):
try:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Creating emails if any..,40')
emails = json.loads(open('%s/emails.json' % (self.extractedPath), 'r').read())['emails']
from mailServer.models import Domains, EUsers
emailDomain = Domains.objects.get(domain=self.website.domain)
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
for email in emails:
try:
eu = EUsers.objects.get(emailOwner=emailDomain, email=email['email'])
except:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Creating %s,40' % (email['email']))
emailAcct = EUsers(emailOwner=emailDomain, email=email['email'], password=email['password'])
emailAcct.mail = 'maildir:/home/vmail/%s/%s/Maildir' % (self.website.domain, email['email'].split('@')[0])
emailAcct.save()
EmailsHome = '/home/vmail/%s' % (self.website.domain)
2021-10-16 00:51:48 +06:00
command = f'rm -rf {EmailsHome}'
2020-12-11 23:07:15 +05:00
ProcessUtilities.executioner(command)
command = 'mv %s/%s /home/vmail' % (self.emailsPath, self.website.domain)
ProcessUtilities.executioner(command)
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
command = 'chown -R vmail:vmail %s' % (EmailsHome)
ProcessUtilities.executioner(command)
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
except BaseException as msg:
logging.CyberCPLogFileWriter.writeToFile('%s [SubmitCloudBackupRestore:1533]' % str(msg))
## Databases
2020-12-06 14:10:09 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
2020-12-11 23:07:15 +05:00
'Restoring databases if any..,70')
databases = json.loads(open('%s/databases.json' % (self.extractedPath), 'r').read())['databases']
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
for db in databases:
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'],
'Restoring database %s..,70' % (db['databaseName']))
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
mysqlUtilities.mysqlUtilities.submitDBDeletion(db['databaseName'])
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
if mysqlUtilities.mysqlUtilities.createDatabase(db['databaseName'], db['databaseUser'], "cyberpanel") == 0:
raise BaseException("Failed to create Databases!")
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
newDB = Databases(website=self.website, dbName=db['databaseName'], dbUser=db['databaseUser'])
newDB.save()
2020-12-06 14:10:09 +05:00
2020-12-11 23:07:15 +05:00
mysqlUtilities.mysqlUtilities.restoreDatabaseBackup(db['databaseName'], self.databasesPath, db['password'])
2020-12-06 14:10:09 +05:00
2020-11-26 18:13:05 +05:00
2020-12-11 23:07:15 +05:00
command = 'rm -rf %s' % (self.extractedPath)
ProcessUtilities.executioner(command)
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], 'Completed [200].')
except BaseException as msg:
logging.CyberCPLogFileWriter.statusWriter(self.extraArgs['tempStatusPath'], '%s [404].' % str(msg))
2020-11-26 18:13:05 +05:00
2018-06-05 00:53:45 +05:00
def submitBackupCreation(tempStoragePath, backupName, backupPath, backupDomain):
2017-12-09 22:30:10 +05:00
try:
## /home/example.com/backup/backup-example.com-02.13.2018_10-24-52 -- tempStoragePath
## backup-example.com-02.13.2018_10-24-52 -- backup name
## /home/example.com/backup - backupPath
## /home/cyberpanel/1047.xml - metaPath
2017-12-09 22:30:10 +05:00
2018-08-23 15:39:28 +05:00
status = os.path.join(backupPath, 'status')
2019-07-18 14:08:00 +05:00
website = Websites.objects.get(domain=backupDomain)
##
2021-10-16 00:51:48 +06:00
schedulerPath = f'/home/cyberpanel/{backupDomain}-backup.txt'
2019-07-18 14:08:00 +05:00
##
2021-10-16 00:51:48 +06:00
command = f'mkdir -p {tempStoragePath}'
2021-08-10 16:37:28 +05:00
ProcessUtilities.executioner(command, website.externalApp)
2019-07-18 14:08:00 +05:00
##
2021-08-10 20:38:53 +05:00
2021-10-16 00:51:48 +06:00
command = f'touch {status}'
2021-08-10 16:37:28 +05:00
ProcessUtilities.executioner(command, website.externalApp)
2019-07-18 14:08:00 +05:00
2021-08-12 14:37:30 +05:00
result = backupUtilities.prepareBackupMeta(backupDomain, backupName, tempStoragePath, backupPath)
2018-08-23 15:39:28 +05:00
2021-08-12 14:37:30 +05:00
if result[0] == 0:
writeToFile = open(schedulerPath, 'w')
2020-06-21 21:39:01 +05:00
writeToFile.writelines('1325')
writeToFile.close()
2021-08-30 23:56:54 +05:00
command = "echo '%s [1084][5009]' > %s" % (str(result[1]), status)
ProcessUtilities.executioner(command, website.externalApp)
2019-07-18 14:08:00 +05:00
return 0
2017-12-09 22:30:10 +05:00
2021-08-10 20:38:53 +05:00
2021-11-16 19:12:34 +05:00
command = 'chown %s:%s %s' % (website.externalApp, website.externalApp, result[2])
ProcessUtilities.executioner(command)
2021-11-16 19:12:34 +05:00
logging.CyberCPLogFileWriter.writeToFile(backupPath)
logging.CyberCPLogFileWriter.writeToFile(tempStoragePath)
2021-08-30 23:56:54 +05:00
2019-12-10 23:04:24 +05:00
execPath = "sudo nice -n 10 /usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/backupUtilities.py"
execPath = execPath + " startBackup --tempStoragePath " + tempStoragePath + " --backupName " \
+ backupName + " --backupPath " + backupPath + ' --backupDomain ' + backupDomain + ' --metaPath %s' % (
2021-08-12 14:37:30 +05:00
result[2])
2021-03-26 00:36:41 +05:00
output = ProcessUtilities.outputExecutioner(execPath, website.externalApp)
2021-03-25 20:46:04 +05:00
if output.find('[5009]') > -1:
2019-07-18 14:08:00 +05:00
logging.CyberCPLogFileWriter.writeToFile(output)
writeToFile = open(schedulerPath, 'w')
2020-06-21 21:39:01 +05:00
writeToFile.writelines(output)
writeToFile.close()
2019-07-18 14:08:00 +05:00
return 0
## Backing up databases
2021-10-16 00:51:48 +06:00
command = f'chown cyberpanel:cyberpanel {result[2]}'
2021-08-12 14:37:30 +05:00
ProcessUtilities.executioner(command)
2021-08-12 14:37:30 +05:00
backupMetaData = ElementTree.parse(result[2])
databases = backupMetaData.findall('Databases/database')
for database in databases:
dbName = database.find('dbName').text
res = mysqlUtilities.mysqlUtilities.createDatabaseBackup(dbName, '/home/cyberpanel')
if res == 0:
## This login can be further improved later.
logging.CyberCPLogFileWriter.writeToFile('Failed to create database backup for %s. This could be false positive, moving on.' % (dbName))
command = 'mv /home/cyberpanel/%s.sql %s/%s.sql' % (dbName, tempStoragePath, dbName)
ProcessUtilities.executioner(command, 'root')
##
2021-03-25 20:46:04 +05:00
#output = ProcessUtilities.outputExecutioner(execPath, website.externalApp)
2021-08-30 23:56:54 +05:00
execPath = "sudo nice -n 10 /usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/backupUtilities.py"
execPath = execPath + " BackupRoot --tempStoragePath " + tempStoragePath + " --backupName " \
2021-11-16 18:06:04 +05:00
+ backupName + " --backupPath " + backupPath + ' --backupDomain ' + backupDomain + ' --metaPath %s --externalApp %s' % (
result[2], website.externalApp)
2021-08-30 23:56:54 +05:00
ProcessUtilities.executioner(execPath, 'root')
2019-07-18 14:08:00 +05:00
command = 'chown -R %s:%s %s' % (website.externalApp, website.externalApp, backupPath)
ProcessUtilities.executioner(command)
2017-12-09 22:30:10 +05:00
2021-10-16 00:51:48 +06:00
command = f'rm -f {result[2]}'
ProcessUtilities.executioner(command, 'cyberpanel')
2017-12-09 22:30:10 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2017-12-09 22:30:10 +05:00
logging.CyberCPLogFileWriter.writeToFile(
2021-10-16 00:51:48 +06:00
f"{str(msg)} [submitBackupCreation]")
2017-12-09 22:30:10 +05:00
def cancelBackupCreation(backupCancellationDomain, fileName):
2017-12-09 22:30:10 +05:00
try:
2021-10-16 00:51:48 +06:00
path = f"/home/{backupCancellationDomain}/backup/pid"
2017-12-09 22:30:10 +05:00
pid = open(path, "r").readlines()[0]
try:
os.kill(int(pid), signal.SIGKILL)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2021-10-16 00:51:48 +06:00
logging.CyberCPLogFileWriter.writeToFile(f"{str(msg)} [cancelBackupCreation]")
2017-12-09 22:30:10 +05:00
2021-10-16 00:51:48 +06:00
backupPath = f"/home/{backupCancellationDomain}/backup/"
2017-12-09 22:30:10 +05:00
tempStoragePath = backupPath + fileName
try:
2021-10-16 00:51:48 +06:00
os.remove(f"{tempStoragePath}.tar.gz")
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2021-10-16 00:51:48 +06:00
logging.CyberCPLogFileWriter.writeToFile(f"{str(msg)} [cancelBackupCreation]")
2017-12-09 22:30:10 +05:00
try:
rmtree(tempStoragePath)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2021-10-16 00:51:48 +06:00
logging.CyberCPLogFileWriter.writeToFile(f"{str(msg)} [cancelBackupCreation]")
2017-12-09 22:30:10 +05:00
status = open(backupPath + 'status', "w")
2019-12-05 22:36:17 +05:00
status.write("Aborted manually. [1165][5009]")
2017-12-09 22:30:10 +05:00
status.close()
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2017-12-09 22:30:10 +05:00
logging.CyberCPLogFileWriter.writeToFile(
2021-10-16 00:51:48 +06:00
f"{str(msg)} [cancelBackupCreation]")
print(f"0, {str(msg)}")
def submitRestore(backupFile, dir):
2017-12-09 22:30:10 +05:00
try:
p = Process(target=backupUtilities.startRestore, args=(backupFile, dir,))
p.start()
2019-12-10 15:09:10 +05:00
print("1,None")
2017-12-09 22:30:10 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2017-12-09 22:30:10 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [cancelBackupCreation]")
2021-10-16 00:51:48 +06:00
print(f"0, {str(msg)}")
2017-12-09 22:30:10 +05:00
def submitDestinationCreation(ipAddress, password, port='22', user='root'):
setupKeys = backupUtilities.setupSSHKeys(ipAddress, password, port, user)
2019-03-02 17:57:09 +05:00
if setupKeys[0] == 1:
backupUtilities.createBackupDir(ipAddress, port, user)
2019-12-10 15:09:10 +05:00
print("1,None")
2019-03-02 17:57:09 +05:00
else:
2019-12-10 15:09:10 +05:00
print(setupKeys[1])
2019-03-02 17:57:09 +05:00
def getConnectionStatus(ipAddress):
try:
checkCon = backupUtilities.checkConnection(ipAddress)
if checkCon[0] == 1:
2019-12-10 15:09:10 +05:00
print("1,None")
2019-03-02 17:57:09 +05:00
else:
2019-12-10 15:09:10 +05:00
print(checkCon[1])
2019-03-02 17:57:09 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
print(str(msg))
2019-03-02 17:57:09 +05:00
2017-12-09 22:30:10 +05:00
def main():
2020-11-26 18:13:05 +05:00
parser = argparse.ArgumentParser(description='CyberPanel Backup Generator')
parser.add_argument('function', help='Specify a function to call!')
2017-12-09 22:30:10 +05:00
parser.add_argument('--tempStoragePath', help='')
parser.add_argument('--backupName', help='!')
parser.add_argument('--backupPath', help='')
2018-06-05 00:53:45 +05:00
parser.add_argument('--backupDomain', help='')
2017-12-09 22:30:10 +05:00
parser.add_argument('--metaPath', help='')
2019-03-02 17:57:09 +05:00
## Destination Creation
parser.add_argument('--ipAddress', help='')
parser.add_argument('--password', help='')
parser.add_argument('--port', help='')
parser.add_argument('--user', help='')
2019-03-02 17:57:09 +05:00
2017-12-09 22:30:10 +05:00
## backup cancellation arguments
parser.add_argument('--backupCancellationDomain', help='')
parser.add_argument('--fileName', help='')
## backup restore arguments
parser.add_argument('--backupFile', help='')
parser.add_argument('--dir', help='')
2020-11-26 18:13:05 +05:00
### For Cloud Backups
parser.add_argument('--data', help='')
parser.add_argument('--emails', help='')
parser.add_argument('--databases', help='')
2020-12-31 22:54:07 +05:00
parser.add_argument('--path', help='')
parser.add_argument('--ip', help='')
parser.add_argument('--sourceDomain', help='')
parser.add_argument('--destinationDomain', help='')
2020-11-26 18:13:05 +05:00
2020-12-11 23:07:15 +05:00
## FOR S3
parser.add_argument('--planName', help='')
2021-11-16 18:06:04 +05:00
parser.add_argument('--externalApp', help='')
2020-12-11 23:07:15 +05:00
2020-11-26 18:13:05 +05:00
2017-12-09 22:30:10 +05:00
args = parser.parse_args()
if args.function == "submitBackupCreation":
submitBackupCreation(args.tempStoragePath, args.backupName, args.backupPath, args.backupDomain)
2017-12-09 22:30:10 +05:00
elif args.function == "cancelBackupCreation":
cancelBackupCreation(args.backupCancellationDomain, args.fileName)
2017-12-09 22:30:10 +05:00
elif args.function == "submitRestore":
submitRestore(args.backupFile, args.dir)
2019-03-02 17:57:09 +05:00
elif args.function == "submitDestinationCreation":
submitDestinationCreation(args.ipAddress, args.password, args.port, args.user)
2019-03-02 17:57:09 +05:00
elif args.function == "getConnectionStatus":
getConnectionStatus(args.ipAddress)
elif args.function == "startBackup":
backupUtilities.startBackup(args.tempStoragePath, args.backupName, args.backupPath, args.metaPath)
elif args.function == "BackupRoot":
2021-11-16 18:06:04 +05:00
backupUtilities.BackupRoot(args.tempStoragePath, args.backupName, args.backupPath, args.metaPath, args.externalApp)
2020-11-26 18:13:05 +05:00
elif args.function == 'CloudBackup':
extraArgs = {}
extraArgs['domain'] = args.backupDomain
extraArgs['tempStatusPath'] = args.tempStoragePath
extraArgs['data'] = int(args.data)
extraArgs['emails'] = int(args.emails)
extraArgs['databases'] = int(args.databases)
2020-12-31 22:54:07 +05:00
extraArgs['path'] = args.path
extraArgs['port'] = args.port
extraArgs['ip'] = args.ip
extraArgs['destinationDomain'] = args.destinationDomain
2020-11-26 18:13:05 +05:00
bu = backupUtilities(extraArgs)
bu.CloudBackups()
2021-08-12 14:37:30 +05:00
2020-12-06 14:10:09 +05:00
elif args.function == 'SubmitCloudBackupRestore':
extraArgs = {}
extraArgs['domain'] = args.backupDomain
extraArgs['tempStatusPath'] = args.tempStoragePath
extraArgs['backupFile'] = args.backupFile
2020-12-31 22:54:07 +05:00
extraArgs['sourceDomain'] = args.sourceDomain
2020-12-06 14:10:09 +05:00
bu = backupUtilities(extraArgs)
bu.SubmitCloudBackupRestore()
2020-12-11 23:07:15 +05:00
elif args.function == 'SubmitS3BackupRestore':
extraArgs = {}
extraArgs['domain'] = args.backupDomain
extraArgs['tempStatusPath'] = args.tempStoragePath
extraArgs['backupFile'] = args.backupFile
extraArgs['planName'] = args.planName
bu = backupUtilities(extraArgs)
bu.SubmitS3BackupRestore()
2017-12-09 22:30:10 +05:00
if __name__ == "__main__":
2021-08-12 14:37:30 +05:00
main()