diff --git a/backup/static/backup/backup.js b/backup/static/backup/backup.js index 82207b41d..9242635ab 100644 --- a/backup/static/backup/backup.js +++ b/backup/static/backup/backup.js @@ -36,7 +36,6 @@ app.controller('backupWebsiteControl', function($scope,$http,$timeout) { }; $scope.fetchDetails = function () { - getBackupStatus(); populateCurrentRecords(); $scope.destination = false; @@ -71,10 +70,9 @@ app.controller('backupWebsiteControl', function($scope,$http,$timeout) { function ListInitialDatas(response) { - if(response.data.backupStatus == 1){ + if(response.data.backupStatus === 1){ if(response.data.abort === 1){ - $timeout.cancel(); $scope.backupLoadingBottom = true; $scope.destination = false; @@ -108,11 +106,7 @@ app.controller('backupWebsiteControl', function($scope,$http,$timeout) { } } - function cantLoadInitialDatas(response) { - - - - } + function cantLoadInitialDatas(response) {} }; @@ -147,22 +141,12 @@ app.controller('backupWebsiteControl', function($scope,$http,$timeout) { if(response.data.fetchStatus == 1){ - $scope.records = JSON.parse(response.data.data); - - - } - else{ - - } - - } - function cantLoadInitialDatas(response) { - } + function cantLoadInitialDatas(response) {} }; @@ -195,7 +179,7 @@ app.controller('backupWebsiteControl', function($scope,$http,$timeout) { function ListInitialDatas(response) { - if(response.data.metaStatus == 1){ + if(response.data.metaStatus === 1){ getBackupStatus(); } diff --git a/backup/views.py b/backup/views.py index 1e9ff79b5..0c03ff8be 100644 --- a/backup/views.py +++ b/backup/views.py @@ -24,6 +24,8 @@ from random import randint from xml.etree.ElementTree import Element, SubElement from xml.etree import ElementTree from xml.dom import minidom +from dns.models import Domains,Records + def loadBackupHome(request): try: @@ -48,14 +50,16 @@ def restoreSite(request): admin = Administrator.objects.get(pk=request.session['userID']) if admin.type == 1: - path = "/home/backup" + + path = os.path.join("/home","backup") + if not os.path.exists(path): return render(request, 'backup/restore.html') else: all_files = [] ext = ".tar.gz" - command = 'sudo chown -R cyberpanel:cyberpanel '+path + command = 'sudo chown -R cyberpanel:cyberpanel '+ path cmd = shlex.split(command) @@ -184,10 +188,9 @@ def submitBackupCreation(request): ## /home/example.com/backup backupPath = os.path.join("/home",backupDomain,"backup/") + domainUser = website.externalApp - domainUser = backupDomain.split('.') - - backupName = 'backup-' + domainUser[0] + "-" + time.strftime("%I-%M-%S-%a-%b-%Y") + backupName = 'backup-' + domainUser + "-" + time.strftime("%I-%M-%S-%a-%b-%Y") ## /home/example.com/backup/backup-example-06-50-03-Thu-Feb-2018 tempStoragePath = os.path.join(backupPath,backupName) @@ -232,6 +235,8 @@ def submitBackupCreation(request): metaFileXML.append(childDomainsXML) + ## Databases + databasesXML = Element('Databases') for items in databases: @@ -250,6 +255,34 @@ def submitBackupCreation(request): metaFileXML.append(databasesXML) + ## DNS Records + + try: + dnsRecordsXML = Element("dnsrecords") + domain = Domains.objects.get(name=backupDomain) + dnsRecords = Records.objects.filter(domain_id=domain.id) + + 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 = items.prio + + dnsRecordsXML.append(dnsRecordXML) + + metaFileXML.append(dnsRecordsXML) + + except BaseException,msg: + logging.CyberCPLogFileWriter.writeToFile(str(msg)) + + + def prettify(elem): """Return a pretty-printed XML string for the Element. @@ -270,8 +303,6 @@ def submitBackupCreation(request): execPath = "sudo python " + virtualHostUtilities.cyberPanel + "/plogical/backupUtilities.py" execPath = execPath + " submitBackupCreation --tempStoragePath " + tempStoragePath + " --backupName " + backupName + " --backupPath " + backupPath + " --metaPath " + metaPath - logging.CyberCPLogFileWriter.writeToFile(execPath) - subprocess.Popen(shlex.split(execPath)) @@ -298,12 +329,12 @@ def backupStatus(request): data = json.loads(request.body) backupDomain = data['websiteToBeBacked'] - status = "/home/"+backupDomain+"/backup/status" + status = os.path.join("/home",backupDomain,"backup/status") ## read file name try: - backupFileNamePath = "/home/" + backupDomain + "/backup/backupFileName" + backupFileNamePath = os.path.join("/home",backupDomain,"backup/backupFileName") command = "sudo cat " + backupFileNamePath fileName = subprocess.check_output(shlex.split(command)) except: @@ -318,11 +349,9 @@ def backupStatus(request): if status.find("completed")> -1: command = 'sudo rm -f ' + status - cmd = shlex.split(command) - res = subprocess.call(cmd) + subprocess.call(shlex.split(command)) backupOb = Backups.objects.get(fileName=fileName) - backupOb.status = 1 ## adding backup data to database. @@ -387,8 +416,6 @@ def cancelBackupCreation(request): execPath = execPath + " cancelBackupCreation --backupCancellationDomain " + backupCancellationDomain + " --fileName " + fileName - - subprocess.call(shlex.split(execPath)) try: @@ -449,7 +476,6 @@ def deleteBackup(request): return HttpResponse(final_json) - def submitRestore(request): try: if request.method == 'POST': @@ -468,9 +494,10 @@ def submitRestore(request): execPath = execPath + " submitRestore --backupFile " + backupFile + " --dir " + dir - subprocess.Popen(shlex.split(execPath)) + time.sleep(4) + final_dic = {'restoreStatus': 1, 'error_message': "None"} final_json = json.dumps(final_dic) return HttpResponse(final_json) @@ -488,18 +515,17 @@ def restoreStatus(request): data = json.loads(request.body) backupFile = data['backupFile'].strip(".tar.gz") - path = "/home/backup/" + data['backupFile'] + path = os.path.join("/home","backup",data['backupFile']) if os.path.exists(path): - path = "/home/backup/" + backupFile + path = os.path.join("/home","backup",backupFile) else: dir = data['dir'] path = "/home/backup/transfer-" + str(dir) + "/" + backupFile if os.path.exists(path): try: - execPath = "sudo cat " + path+"/status" - + execPath = "sudo cat " + path + "/status" status = subprocess.check_output(shlex.split(execPath)) diff --git a/plogical/backupUtilities.py b/plogical/backupUtilities.py index 5f3ec5783..d36daa5e9 100644 --- a/plogical/backupUtilities.py +++ b/plogical/backupUtilities.py @@ -18,6 +18,7 @@ import sys from xml.etree import ElementTree + class backupUtilities: completeKeyPath = "/home/cyberpanel/.ssh" @@ -44,7 +45,7 @@ class backupUtilities: ## Parsing XML Meta file! - ## /home/example.com/backup/backup-example-06-50-03-Thu-Feb-2018/meta.xml -- tempStoragePath + ## /home/example.com/backup/backup-example-06-50-03-Thu-Feb-2018 -- tempStoragePath backupMetaData = ElementTree.parse(os.path.join(tempStoragePath,'meta.xml')) @@ -52,6 +53,7 @@ class backupUtilities: domainName = backupMetaData.find('masterDomain').text ## /home/example.com/backup/backup-example-06-50-03-Thu-Feb-2018 -- tempStoragePath + ## shutil.make_archive make_archive(os.path.join(tempStoragePath,"public_html"), 'gztar', os.path.join("/home",domainName,"public_html")) ## Backing up databases @@ -63,9 +65,10 @@ class backupUtilities: status = open(os.path.join(backupPath,'status'), "w") status.write("Backing up database: " + dbName) status.close() - mysqlUtilities.mysqlUtilities.createDatabaseBackup(dbName, tempStoragePath) - + if mysqlUtilities.mysqlUtilities.createDatabaseBackup(dbName, tempStoragePath) == 0: + raise BaseException + ## shutil.make_archive, ## shutil. make_archive(os.path.join(backupPath,backupName), 'gztar', tempStoragePath) rmtree(tempStoragePath) @@ -130,16 +133,22 @@ class backupUtilities: pid.write(str(os.getpid())) pid.close() + status = open(os.path.join(completPath,'status'), "w") status.write("Extracting Main Archive") status.close() + ## Converting /home/backup/backup-example-06-50-03-Thu-Feb-2018.tar.gz -> /home/backup/backup-example-06-50-03-Thu-Feb-2018 + + tar = tarfile.open(originalFile) tar.extractall(completPath) tar.close() + + status = open(os.path.join(completPath,'status'), "w") - status.write("Creating Account and databases") + status.write("Creating Account and databases!") status.close() ## creating website and its dabases @@ -153,7 +162,7 @@ class backupUtilities: pass else: status = open(os.path.join(completPath,'status'), "w") - status.write("Error Message: " + data['error_message'] +". Not able to create Account and databases, aborting. [5009]") + status.write("Error Message: " + data['error_message'] +". Not able to create Account and Databases, aborting. [5009]") status.close() logging.CyberCPLogFileWriter.writeToFile(r.text) return 0 @@ -164,10 +173,10 @@ class backupUtilities: logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startRestore]") return 0 - ########### creating sub/addon/parked domains + ########### creating child/sub/addon/parked domains status = open(os.path.join(completPath,'status'), "w") - status.write("Creating Child Domains") + status.write("Creating Child Domains!") status.close() ## reading meta file to create subdomains @@ -205,7 +214,6 @@ class backupUtilities: status.write("Error Message: " + data[ 'error_message'] + ". Not able to create child domains, aborting. [5009]") status.close() - logging.CyberCPLogFileWriter.writeToFile(r.text) return 0 except BaseException, msg: @@ -226,7 +234,8 @@ class backupUtilities: for database in databases: dbName = database.find('dbName').text password = database.find('password').text - mysqlUtilities.mysqlUtilities.restoreDatabaseBackup(dbName, completPath, password) + if mysqlUtilities.mysqlUtilities.restoreDatabaseBackup(dbName, completPath, password) == 0: + raise BaseException status = open(os.path.join(completPath, 'status'), "w") @@ -498,6 +507,10 @@ class backupUtilities: def submitBackupCreation(tempStoragePath,backupName,backupPath,metaPath): try: + ## /home/example.com/backup/backup-example-06-50-03-Thu-Feb-2018 -- tempStoragePath + ## backup-example-06-50-03-Thu-Feb-2018 -- backup name + ## /home/example.com/backup - backupPath + ## /home/cyberpanel/1047.xml - metaPath if not os.path.exists(backupPath): os.mkdir(backupPath) @@ -505,8 +518,9 @@ def submitBackupCreation(tempStoragePath,backupName,backupPath,metaPath): if not os.path.exists(tempStoragePath): os.mkdir(tempStoragePath) + ## Move meta file inside the temporary storage created to store backup data. - move(metaPath,tempStoragePath+"/meta.xml") + move(metaPath,os.path.join(tempStoragePath,"meta.xml")) p = Process(target=backupUtilities.startBackup, args=(tempStoragePath, backupName, backupPath,)) p.start() diff --git a/plogical/mysqlUtilities.py b/plogical/mysqlUtilities.py index 34e4c4944..031a41a23 100644 --- a/plogical/mysqlUtilities.py +++ b/plogical/mysqlUtilities.py @@ -68,14 +68,10 @@ class mysqlUtilities: if res == 1: logging.CyberCPLogFileWriter.writeToFile("Can not create Database User: " + dbuser) - ## reverting the db creation which was created earlier - mysqlUtilities.deleteDatabase(dbname,dbuser) - return 0 else: - dropDB = "GRANT ALL PRIVILEGES ON " +dbname+ ".* TO '" +dbuser+ "'@'localhost'" command = 'sudo mysql -u root -p' + password + ' -e "' + dropDB + '"' cmd = shlex.split(command) @@ -86,7 +82,6 @@ class mysqlUtilities: logging.CyberCPLogFileWriter.writeToFile("Can not grant privileges to user: " + dbuser) return 0 - return 1 except BaseException, msg: @@ -147,10 +142,12 @@ class mysqlUtilities: if res == 1: logging.CyberCPLogFileWriter.writeToFile("Database: "+databaseName + "could not be backed! [createDatabaseBackup]") + return 0 return 1 except BaseException, msg: logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[createDatabase]") + return 0 @staticmethod def restoreDatabaseBackup(databaseName, tempStoragePath,dbPassword): diff --git a/plogical/test.py b/plogical/test.py index 0403e80be..73eb8ac6d 100644 --- a/plogical/test.py +++ b/plogical/test.py @@ -2,24 +2,30 @@ from xml.etree import ElementTree import os from random import randint -mydoc = ElementTree.parse('domain.xml') +try: + mydoc = ElementTree.parse('domain.xml') -print mydoc.find('masterDomain').text + len = 1 -domains = mydoc.findall('ChildDomains/domain') + if len==1: + raise BaseException -for d in domains: - print d.find('domain').text - print d.find('phpSelection').text - print d.find('path').text + print mydoc.find('masterDomain').text + domains = mydoc.findall('ChildDomains/domain') -databases = mydoc.findall('Databases/database') + for d in domains: + print d.find('domain').text + print d.find('phpSelection').text + print d.find('path').text -for d in databases: - print d.find('dbName').text - print d.find('dbUser').text - print d.find('password').text + databases = mydoc.findall('Databases/database') + for d in databases: + print d.find('dbName').text + print d.find('dbUser').text + print d.find('password').text -print os.path.join("/home","cyberpanel",str(randint(1000, 9999))+".xml/","test") + print os.path.join("/home", "cyberpanel", str(randint(1000, 9999)) + ".xml/", "test") +except BaseException,msg: + print "hello" diff --git a/websiteFunctions/views.py b/websiteFunctions/views.py index 1270dd5f0..d5fba697d 100644 --- a/websiteFunctions/views.py +++ b/websiteFunctions/views.py @@ -673,9 +673,12 @@ def submitWebsiteDeletion(request): delWebsite.delete() - - delZone = Domains.objects.get(name=websiteName) - delZone.delete() + try: + delZone = Domains.objects.get(name=websiteName) + delZone.delete() + except: + ## There does not exist a zone for this domain. + pass installUtilities.reStartLiteSpeed() @@ -1713,7 +1716,7 @@ def CreateWebsiteFromBackup(request): ## Parsing XML Meta file! - backupMetaData = ElementTree.parse(path + '/meta.xml') + backupMetaData = ElementTree.parse(os.path.join(path,'meta.xml')) domain = backupMetaData.find('masterDomain').text phpSelection = backupMetaData.find('phpSelection').text @@ -1753,7 +1756,11 @@ def CreateWebsiteFromBackup(request): dbName = database.find('dbName').text dbUser = database.find('dbUser').text - mysqlUtilities.createDatabase(dbName, dbUser, "cyberpanel") + if mysqlUtilities.createDatabase(dbName, dbUser, "cyberpanel") == 0: + data_ret = {'createWebSiteStatus': 0, 'error_message': "Failed to create Databases!", "existsStatus": 0} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + newDB = Databases(website=website, dbName=dbName, dbUser=dbUser) newDB.save()