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 () {
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();
}

View File

@@ -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,7 +50,9 @@ 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:
@@ -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,10 +515,10 @@ 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
@@ -500,7 +527,6 @@ def restoreStatus(request):
try:
execPath = "sudo cat " + path + "/status"
status = subprocess.check_output(shlex.split(execPath))
if status.find("Done") > -1:

View File

@@ -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()

View File

@@ -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):

View File

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