mirror of
https://github.com/usmannasir/cyberpanel.git
synced 2025-11-07 13:56:01 +01:00
oc backups
This commit is contained in:
@@ -2,8 +2,12 @@
|
||||
import os
|
||||
import os.path
|
||||
import sys
|
||||
import django
|
||||
from io import StringIO
|
||||
|
||||
import django
|
||||
import paramiko
|
||||
|
||||
from plogical.applicationInstaller import ApplicationInstaller
|
||||
from plogical.httpProc import httpProc
|
||||
|
||||
sys.path.append('/usr/local/CyberCP')
|
||||
@@ -1912,4 +1916,405 @@ class BackupManager:
|
||||
except BaseException as msg:
|
||||
data_ret = {'abort': 0, 'installStatus': 0, 'installationProgress': "0", 'error_message': str(msg)}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
return HttpResponse(json_data)
|
||||
|
||||
def OneClickBackups(self, request=None, userID=None, data=None):
|
||||
user = Administrator.objects.get(pk=userID)
|
||||
|
||||
data = {}
|
||||
|
||||
import requests
|
||||
try:
|
||||
if request.GET.get('status', 'none') == 'success':
|
||||
plan_name = request.GET.get('planName')
|
||||
months = request.GET.get('months')
|
||||
monthly_price = request.GET.get('monthlyPrice')
|
||||
yearly_price = request.GET.get('yearlyPrice')
|
||||
customer = request.GET.get('customer')
|
||||
subscription = request.GET.get('subscription')
|
||||
|
||||
from IncBackups.models import OneClickBackups
|
||||
|
||||
if months == '1':
|
||||
price = monthly_price
|
||||
else:
|
||||
price = yearly_price
|
||||
|
||||
try:
|
||||
|
||||
backup_plan = OneClickBackups(
|
||||
owner=user,
|
||||
planName=plan_name,
|
||||
months=months,
|
||||
price=price,
|
||||
customer=customer,
|
||||
subscription=subscription,
|
||||
sftpUser=f'{user.userName}{str(randint(1000, 9999))}',
|
||||
)
|
||||
backup_plan.save()
|
||||
|
||||
####
|
||||
|
||||
import requests
|
||||
import json
|
||||
|
||||
|
||||
# Define the URL of the endpoint
|
||||
url = 'http://platform.cyberpersons.com/Billing/CreateSFTPAccount' # Replace with your actual endpoint URL
|
||||
|
||||
# Define the payload to send in the POST request
|
||||
payload = {
|
||||
'sub': subscription,
|
||||
'key': ProcessUtilities.outputExecutioner(f'cat /root/.ssh/cyberpanel.pub'), # Replace with the actual SSH public key
|
||||
'sftpUser': backup_plan.sftpUser,
|
||||
'serverIP': ACLManager.fetchIP(), # Replace with the actual server IP,
|
||||
'planName': plan_name
|
||||
}
|
||||
|
||||
# Convert the payload to JSON format
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
dataRet = json.dumps(payload)
|
||||
|
||||
# Make the POST request
|
||||
response = requests.post(url, headers=headers, data=dataRet)
|
||||
|
||||
# Handle the response
|
||||
if response.status_code == 200:
|
||||
response_data = response.json()
|
||||
if response_data.get('status') == 1:
|
||||
|
||||
ocbkup = OneClickBackups.objects.get(owner=user,subscription=subscription)
|
||||
ocbkup.state = 1
|
||||
ocbkup.save()
|
||||
|
||||
finalDic = {}
|
||||
|
||||
finalDic['IPAddress'] = response_data.get('ipAddress')
|
||||
finalDic['password'] = 'NOT-NEEDED'
|
||||
finalDic['backupSSHPort'] = '22'
|
||||
finalDic['userName'] = backup_plan.sftpUser
|
||||
finalDic['type'] = 'SFTP'
|
||||
finalDic['path'] = 'cpbackups'
|
||||
finalDic['name'] = backup_plan.sftpUser
|
||||
|
||||
wm = BackupManager()
|
||||
response_inner = wm.submitDestinationCreation(userID, finalDic)
|
||||
|
||||
response_data_inner = json.loads(response_inner.content.decode('utf-8'))
|
||||
|
||||
# Extract the value of 'status'
|
||||
if response_data_inner.get('status') == 0:
|
||||
data['status'] = 0
|
||||
data[
|
||||
'message'] = f"[2109] Failed to create sftp account {response_data_inner.get('error_message')}"
|
||||
print("Failed to create SFTP account:", response_data_inner.get('error_message'))
|
||||
else:
|
||||
data['status'] = 1
|
||||
|
||||
else:
|
||||
data['status'] = 0
|
||||
data['message'] = f"[1985] Failed to create sftp account {response_data.get('error_message')}"
|
||||
print("Failed to create SFTP account:", response_data.get('error_message'))
|
||||
else:
|
||||
print("Failed to connect to the server. Status code:", response.status_code)
|
||||
print("Response:", response.text)
|
||||
data['status'] = 0
|
||||
data['message'] = f"[1991] Failed to create sftp account {response.text}"
|
||||
|
||||
####
|
||||
|
||||
except BaseException as msg:
|
||||
data['status'] = 4
|
||||
data['message'] = str(msg)
|
||||
|
||||
elif request.GET.get('status', 'none') == 'cancelled':
|
||||
data['status'] = 0
|
||||
else:
|
||||
data['status'] = 2
|
||||
except BaseException as msg:
|
||||
data['status'] = 0
|
||||
data['message'] = f"[2038] Unkown error occured in purchase process. Error message: {str(msg)}"
|
||||
|
||||
|
||||
url = 'https://platform.cyberpersons.com/Billing/FetchBackupPlans'
|
||||
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status() # Check if the request was successful
|
||||
data['plans'] = response.json() # Convert the response to a Python dictionary
|
||||
except requests.exceptions.HTTPError as http_err:
|
||||
print(f'HTTP error occurred: {http_err}')
|
||||
except Exception as err:
|
||||
print(f'Other error occurred: {err}')
|
||||
|
||||
data['bPlans'] = user.oneclickbackups_set.all()
|
||||
|
||||
proc = httpProc(request, 'backup/oneClickBackups.html', data, 'addDeleteDestinations')
|
||||
return proc.render()
|
||||
|
||||
def ManageOCBackups(self, request=None, userID=None, data=None):
|
||||
userID = request.session['userID']
|
||||
currentACL = ACLManager.loadedACL(userID)
|
||||
admin = Administrator.objects.get(pk=userID)
|
||||
from IncBackups.models import OneClickBackups
|
||||
ocb = OneClickBackups.objects.get(pk = request.GET.get('id'), owner=admin)
|
||||
destinations = [NormalBackupDests.objects.get(name=ocb.sftpUser)]
|
||||
dests = []
|
||||
for dest in destinations:
|
||||
dests.append(dest.name)
|
||||
|
||||
websitesName = ACLManager.findAllSites(currentACL, userID)
|
||||
|
||||
proc = httpProc(request, 'backup/OneClickBackupSchedule.html', {'destination': NormalBackupDests.objects.get(name=ocb.sftpUser).name, 'websites': websitesName},
|
||||
'scheduleBackups')
|
||||
return proc.render()
|
||||
|
||||
def RestoreOCBackups(self, request=None, userID=None, data=None):
|
||||
userID = request.session['userID']
|
||||
currentACL = ACLManager.loadedACL(userID)
|
||||
admin = Administrator.objects.get(pk=userID)
|
||||
from IncBackups.models import OneClickBackups
|
||||
ocb = OneClickBackups.objects.get(pk = request.GET.get('id'), owner=admin)
|
||||
|
||||
# Load the private key
|
||||
|
||||
nbd = NormalBackupDests.objects.get(name=ocb.sftpUser)
|
||||
ip = json.loads(nbd.config)['ip']
|
||||
|
||||
# Connect to the remote server using the private key
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
# Read the private key content
|
||||
private_key_path = '/root/.ssh/cyberpanel'
|
||||
key_content = ProcessUtilities.outputExecutioner(f'cat {private_key_path}').rstrip('\n')
|
||||
|
||||
# Load the private key from the content
|
||||
key_file = StringIO(key_content)
|
||||
key = paramiko.RSAKey.from_private_key(key_file)
|
||||
# Connect to the server using the private key
|
||||
ssh.connect(ip, username=ocb.sftpUser, pkey=key)
|
||||
# Command to list directories under the specified path
|
||||
command = f"ls -d cpbackups/*/"
|
||||
|
||||
# Execute the command
|
||||
stdin, stdout, stderr = ssh.exec_command(command)
|
||||
|
||||
# Read the results
|
||||
directories = stdout.read().decode().splitlines()
|
||||
|
||||
finalDirs = []
|
||||
|
||||
# Print directories
|
||||
for directory in directories:
|
||||
finalDirs.append(directory.split('/')[1])
|
||||
|
||||
proc = httpProc(request, 'backup/restoreOCBackups.html', {'directories': finalDirs},
|
||||
'scheduleBackups')
|
||||
return proc.render()
|
||||
|
||||
def fetchOCSites(self, request=None, userID=None, data=None):
|
||||
try:
|
||||
userID = request.session['userID']
|
||||
currentACL = ACLManager.loadedACL(userID)
|
||||
|
||||
data = json.loads(request.body)
|
||||
id = data['idValue']
|
||||
folder = data['folder']
|
||||
|
||||
admin = Administrator.objects.get(pk=userID)
|
||||
from IncBackups.models import OneClickBackups
|
||||
ocb = OneClickBackups.objects.get(pk = id, owner=admin)
|
||||
|
||||
# Load the private key
|
||||
|
||||
nbd = NormalBackupDests.objects.get(name=ocb.sftpUser)
|
||||
ip = json.loads(nbd.config)['ip']
|
||||
|
||||
# Connect to the remote server using the private key
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
# Read the private key content
|
||||
private_key_path = '/root/.ssh/cyberpanel'
|
||||
key_content = ProcessUtilities.outputExecutioner(f'cat {private_key_path}').rstrip('\n')
|
||||
|
||||
# Load the private key from the content
|
||||
key_file = StringIO(key_content)
|
||||
key = paramiko.RSAKey.from_private_key(key_file)
|
||||
# Connect to the server using the private key
|
||||
ssh.connect(ip, username=ocb.sftpUser, pkey=key)
|
||||
# Command to list directories under the specified path
|
||||
command = f"ls -d cpbackups/{folder}/*"
|
||||
|
||||
# Execute the command
|
||||
stdin, stdout, stderr = ssh.exec_command(command)
|
||||
|
||||
# Read the results
|
||||
directories = stdout.read().decode().splitlines()
|
||||
|
||||
finalDirs = []
|
||||
|
||||
# Print directories
|
||||
for directory in directories:
|
||||
finalDirs.append(directory.split('/')[2])
|
||||
|
||||
data_ret = {'status': 1, 'finalDirs': finalDirs}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
except BaseException as msg:
|
||||
data_ret = {'status': 0, 'error_message': str(msg)}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
|
||||
def StartOCRestore(self, request=None, userID=None, data=None):
|
||||
try:
|
||||
userID = request.session['userID']
|
||||
currentACL = ACLManager.loadedACL(userID)
|
||||
|
||||
data = json.loads(request.body)
|
||||
id = data['idValue']
|
||||
folder = data['folder']
|
||||
backupfile = data['backupfile']
|
||||
|
||||
|
||||
extraArgs = {}
|
||||
extraArgs['id'] = id
|
||||
extraArgs['folder'] = folder
|
||||
extraArgs['backupfile'] = backupfile
|
||||
extraArgs['userID'] = userID
|
||||
extraArgs['tempStatusPath'] = "/home/cyberpanel/" + str(randint(1000, 9999))
|
||||
|
||||
statusFile = open(extraArgs['tempStatusPath'], 'w')
|
||||
statusFile.writelines("Restore started..")
|
||||
statusFile.close()
|
||||
|
||||
background = ApplicationInstaller('StartOCRestore', extraArgs)
|
||||
background.start()
|
||||
|
||||
data_ret = {'status': 1, 'installStatus': 1, 'error_message': 'None',
|
||||
'tempStatusPath': extraArgs['tempStatusPath']}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
except BaseException as msg:
|
||||
data_ret = {'status': 0, 'error_message': str(msg)}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
|
||||
def DeployAccount(self, request=None, userID=None, data=None):
|
||||
user = Administrator.objects.get(pk=userID)
|
||||
|
||||
userID = request.session['userID']
|
||||
currentACL = ACLManager.loadedACL(userID)
|
||||
import json
|
||||
|
||||
data = json.loads(request.body)
|
||||
id = data['id']
|
||||
|
||||
from IncBackups.models import OneClickBackups
|
||||
ocb = OneClickBackups.objects.get(pk=id, owner=user)
|
||||
|
||||
data = {}
|
||||
|
||||
####
|
||||
|
||||
import requests
|
||||
import json
|
||||
|
||||
# Define the URL of the endpoint
|
||||
url = 'http://platform.cyberpersons.com/Billing/CreateSFTPAccount' # Replace with your actual endpoint URL
|
||||
|
||||
# Define the payload to send in the POST request
|
||||
payload = {
|
||||
'sub': ocb.subscription,
|
||||
'key': ProcessUtilities.outputExecutioner(f'cat /root/.ssh/cyberpanel.pub'),
|
||||
# Replace with the actual SSH public key
|
||||
'sftpUser': ocb.sftpUser,
|
||||
'serverIP': ACLManager.fetchIP(), # Replace with the actual server IP
|
||||
'planName': ocb.planName
|
||||
}
|
||||
|
||||
# Convert the payload to JSON format
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
dataRet = json.dumps(payload)
|
||||
|
||||
# Make the POST request
|
||||
response = requests.post(url, headers=headers, data=dataRet)
|
||||
|
||||
# Handle the response
|
||||
# Handle the response
|
||||
if response.status_code == 200:
|
||||
response_data = response.json()
|
||||
if response_data.get('status') == 1:
|
||||
|
||||
ocb.state = 1
|
||||
ocb.save()
|
||||
|
||||
print("SFTP account created successfully.")
|
||||
|
||||
finalDic = {}
|
||||
|
||||
finalDic['IPAddress'] = response_data.get('ipAddress')
|
||||
finalDic['password'] = 'NOT-NEEDED'
|
||||
finalDic['backupSSHPort'] = '22'
|
||||
finalDic['userName'] = ocb.sftpUser
|
||||
finalDic['type'] = 'SFTP'
|
||||
finalDic['path'] = 'cpbackups'
|
||||
finalDic['name'] = ocb.sftpUser
|
||||
|
||||
wm = BackupManager()
|
||||
response_inner = wm.submitDestinationCreation(userID, finalDic)
|
||||
|
||||
response_data_inner = json.loads(response_inner.content.decode('utf-8'))
|
||||
|
||||
# Extract the value of 'status'
|
||||
if response_data_inner.get('status') == 0:
|
||||
data_ret = {'status': 1, 'error_message': response_data_inner.get('error_message')}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
else:
|
||||
data_ret = {'status': 1,}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
|
||||
else:
|
||||
|
||||
if response_data.get('error_message') == "Already deployed.":
|
||||
ocb.state = 1
|
||||
ocb.save()
|
||||
|
||||
print("SFTP account created successfully.")
|
||||
|
||||
finalDic = {}
|
||||
|
||||
finalDic['IPAddress'] = response_data.get('ipAddress')
|
||||
finalDic['password'] = 'NOT-NEEDED'
|
||||
finalDic['backupSSHPort'] = '22'
|
||||
finalDic['userName'] = ocb.sftpUser
|
||||
finalDic['type'] = 'SFTP'
|
||||
finalDic['path'] = 'cpbackups'
|
||||
finalDic['name'] = ocb.sftpUser
|
||||
|
||||
wm = BackupManager()
|
||||
response_inner = wm.submitDestinationCreation(userID, finalDic)
|
||||
|
||||
response_data_inner = json.loads(response_inner.content.decode('utf-8'))
|
||||
|
||||
# Extract the value of 'status'
|
||||
if response_data_inner.get('status') == 0:
|
||||
data_ret = {'status': 1, 'error_message': response_data_inner.get('error_message')}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
else:
|
||||
data_ret = {'status': 1, }
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
|
||||
data_ret = {'status': 0, 'error_message': response_data.get('error_message')}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
else:
|
||||
data['message'] = f"[1991] Failed to create sftp account {response.text}"
|
||||
data_ret = {'status': 0, 'error_message': response.text}
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user