Fix permissions via Filemanager and forward compatible Backups!

This commit is contained in:
usmannasir
2018-02-16 21:05:15 +05:00
parent 23b0e34437
commit 87214cb4eb
6 changed files with 107 additions and 73 deletions

View File

@@ -36,7 +36,6 @@ app.controller('backupWebsiteControl', function($scope,$http,$timeout) {
}; };
$scope.fetchDetails = function () { $scope.fetchDetails = function () {
getBackupStatus(); getBackupStatus();
populateCurrentRecords(); populateCurrentRecords();
$scope.destination = false; $scope.destination = false;
@@ -71,10 +70,9 @@ app.controller('backupWebsiteControl', function($scope,$http,$timeout) {
function ListInitialDatas(response) { function ListInitialDatas(response) {
if(response.data.backupStatus == 1){ if(response.data.backupStatus === 1){
if(response.data.abort === 1){ if(response.data.abort === 1){
$timeout.cancel(); $timeout.cancel();
$scope.backupLoadingBottom = true; $scope.backupLoadingBottom = true;
$scope.destination = false; $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){ if(response.data.fetchStatus == 1){
$scope.records = JSON.parse(response.data.data); $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) { function ListInitialDatas(response) {
if(response.data.metaStatus == 1){ if(response.data.metaStatus === 1){
getBackupStatus(); getBackupStatus();
} }

View File

@@ -24,6 +24,8 @@ from random import randint
from xml.etree.ElementTree import Element, SubElement from xml.etree.ElementTree import Element, SubElement
from xml.etree import ElementTree from xml.etree import ElementTree
from xml.dom import minidom from xml.dom import minidom
from dns.models import Domains,Records
def loadBackupHome(request): def loadBackupHome(request):
try: try:
@@ -48,7 +50,9 @@ def restoreSite(request):
admin = Administrator.objects.get(pk=request.session['userID']) admin = Administrator.objects.get(pk=request.session['userID'])
if admin.type == 1: if admin.type == 1:
path = "/home/backup"
path = os.path.join("/home","backup")
if not os.path.exists(path): if not os.path.exists(path):
return render(request, 'backup/restore.html') return render(request, 'backup/restore.html')
else: else:
@@ -184,10 +188,9 @@ def submitBackupCreation(request):
## /home/example.com/backup ## /home/example.com/backup
backupPath = os.path.join("/home",backupDomain,"backup/") backupPath = os.path.join("/home",backupDomain,"backup/")
domainUser = website.externalApp
domainUser = backupDomain.split('.') backupName = 'backup-' + domainUser + "-" + time.strftime("%I-%M-%S-%a-%b-%Y")
backupName = 'backup-' + domainUser[0] + "-" + time.strftime("%I-%M-%S-%a-%b-%Y")
## /home/example.com/backup/backup-example-06-50-03-Thu-Feb-2018 ## /home/example.com/backup/backup-example-06-50-03-Thu-Feb-2018
tempStoragePath = os.path.join(backupPath,backupName) tempStoragePath = os.path.join(backupPath,backupName)
@@ -232,6 +235,8 @@ def submitBackupCreation(request):
metaFileXML.append(childDomainsXML) metaFileXML.append(childDomainsXML)
## Databases
databasesXML = Element('Databases') databasesXML = Element('Databases')
for items in databases: for items in databases:
@@ -250,6 +255,34 @@ def submitBackupCreation(request):
metaFileXML.append(databasesXML) 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): def prettify(elem):
"""Return a pretty-printed XML string for the Element. """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 = "sudo python " + virtualHostUtilities.cyberPanel + "/plogical/backupUtilities.py"
execPath = execPath + " submitBackupCreation --tempStoragePath " + tempStoragePath + " --backupName " + backupName + " --backupPath " + backupPath + " --metaPath " + metaPath execPath = execPath + " submitBackupCreation --tempStoragePath " + tempStoragePath + " --backupName " + backupName + " --backupPath " + backupPath + " --metaPath " + metaPath
logging.CyberCPLogFileWriter.writeToFile(execPath)
subprocess.Popen(shlex.split(execPath)) subprocess.Popen(shlex.split(execPath))
@@ -298,12 +329,12 @@ def backupStatus(request):
data = json.loads(request.body) data = json.loads(request.body)
backupDomain = data['websiteToBeBacked'] backupDomain = data['websiteToBeBacked']
status = "/home/"+backupDomain+"/backup/status" status = os.path.join("/home",backupDomain,"backup/status")
## read file name ## read file name
try: try:
backupFileNamePath = "/home/" + backupDomain + "/backup/backupFileName" backupFileNamePath = os.path.join("/home",backupDomain,"backup/backupFileName")
command = "sudo cat " + backupFileNamePath command = "sudo cat " + backupFileNamePath
fileName = subprocess.check_output(shlex.split(command)) fileName = subprocess.check_output(shlex.split(command))
except: except:
@@ -318,11 +349,9 @@ def backupStatus(request):
if status.find("completed")> -1: if status.find("completed")> -1:
command = 'sudo rm -f ' + status command = 'sudo rm -f ' + status
cmd = shlex.split(command) subprocess.call(shlex.split(command))
res = subprocess.call(cmd)
backupOb = Backups.objects.get(fileName=fileName) backupOb = Backups.objects.get(fileName=fileName)
backupOb.status = 1 backupOb.status = 1
## adding backup data to database. ## adding backup data to database.
@@ -387,8 +416,6 @@ def cancelBackupCreation(request):
execPath = execPath + " cancelBackupCreation --backupCancellationDomain " + backupCancellationDomain + " --fileName " + fileName execPath = execPath + " cancelBackupCreation --backupCancellationDomain " + backupCancellationDomain + " --fileName " + fileName
subprocess.call(shlex.split(execPath)) subprocess.call(shlex.split(execPath))
try: try:
@@ -449,7 +476,6 @@ def deleteBackup(request):
return HttpResponse(final_json) return HttpResponse(final_json)
def submitRestore(request): def submitRestore(request):
try: try:
if request.method == 'POST': if request.method == 'POST':
@@ -468,9 +494,10 @@ def submitRestore(request):
execPath = execPath + " submitRestore --backupFile " + backupFile + " --dir " + dir execPath = execPath + " submitRestore --backupFile " + backupFile + " --dir " + dir
subprocess.Popen(shlex.split(execPath)) subprocess.Popen(shlex.split(execPath))
time.sleep(4)
final_dic = {'restoreStatus': 1, 'error_message': "None"} final_dic = {'restoreStatus': 1, 'error_message': "None"}
final_json = json.dumps(final_dic) final_json = json.dumps(final_dic)
return HttpResponse(final_json) return HttpResponse(final_json)
@@ -488,10 +515,10 @@ def restoreStatus(request):
data = json.loads(request.body) data = json.loads(request.body)
backupFile = data['backupFile'].strip(".tar.gz") backupFile = data['backupFile'].strip(".tar.gz")
path = "/home/backup/" + data['backupFile'] path = os.path.join("/home","backup",data['backupFile'])
if os.path.exists(path): if os.path.exists(path):
path = "/home/backup/" + backupFile path = os.path.join("/home","backup",backupFile)
else: else:
dir = data['dir'] dir = data['dir']
path = "/home/backup/transfer-" + str(dir) + "/" + backupFile path = "/home/backup/transfer-" + str(dir) + "/" + backupFile
@@ -500,7 +527,6 @@ def restoreStatus(request):
try: try:
execPath = "sudo cat " + path + "/status" execPath = "sudo cat " + path + "/status"
status = subprocess.check_output(shlex.split(execPath)) status = subprocess.check_output(shlex.split(execPath))
if status.find("Done") > -1: if status.find("Done") > -1:

View File

@@ -18,6 +18,7 @@ import sys
from xml.etree import ElementTree from xml.etree import ElementTree
class backupUtilities: class backupUtilities:
completeKeyPath = "/home/cyberpanel/.ssh" completeKeyPath = "/home/cyberpanel/.ssh"
@@ -44,7 +45,7 @@ class backupUtilities:
## Parsing XML Meta file! ## 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')) backupMetaData = ElementTree.parse(os.path.join(tempStoragePath,'meta.xml'))
@@ -52,6 +53,7 @@ class backupUtilities:
domainName = backupMetaData.find('masterDomain').text domainName = backupMetaData.find('masterDomain').text
## /home/example.com/backup/backup-example-06-50-03-Thu-Feb-2018 -- tempStoragePath ## /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")) make_archive(os.path.join(tempStoragePath,"public_html"), 'gztar', os.path.join("/home",domainName,"public_html"))
## Backing up databases ## Backing up databases
@@ -63,9 +65,10 @@ class backupUtilities:
status = open(os.path.join(backupPath,'status'), "w") status = open(os.path.join(backupPath,'status'), "w")
status.write("Backing up database: " + dbName) status.write("Backing up database: " + dbName)
status.close() 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) make_archive(os.path.join(backupPath,backupName), 'gztar', tempStoragePath)
rmtree(tempStoragePath) rmtree(tempStoragePath)
@@ -130,16 +133,22 @@ class backupUtilities:
pid.write(str(os.getpid())) pid.write(str(os.getpid()))
pid.close() pid.close()
status = open(os.path.join(completPath,'status'), "w") status = open(os.path.join(completPath,'status'), "w")
status.write("Extracting Main Archive") status.write("Extracting Main Archive")
status.close() 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 = tarfile.open(originalFile)
tar.extractall(completPath) tar.extractall(completPath)
tar.close() tar.close()
status = open(os.path.join(completPath,'status'), "w") status = open(os.path.join(completPath,'status'), "w")
status.write("Creating Account and databases") status.write("Creating Account and databases!")
status.close() status.close()
## creating website and its dabases ## creating website and its dabases
@@ -153,7 +162,7 @@ class backupUtilities:
pass pass
else: else:
status = open(os.path.join(completPath,'status'), "w") 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() status.close()
logging.CyberCPLogFileWriter.writeToFile(r.text) logging.CyberCPLogFileWriter.writeToFile(r.text)
return 0 return 0
@@ -164,10 +173,10 @@ class backupUtilities:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startRestore]") logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startRestore]")
return 0 return 0
########### creating sub/addon/parked domains ########### creating child/sub/addon/parked domains
status = open(os.path.join(completPath,'status'), "w") status = open(os.path.join(completPath,'status'), "w")
status.write("Creating Child Domains") status.write("Creating Child Domains!")
status.close() status.close()
## reading meta file to create subdomains ## reading meta file to create subdomains
@@ -205,7 +214,6 @@ class backupUtilities:
status.write("Error Message: " + data[ status.write("Error Message: " + data[
'error_message'] + ". Not able to create child domains, aborting. [5009]") 'error_message'] + ". Not able to create child domains, aborting. [5009]")
status.close() status.close()
logging.CyberCPLogFileWriter.writeToFile(r.text)
return 0 return 0
except BaseException, msg: except BaseException, msg:
@@ -226,7 +234,8 @@ class backupUtilities:
for database in databases: for database in databases:
dbName = database.find('dbName').text dbName = database.find('dbName').text
password = database.find('password').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") status = open(os.path.join(completPath, 'status'), "w")
@@ -498,6 +507,10 @@ class backupUtilities:
def submitBackupCreation(tempStoragePath,backupName,backupPath,metaPath): def submitBackupCreation(tempStoragePath,backupName,backupPath,metaPath):
try: 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): if not os.path.exists(backupPath):
os.mkdir(backupPath) os.mkdir(backupPath)
@@ -505,8 +518,9 @@ def submitBackupCreation(tempStoragePath,backupName,backupPath,metaPath):
if not os.path.exists(tempStoragePath): if not os.path.exists(tempStoragePath):
os.mkdir(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 = Process(target=backupUtilities.startBackup, args=(tempStoragePath, backupName, backupPath,))
p.start() p.start()

View File

@@ -68,14 +68,10 @@ class mysqlUtilities:
if res == 1: if res == 1:
logging.CyberCPLogFileWriter.writeToFile("Can not create Database User: " + dbuser) logging.CyberCPLogFileWriter.writeToFile("Can not create Database User: " + dbuser)
## reverting the db creation which was created earlier ## reverting the db creation which was created earlier
mysqlUtilities.deleteDatabase(dbname,dbuser) mysqlUtilities.deleteDatabase(dbname,dbuser)
return 0 return 0
else: else:
dropDB = "GRANT ALL PRIVILEGES ON " +dbname+ ".* TO '" +dbuser+ "'@'localhost'" dropDB = "GRANT ALL PRIVILEGES ON " +dbname+ ".* TO '" +dbuser+ "'@'localhost'"
command = 'sudo mysql -u root -p' + password + ' -e "' + dropDB + '"' command = 'sudo mysql -u root -p' + password + ' -e "' + dropDB + '"'
cmd = shlex.split(command) cmd = shlex.split(command)
@@ -86,7 +82,6 @@ class mysqlUtilities:
logging.CyberCPLogFileWriter.writeToFile("Can not grant privileges to user: " + dbuser) logging.CyberCPLogFileWriter.writeToFile("Can not grant privileges to user: " + dbuser)
return 0 return 0
return 1 return 1
except BaseException, msg: except BaseException, msg:
@@ -147,10 +142,12 @@ class mysqlUtilities:
if res == 1: if res == 1:
logging.CyberCPLogFileWriter.writeToFile("Database: "+databaseName + "could not be backed! [createDatabaseBackup]") logging.CyberCPLogFileWriter.writeToFile("Database: "+databaseName + "could not be backed! [createDatabaseBackup]")
return 0
return 1 return 1
except BaseException, msg: except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[createDatabase]") logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[createDatabase]")
return 0
@staticmethod @staticmethod
def restoreDatabaseBackup(databaseName, tempStoragePath,dbPassword): def restoreDatabaseBackup(databaseName, tempStoragePath,dbPassword):

View File

@@ -2,8 +2,14 @@ from xml.etree import ElementTree
import os import os
from random import randint from random import randint
try:
mydoc = ElementTree.parse('domain.xml') mydoc = ElementTree.parse('domain.xml')
len = 1
if len==1:
raise BaseException
print mydoc.find('masterDomain').text print mydoc.find('masterDomain').text
domains = mydoc.findall('ChildDomains/domain') domains = mydoc.findall('ChildDomains/domain')
@@ -13,7 +19,6 @@ for d in domains:
print d.find('phpSelection').text print d.find('phpSelection').text
print d.find('path').text print d.find('path').text
databases = mydoc.findall('Databases/database') databases = mydoc.findall('Databases/database')
for d in databases: for d in databases:
@@ -21,5 +26,6 @@ for d in databases:
print d.find('dbUser').text print d.find('dbUser').text
print d.find('password').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"

View File

@@ -673,9 +673,12 @@ def submitWebsiteDeletion(request):
delWebsite.delete() delWebsite.delete()
try:
delZone = Domains.objects.get(name=websiteName) delZone = Domains.objects.get(name=websiteName)
delZone.delete() delZone.delete()
except:
## There does not exist a zone for this domain.
pass
installUtilities.reStartLiteSpeed() installUtilities.reStartLiteSpeed()
@@ -1713,7 +1716,7 @@ def CreateWebsiteFromBackup(request):
## Parsing XML Meta file! ## Parsing XML Meta file!
backupMetaData = ElementTree.parse(path + '/meta.xml') backupMetaData = ElementTree.parse(os.path.join(path,'meta.xml'))
domain = backupMetaData.find('masterDomain').text domain = backupMetaData.find('masterDomain').text
phpSelection = backupMetaData.find('phpSelection').text phpSelection = backupMetaData.find('phpSelection').text
@@ -1753,7 +1756,11 @@ def CreateWebsiteFromBackup(request):
dbName = database.find('dbName').text dbName = database.find('dbName').text
dbUser = database.find('dbUser').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 = Databases(website=website, dbName=dbName, dbUser=dbUser)
newDB.save() newDB.save()