2017-10-24 19:16:36 +05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
|
|
from django.shortcuts import render,redirect
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
|
# Create your views here.
|
|
|
|
|
from .models import DBUsers
|
|
|
|
|
from loginSystem.models import Administrator
|
|
|
|
|
import json
|
2017-12-09 22:30:10 +05:00
|
|
|
from websiteFunctions.models import Websites,Backups,dest,backupSchedules
|
2017-10-24 19:16:36 +05:00
|
|
|
import plogical.CyberCPLogFileWriter as logging
|
|
|
|
|
from loginSystem.views import loadLoginPage
|
|
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
import plogical.backupUtilities as backupUtil
|
|
|
|
|
from shutil import rmtree
|
|
|
|
|
import shlex
|
|
|
|
|
import subprocess
|
|
|
|
|
import signal
|
|
|
|
|
import requests
|
2017-11-02 02:09:47 +05:00
|
|
|
from baseTemplate.models import version
|
2017-11-05 03:02:51 +05:00
|
|
|
from plogical.virtualHostUtilities import virtualHostUtilities
|
2017-12-09 22:30:10 +05:00
|
|
|
from random import randint
|
2018-02-16 00:57:46 +05:00
|
|
|
from xml.etree.ElementTree import Element, SubElement
|
|
|
|
|
from xml.etree import ElementTree
|
|
|
|
|
from xml.dom import minidom
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
def loadBackupHome(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
|
|
|
|
|
admin = Administrator.objects.get(pk=val)
|
|
|
|
|
|
|
|
|
|
viewStatus = 1
|
|
|
|
|
|
|
|
|
|
if admin.type == 3:
|
|
|
|
|
viewStatus = 0
|
|
|
|
|
|
|
|
|
|
return render(request,'backup/index.html',{"viewStatus":viewStatus})
|
|
|
|
|
except KeyError:
|
|
|
|
|
return redirect(loadLoginPage)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def restoreSite(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
try:
|
|
|
|
|
admin = Administrator.objects.get(pk=request.session['userID'])
|
|
|
|
|
|
|
|
|
|
if admin.type == 1:
|
|
|
|
|
path = "/home/backup"
|
|
|
|
|
if not os.path.exists(path):
|
|
|
|
|
return render(request, 'backup/restore.html')
|
|
|
|
|
else:
|
|
|
|
|
all_files = []
|
|
|
|
|
ext = ".tar.gz"
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
command = 'sudo chown -R cyberpanel:cyberpanel '+path
|
|
|
|
|
|
|
|
|
|
cmd = shlex.split(command)
|
|
|
|
|
|
|
|
|
|
res = subprocess.call(cmd)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
files = os.listdir(path)
|
|
|
|
|
for filename in files:
|
|
|
|
|
if filename.endswith(ext):
|
|
|
|
|
all_files.append(filename)
|
|
|
|
|
|
|
|
|
|
return render(request, 'backup/restore.html',{'backups':all_files})
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
return HttpResponse("You should be admin to perform restores.")
|
|
|
|
|
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg))
|
|
|
|
|
return HttpResponse(str(msg))
|
|
|
|
|
except KeyError:
|
|
|
|
|
return redirect(loadLoginPage)
|
|
|
|
|
|
|
|
|
|
def backupSite(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
try:
|
|
|
|
|
admin = Administrator.objects.get(pk=request.session['userID'])
|
|
|
|
|
|
|
|
|
|
if admin.type == 1:
|
|
|
|
|
websites = Websites.objects.all()
|
|
|
|
|
websitesName = []
|
|
|
|
|
|
|
|
|
|
for items in websites:
|
|
|
|
|
websitesName.append(items.domain)
|
|
|
|
|
else:
|
|
|
|
|
if admin.type == 2:
|
|
|
|
|
websites = admin.websites_set.all()
|
|
|
|
|
admins = Administrator.objects.filter(owner=admin.pk)
|
|
|
|
|
websitesName = []
|
|
|
|
|
|
|
|
|
|
for items in websites:
|
|
|
|
|
websitesName.append(items.domain)
|
|
|
|
|
|
|
|
|
|
for items in admins:
|
|
|
|
|
webs = items.websites_set.all()
|
|
|
|
|
|
|
|
|
|
for web in webs:
|
|
|
|
|
websitesName.append(web.domain)
|
|
|
|
|
else:
|
|
|
|
|
websitesName = []
|
|
|
|
|
websites = Websites.objects.filter(admin=admin)
|
|
|
|
|
for items in websites:
|
|
|
|
|
websitesName.append(items.domain)
|
|
|
|
|
|
|
|
|
|
return render(request, 'backup/backup.html', {'websiteList':websitesName})
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg))
|
|
|
|
|
return HttpResponse(str(msg))
|
|
|
|
|
except KeyError:
|
|
|
|
|
return redirect(loadLoginPage)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getCurrentBackups(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
try:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
backupDomain = data['websiteToBeBacked']
|
|
|
|
|
|
|
|
|
|
website = Websites.objects.get(domain=backupDomain)
|
|
|
|
|
|
|
|
|
|
backups = website.backups_set.all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
json_data = "["
|
|
|
|
|
checker = 0
|
|
|
|
|
|
|
|
|
|
for items in backups:
|
|
|
|
|
if items.status == 0:
|
|
|
|
|
status="Pending"
|
|
|
|
|
else:
|
|
|
|
|
status="Completed"
|
|
|
|
|
dic = {'id': items.id,
|
|
|
|
|
'file': items.fileName,
|
|
|
|
|
'date': items.date,
|
|
|
|
|
'size': items.size,
|
|
|
|
|
'status': status
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if checker == 0:
|
|
|
|
|
json_data = json_data + json.dumps(dic)
|
|
|
|
|
checker = 1
|
|
|
|
|
else:
|
|
|
|
|
json_data = json_data + ',' + json.dumps(dic)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
json_data = json_data + ']'
|
|
|
|
|
final_json = json.dumps({'fetchStatus': 1, 'error_message': "None","data":json_data})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
final_dic = {'fetchStatus': 0, 'error_message': str(msg)}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except KeyError:
|
|
|
|
|
final_dic = {'fetchStatus': 0, 'error_message': "Not Logged In, please refresh the page or login again."}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def submitBackupCreation(request):
|
|
|
|
|
try:
|
2017-10-26 23:50:59 +05:00
|
|
|
if request.method == 'POST':
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
data = json.loads(request.body)
|
|
|
|
|
backupDomain = data['websiteToBeBacked']
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
website = Websites.objects.get(domain=backupDomain)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
## defining paths
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
|
|
|
|
|
## /home/example.com/backup
|
|
|
|
|
backupPath = os.path.join("/home",backupDomain,"backup/")
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
domainUser = backupDomain.split('.')
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
backupName = 'backup-' + domainUser[0] + "-" + time.strftime("%I-%M-%S-%a-%b-%Y")
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
## /home/example.com/backup/backup-example-06-50-03-Thu-Feb-2018
|
|
|
|
|
tempStoragePath = os.path.join(backupPath,backupName)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
## Generating meta
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
## xml generation
|
|
|
|
|
|
|
|
|
|
metaFileXML = Element('metaFile')
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-02-16 00:57:46 +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
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-29 22:16:06 +05:00
|
|
|
|
|
|
|
|
childDomains = website.childdomains_set.all()
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
databases = website.databases_set.all()
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
## child domains xml
|
|
|
|
|
|
|
|
|
|
childDomainsXML = Element('ChildDomains')
|
2017-10-29 22:16:06 +05:00
|
|
|
|
|
|
|
|
for items in childDomains:
|
|
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
databasesXML = Element('Databases')
|
2017-10-29 22:16:06 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
for items in databases:
|
|
|
|
|
dbuser = DBUsers.objects.get(user=items.dbUser)
|
2017-12-09 22:30:10 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
databaseXML = Element('database')
|
|
|
|
|
|
|
|
|
|
child = SubElement(databaseXML, 'dbName')
|
|
|
|
|
child.text = items.dbName
|
|
|
|
|
child = SubElement(databaseXML, 'dbUser')
|
|
|
|
|
child.text = items.dbUser
|
|
|
|
|
child = SubElement(databaseXML, 'password')
|
|
|
|
|
child.text = dbuser.password
|
|
|
|
|
|
|
|
|
|
databasesXML.append(databaseXML)
|
|
|
|
|
|
|
|
|
|
metaFileXML.append(databasesXML)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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/cyberpanel/1047.xml
|
|
|
|
|
metaPath = os.path.join("/home", "cyberpanel", str(randint(1000, 9999)) + ".xml")
|
|
|
|
|
|
|
|
|
|
xmlpretty = prettify(metaFileXML).encode('ascii', 'ignore')
|
|
|
|
|
metaFile = open(metaPath,'w')
|
|
|
|
|
metaFile.write(xmlpretty)
|
2017-10-26 23:50:59 +05:00
|
|
|
metaFile.close()
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
## meta generated
|
|
|
|
|
|
|
|
|
|
execPath = "sudo python " + virtualHostUtilities.cyberPanel + "/plogical/backupUtilities.py"
|
|
|
|
|
execPath = execPath + " submitBackupCreation --tempStoragePath " + tempStoragePath + " --backupName " + backupName + " --backupPath " + backupPath + " --metaPath " + metaPath
|
2018-02-16 00:57:46 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(execPath)
|
2017-12-09 22:30:10 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
subprocess.Popen(shlex.split(execPath))
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
newBackup = Backups(website=website, fileName=backupName, date=time.strftime("%I-%M-%S-%a-%b-%Y"),
|
|
|
|
|
size=0, status=0)
|
|
|
|
|
newBackup.save()
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-01-02 11:49:03 +05:00
|
|
|
time.sleep(2)
|
|
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
final_json = json.dumps({'metaStatus': 1, 'error_message': "None", 'tempStorage': tempStoragePath})
|
2017-10-24 19:16:36 +05:00
|
|
|
return HttpResponse(final_json)
|
2017-10-26 23:50:59 +05:00
|
|
|
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
final_dic = {'metaStatus': 0, 'error_message': str(msg)}
|
2017-10-24 19:16:36 +05:00
|
|
|
final_json = json.dumps(final_dic)
|
2017-10-26 23:50:59 +05:00
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
def backupStatus(request):
|
|
|
|
|
try:
|
|
|
|
|
try:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
backupDomain = data['websiteToBeBacked']
|
|
|
|
|
|
|
|
|
|
status = "/home/"+backupDomain+"/backup/status"
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
## read file name
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
backupFileNamePath = "/home/" + backupDomain + "/backup/backupFileName"
|
|
|
|
|
command = "sudo cat " + backupFileNamePath
|
|
|
|
|
fileName = subprocess.check_output(shlex.split(command))
|
|
|
|
|
except:
|
|
|
|
|
fileName = "Fetching.."
|
|
|
|
|
|
|
|
|
|
## file name read ends
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
if os.path.exists(status):
|
2017-12-09 22:30:10 +05:00
|
|
|
command = "sudo cat " + status
|
|
|
|
|
status = subprocess.check_output(shlex.split(command))
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
if status.find("completed")> -1:
|
|
|
|
|
|
|
|
|
|
command = 'sudo rm -f ' + status
|
|
|
|
|
cmd = shlex.split(command)
|
|
|
|
|
res = subprocess.call(cmd)
|
|
|
|
|
|
|
|
|
|
backupOb = Backups.objects.get(fileName=fileName)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
backupOb.status = 1
|
|
|
|
|
|
|
|
|
|
## adding backup data to database.
|
|
|
|
|
try:
|
2017-12-09 22:30:10 +05:00
|
|
|
backupOb.size = str(int(float(os.path.getsize("/home/"+backupDomain+"/backup/"+fileName+".tar.gz"))/(1024.0 * 1024.0)))+"MB"
|
2017-10-24 19:16:36 +05:00
|
|
|
backupOb.save()
|
|
|
|
|
except:
|
2017-12-09 22:30:10 +05:00
|
|
|
backupOb.size = str(int(os.path.getsize("/home/"+backupDomain+"/backup/"+fileName+".tar.gz")))
|
2017-10-24 19:16:36 +05:00
|
|
|
backupOb.save()
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
final_json = json.dumps({'backupStatus': 1, 'error_message': "None", "status": status,"abort": 1,'fileName': fileName,})
|
2017-10-24 19:16:36 +05:00
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
elif status.find("[5009]")> -1:
|
2017-10-24 19:16:36 +05:00
|
|
|
## removing status file, so that backup can re-run
|
|
|
|
|
try:
|
2017-12-09 22:30:10 +05:00
|
|
|
command = 'sudo rm -f ' + status
|
|
|
|
|
cmd = shlex.split(command)
|
|
|
|
|
res = subprocess.call(cmd)
|
|
|
|
|
|
|
|
|
|
backupOb = Backups.objects.get(fileName=fileName)
|
2017-10-24 19:16:36 +05:00
|
|
|
backupOb.delete()
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [backupStatus]")
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
final_json = json.dumps({'backupStatus': 1,'fileName': fileName, 'error_message': "None", "status": status, "abort": 1})
|
2017-10-24 19:16:36 +05:00
|
|
|
return HttpResponse(final_json)
|
2017-12-09 22:30:10 +05:00
|
|
|
else:
|
|
|
|
|
final_json = json.dumps(
|
|
|
|
|
{'backupStatus': 1, 'error_message': "None", 'fileName': fileName, "status": status,"abort": 0})
|
2017-10-24 19:16:36 +05:00
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
else:
|
2017-12-09 22:30:10 +05:00
|
|
|
final_json = json.dumps({'backupStatus': 0, 'error_message': "None", "status": 0,"abort": 0})
|
2017-10-24 19:16:36 +05:00
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
final_dic = {'backupStatus': 0, 'error_message': str(msg)}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [backupStatus]")
|
|
|
|
|
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except KeyError:
|
|
|
|
|
final_dic = {'backupStatus': 0, 'error_message': "Not Logged In, please refresh the page or login again."}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [backupStatus]")
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
def cancelBackupCreation(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
try:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
backupCancellationDomain = data['backupCancellationDomain']
|
|
|
|
|
fileName = data['fileName']
|
|
|
|
|
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
execPath = "sudo python " + virtualHostUtilities.cyberPanel + "/plogical/backupUtilities.py"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
execPath = execPath + " cancelBackupCreation --backupCancellationDomain " + backupCancellationDomain + " --fileName " + fileName
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
subprocess.call(shlex.split(execPath))
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
backupOb = Backups.objects.get(fileName=fileName)
|
|
|
|
|
backupOb.delete()
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [cancelBackupCreation]")
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'abortStatus': 1, 'error_message': "None", "status": 0})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
final_dic = {'abortStatus': 0, 'error_message': str(msg)}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except KeyError:
|
|
|
|
|
final_dic = {'abortStatus': 0, 'error_message': "Not Logged In, please refresh the page or login again."}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def deleteBackup(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
try:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
backupID = data['backupID']
|
|
|
|
|
|
|
|
|
|
backup = Backups.objects.get(id=backupID)
|
|
|
|
|
domainName = backup.website.domain
|
|
|
|
|
|
|
|
|
|
path = "/home/"+domainName+"/backup/"+backup.fileName+".tar.gz"
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
command = 'sudo rm -f ' + path
|
|
|
|
|
cmd = shlex.split(command)
|
|
|
|
|
res = subprocess.call(cmd)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
backup.delete()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'deleteStatus': 1, 'error_message': "None", "status": 0})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
final_dic = {'deleteStatus': 0, 'error_message': str(msg)}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except KeyError:
|
|
|
|
|
final_dic = {'deleteStatus': 0, 'error_message': "Not Logged In, please refresh the page or login again."}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def submitRestore(request):
|
|
|
|
|
try:
|
2017-10-26 23:50:59 +05:00
|
|
|
if request.method == 'POST':
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
data = json.loads(request.body)
|
|
|
|
|
backupFile = data['backupFile']
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
originalFile = "/home/backup/" + backupFile
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
if not os.path.exists(originalFile):
|
|
|
|
|
dir = data['dir']
|
|
|
|
|
else:
|
2017-12-09 22:30:10 +05:00
|
|
|
dir = "CyberPanelRestore"
|
|
|
|
|
|
|
|
|
|
execPath = "sudo python " + virtualHostUtilities.cyberPanel + "/plogical/backupUtilities.py"
|
|
|
|
|
|
|
|
|
|
execPath = execPath + " submitRestore --backupFile " + backupFile + " --dir " + dir
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
subprocess.Popen(shlex.split(execPath))
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
final_dic = {'restoreStatus': 1, 'error_message': "None"}
|
2017-10-24 19:16:36 +05:00
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
2017-10-26 23:50:59 +05:00
|
|
|
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
final_dic = {'restoreStatus': 0, 'error_message': str(msg)}
|
2017-10-24 19:16:36 +05:00
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def restoreStatus(request):
|
|
|
|
|
try:
|
2017-10-26 23:50:59 +05:00
|
|
|
if request.method == 'POST':
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
data = json.loads(request.body)
|
|
|
|
|
backupFile = data['backupFile'].strip(".tar.gz")
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-29 22:16:06 +05:00
|
|
|
path = "/home/backup/" + data['backupFile']
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
if os.path.exists(path):
|
2017-10-29 22:16:06 +05:00
|
|
|
path = "/home/backup/" + backupFile
|
2017-10-26 23:50:59 +05:00
|
|
|
else:
|
|
|
|
|
dir = data['dir']
|
|
|
|
|
path = "/home/backup/transfer-" + str(dir) + "/" + backupFile
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
if os.path.exists(path):
|
|
|
|
|
try:
|
2017-12-09 22:30:10 +05:00
|
|
|
execPath = "sudo cat " + path+"/status"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
status = subprocess.check_output(shlex.split(execPath))
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
if status.find("Done") > -1:
|
|
|
|
|
|
|
|
|
|
command = "sudo rm -rf " + path
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps(
|
|
|
|
|
{'restoreStatus': 1, 'error_message': "None", "status": status, 'abort': 1,'running': 'Completed'})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
elif status.find("[5009]") > -1:
|
|
|
|
|
## removing temporarily generated files while restoring
|
|
|
|
|
command = "sudo rm -rf " + path
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
final_json = json.dumps({'restoreStatus': 1, 'error_message': "None",
|
|
|
|
|
"status": status, 'abort': 1,'alreadyRunning': 0,'running': 'Error'})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
else:
|
|
|
|
|
final_json = json.dumps({'restoreStatus': 1, 'error_message': "None", "status": status, 'abort': 0,'running': 'Running..'})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg))
|
|
|
|
|
status = "Just Started"
|
|
|
|
|
final_json = json.dumps({'restoreStatus': 1, 'error_message': "None", "status": status, 'abort': 0,'running': 'Running..'})
|
|
|
|
|
return HttpResponse(final_json)
|
2017-10-26 23:50:59 +05:00
|
|
|
else:
|
2017-12-09 22:30:10 +05:00
|
|
|
final_json = json.dumps({'restoreStatus': 1, 'error_message': "None", "status": "OK To Run",'running': 'Halted','abort': 1})
|
2017-10-26 23:50:59 +05:00
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
except BaseException, msg:
|
|
|
|
|
final_dic = {'restoreStatus': 0, 'error_message': str(msg)}
|
2017-10-24 19:16:36 +05:00
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def backupDestinations(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
|
|
|
|
|
admin = Administrator.objects.get(pk=val)
|
|
|
|
|
|
|
|
|
|
if admin.type==1:
|
|
|
|
|
return render(request, 'backup/backupDestinations.html', {})
|
|
|
|
|
else:
|
|
|
|
|
return HttpResponse("You should be admin to add backup destinations.")
|
|
|
|
|
except KeyError:
|
|
|
|
|
return redirect(loadLoginPage)
|
|
|
|
|
|
|
|
|
|
def submitDestinationCreation(request):
|
|
|
|
|
try:
|
|
|
|
|
try:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
destinations = backupUtil.backupUtilities.destinationsPath
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
ipAddress = data['IPAddress']
|
|
|
|
|
password = data['password']
|
2017-11-05 21:07:12 +05:00
|
|
|
port = "22"
|
|
|
|
|
try:
|
|
|
|
|
port = data['backupSSHPort']
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
if dest.objects.all().count() == 2:
|
|
|
|
|
final_dic = {'destStatus': 0, 'error_message': "Currently only one remote destination is allowed."}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
try:
|
2017-11-05 03:02:51 +05:00
|
|
|
d = dest.objects.get(destLoc=ipAddress)
|
2017-10-24 19:16:36 +05:00
|
|
|
final_dic = {'destStatus': 0, 'error_message': "This destination already exists."}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except:
|
2017-11-05 21:07:12 +05:00
|
|
|
setupKeys = backupUtil.backupUtilities.setupSSHKeys(ipAddress,password,port)
|
|
|
|
|
if setupKeys[0] == 1:
|
|
|
|
|
backupUtil.backupUtilities.initiateBackupDirCreation(ipAddress,port)
|
|
|
|
|
try:
|
|
|
|
|
writeToFile = open(destinations, "w")
|
|
|
|
|
writeToFile.writelines(ipAddress + "\n")
|
|
|
|
|
writeToFile.writelines(data['backupSSHPort'] + "\n")
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
newDest = dest(destLoc=ipAddress)
|
|
|
|
|
newDest.save()
|
|
|
|
|
except:
|
|
|
|
|
writeToFile = open(destinations, "w")
|
|
|
|
|
writeToFile.writelines(ipAddress + "\n")
|
|
|
|
|
writeToFile.writelines("22"+"\n")
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
newDest = dest(destLoc=ipAddress)
|
|
|
|
|
newDest.save()
|
2017-11-05 03:02:51 +05:00
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
final_dic = {'destStatus': 1, 'error_message': "None"}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
else:
|
2017-11-05 21:07:12 +05:00
|
|
|
final_dic = {'destStatus': 0, 'error_message': setupKeys[1]}
|
2017-10-24 19:16:36 +05:00
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
final_dic = {'destStatus': 0, 'error_message': str(msg)}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except KeyError:
|
|
|
|
|
final_dic = {'destStatus': 0, 'error_message': str(msg)}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
def getCurrentBackupDestinations(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
try:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
|
|
|
|
records = dest.objects.all()
|
|
|
|
|
|
|
|
|
|
json_data = "["
|
|
|
|
|
checker = 0
|
|
|
|
|
|
|
|
|
|
for items in records:
|
|
|
|
|
if items.destLoc == "Home":
|
|
|
|
|
continue
|
|
|
|
|
dic = {'id': items.id,
|
|
|
|
|
'ip': items.destLoc,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if checker == 0:
|
|
|
|
|
json_data = json_data + json.dumps(dic)
|
|
|
|
|
checker = 1
|
|
|
|
|
else:
|
|
|
|
|
json_data = json_data + ',' + json.dumps(dic)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
json_data = json_data + ']'
|
|
|
|
|
final_json = json.dumps({'fetchStatus': 1, 'error_message': "None","data":json_data})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
final_dic = {'fetchStatus': 0, 'error_message': str(msg)}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except KeyError:
|
|
|
|
|
final_dic = {'fetchStatus': 0, 'error_message': "Not Logged In, please refresh the page or login again."}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getConnectionStatus(request):
|
|
|
|
|
try:
|
|
|
|
|
try:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
ipAddress = data['IPAddress']
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
checkCon = backupUtil.backupUtilities.checkConnection(ipAddress)
|
|
|
|
|
|
|
|
|
|
if checkCon[0]==1:
|
2017-10-24 19:16:36 +05:00
|
|
|
final_dic = {'connStatus': 1, 'error_message': "None"}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
else:
|
2017-11-05 03:02:51 +05:00
|
|
|
final_dic = {'connStatus': 0, 'error_message': checkCon[1]}
|
2017-10-24 19:16:36 +05:00
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
final_dic = {'connStatus': 1, 'error_message': str(msg)}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except KeyError:
|
|
|
|
|
final_dic = {'connStatus': 1, 'error_message': str(msg)}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
def deleteDestination(request):
|
|
|
|
|
try:
|
|
|
|
|
try:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
ipAddress = data['IPAddress']
|
|
|
|
|
|
|
|
|
|
delDest = dest.objects.get(destLoc=ipAddress)
|
|
|
|
|
delDest.delete()
|
|
|
|
|
|
|
|
|
|
path = "/usr/local/CyberCP/backup/"
|
|
|
|
|
destinations = path + "destinations"
|
|
|
|
|
|
|
|
|
|
data = open(destinations,'r').readlines()
|
|
|
|
|
|
|
|
|
|
writeToFile = open(destinations,'r')
|
|
|
|
|
|
|
|
|
|
for items in data:
|
|
|
|
|
if items.find(ipAddress) > -1:
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
writeToFile.writelines(items)
|
|
|
|
|
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
|
|
|
|
## Deleting Cron Tab Entries for this destination
|
|
|
|
|
|
|
|
|
|
path = "/etc/crontab"
|
|
|
|
|
|
|
|
|
|
data = open(path, 'r').readlines()
|
|
|
|
|
|
|
|
|
|
writeToFile = open(path, 'w')
|
|
|
|
|
|
|
|
|
|
for items in data:
|
|
|
|
|
if items.find("backupSchedule.py") > -1:
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
writeToFile.writelines(items)
|
|
|
|
|
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final_dic = {'delStatus': 1, 'error_message': "None"}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
final_dic = {'delStatus': 1, 'error_message': str(msg)}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except KeyError:
|
|
|
|
|
final_dic = {'delStatus': 1, 'error_message': str(msg)}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def scheduleBackup(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
|
|
|
|
|
admin = Administrator.objects.get(pk=val)
|
|
|
|
|
|
|
|
|
|
if admin.type == 1:
|
|
|
|
|
|
|
|
|
|
if dest.objects.all().count() <= 1:
|
|
|
|
|
try:
|
|
|
|
|
homeDest = dest(destLoc="Home")
|
|
|
|
|
homeDest.save()
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
backups = dest.objects.all()
|
|
|
|
|
|
|
|
|
|
destinations = []
|
|
|
|
|
|
|
|
|
|
for items in backups:
|
|
|
|
|
destinations.append(items.destLoc)
|
|
|
|
|
|
|
|
|
|
return render(request,'backup/backupSchedule.html',{'destinations':destinations})
|
|
|
|
|
else:
|
|
|
|
|
return HttpResponse("You should be admin to schedule backups.")
|
|
|
|
|
except KeyError:
|
|
|
|
|
return redirect(loadLoginPage)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getCurrentBackupSchedules(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
try:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
|
|
|
|
records = backupSchedules.objects.all()
|
|
|
|
|
|
|
|
|
|
json_data = "["
|
|
|
|
|
checker = 0
|
|
|
|
|
|
|
|
|
|
for items in records:
|
|
|
|
|
dic = {'id': items.id,
|
|
|
|
|
'destLoc': items.dest.destLoc,
|
|
|
|
|
'frequency':items.frequency,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if checker == 0:
|
|
|
|
|
json_data = json_data + json.dumps(dic)
|
|
|
|
|
checker = 1
|
|
|
|
|
else:
|
|
|
|
|
json_data = json_data + ',' + json.dumps(dic)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
json_data = json_data + ']'
|
|
|
|
|
final_json = json.dumps({'fetchStatus': 1, 'error_message': "None","data":json_data})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
final_dic = {'fetchStatus': 0, 'error_message': str(msg)}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except KeyError:
|
|
|
|
|
final_dic = {'fetchStatus': 0, 'error_message': "Not Logged In, please refresh the page or login again."}
|
|
|
|
|
final_json = json.dumps(final_dic)
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
def submitBackupSchedule(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
try:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
backupDest = data['backupDest']
|
|
|
|
|
backupFreq = data['backupFreq']
|
|
|
|
|
|
|
|
|
|
path = "/etc/crontab"
|
|
|
|
|
|
|
|
|
|
## check if already exists
|
|
|
|
|
try:
|
|
|
|
|
schedule = backupSchedules.objects.get(frequency=backupFreq)
|
|
|
|
|
if schedule.dest.destLoc == backupDest:
|
|
|
|
|
final_json = json.dumps({'scheduleStatus': 0, 'error_message': "This schedule already exists"})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
else:
|
|
|
|
|
if backupDest == "Home" and backupFreq == "Daily":
|
|
|
|
|
cronJob = "0 3 * * 0-6 root python /usr/local/CyberCP/plogical/backupScheduleLocal.py"
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.permissionControl(path)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
writeToFile = open(path,'a')
|
|
|
|
|
writeToFile.writelines(cronJob+"\n")
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.leaveControl(path)
|
|
|
|
|
|
|
|
|
|
command = "sudo systemctl restart crond"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
|
|
|
|
destination = dest.objects.get(destLoc=backupDest)
|
|
|
|
|
newSchedule = backupSchedules(dest=destination, frequency=backupFreq)
|
|
|
|
|
newSchedule.save()
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'scheduleStatus': 1, 'error_message': "None"})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
elif backupDest == "Home" and backupFreq == "Weekly":
|
|
|
|
|
cronJob = "0 3 * * 3 root python /usr/local/CyberCP/plogical/backupScheduleLocal.py "
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.permissionControl(path)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
writeToFile = open(path, 'a')
|
|
|
|
|
writeToFile.writelines(cronJob + "\n")
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.leaveControl(path)
|
|
|
|
|
|
|
|
|
|
command = "sudo systemctl restart crond"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
|
|
|
|
destination = dest.objects.get(destLoc=backupDest)
|
|
|
|
|
newSchedule = backupSchedules(dest=destination, frequency=backupFreq)
|
|
|
|
|
newSchedule.save()
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'scheduleStatus': 1, 'error_message': "None"})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
elif backupDest != "Home" and backupFreq == "Daily":
|
|
|
|
|
cronJob = "0 3 * * 0-6 root python /usr/local/CyberCP/plogical/backupSchedule.py"
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.permissionControl(path)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
writeToFile = open(path, 'a')
|
|
|
|
|
writeToFile.writelines(cronJob + "\n")
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.leaveControl(path)
|
|
|
|
|
|
|
|
|
|
command = "sudo systemctl restart crond"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
|
|
|
|
destination = dest.objects.get(destLoc=backupDest)
|
|
|
|
|
newSchedule = backupSchedules(dest=destination, frequency=backupFreq)
|
|
|
|
|
newSchedule.save()
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'scheduleStatus': 1, 'error_message': "None"})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
elif backupDest != "Home" and backupFreq == "Weekly":
|
|
|
|
|
cronJob = "0 3 * * 3 root python /usr/local/CyberCP/plogical/backupSchedule.py "
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.permissionControl(path)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
writeToFile = open(path, 'a')
|
|
|
|
|
writeToFile.writelines(cronJob + "\n")
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.leaveControl(path)
|
|
|
|
|
|
|
|
|
|
command = "sudo systemctl restart crond"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
|
|
|
|
destination = dest.objects.get(destLoc=backupDest)
|
|
|
|
|
newSchedule = backupSchedules(dest=destination,frequency=backupFreq)
|
|
|
|
|
newSchedule.save()
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'scheduleStatus': 1, 'error_message': "None"})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except:
|
|
|
|
|
if backupDest == "Home" and backupFreq == "Daily":
|
|
|
|
|
cronJob = "0 3 * * 0-6 root python /usr/local/CyberCP/plogical/backupScheduleLocal.py"
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.permissionControl(path)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
writeToFile = open(path, 'a')
|
|
|
|
|
writeToFile.writelines(cronJob + "\n")
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.leaveControl(path)
|
|
|
|
|
|
|
|
|
|
command = "sudo systemctl restart crond"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
|
|
|
|
destination = dest.objects.get(destLoc=backupDest)
|
|
|
|
|
newSchedule = backupSchedules(dest=destination, frequency=backupFreq)
|
|
|
|
|
newSchedule.save()
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'scheduleStatus': 1, 'error_message': "None"})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
elif backupDest == "Home" and backupFreq == "Weekly":
|
|
|
|
|
cronJob = "0 3 * * 3 root python /usr/local/CyberCP/plogical/backupScheduleLocal.py "
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.permissionControl(path)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
writeToFile = open(path, 'a')
|
|
|
|
|
writeToFile.writelines(cronJob + "\n")
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.leaveControl(path)
|
|
|
|
|
|
|
|
|
|
command = "sudo systemctl restart crond"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
|
|
|
|
destination = dest.objects.get(destLoc=backupDest)
|
|
|
|
|
newSchedule = backupSchedules(dest=destination, frequency=backupFreq)
|
|
|
|
|
newSchedule.save()
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'scheduleStatus': 1, 'error_message': "None"})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
elif backupDest != "Home" and backupFreq == "Daily":
|
|
|
|
|
cronJob = "0 3 * * 0-6 root python /usr/local/CyberCP/plogical/backupSchedule.py"
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.permissionControl(path)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
writeToFile = open(path, 'a')
|
|
|
|
|
writeToFile.writelines(cronJob + "\n")
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.leaveControl(path)
|
|
|
|
|
|
|
|
|
|
command = "sudo systemctl restart crond"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
|
|
|
|
destination = dest.objects.get(destLoc=backupDest)
|
|
|
|
|
newSchedule = backupSchedules(dest=destination, frequency=backupFreq)
|
|
|
|
|
newSchedule.save()
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'scheduleStatus': 1, 'error_message': "None"})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
elif backupDest != "Home" and backupFreq == "Weekly":
|
|
|
|
|
cronJob = "0 3 * * 3 root python /usr/local/CyberCP/plogical/backupSchedule.py "
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.permissionControl(path)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
writeToFile = open(path, 'a')
|
|
|
|
|
writeToFile.writelines(cronJob + "\n")
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.leaveControl(path)
|
|
|
|
|
|
|
|
|
|
command = "sudo systemctl restart crond"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
|
|
|
|
destination = dest.objects.get(destLoc=backupDest)
|
|
|
|
|
newSchedule = backupSchedules(dest=destination, frequency=backupFreq)
|
|
|
|
|
newSchedule.save()
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'scheduleStatus': 1, 'error_message': "None"})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
final_json = json.dumps({'scheduleStatus': 0, 'error_message': str(msg)})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except KeyError:
|
|
|
|
|
final_json = json.dumps({'scheduleStatus': 0, 'error_message': str(msg)})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def scheduleDelete(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
try:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
backupDest = data['destLoc']
|
|
|
|
|
backupFreq = data['frequency']
|
|
|
|
|
|
|
|
|
|
path = "/etc/crontab"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if backupDest == "Home" and backupFreq == "Daily":
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.permissionControl(path)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
data = open(path, "r").readlines()
|
|
|
|
|
writeToFile = open(path, 'w')
|
|
|
|
|
|
|
|
|
|
for items in data:
|
|
|
|
|
if items.find("0-6") > -1 and items.find("backupScheduleLocal.py") > -1:
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
writeToFile.writelines(items)
|
|
|
|
|
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.leaveControl(path)
|
|
|
|
|
|
|
|
|
|
command = "sudo systemctl restart crond"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
|
|
|
|
destination = dest.objects.get(destLoc=backupDest)
|
|
|
|
|
newSchedule = backupSchedules.objects.get(dest=destination, frequency=backupFreq)
|
|
|
|
|
newSchedule.delete()
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'delStatus': 1, 'error_message': "None"})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
elif backupDest == "Home" and backupFreq == "Weekly":
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.permissionControl(path)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
data = open(path, "r").readlines()
|
|
|
|
|
writeToFile = open(path, 'w')
|
|
|
|
|
|
|
|
|
|
for items in data:
|
|
|
|
|
if items.find("* 3") > -1 and items.find("backupScheduleLocal.py") > -1:
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
writeToFile.writelines(items)
|
|
|
|
|
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.leaveControl(path)
|
|
|
|
|
|
|
|
|
|
command = "sudo systemctl restart crond"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
|
|
|
|
destination = dest.objects.get(destLoc=backupDest)
|
|
|
|
|
newSchedule = backupSchedules.objects.get(dest=destination, frequency=backupFreq)
|
|
|
|
|
newSchedule.delete()
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'delStatus': 1, 'error_message': "None"})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
elif backupDest != "Home" and backupFreq == "Daily":
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.permissionControl(path)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
data = open(path, "r").readlines()
|
|
|
|
|
writeToFile = open(path, 'w')
|
|
|
|
|
|
|
|
|
|
for items in data:
|
|
|
|
|
if items.find("0-6") > -1 and items.find("backupSchedule.py") > -1:
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
writeToFile.writelines(items)
|
|
|
|
|
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.leaveControl(path)
|
|
|
|
|
|
|
|
|
|
command = "sudo systemctl restart crond"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
|
|
|
|
destination = dest.objects.get(destLoc=backupDest)
|
|
|
|
|
newSchedule = backupSchedules.objects.get(dest=destination, frequency=backupFreq)
|
|
|
|
|
newSchedule.delete()
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'delStatus': 1, 'error_message': "None"})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
elif backupDest != "Home" and backupFreq == "Weekly":
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.permissionControl(path)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
data = open(path, "r").readlines()
|
|
|
|
|
writeToFile = open(path, 'w')
|
|
|
|
|
|
|
|
|
|
for items in data:
|
|
|
|
|
if items.find("* 3") > -1 and items.find("backupSchedule.py") > -1:
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
writeToFile.writelines(items)
|
|
|
|
|
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
virtualHostUtilities.leaveControl(path)
|
|
|
|
|
|
|
|
|
|
command = "sudo systemctl restart crond"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
|
|
|
|
destination = dest.objects.get(destLoc=backupDest)
|
|
|
|
|
newSchedule = backupSchedules.objects.get(dest=destination, frequency=backupFreq)
|
|
|
|
|
newSchedule.delete()
|
|
|
|
|
|
|
|
|
|
final_json = json.dumps({'delStatus': 1, 'error_message': "None"})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
final_json = json.dumps({'delStatus': 0, 'error_message': str(msg)})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except KeyError:
|
|
|
|
|
final_json = json.dumps({'delStatus': 0, 'error_message': str(msg)})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remoteBackups(request):
|
|
|
|
|
try:
|
|
|
|
|
userID = request.session['userID']
|
|
|
|
|
|
|
|
|
|
admin = Administrator.objects.get(pk=userID)
|
|
|
|
|
|
|
|
|
|
if admin.type == 3:
|
|
|
|
|
return HttpResponse("You don't have enough priviliges to access this page.")
|
|
|
|
|
|
|
|
|
|
return render(request,'backup/remoteBackups.html')
|
|
|
|
|
except KeyError:
|
|
|
|
|
return redirect(loadLoginPage)
|
|
|
|
|
|
|
|
|
|
def submitRemoteBackups(request):
|
|
|
|
|
try:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
ipAddress = data['ipAddress']
|
|
|
|
|
password = data['password']
|
|
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
## ask for remote version
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
finalData = json.dumps({'username': "admin","password": password})
|
|
|
|
|
|
|
|
|
|
url = "https://" + ipAddress + ":8090/api/cyberPanelVersion"
|
|
|
|
|
|
|
|
|
|
r = requests.post(url, data=finalData, verify=False)
|
|
|
|
|
|
|
|
|
|
data = json.loads(r.text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if data['getVersion'] == 1:
|
|
|
|
|
|
|
|
|
|
Version = version.objects.get(pk=1)
|
|
|
|
|
|
|
|
|
|
if data['currentVersion'] == Version.currentVersion and data['build'] == Version.build:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
data_ret = {'status': 0, 'error_message': "Your version does not match with version of remote server.",
|
2017-12-09 22:30:10 +05:00
|
|
|
"dir": "Null" }
|
2017-11-02 02:09:47 +05:00
|
|
|
data_ret = json.dumps(data_ret)
|
|
|
|
|
return HttpResponse(data_ret)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
data_ret = {'status': 0, 'error_message': "Not able to fetch version of remote server. Error Message: "+data['error_message'], "dir": "Null"}
|
|
|
|
|
data_ret = json.dumps(data_ret)
|
|
|
|
|
return HttpResponse(data_ret)
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
data_ret = {'status': 0,
|
|
|
|
|
'error_message': "Not able to fetch version of remote server. Error Message: " + str(msg), "dir": "Null"}
|
|
|
|
|
data_ret = json.dumps(data_ret)
|
|
|
|
|
return HttpResponse(data_ret)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## setup ssh key
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
finalData = json.dumps({'username': "admin", "password": password})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
url = "https://" + ipAddress + ":8090/api/fetchSSHkey"
|
|
|
|
|
r = requests.post(url, data=finalData, verify=False)
|
|
|
|
|
data = json.loads(r.text)
|
|
|
|
|
|
|
|
|
|
if data['pubKeyStatus'] == 1:
|
|
|
|
|
pubKey = data["pubKey"].strip("\n")
|
2017-10-24 19:16:36 +05:00
|
|
|
else:
|
2017-12-09 22:30:10 +05:00
|
|
|
final_json = json.dumps({'status': 0, 'error_message': "I am sorry, I could not fetch key from remote server. Error Message: "+data['error_message']})
|
2017-10-24 19:16:36 +05:00
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
## write key
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
##
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
pathToKey = "/home/cyberpanel/" + str(randint(1000, 9999))
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
vhost = open(pathToKey, "w")
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
vhost.write(pubKey)
|
2017-11-05 03:02:51 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
vhost.close()
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
##
|
|
|
|
|
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
execPath = "sudo python " + virtualHostUtilities.cyberPanel + "/plogical/remoteTransferUtilities.py"
|
|
|
|
|
|
|
|
|
|
execPath = execPath + " writeAuthKey --pathToKey " + pathToKey
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
|
|
|
|
|
output = subprocess.check_output(shlex.split(execPath))
|
|
|
|
|
|
|
|
|
|
if output.find("1,None") > -1:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
final_json = json.dumps({'status': 0, 'type': 'exception', 'error_message': output})
|
|
|
|
|
return HttpResponse(final_json)
|
2017-11-05 03:02:51 +05:00
|
|
|
|
|
|
|
|
##
|
|
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
try:
|
|
|
|
|
finalData = json.dumps({'username': "admin","password": password})
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
url = "https://" + ipAddress + ":8090/api/fetchAccountsFromRemoteServer"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
r = requests.post(url, data=finalData, verify=False)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
data = json.loads(r.text)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
if data['fetchStatus'] == 1:
|
|
|
|
|
json_data = data['data']
|
|
|
|
|
data_ret = {'status': 1, 'error_message': "None",
|
|
|
|
|
"dir": "Null",'data':json_data}
|
|
|
|
|
data_ret = json.dumps(data_ret)
|
|
|
|
|
return HttpResponse(data_ret)
|
|
|
|
|
else:
|
|
|
|
|
data_ret = {'status': 0, 'error_message': "Not able to fetch accounts from remote server. Error Message: "+data['error_message'], "dir": "Null"}
|
|
|
|
|
data_ret = json.dumps(data_ret)
|
|
|
|
|
return HttpResponse(data_ret)
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
data_ret = {'status': 0,
|
|
|
|
|
'error_message': "Not able to fetch accounts from remote server. Error Message: " + str(msg), "dir": "Null"}
|
2017-10-24 19:16:36 +05:00
|
|
|
data_ret = json.dumps(data_ret)
|
|
|
|
|
return HttpResponse(data_ret)
|
|
|
|
|
else:
|
|
|
|
|
return HttpResponse("This URL only accepts POST requests")
|
|
|
|
|
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
final_json = json.dumps({'status': 0, 'type':'exception', 'error_message': str(msg)})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
def starRemoteTransfer(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
try:
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
data = json.loads(request.body)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
ipAddress = data['ipAddress']
|
|
|
|
|
password = data['password']
|
2017-10-27 00:09:34 +05:00
|
|
|
accountsToTransfer = data['accountsToTransfer']
|
|
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
try:
|
|
|
|
|
ownIP = requests.get('https://api.ipify.org').text
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
finalData = json.dumps({'username': "admin", "password": password,"ipAddress": ownIP,"accountsToTransfer":accountsToTransfer})
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
url = "https://" + ipAddress + ":8090/api/remoteTransfer"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
r = requests.post(url, data=finalData, verify=False)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
data = json.loads(r.text)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
if data['transferStatus'] == 1:
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
## create local directory that will host backups
|
2017-10-29 22:16:06 +05:00
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
localStoragePath = "/home/backup/transfer-" + str(data['dir'])
|
2017-10-29 22:16:06 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
## making local storage directory for backups
|
|
|
|
|
|
|
|
|
|
command = "sudo mkdir " + localStoragePath
|
|
|
|
|
subprocess.call(shlex.split(command))
|
2017-10-29 22:16:06 +05:00
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
final_json = json.dumps({'remoteTransferStatus': 1, 'error_message': "None","dir":data['dir']})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
else:
|
|
|
|
|
final_json = json.dumps({'remoteTransferStatus': 0, 'error_message':"Can not initiate remote transfer. Error message: "+ data['error_message']})
|
|
|
|
|
return HttpResponse(final_json)
|
2017-10-29 22:16:06 +05:00
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
except BaseException,msg:
|
|
|
|
|
final_json = json.dumps({'remoteTransferStatus': 0,
|
|
|
|
|
'error_message': "Can not initiate remote transfer. Error message: " +
|
|
|
|
|
str(msg)})
|
2017-10-26 23:50:59 +05:00
|
|
|
return HttpResponse(final_json)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-02 02:09:47 +05:00
|
|
|
|
|
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
except BaseException,msg:
|
|
|
|
|
final_json = json.dumps({'remoteTransferStatus': 0, 'error_message': str(msg)})
|
|
|
|
|
return HttpResponse(final_json)
|
|
|
|
|
except KeyError:
|
|
|
|
|
final_json = json.dumps({'remoteTransferStatus': 0, 'error_message': str(msg)})
|
|
|
|
|
return HttpResponse(final_json)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
def getRemoteTransferStatus(request):
|
|
|
|
|
try:
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
ipAddress = data['ipAddress']
|
2017-10-26 23:50:59 +05:00
|
|
|
password = data['password']
|
|
|
|
|
dir = data['dir']
|
|
|
|
|
username = "admin"
|
|
|
|
|
|
|
|
|
|
finalData = json.dumps({'dir': dir, "username":username,"password":password})
|
2017-10-29 22:16:06 +05:00
|
|
|
r = requests.post("https://"+ipAddress+":8090/api/FetchRemoteTransferStatus", data=finalData,verify=False)
|
2017-10-26 23:50:59 +05:00
|
|
|
|
|
|
|
|
data = json.loads(r.text)
|
|
|
|
|
|
|
|
|
|
if data['fetchStatus'] == 1:
|
|
|
|
|
if data['status'].find("Backups are successfully generated and received on") > -1:
|
|
|
|
|
|
|
|
|
|
data = {'remoteTransferStatus': 1, 'error_message': "None", "status": data['status'],'backupsSent': 1}
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
return HttpResponse(json_data)
|
2017-12-09 22:30:10 +05:00
|
|
|
elif data['status'].find("[5010]") > -1:
|
|
|
|
|
data = {'remoteTransferStatus': 0, 'error_message': data['status'],
|
|
|
|
|
'backupsSent': 0}
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
return HttpResponse(json_data)
|
2017-10-26 23:50:59 +05:00
|
|
|
else:
|
|
|
|
|
data = {'remoteTransferStatus': 1, 'error_message': "None", "status": data['status'],
|
|
|
|
|
'backupsSent': 0}
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
return HttpResponse(json_data)
|
|
|
|
|
else:
|
|
|
|
|
data = {'remoteTransferStatus': 0, 'error_message': data['error_message'],
|
|
|
|
|
'backupsSent': 0}
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
return HttpResponse(json_data)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
except BaseException, msg:
|
2017-10-26 23:50:59 +05:00
|
|
|
data = {'remoteTransferStatus': 0, 'error_message': str(msg),'backupsSent': 0}
|
2017-10-24 19:16:36 +05:00
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
return HttpResponse(json_data)
|
|
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
def remoteBackupRestore(request):
|
|
|
|
|
try:
|
|
|
|
|
val = request.session['userID']
|
|
|
|
|
try:
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
backupDir = data['backupDir']
|
|
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
backupDirComplete = "/home/backup/transfer-"+str(backupDir)
|
2017-10-24 19:16:36 +05:00
|
|
|
#adminEmail = admin.email
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
##
|
|
|
|
|
|
|
|
|
|
execPath = "sudo python " + virtualHostUtilities.cyberPanel + "/plogical/remoteTransferUtilities.py"
|
|
|
|
|
|
|
|
|
|
execPath = execPath + " remoteBackupRestore --backupDirComplete " + backupDirComplete + " --backupDir " + str(backupDir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
subprocess.Popen(shlex.split(execPath))
|
|
|
|
|
|
|
|
|
|
data = {'remoteRestoreStatus': 1, 'error_message': 'None'}
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
return HttpResponse(json_data)
|
|
|
|
|
|
|
|
|
|
##
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
data = {'remoteRestoreStatus': 0, 'error_message': str(msg)}
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
return HttpResponse(json_data)
|
|
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
|
data_ret = {'remoteRestoreStatus': 0, 'error_message': "not logged in as admin", "existsStatus": 0}
|
|
|
|
|
json_data = json.dumps(data_ret)
|
|
|
|
|
return HttpResponse(json_data)
|
|
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
def localRestoreStatus(request):
|
2017-10-24 19:16:36 +05:00
|
|
|
try:
|
|
|
|
|
if request.method == "POST":
|
2017-10-26 23:50:59 +05:00
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
data = json.loads(request.body)
|
|
|
|
|
backupDir = data['backupDir']
|
|
|
|
|
|
|
|
|
|
#admin = Administrator.objects.get(userName=username)
|
2017-10-26 23:50:59 +05:00
|
|
|
backupLogPath = "/home/backup/transfer-"+ backupDir +"/" + "backup_log"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
removalPath = "/home/backup/transfer-"+ str(backupDir)
|
|
|
|
|
|
|
|
|
|
if os.path.isfile(backupLogPath):
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
command = "sudo cat " + backupLogPath
|
|
|
|
|
status = subprocess.check_output(shlex.split(command))
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
if status.find("completed[success]")>-1:
|
2017-12-09 22:30:10 +05:00
|
|
|
command = "sudo rm -rf " + removalPath
|
|
|
|
|
#subprocess.call(shlex.split(command))
|
2017-10-26 23:50:59 +05:00
|
|
|
data_ret = {'remoteTransferStatus': 1, 'error_message': "None", "status": status, "complete": 1}
|
2017-10-24 19:16:36 +05:00
|
|
|
json_data = json.dumps(data_ret)
|
|
|
|
|
return HttpResponse(json_data)
|
2017-12-09 22:30:10 +05:00
|
|
|
elif status.find("[5010]") > -1:
|
|
|
|
|
command = "sudo rm -rf " + removalPath
|
|
|
|
|
#subprocess.call(shlex.split(command))
|
|
|
|
|
data = {'remoteTransferStatus': 0, 'error_message': status,
|
|
|
|
|
"status":"None","complete":0}
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
return HttpResponse(json_data)
|
2017-10-24 19:16:36 +05:00
|
|
|
else:
|
2017-10-26 23:50:59 +05:00
|
|
|
data_ret = {'remoteTransferStatus': 1, 'error_message': "None", "status": status, "complete": 0}
|
2017-10-24 19:16:36 +05:00
|
|
|
json_data = json.dumps(data_ret)
|
|
|
|
|
return HttpResponse(json_data)
|
|
|
|
|
else:
|
2017-10-26 23:50:59 +05:00
|
|
|
data_ret = {'remoteTransferStatus': 0, 'error_message': "No such log found","status":"None","complete":0}
|
2017-10-24 19:16:36 +05:00
|
|
|
json_data = json.dumps(data_ret)
|
|
|
|
|
return HttpResponse(json_data)
|
|
|
|
|
|
2017-10-26 23:50:59 +05:00
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
except BaseException, msg:
|
2017-10-26 23:50:59 +05:00
|
|
|
data = {'remoteTransferStatus': 0,'error_message': str(msg),"status":"None","complete":0}
|
2017-10-24 19:16:36 +05:00
|
|
|
json_data = json.dumps(data)
|
2017-10-26 23:50:59 +05:00
|
|
|
return HttpResponse(json_data)
|
|
|
|
|
|
|
|
|
|
def cancelRemoteBackup(request):
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
ipAddress = data['ipAddress']
|
|
|
|
|
password = data['password']
|
|
|
|
|
dir = data['dir']
|
|
|
|
|
username = "admin"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
finalData = json.dumps({'dir': dir, "username":username,"password":password})
|
2017-10-29 22:16:06 +05:00
|
|
|
r = requests.post("https://"+ipAddress+":8090/api/cancelRemoteTransfer", data=finalData,verify=False)
|
2017-10-26 23:50:59 +05:00
|
|
|
|
|
|
|
|
data = json.loads(r.text)
|
|
|
|
|
|
|
|
|
|
if data['cancelStatus'] == 1:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile("Some error cancelling at remote server, see the log file for remote server.")
|
|
|
|
|
|
|
|
|
|
path = "/home/backup/transfer-" + str(dir)
|
|
|
|
|
|
|
|
|
|
if os.path.exists(path):
|
|
|
|
|
try:
|
|
|
|
|
pathpid = path + "/pid"
|
|
|
|
|
|
|
|
|
|
pid = open(pathpid, "r").readlines()[0]
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
os.kill(int(pid), signal.SIGKILL)
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [cancelRemoteBackup]")
|
|
|
|
|
|
|
|
|
|
rmtree(path)
|
|
|
|
|
except:
|
|
|
|
|
rmtree(path)
|
|
|
|
|
|
|
|
|
|
data = {'cancelStatus': 1, 'error_message': "None"}
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
return HttpResponse(json_data)
|
|
|
|
|
else:
|
|
|
|
|
data = {'cancelStatus': 1, 'error_message': "None"}
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
return HttpResponse(json_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
data = {'cancelStatus': 0, 'error_message': str(msg)}
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
return HttpResponse(json_data)
|