2017-10-24 19:16:36 +05:00
|
|
|
import pexpect
|
|
|
|
|
import CyberCPLogFileWriter as logging
|
|
|
|
|
import subprocess
|
|
|
|
|
import shlex
|
|
|
|
|
from shutil import make_archive,rmtree
|
|
|
|
|
import os
|
|
|
|
|
import mysqlUtilities
|
|
|
|
|
import tarfile
|
|
|
|
|
from multiprocessing import Process
|
|
|
|
|
import json
|
|
|
|
|
import requests
|
2017-10-29 22:16:06 +05:00
|
|
|
import signal
|
2017-11-05 03:02:51 +05:00
|
|
|
from installUtilities import installUtilities
|
2017-12-09 22:30:10 +05:00
|
|
|
import argparse
|
|
|
|
|
from shutil import move,copy
|
|
|
|
|
import sys
|
2018-02-16 00:57:46 +05:00
|
|
|
from xml.etree import ElementTree
|
2018-02-19 23:42:57 +05:00
|
|
|
import time
|
2017-12-09 22:30:10 +05:00
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-02-16 21:05:15 +05:00
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
class backupUtilities:
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
completeKeyPath = "/home/cyberpanel/.ssh"
|
2017-12-09 22:30:10 +05:00
|
|
|
destinationsPath = "/home/cyberpanel/destinations"
|
2017-11-05 03:02:51 +05:00
|
|
|
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
@staticmethod
|
|
|
|
|
def startBackup(tempStoragePath,backupName,backupPath):
|
|
|
|
|
try:
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
## writing the name of backup file
|
|
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
## /home/example.com/backup/backupFileName
|
|
|
|
|
backupFileNamePath = os.path.join(backupPath,"backupFileName")
|
2017-12-09 22:30:10 +05:00
|
|
|
status = open(backupFileNamePath, "w")
|
|
|
|
|
status.write(backupName)
|
|
|
|
|
status.close()
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
|
|
|
|
|
status = open(os.path.join(backupPath,'status'),"w")
|
2017-10-24 19:16:36 +05:00
|
|
|
status.write("Making archive of home directory\n")
|
|
|
|
|
status.close()
|
|
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
## Parsing XML Meta file!
|
2017-12-09 22:30:10 +05:00
|
|
|
|
2018-02-16 21:05:15 +05:00
|
|
|
## /home/example.com/backup/backup-example-06-50-03-Thu-Feb-2018 -- tempStoragePath
|
2018-02-16 00:57:46 +05:00
|
|
|
backupMetaData = ElementTree.parse(os.path.join(tempStoragePath,'meta.xml'))
|
2017-10-29 22:16:06 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
|
|
|
|
|
## Making archive of home directory
|
|
|
|
|
|
|
|
|
|
domainName = backupMetaData.find('masterDomain').text
|
2018-02-17 15:34:59 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
## /home/example.com/backup/backup-example-06-50-03-Thu-Feb-2018 -- tempStoragePath
|
2018-02-16 21:05:15 +05:00
|
|
|
## shutil.make_archive
|
2018-02-16 00:57:46 +05:00
|
|
|
make_archive(os.path.join(tempStoragePath,"public_html"), 'gztar', os.path.join("/home",domainName,"public_html"))
|
|
|
|
|
|
2018-02-17 15:34:59 +05:00
|
|
|
## backup email accounts
|
|
|
|
|
|
|
|
|
|
status = open(os.path.join(backupPath, 'status'), "w")
|
|
|
|
|
status.write("Backing up email accounts!\n")
|
|
|
|
|
status.close()
|
|
|
|
|
|
2018-02-21 20:10:39 +05:00
|
|
|
try:
|
|
|
|
|
make_archive(os.path.join(tempStoragePath,domainName),'gztar',os.path.join("/home","vmail",domainName))
|
|
|
|
|
except:
|
|
|
|
|
pass
|
2018-02-17 15:34:59 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
## Backing up databases
|
|
|
|
|
databases = backupMetaData.findall('Databases/database')
|
|
|
|
|
|
|
|
|
|
for database in databases:
|
|
|
|
|
dbName = database.find('dbName').text
|
|
|
|
|
|
|
|
|
|
status = open(os.path.join(backupPath,'status'), "w")
|
|
|
|
|
status.write("Backing up database: " + dbName)
|
|
|
|
|
status.close()
|
2018-02-16 21:05:15 +05:00
|
|
|
if mysqlUtilities.mysqlUtilities.createDatabaseBackup(dbName, tempStoragePath) == 0:
|
|
|
|
|
raise BaseException
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-02-16 21:05:15 +05:00
|
|
|
## shutil.make_archive, ## shutil.
|
2018-02-16 00:57:46 +05:00
|
|
|
make_archive(os.path.join(backupPath,backupName), 'gztar', tempStoragePath)
|
2017-10-24 19:16:36 +05:00
|
|
|
rmtree(tempStoragePath)
|
|
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
status = open(os.path.join(backupPath,'status'), "w")
|
2018-02-19 23:42:57 +05:00
|
|
|
status.write("Completed\n")
|
2017-10-24 19:16:36 +05:00
|
|
|
status.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
try:
|
2018-02-16 00:57:46 +05:00
|
|
|
os.remove(os.path.join(backupPath,backupName+".tar.gz"))
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startBackup]")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
rmtree(tempStoragePath)
|
|
|
|
|
except:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startBackup]")
|
|
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
status = open(os.path.join(backupPath,'status'), "w")
|
2017-10-24 19:16:36 +05:00
|
|
|
status.write(backupName + "\n")
|
2017-12-09 22:30:10 +05:00
|
|
|
status.write("Aborted, please check CyberPanel main log file. [5009]")
|
2017-10-24 19:16:36 +05:00
|
|
|
status.close()
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startBackup]")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def initiateBackup(tempStoragePath,backupName,backupPath):
|
|
|
|
|
try:
|
|
|
|
|
p = Process(target=backupUtilities.startBackup, args=(tempStoragePath,backupName,backupPath,))
|
|
|
|
|
p.start()
|
|
|
|
|
pid = open(backupPath + 'pid', "w")
|
|
|
|
|
pid.write(str(p.pid))
|
|
|
|
|
pid.close()
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [initiateBackup]")
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2017-10-26 23:50:59 +05:00
|
|
|
def startRestore(backupName, dir):
|
2017-10-24 19:16:36 +05:00
|
|
|
try:
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
if dir == "CyberPanelRestore":
|
2017-10-26 23:50:59 +05:00
|
|
|
backupFileName = backupName.strip(".tar.gz")
|
2018-02-16 00:57:46 +05:00
|
|
|
completPath = os.path.join("/home","backup",backupFileName) ## without extension
|
|
|
|
|
originalFile = os.path.join("/home","backup",backupName) ## with extension
|
2017-10-26 23:50:59 +05:00
|
|
|
else:
|
|
|
|
|
backupFileName = backupName.strip(".tar.gz")
|
|
|
|
|
completPath = "/home/backup/transfer-"+str(dir)+"/"+backupFileName ## without extension
|
|
|
|
|
originalFile = "/home/backup/transfer-"+str(dir)+"/"+backupName ## with extension
|
|
|
|
|
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
pathToCompressedHome = os.path.join(completPath,"public_html.tar.gz")
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
if not os.path.exists(completPath):
|
|
|
|
|
os.mkdir(completPath)
|
|
|
|
|
|
|
|
|
|
## writing pid of restore process
|
|
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
pid = open(os.path.join(completPath,'pid'), "w")
|
2017-10-24 19:16:36 +05:00
|
|
|
pid.write(str(os.getpid()))
|
|
|
|
|
pid.close()
|
|
|
|
|
|
2018-02-16 21:05:15 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
status = open(os.path.join(completPath,'status'), "w")
|
2018-02-19 23:42:57 +05:00
|
|
|
status.write("Extracting Main Archive!")
|
2017-10-24 19:16:36 +05:00
|
|
|
status.close()
|
|
|
|
|
|
2018-02-16 21:05:15 +05:00
|
|
|
## Converting /home/backup/backup-example-06-50-03-Thu-Feb-2018.tar.gz -> /home/backup/backup-example-06-50-03-Thu-Feb-2018
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
tar = tarfile.open(originalFile)
|
|
|
|
|
tar.extractall(completPath)
|
|
|
|
|
tar.close()
|
|
|
|
|
|
2018-02-16 21:05:15 +05:00
|
|
|
|
|
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
status = open(os.path.join(completPath,'status'), "w")
|
2018-02-19 23:42:57 +05:00
|
|
|
status.write("Creating Accounts,Databases and DNS records!")
|
2017-10-24 19:16:36 +05:00
|
|
|
status.close()
|
|
|
|
|
|
|
|
|
|
## creating website and its dabases
|
|
|
|
|
|
|
|
|
|
try:
|
2017-10-26 23:50:59 +05:00
|
|
|
finalData = json.dumps({'backupFile': backupName,"dir":dir})
|
2017-10-29 22:16:06 +05:00
|
|
|
r = requests.post("http://localhost:5003/websites/CreateWebsiteFromBackup", data=finalData,verify=False)
|
2017-10-24 19:16:36 +05:00
|
|
|
data = json.loads(r.text)
|
|
|
|
|
|
|
|
|
|
if data['createWebSiteStatus'] == 1:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
2018-02-16 00:57:46 +05:00
|
|
|
status = open(os.path.join(completPath,'status'), "w")
|
2018-02-19 23:42:57 +05:00
|
|
|
status.write("Error Message: " + data['error_message'] +". Not able to create Account, Databasesand DNS Records, aborting. [5009]")
|
2017-10-24 19:16:36 +05:00
|
|
|
status.close()
|
|
|
|
|
return 0
|
|
|
|
|
except BaseException,msg:
|
2018-02-16 00:57:46 +05:00
|
|
|
status = open(os.path.join(completPath,'status'), "w")
|
2018-02-19 23:42:57 +05:00
|
|
|
status.write("Error Message: " + str(msg) +". Not able to create Account, Databasesand DNS Records, aborting. [5009]")
|
2017-10-24 19:16:36 +05:00
|
|
|
status.close()
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startRestore]")
|
|
|
|
|
return 0
|
|
|
|
|
|
2018-02-16 21:05:15 +05:00
|
|
|
########### creating child/sub/addon/parked domains
|
2017-10-29 22:16:06 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
status = open(os.path.join(completPath,'status'), "w")
|
2018-02-16 21:05:15 +05:00
|
|
|
status.write("Creating Child Domains!")
|
2017-10-29 22:16:06 +05:00
|
|
|
status.close()
|
|
|
|
|
|
|
|
|
|
## reading meta file to create subdomains
|
|
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
backupMetaData = ElementTree.parse(os.path.join(completPath,"meta.xml"))
|
2017-10-29 22:16:06 +05:00
|
|
|
|
|
|
|
|
## extracting master domain for later use
|
2018-02-16 00:57:46 +05:00
|
|
|
masterDomain = backupMetaData.find('masterDomain').text
|
|
|
|
|
externalApp = backupMetaData.find('externalApp').text
|
|
|
|
|
websiteHome = os.path.join("/home",masterDomain,"public_html")
|
2017-10-29 22:16:06 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
childDomains = backupMetaData.findall('ChildDomains/domain')
|
2017-10-29 22:16:06 +05:00
|
|
|
|
|
|
|
|
try:
|
2018-02-16 00:57:46 +05:00
|
|
|
for childDomain in childDomains:
|
2017-10-29 22:16:06 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
domain = childDomain.find('domain').text
|
|
|
|
|
phpSelection = childDomain.find('phpSelection').text
|
|
|
|
|
path = childDomain.find('path').text
|
2017-10-29 22:16:06 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
finalData = json.dumps(
|
|
|
|
|
{'masterDomain': masterDomain, 'domainName': domain, 'phpSelection': phpSelection,
|
|
|
|
|
'path': path,
|
|
|
|
|
'ssl': 0, 'restore': 1})
|
|
|
|
|
r = requests.post("http://localhost:5003/websites/submitDomainCreation", data=finalData,
|
|
|
|
|
verify=False)
|
2017-10-29 22:16:06 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
data = json.loads(r.text)
|
2017-10-29 22:16:06 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
if data['createWebSiteStatus'] == 1:
|
|
|
|
|
rmtree(websiteHome)
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
status = open(os.path.join(completPath,'status'), "w")
|
|
|
|
|
status.write("Error Message: " + data[
|
|
|
|
|
'error_message'] + ". Not able to create child domains, aborting. [5009]")
|
|
|
|
|
status.close()
|
|
|
|
|
return 0
|
2017-10-29 22:16:06 +05:00
|
|
|
|
|
|
|
|
except BaseException, msg:
|
2018-02-16 00:57:46 +05:00
|
|
|
status = open(os.path.join(completPath,'status'), "w")
|
2017-12-09 22:30:10 +05:00
|
|
|
status.write("Error Message: " + str(msg) +". Not able to create child domains, aborting. [5009]")
|
2017-10-29 22:16:06 +05:00
|
|
|
status.close()
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startRestore]")
|
|
|
|
|
return 0
|
|
|
|
|
|
2018-02-17 15:34:59 +05:00
|
|
|
## Restoring email accounts
|
|
|
|
|
|
|
|
|
|
status = open(os.path.join(completPath, 'status'), "w")
|
|
|
|
|
status.write("Restoring email accounts!")
|
|
|
|
|
status.close()
|
|
|
|
|
|
|
|
|
|
emailAccounts = backupMetaData.findall('emails/emailAccount')
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
for emailAccount in emailAccounts:
|
|
|
|
|
|
|
|
|
|
email = emailAccount.find('email').text
|
|
|
|
|
username = email.split("@")[0]
|
|
|
|
|
password = emailAccount.find('password').text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
finalData = json.dumps({'domain': masterDomain, 'username': username, 'password': password})
|
|
|
|
|
|
|
|
|
|
r = requests.post("http://localhost:5003/email/submitEmailCreation", data=finalData,verify=False)
|
|
|
|
|
|
|
|
|
|
data = json.loads(r.text)
|
|
|
|
|
|
|
|
|
|
if data['createEmailStatus'] == 1:
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
status = open(os.path.join(completPath,'status'), "w")
|
|
|
|
|
status.write("Error Message: " + data[
|
|
|
|
|
'error_message'] + ". Not able to create email accounts, aborting. [5009]")
|
|
|
|
|
status.close()
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
status = open(os.path.join(completPath,'status'), "w")
|
|
|
|
|
status.write("Error Message: " + str(msg) +". Not able to create email accounts, aborting. [5009]")
|
|
|
|
|
status.close()
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startRestore]")
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
## Emails restored
|
|
|
|
|
|
2017-10-29 22:16:06 +05:00
|
|
|
## restoring databases
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
status = open(os.path.join(completPath,'status'), "w")
|
2017-10-24 19:16:36 +05:00
|
|
|
status.write("Restoring Databases")
|
|
|
|
|
status.close()
|
|
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
databases = backupMetaData.findall('Databases/database')
|
|
|
|
|
|
|
|
|
|
for database in databases:
|
|
|
|
|
dbName = database.find('dbName').text
|
|
|
|
|
password = database.find('password').text
|
2018-02-16 21:05:15 +05:00
|
|
|
if mysqlUtilities.mysqlUtilities.restoreDatabaseBackup(dbName, completPath, password) == 0:
|
|
|
|
|
raise BaseException
|
2017-10-29 22:16:06 +05:00
|
|
|
|
2018-02-17 15:34:59 +05:00
|
|
|
## Databases restored
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
status = open(os.path.join(completPath, 'status'), "w")
|
2018-02-17 15:34:59 +05:00
|
|
|
status.write("Extracting web home data!")
|
2017-10-24 19:16:36 +05:00
|
|
|
status.close()
|
|
|
|
|
|
2018-02-17 15:34:59 +05:00
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
tar = tarfile.open(pathToCompressedHome)
|
|
|
|
|
tar.extractall(websiteHome)
|
|
|
|
|
tar.close()
|
|
|
|
|
|
2018-02-17 15:34:59 +05:00
|
|
|
## extracting email accounts
|
|
|
|
|
|
|
|
|
|
status = open(os.path.join(completPath, 'status'), "w")
|
|
|
|
|
status.write("Extracting email accounts!")
|
|
|
|
|
status.close()
|
|
|
|
|
|
2018-02-21 20:10:39 +05:00
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
pathToCompressedEmails = os.path.join(completPath, masterDomain + ".tar.gz")
|
|
|
|
|
emailHome = os.path.join("/home","vmail",masterDomain)
|
2018-02-17 15:34:59 +05:00
|
|
|
|
2018-02-21 20:10:39 +05:00
|
|
|
tar = tarfile.open(pathToCompressedEmails)
|
|
|
|
|
tar.extractall(emailHome)
|
|
|
|
|
tar.close()
|
|
|
|
|
except:
|
|
|
|
|
pass
|
2018-02-17 15:34:59 +05:00
|
|
|
|
|
|
|
|
## emails extracted
|
|
|
|
|
|
2018-02-19 23:42:57 +05:00
|
|
|
## Change permissions
|
2018-02-17 15:34:59 +05:00
|
|
|
|
|
|
|
|
command = "chmod -r vmail:vmail " + emailHome
|
|
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
|
2018-02-19 23:42:57 +05:00
|
|
|
##
|
|
|
|
|
|
2018-02-16 00:57:46 +05:00
|
|
|
status = open(os.path.join(completPath,'status'), "w")
|
2017-10-24 19:16:36 +05:00
|
|
|
status.write("Done")
|
|
|
|
|
status.close()
|
2018-02-19 23:42:57 +05:00
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
installUtilities.reStartLiteSpeed()
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
command = "chown -R " + externalApp + ":" + externalApp + " " + websiteHome
|
2017-11-05 03:02:51 +05:00
|
|
|
cmd = shlex.split(command)
|
|
|
|
|
subprocess.call(cmd)
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
except BaseException, msg:
|
2018-02-16 00:57:46 +05:00
|
|
|
status = open(os.path.join(completPath,'status'), "w")
|
2017-12-09 22:30:10 +05:00
|
|
|
status.write(str(msg) + " [5009]")
|
|
|
|
|
status.close()
|
2017-10-24 19:16:36 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startRestore]")
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2017-10-26 23:50:59 +05:00
|
|
|
def initiateRestore(backupName,dir):
|
2017-10-24 19:16:36 +05:00
|
|
|
try:
|
2017-10-26 23:50:59 +05:00
|
|
|
p = Process(target=backupUtilities.startRestore, args=(backupName, dir,))
|
2017-10-24 19:16:36 +05:00
|
|
|
p.start()
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [initiateRestore]")
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2017-11-05 21:07:12 +05:00
|
|
|
def sendKey(IPAddress, password,port):
|
2017-10-24 19:16:36 +05:00
|
|
|
try:
|
|
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
command = "sudo scp -o StrictHostKeyChecking=no -P "+ port +" /root/.ssh/cyberpanel.pub root@" + IPAddress + ":/root/.ssh/authorized_keys"
|
|
|
|
|
|
|
|
|
|
sendKeyProc = pexpect.spawn(command,timeout=3)
|
2017-10-24 19:16:36 +05:00
|
|
|
sendKeyProc.expect("password:")
|
|
|
|
|
|
|
|
|
|
sendKeyProc.sendline(password)
|
|
|
|
|
sendKeyProc.expect("100%")
|
|
|
|
|
|
|
|
|
|
sendKeyProc.wait()
|
|
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
return [1, "None"]
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
except pexpect.TIMEOUT, msg:
|
2017-10-24 19:16:36 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
|
2017-11-05 21:07:12 +05:00
|
|
|
return [0, "TIMEOUT [sendKey]"]
|
|
|
|
|
except pexpect.EOF, msg:
|
2017-10-24 19:16:36 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
|
2017-11-05 21:07:12 +05:00
|
|
|
return [0, "EOF [sendKey]"]
|
2017-10-24 19:16:36 +05:00
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
|
2017-11-05 21:07:12 +05:00
|
|
|
return [0, str(msg) + " [sendKey]"]
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
@staticmethod
|
2017-11-05 21:07:12 +05:00
|
|
|
def setupSSHKeys(IPAddress, password,port):
|
2017-10-24 19:16:36 +05:00
|
|
|
try:
|
|
|
|
|
## Checking for host verification
|
|
|
|
|
|
|
|
|
|
backupUtilities.host_key_verification(IPAddress)
|
|
|
|
|
|
|
|
|
|
if backupUtilities.checkIfHostIsUp(IPAddress) == 1:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
2017-11-05 21:07:12 +05:00
|
|
|
return [0,"Host is Down."]
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
expectation = "password:"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
command = "ssh -o StrictHostKeyChecking=no -p "+ port +" root@"+IPAddress+" mkdir /root/.ssh"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
setupKeys = pexpect.spawn(command,timeout=3)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
setupKeys.expect(expectation)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
## on first login attempt send password
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
setupKeys.sendline(password)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
## if it again give you password, than provided password is wrong
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
expectation = []
|
|
|
|
|
expectation.append("please try again.")
|
|
|
|
|
expectation.append(pexpect.EOF)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
index = setupKeys.expect(expectation)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
if index == 0:
|
2018-02-19 23:42:57 +05:00
|
|
|
return [0,"Wrong Password!"]
|
2017-10-24 19:16:36 +05:00
|
|
|
elif index == 1:
|
2017-11-05 21:07:12 +05:00
|
|
|
setupKeys.wait()
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
sendKey = backupUtilities.sendKey(IPAddress,password,port)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
if sendKey[0] == 1:
|
|
|
|
|
return [1, "None"]
|
|
|
|
|
else:
|
|
|
|
|
return [0,sendKey[1]]
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
except pexpect.TIMEOUT, msg:
|
2017-11-05 21:07:12 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(setupKeys.before + " " + str(msg) + " [setupSSHKeys]")
|
|
|
|
|
return [0, str(msg) + " [TIMEOUT setupSSHKeys]"]
|
2017-10-24 19:16:36 +05:00
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [setupSSHKeys]")
|
2017-11-05 21:07:12 +05:00
|
|
|
return [0, str(msg) + " [setupSSHKeys]"]
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def checkIfHostIsUp(IPAddress):
|
|
|
|
|
try:
|
|
|
|
|
if subprocess.check_output(['ping', IPAddress, '-c 1']).find("0% packet loss") > -1:
|
|
|
|
|
return 1
|
|
|
|
|
else:
|
|
|
|
|
return 0
|
|
|
|
|
except BaseException, msg:
|
2017-10-29 22:16:06 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[checkIfHostIsUp]")
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def checkConnection(IPAddress):
|
|
|
|
|
try:
|
2017-11-05 21:07:12 +05:00
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
try:
|
|
|
|
|
destinations = backupUtilities.destinationsPath
|
|
|
|
|
data = open(destinations, 'r').readlines()
|
|
|
|
|
port = data[1].strip("\n")
|
|
|
|
|
except:
|
|
|
|
|
port = "22"
|
2017-11-05 21:07:12 +05:00
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
expectation = []
|
|
|
|
|
expectation.append("password:")
|
|
|
|
|
expectation.append("Last login")
|
|
|
|
|
expectation.append(pexpect.EOF)
|
|
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
checkConn = pexpect.spawn("sudo ssh -i /root/.ssh/cyberpanel -o StrictHostKeyChecking=no -p "+ port+" root@"+IPAddress, timeout=3)
|
2017-10-24 19:16:36 +05:00
|
|
|
index = checkConn.expect(expectation)
|
|
|
|
|
|
|
|
|
|
if index == 0:
|
|
|
|
|
subprocess.call(['kill', str(checkConn.pid)])
|
2017-11-02 02:09:47 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile("Remote Server is not able to authenticate for transfer to initiate, IP Address:" + IPAddress)
|
2017-10-29 22:16:06 +05:00
|
|
|
return [0,"Remote Server is not able to authenticate for transfer to initiate."]
|
2017-10-24 19:16:36 +05:00
|
|
|
elif index == 1:
|
|
|
|
|
subprocess.call(['kill', str(checkConn.pid)])
|
2017-10-29 22:16:06 +05:00
|
|
|
return [1, "None"]
|
2017-10-24 19:16:36 +05:00
|
|
|
else:
|
|
|
|
|
subprocess.call(['kill', str(checkConn.pid)])
|
2017-11-02 02:09:47 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(
|
|
|
|
|
"Remote Server is not able to authenticate for transfer to initiate, IP Address:" + IPAddress)
|
2017-11-05 03:02:51 +05:00
|
|
|
return [0, "Remote Server is not able to authenticate for transfer to initiate, IP Address:" + IPAddress]
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
except pexpect.TIMEOUT, msg:
|
2017-10-29 22:16:06 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile("Timeout "+IPAddress+ " [checkConnection]")
|
|
|
|
|
return [0, "371 Timeout while making connection to this server [checkConnection]"]
|
2017-10-24 19:16:36 +05:00
|
|
|
except pexpect.EOF, msg:
|
2017-10-29 22:16:06 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile("EOF "+IPAddress+ "[checkConnection]")
|
|
|
|
|
return [0, "374 Remote Server is not able to authenticate for transfer to initiate. [checkConnection]"]
|
2017-10-24 19:16:36 +05:00
|
|
|
except BaseException, msg:
|
2017-10-29 22:16:06 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg)+" " +IPAddress+ " [checkConnection]")
|
|
|
|
|
return [0, "377 Remote Server is not able to authenticate for transfer to initiate. [checkConnection]"]
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def verifyHostKey(IPAddress):
|
|
|
|
|
try:
|
2017-10-29 22:16:06 +05:00
|
|
|
backupUtilities.host_key_verification(IPAddress)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-10-29 22:16:06 +05:00
|
|
|
password = "hello" ## dumb password, not used anywhere.
|
|
|
|
|
|
|
|
|
|
expectation = []
|
|
|
|
|
|
|
|
|
|
expectation.append("continue connecting (yes/no)?")
|
|
|
|
|
expectation.append("password:")
|
|
|
|
|
|
2017-11-05 03:02:51 +05:00
|
|
|
setupSSHKeys = pexpect.spawn("ssh cyberpanel@" + IPAddress, timeout=3)
|
2017-10-29 22:16:06 +05:00
|
|
|
|
|
|
|
|
index = setupSSHKeys.expect(expectation)
|
|
|
|
|
|
|
|
|
|
if index == 0:
|
|
|
|
|
setupSSHKeys.sendline("yes")
|
|
|
|
|
|
|
|
|
|
setupSSHKeys.expect("password:")
|
|
|
|
|
setupSSHKeys.sendline(password)
|
|
|
|
|
|
|
|
|
|
expectation = []
|
|
|
|
|
|
|
|
|
|
expectation.append("password:")
|
|
|
|
|
expectation.append(pexpect.EOF)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
innerIndex = setupSSHKeys.expect(expectation)
|
|
|
|
|
|
|
|
|
|
if innerIndex == 0:
|
|
|
|
|
setupSSHKeys.kill(signal.SIGTERM)
|
|
|
|
|
return [1, "None"]
|
|
|
|
|
elif innerIndex == 1:
|
|
|
|
|
setupSSHKeys.kill(signal.SIGTERM)
|
|
|
|
|
return [1, "None"]
|
|
|
|
|
|
|
|
|
|
elif index == 1:
|
|
|
|
|
|
|
|
|
|
setupSSHKeys.expect("password:")
|
|
|
|
|
setupSSHKeys.sendline(password)
|
|
|
|
|
|
|
|
|
|
expectation = []
|
|
|
|
|
|
|
|
|
|
expectation.append("password:")
|
|
|
|
|
expectation.append(pexpect.EOF)
|
|
|
|
|
|
|
|
|
|
innerIndex = setupSSHKeys.expect(expectation)
|
|
|
|
|
|
|
|
|
|
if innerIndex == 0:
|
|
|
|
|
setupSSHKeys.kill(signal.SIGTERM)
|
|
|
|
|
return [1, "None"]
|
|
|
|
|
elif innerIndex == 1:
|
|
|
|
|
setupSSHKeys.kill(signal.SIGTERM)
|
|
|
|
|
return [1, "None"]
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
except pexpect.TIMEOUT, msg:
|
2017-10-26 23:50:59 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile("Timeout [verifyHostKey]")
|
2017-10-29 22:16:06 +05:00
|
|
|
return [0,"Timeout [verifyHostKey]"]
|
2017-10-24 19:16:36 +05:00
|
|
|
except pexpect.EOF, msg:
|
2017-10-26 23:50:59 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile("EOF [verifyHostKey]")
|
2017-10-29 22:16:06 +05:00
|
|
|
return [0,"EOF [verifyHostKey]"]
|
2017-10-24 19:16:36 +05:00
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [verifyHostKey]")
|
2017-10-29 22:16:06 +05:00
|
|
|
return [0,str(msg)+" [verifyHostKey]"]
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2017-11-05 21:07:12 +05:00
|
|
|
def createBackupDir(IPAddress,port):
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
try:
|
2018-02-19 23:42:57 +05:00
|
|
|
command = "sudo ssh -o StrictHostKeyChecking=no -p "+ port +" -i /root/.ssh/cyberpanel root@"+IPAddress+" mkdir /home/backup"
|
2017-10-24 19:16:36 +05:00
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [createBackupDir]")
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def host_key_verification(IPAddress):
|
|
|
|
|
try:
|
2017-11-05 03:02:51 +05:00
|
|
|
command = 'sudo ssh-keygen -R '+IPAddress
|
2017-10-24 19:16:36 +05:00
|
|
|
subprocess.call(shlex.split(command))
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [host_key_verification]")
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
2017-12-09 22:30:10 +05:00
|
|
|
def submitBackupCreation(tempStoragePath,backupName,backupPath,metaPath):
|
|
|
|
|
try:
|
2018-02-16 21:05:15 +05:00
|
|
|
## /home/example.com/backup/backup-example-06-50-03-Thu-Feb-2018 -- tempStoragePath
|
|
|
|
|
## backup-example-06-50-03-Thu-Feb-2018 -- backup name
|
|
|
|
|
## /home/example.com/backup - backupPath
|
|
|
|
|
## /home/cyberpanel/1047.xml - metaPath
|
2017-12-09 22:30:10 +05:00
|
|
|
|
|
|
|
|
if not os.path.exists(backupPath):
|
|
|
|
|
os.mkdir(backupPath)
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(tempStoragePath):
|
|
|
|
|
os.mkdir(tempStoragePath)
|
|
|
|
|
|
2018-02-16 21:05:15 +05:00
|
|
|
## Move meta file inside the temporary storage created to store backup data.
|
2017-12-09 22:30:10 +05:00
|
|
|
|
2018-02-16 21:05:15 +05:00
|
|
|
move(metaPath,os.path.join(tempStoragePath,"meta.xml"))
|
2017-12-09 22:30:10 +05:00
|
|
|
|
|
|
|
|
p = Process(target=backupUtilities.startBackup, args=(tempStoragePath, backupName, backupPath,))
|
|
|
|
|
p.start()
|
2018-02-19 23:42:57 +05:00
|
|
|
pid = open(os.path.join(backupPath,'pid'), "w")
|
2017-12-09 22:30:10 +05:00
|
|
|
pid.write(str(p.pid))
|
|
|
|
|
pid.close()
|
|
|
|
|
|
|
|
|
|
print "1,None"
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(
|
|
|
|
|
str(msg) + " [submitBackupCreation]")
|
|
|
|
|
print "0,"+str(msg)
|
|
|
|
|
|
|
|
|
|
def cancelBackupCreation(backupCancellationDomain,fileName):
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
path = "/home/" + backupCancellationDomain + "/backup/pid"
|
|
|
|
|
|
|
|
|
|
pid = open(path, "r").readlines()[0]
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
os.kill(int(pid), signal.SIGKILL)
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [cancelBackupCreation]")
|
|
|
|
|
|
|
|
|
|
backupPath = "/home/" + backupCancellationDomain + "/backup/"
|
|
|
|
|
|
|
|
|
|
tempStoragePath = backupPath + fileName
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
os.remove(tempStoragePath + ".tar.gz")
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [cancelBackupCreation]")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
rmtree(tempStoragePath)
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [cancelBackupCreation]")
|
|
|
|
|
|
|
|
|
|
status = open(backupPath + 'status', "w")
|
|
|
|
|
status.write("Aborted manually. [5009]")
|
|
|
|
|
status.close()
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(
|
|
|
|
|
str(msg) + " [cancelBackupCreation]")
|
|
|
|
|
print "0,"+str(msg)
|
|
|
|
|
|
|
|
|
|
def submitRestore(backupFile,dir):
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
p = Process(target=backupUtilities.startRestore, args=(backupFile, dir,))
|
|
|
|
|
p.start()
|
|
|
|
|
|
|
|
|
|
print "1,None"
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile(
|
|
|
|
|
str(msg) + " [cancelBackupCreation]")
|
|
|
|
|
print "0,"+str(msg)
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='CyberPanel Installer')
|
|
|
|
|
parser.add_argument('function', help='Specific a function to call!')
|
|
|
|
|
parser.add_argument('--tempStoragePath', help='')
|
|
|
|
|
parser.add_argument('--backupName', help='!')
|
|
|
|
|
parser.add_argument('--backupPath', help='')
|
|
|
|
|
parser.add_argument('--metaPath', help='')
|
|
|
|
|
|
|
|
|
|
## backup cancellation arguments
|
|
|
|
|
|
|
|
|
|
parser.add_argument('--backupCancellationDomain', help='')
|
|
|
|
|
parser.add_argument('--fileName', help='')
|
|
|
|
|
|
|
|
|
|
## backup restore arguments
|
|
|
|
|
|
|
|
|
|
parser.add_argument('--backupFile', help='')
|
|
|
|
|
parser.add_argument('--dir', help='')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
if args.function == "submitBackupCreation":
|
|
|
|
|
submitBackupCreation(args.tempStoragePath,args.backupName,args.backupPath,args.metaPath)
|
|
|
|
|
elif args.function == "cancelBackupCreation":
|
|
|
|
|
cancelBackupCreation(args.backupCancellationDomain,args.fileName)
|
|
|
|
|
elif args.function == "submitRestore":
|
|
|
|
|
submitRestore(args.backupFile,args.dir)
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-02-19 23:42:57 +05:00
|
|
|
main()
|