Files
CyberPanel/plogical/remoteTransferUtilities.py

356 lines
15 KiB
Python
Raw Normal View History

2017-12-09 22:30:10 +05:00
import argparse
import os
2019-12-12 08:46:07 +05:00
import CyberCPLogFileWriter as logging
import backupUtilities as backupUtil
2017-12-09 22:30:10 +05:00
import time
from multiprocessing import Process
2018-02-20 22:39:07 +05:00
import json
import requests
import subprocess
import shlex
from shutil import move
2019-03-31 02:47:35 +05:00
from plogical.virtualHostUtilities import virtualHostUtilities
from plogical.processUtilities import ProcessUtilities
2019-12-11 10:40:35 +05:00
from .backupSchedule import backupSchedule
2019-06-08 21:41:43 +00:00
import shutil
2017-12-09 22:30:10 +05:00
class remoteTransferUtilities:
@staticmethod
def writeAuthKey(pathToKey):
try:
2018-02-22 12:17:38 +05:00
authorized_keys = os.path.join("/root",".ssh","authorized_keys")
2017-12-09 22:30:10 +05:00
presenseCheck = 0
try:
data = open(authorized_keys, "r").readlines()
for items in data:
if items.find(open(pathToKey,"r").read()) > -1:
try:
os.remove(pathToKey)
except:
pass
2019-12-10 15:09:10 +05:00
print("1,None")
2017-12-09 22:30:10 +05:00
return
except:
pass
if presenseCheck == 0:
writeToFile = open(authorized_keys, 'a')
writeToFile.writelines("#Added by CyberPanel\n")
writeToFile.writelines(open(pathToKey,"r").read())
writeToFile.writelines("\n")
writeToFile.close()
try:
os.remove(pathToKey)
except:
pass
2019-12-10 15:09:10 +05:00
print("1,None")
2017-12-09 22:30:10 +05:00
return
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2017-12-09 22:30:10 +05:00
logging.CyberCPLogFileWriter.writeToFile("For remote transfer, I am not able to write key to auth file, Error Message: "+str(msg))
2019-12-10 15:09:10 +05:00
print("0,"+"For remote transfer, I am not able to write key to auth file, Error Message: " + str(msg))
2017-12-09 22:30:10 +05:00
2018-05-11 00:03:26 +05:00
## House keeping function to run remote backups
2017-12-09 22:30:10 +05:00
@staticmethod
2018-05-11 00:03:26 +05:00
def remoteTransfer(ipAddress, dir, accountsToTransfer):
2017-12-09 22:30:10 +05:00
try:
2018-02-20 22:39:07 +05:00
destination = "/home/backup/transfer-" + dir
backupLogPath = destination + "/backup_log"
2017-12-09 22:30:10 +05:00
2018-05-11 00:03:26 +05:00
data = open(accountsToTransfer, 'r').readlines()
accountsToTransfer = []
for items in data:
accountsToTransfer.append(items.strip('\n'))
2017-12-09 22:30:10 +05:00
2018-02-20 22:39:07 +05:00
if not os.path.exists(destination):
os.makedirs(destination)
writeToFile = open(backupLogPath, "w+")
2017-12-09 22:30:10 +05:00
writeToFile.writelines("############################\n")
2018-02-20 22:39:07 +05:00
writeToFile.writelines(" Starting remote Backup\n")
writeToFile.writelines(" Start date: " + time.strftime("%m.%d.%Y_%H-%M-%S") + "\n")
2017-12-09 22:30:10 +05:00
writeToFile.writelines("############################\n")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
2018-02-20 22:39:07 +05:00
if backupUtil.backupUtilities.checkIfHostIsUp(ipAddress) == 1:
checkConn = backupUtil.backupUtilities.checkConnection(ipAddress)
if checkConn[0] == 0:
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " Connection to:" + ipAddress + " Failed, please resetup this destination from CyberPanel, aborting. [5010]" + "\n")
2018-02-20 22:39:07 +05:00
writeToFile.close()
return
else:
pass
2017-12-09 22:30:10 +05:00
else:
2018-02-20 22:39:07 +05:00
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " Host:" + ipAddress + " could be down, we are continuing..." + "\n")
2017-12-09 22:30:10 +05:00
writeToFile.close()
2018-02-20 22:39:07 +05:00
writeToFile.close()
2017-12-09 22:30:10 +05:00
2018-02-20 22:39:07 +05:00
## destination = /home/backup/transfer-2558
## backupLogPath = /home/backup/transfer-2558/backup_log
## dir = 2558
## Array of domains to be transferred
2017-12-09 22:30:10 +05:00
2018-02-20 22:39:07 +05:00
p = Process(target=remoteTransferUtilities.backupProcess,
args=(ipAddress, destination, backupLogPath, dir, accountsToTransfer))
p.start()
2017-12-09 22:30:10 +05:00
2018-02-20 22:39:07 +05:00
pid = open(destination + '/pid', "w")
2017-12-09 22:30:10 +05:00
pid.write(str(p.pid))
pid.close()
return
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2017-12-09 22:30:10 +05:00
writeToFile = open(backupLogPath, "w+")
writeToFile.writelines(str(msg) + " [5010]" + "\n")
writeToFile.close()
2018-02-20 22:39:07 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [remoteTransfer]")
2018-05-11 00:03:26 +05:00
return [0, str(msg)]
2017-12-09 22:30:10 +05:00
2018-02-20 22:39:07 +05:00
## destination = /home/backup/transfer-2558
## backupLogPath = /home/backup/transfer-2558/backup_log
## dir = 2558
## Array of domains to be transferred
2017-12-09 22:30:10 +05:00
@staticmethod
2018-02-20 22:39:07 +05:00
def backupProcess(ipAddress, dir, backupLogPath, folderNumber, accountsToTransfer):
try:
## dir is without forward slash
for virtualHost in accountsToTransfer:
try:
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " Currently generating local backups for: " + virtualHost + "\n")
2018-02-20 22:39:07 +05:00
writeToFile.close()
2019-06-08 21:41:43 +00:00
retValue = backupSchedule.createLocalBackup(virtualHost, backupLogPath)
2018-02-20 22:39:07 +05:00
2019-06-08 21:41:43 +00:00
if retValue[0] == 1:
writeToFile = open(backupLogPath, 'a')
2018-02-20 22:39:07 +05:00
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " Local Backup Completed for: " + virtualHost + "\n")
2018-02-20 22:39:07 +05:00
2019-06-08 21:41:43 +00:00
completePathToBackupFile = retValue[1] + '.tar.gz'
2018-02-20 22:39:07 +05:00
2019-06-08 21:41:43 +00:00
## move the generated backup file to specified destination
2018-02-20 22:39:07 +05:00
2019-06-08 21:41:43 +00:00
if os.path.exists(completePathToBackupFile):
move(completePathToBackupFile, dir)
2018-02-20 22:39:07 +05:00
2019-06-08 21:41:43 +00:00
completedPathToSend = dir + "/" + completePathToBackupFile.split("/")[-1]
2018-02-20 22:39:07 +05:00
2019-06-08 21:41:43 +00:00
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " Sending " + completedPathToSend + " to " + ipAddress + ".\n")
2018-02-20 22:39:07 +05:00
2019-06-08 21:41:43 +00:00
remoteTransferUtilities.sendBackup(completedPathToSend, ipAddress, str(folderNumber),
writeToFile)
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " Sent " + completedPathToSend + " to " + ipAddress + ".\n")
2018-02-20 22:39:07 +05:00
2019-06-08 21:41:43 +00:00
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " #############################################" + "\n")
2018-02-20 22:39:07 +05:00
2019-06-08 21:41:43 +00:00
writeToFile.close()
else:
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + "Failed to generate local backup for: " + virtualHost + "\n")
2018-02-20 22:39:07 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2019-06-08 21:41:43 +00:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [remoteTransferUtilities.backupProcess:173]")
2018-02-20 22:39:07 +05:00
pass
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " Backups are successfully generated and received on: " + ipAddress + "\n")
2018-02-20 22:39:07 +05:00
writeToFile.close()
## removing local directory where backups were generated
#time.sleep(5)
# rmtree(dir)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-02-20 22:39:07 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [backupProcess]")
@staticmethod
def sendBackup(completedPathToSend, IPAddress, folderNumber,writeToFile):
2017-12-09 22:30:10 +05:00
try:
2018-02-20 22:39:07 +05:00
## complete path is a path to the file need to send
2017-12-09 22:30:10 +05:00
2018-02-22 12:17:38 +05:00
command = "sudo scp -o StrictHostKeyChecking=no -i /root/.ssh/cyberpanel " + completedPathToSend + " root@" + IPAddress + ":/home/backup/transfer-" + folderNumber + "/"
2019-03-26 16:19:03 +05:00
subprocess.call(shlex.split(command), stdout=writeToFile)
2017-12-09 22:30:10 +05:00
2019-06-08 21:41:43 +00:00
os.remove(completedPathToSend)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-02-20 22:39:07 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startBackup]")
2017-12-09 22:30:10 +05:00
2018-02-20 22:39:07 +05:00
@staticmethod
def remoteBackupRestore(backupDir, dir):
try:
2017-12-09 22:30:10 +05:00
2018-02-20 22:39:07 +05:00
## dir is transfer-###
# backupDir is /home/backup/transfer-###
backupLogPath = backupDir + "/backup_log"
2017-12-09 22:30:10 +05:00
2018-02-20 22:39:07 +05:00
writeToFile = open(backupLogPath, "a+")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
2017-12-09 22:30:10 +05:00
writeToFile.writelines("############################\n")
2018-02-20 22:39:07 +05:00
writeToFile.writelines(" Starting Backup Restore\n")
writeToFile.writelines(" Start date: " + time.strftime("%m.%d.%Y_%H-%M-%S") + "\n")
2017-12-09 22:30:10 +05:00
writeToFile.writelines("############################\n")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
2018-02-20 22:39:07 +05:00
writeToFile.close()
2017-12-09 22:30:10 +05:00
2018-02-20 22:39:07 +05:00
if os.path.exists(backupDir):
pass
2017-12-09 22:30:10 +05:00
else:
2018-02-20 22:39:07 +05:00
writeToFile = open(backupLogPath, "w+")
writeToFile.writelines(
"No such directory found (Local directory where backups are placed does not exists)' [5010]" + "\n")
2017-12-09 22:30:10 +05:00
writeToFile.close()
return
2018-02-20 22:39:07 +05:00
p = Process(target=remoteTransferUtilities.startRestore, args=(backupDir, backupLogPath, dir,))
2017-12-09 22:30:10 +05:00
p.start()
2018-02-20 22:39:07 +05:00
pid = open(backupDir + '/pid', "w")
2017-12-09 22:30:10 +05:00
pid.write(str(p.pid))
pid.close()
return
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-02-20 22:39:07 +05:00
backupLogPath = backupDir + "/backup_log"
2017-12-09 22:30:10 +05:00
writeToFile = open(backupLogPath, "w+")
writeToFile.writelines(str(msg) + " [5010]" + "\n")
writeToFile.close()
2018-02-20 22:39:07 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [remoteRestore]")
2017-12-09 22:30:10 +05:00
return [0, msg]
2018-02-20 22:39:07 +05:00
@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(
"%m.%d.%Y_%H-%M-%S") + "]" + " Starting restore for: " + backup + ".\n")
2018-02-20 22:39:07 +05:00
writeToFile.close()
2019-03-31 02:47:35 +05:00
backupFile = backup
2019-12-10 23:04:24 +05:00
execPath = "sudo nice -n 10 /usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/backupUtilities.py"
2019-03-31 02:47:35 +05:00
execPath = execPath + " submitRestore --backupFile " + backupFile + " --dir " + dir
subprocess.Popen(shlex.split(execPath))
time.sleep(4)
2018-02-20 22:39:07 +05:00
2019-03-31 02:47:35 +05:00
while (1):
time.sleep(1)
2018-02-20 22:39:07 +05:00
2019-03-31 02:47:35 +05:00
backupFile = backup.strip(".tar.gz")
path = "/home/backup/transfer-" + str(dir) + "/" + backupFile
status = open(path + "/status", 'r').read()
if status.find("Done") > -1:
command = "sudo rm -rf " + path
ProcessUtilities.normalExecutioner(command)
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " Restore Completed for: " + backup + ".\n")
2019-03-31 02:47:35 +05:00
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " #########################################\n")
2019-03-31 02:47:35 +05:00
writeToFile.close()
break
elif status.find("[5009]") > -1:
## removing temporarily generated files while restoring
command = "sudo rm -rf " + path
ProcessUtilities.normalExecutioner(command)
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " Restore aborted for: " + backup + ". Error message: " +
2019-03-31 02:47:35 +05:00
status + "\n")
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " #########################################\n")
2019-03-31 02:47:35 +05:00
writeToFile.close()
break
else:
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " Waiting for restore to complete.\n")
2019-03-31 02:47:35 +05:00
writeToFile.close()
time.sleep(3)
pass
2018-02-20 22:39:07 +05:00
writeToFile = open(backupLogPath, "a")
writeToFile.writelines("\n")
writeToFile.writelines("\n")
writeToFile.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "]" + " Backup Restore complete\n")
2018-02-20 22:39:07 +05:00
writeToFile.writelines("completed[success]")
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-09-05 19:06:06 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [remoteTransferUtilities.startRestore]")
2018-02-20 22:39:07 +05:00
2017-12-09 22:30:10 +05:00
def main():
parser = argparse.ArgumentParser(description='CyberPanel Installer')
2018-02-20 22:39:07 +05:00
parser.add_argument('function', help='Specific function to call!')
2017-12-09 22:30:10 +05:00
parser.add_argument('--pathToKey', help='')
## remote transfer arguments
parser.add_argument('--ipAddress', help='')
parser.add_argument('--dir', help='')
parser.add_argument('--accountsToTransfer', help='')
## remote backup restore arguments
parser.add_argument('--backupDirComplete', help='')
parser.add_argument('--backupDir', help='')
args = parser.parse_args()
if args.function == "writeAuthKey":
remoteTransferUtilities.writeAuthKey(args.pathToKey)
elif args.function == "remoteTransfer":
remoteTransferUtilities.remoteTransfer(args.ipAddress,args.dir,args.accountsToTransfer)
elif args.function == "remoteBackupRestore":
remoteTransferUtilities.remoteBackupRestore(args.backupDirComplete,args.backupDir)
if __name__ == "__main__":
main()