Bug fix to Remote Backups!

This commit is contained in:
usmannasir 2018-02-20 22:39:07 +05:00
parent dd419cc933
commit 4371cc9caf
7 changed files with 506 additions and 222 deletions

View File

@ -353,7 +353,6 @@ def loginAPI(request):
return HttpResponse(json_data)
def fetchSSHkey(request):
try:
if request.method == "POST":
@ -365,16 +364,13 @@ def fetchSSHkey(request):
if hashPassword.check_password(admin.password, password):
keyPath = "/root/.ssh"
keyPath = os.path.join("/root",".ssh")
pubKey = keyPath + "/cyberpanel.pub"
execPath = "sudo cat " + pubKey
data = subprocess.check_output(shlex.split(execPath))
data_ret = {'pubKeyStatus': 1, 'error_message': "None", "pubKey":data}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
@ -412,8 +408,6 @@ def remoteTransfer(request):
execPath = execPath + " remoteTransfer --ipAddress " + ipAddress + " --dir " + dir + " --accountsToTransfer " + accountsToTransfer
subprocess.Popen(shlex.split(execPath))
return HttpResponse(json.dumps({"transferStatus": 1, "dir": dir}))
@ -432,7 +426,6 @@ def remoteTransfer(request):
json_data = json.dumps(data)
return HttpResponse(json_data)
def fetchAccountsFromRemoteServer(request):
try:
if request.method == "POST":

View File

@ -1279,11 +1279,7 @@ def submitRemoteBackups(request):
execPath = "sudo python " + virtualHostUtilities.cyberPanel + "/plogical/remoteTransferUtilities.py"
execPath = execPath + " writeAuthKey --pathToKey " + pathToKey
output = subprocess.check_output(shlex.split(execPath))
if output.find("1,None") > -1:
@ -1338,7 +1334,10 @@ def starRemoteTransfer(request):
accountsToTransfer = data['accountsToTransfer']
try:
ownIP = requests.get('https://api.ipify.org').text
ipFile = "/etc/cyberpanel/machineIP"
f = open(ipFile)
ownIP = f.read()
finalData = json.dumps({'username': "admin", "password": password,"ipAddress": ownIP,"accountsToTransfer":accountsToTransfer})
@ -1441,10 +1440,10 @@ def remoteBackupRestore(request):
execPath = execPath + " remoteBackupRestore --backupDirComplete " + backupDirComplete + " --backupDir " + str(backupDir)
subprocess.Popen(shlex.split(execPath))
time.sleep(3)
data = {'remoteRestoreStatus': 1, 'error_message': 'None'}
json_data = json.dumps(data)
return HttpResponse(json_data)
@ -1474,8 +1473,9 @@ def localRestoreStatus(request):
removalPath = "/home/backup/transfer-"+ str(backupDir)
if os.path.isfile(backupLogPath):
time.sleep(3)
if os.path.isfile(backupLogPath):
command = "sudo cat " + backupLogPath
status = subprocess.check_output(shlex.split(command))

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -47,7 +47,7 @@ class backupSchedule:
backupSchedule.sendBackup(backupPath+".tar.gz", ipAddress,writeToFile,port)
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " Backup for: " + virtualHost + " is sent to " + ipAddress + "\n")
"%I-%M-%S-%a-%b-%Y") + "]" + " Backup for: " + virtualHost + " is sent to " + ipAddress + "\n")
writeToFile.writelines("\n")
writeToFile.writelines("\n")

View File

@ -221,7 +221,6 @@ class remoteBackup:
## complete path is a path to the file need to send
command = 'sudo rsync -avz -e "ssh -i /root/.ssh/cyberpanel -o StrictHostKeyChecking=no" ' + completedPathToSend + ' root@' + IPAddress + ':/home/backup/transfer-'+folderNumber
logging.CyberCPLogFileWriter.writeToFile(command)
subprocess.call(shlex.split(command), stdout=writeToFile)
except BaseException, msg:

View File

@ -5,6 +5,11 @@ import remoteBackup as rBackup
import backupUtilities as backupUtil
import time
from multiprocessing import Process
import json
import requests
import subprocess
import shlex
from shutil import move
class remoteTransferUtilities:
@ -44,54 +49,7 @@ class remoteTransferUtilities:
logging.CyberCPLogFileWriter.writeToFile("For remote transfer, I am not able to write key to auth file, Error Message: "+str(msg))
print "0,"+"For remote transfer, I am not able to write key to auth file, Error Message: "+str(msg)
@staticmethod
def remoteBackupRestore(backupDir, dir):
try:
## dir is transfer-###
# backupDir is /home/backup/transfer-###
backupLogPath = backupDir + "/backup_log"
writeToFile = open(backupLogPath, "a+")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.writelines("############################\n")
writeToFile.writelines(" Starting Backup Restore\n")
writeToFile.writelines(" Start date: " + time.strftime("%I-%M-%S-%a-%b-%Y") + "\n")
writeToFile.writelines("############################\n")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.close()
if os.path.exists(backupDir):
pass
else:
writeToFile = open(backupLogPath, "w+")
writeToFile.writelines("No such directory found (Local directory where backups are placed does not exists)' [5010]" + "\n")
writeToFile.close()
return
p = Process(target=rBackup.remoteBackup.startRestore, args=(backupDir, backupLogPath,dir,))
p.start()
pid = open(backupDir + '/pid', "w")
pid.write(str(p.pid))
pid.close()
return
except BaseException, msg:
backupLogPath = backupDir + "/backup_log"
writeToFile = open(backupLogPath, "w+")
writeToFile.writelines(str(msg) + " [5010]" + "\n")
writeToFile.close()
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [remoteRestore]")
return [0, msg]
## house keeping function to run remote backups
@staticmethod
def remoteTransfer(ipAddress,dir,accountsToTransfer):
try:
@ -130,7 +88,12 @@ class remoteTransferUtilities:
writeToFile.close()
p = Process(target=rBackup.remoteBackup.backupProcess,
## destination = /home/backup/transfer-2558
## backupLogPath = /home/backup/transfer-2558/backup_log
## dir = 2558
## Array of domains to be transferred
p = Process(target=remoteTransferUtilities.backupProcess,
args=(ipAddress, destination, backupLogPath, dir, accountsToTransfer))
p.start()
@ -148,10 +111,242 @@ class remoteTransferUtilities:
return [0, msg]
## destination = /home/backup/transfer-2558
## backupLogPath = /home/backup/transfer-2558/backup_log
## dir = 2558
## Array of domains to be transferred
@staticmethod
def backupProcess(ipAddress, dir, backupLogPath, folderNumber, accountsToTransfer):
try:
## dir is without forward slash
for virtualHost in accountsToTransfer:
try:
if virtualHost == "vmail" or virtualHost == "backup":
continue
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " Currently generating local backups for: " + virtualHost + "\n")
writeToFile.close()
finalData = json.dumps({'websiteToBeBacked': virtualHost})
r = requests.post("http://localhost:5003/backup/submitBackupCreation", data=finalData,
verify=False)
data = json.loads(r.text)
fileName = data['tempStorage'] + ".tar.gz"
completePathToBackupFile = fileName
while (1):
time.sleep(2)
r = requests.post("http://localhost:5003/backup/backupStatus", data=finalData, verify=False)
data = json.loads(r.text)
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " Waiting for backup to complete.. " + "\n")
writeToFile.close()
if data['backupStatus'] == 0:
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + "Failed to generate local backup for: " + virtualHost + ". Error message: " +
data['error_message'] + "\n")
break
elif data['abort'] == 1:
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " Local Backup Completed for: " + virtualHost + " with status: " +
data['status'] + "\n")
## move the generated backup file to specified destination
if os.path.exists(completePathToBackupFile):
move(completePathToBackupFile, dir)
completedPathToSend = dir + "/" + completePathToBackupFile.split("/")[-1]
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " Sending " + completedPathToSend + " to " + ipAddress + ".\n")
remoteTransferUtilities.sendBackup(completedPathToSend, ipAddress, str(folderNumber), writeToFile)
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " Sent " + completedPathToSend + " to " + ipAddress + ".\n")
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " #############################################" + "\n")
writeToFile.close()
break
except:
pass
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " Backups are successfully generated and received on: " + ipAddress + "\n")
writeToFile.close()
## removing local directory where backups were generated
#time.sleep(5)
# rmtree(dir)
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [backupProcess]")
@staticmethod
def sendBackup(completedPathToSend, IPAddress, folderNumber,writeToFile):
try:
## complete path is a path to the file need to send
command = "sudo scp -i /root/.ssh/cyberpanel " + completedPathToSend + " root@" + IPAddress + ":/home/backup/transfer-" + folderNumber + "/"
subprocess.call(shlex.split(command), stdout=writeToFile)
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startBackup]")
@staticmethod
def remoteBackupRestore(backupDir, dir):
try:
## dir is transfer-###
# backupDir is /home/backup/transfer-###
backupLogPath = backupDir + "/backup_log"
writeToFile = open(backupLogPath, "a+")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.writelines("############################\n")
writeToFile.writelines(" Starting Backup Restore\n")
writeToFile.writelines(" Start date: " + time.strftime("%I-%M-%S-%a-%b-%Y") + "\n")
writeToFile.writelines("############################\n")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.close()
if os.path.exists(backupDir):
pass
else:
writeToFile = open(backupLogPath, "w+")
writeToFile.writelines(
"No such directory found (Local directory where backups are placed does not exists)' [5010]" + "\n")
writeToFile.close()
return
p = Process(target=remoteTransferUtilities.startRestore, args=(backupDir, backupLogPath, dir,))
p.start()
pid = open(backupDir + '/pid', "w")
pid.write(str(p.pid))
pid.close()
return
except BaseException, msg:
backupLogPath = backupDir + "/backup_log"
writeToFile = open(backupLogPath, "w+")
writeToFile.writelines(str(msg) + " [5010]" + "\n")
writeToFile.close()
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [remoteRestore]")
return [0, msg]
@staticmethod
def startRestore(backupDir, backupLogPath, dir):
try:
ext = ".tar.gz"
for backup in os.listdir(backupDir):
if backup.endswith(ext):
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " Starting restore for: " + backup + ".\n")
writeToFile.close()
finalData = json.dumps({'backupFile': backup, "dir": dir})
r = requests.post("http://localhost:5003/backup/submitRestore", data=finalData, verify=False)
data = json.loads(r.text)
if data['restoreStatus'] == 1:
while (1):
time.sleep(1)
finalData = json.dumps({'backupFile': backup, "dir": dir})
r = requests.post("http://localhost:5003/backup/restoreStatus", data=finalData,
verify=False)
data = json.loads(r.text)
if data['abort'] == 1 and data['running'] == "Error":
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " Restore aborted for: " + backup + ". Error message: " +
data['status'] + "\n")
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " #########################################\n")
writeToFile.close()
break
elif data['abort'] == 1 and data['running'] == "Completed":
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " Restore Completed for: " + backup + ".\n")
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " #########################################\n")
writeToFile.close()
break
else:
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " Waiting for restore to complete.\n")
writeToFile.close()
time.sleep(3)
pass
else:
logging.CyberCPLogFileWriter.writeToFile("Could not start restore process for: " + backup)
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + "Could not start restore process for: " + backup + "\n")
writeToFile.close()
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "]" + " Backup Restore complete\n")
writeToFile.writelines("completed[success]")
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [initiateRestore]")
def main():
parser = argparse.ArgumentParser(description='CyberPanel Installer')
parser.add_argument('function', help='Specific a function to call!')
parser.add_argument('function', help='Specific function to call!')
parser.add_argument('--pathToKey', help='')