2017-10-24 19:16:36 +05:00
|
|
|
import thread
|
|
|
|
|
import pexpect
|
|
|
|
|
import CyberCPLogFileWriter as logging
|
|
|
|
|
import subprocess
|
|
|
|
|
import shlex
|
|
|
|
|
from shutil import rmtree
|
|
|
|
|
import os
|
|
|
|
|
import requests
|
|
|
|
|
import json
|
|
|
|
|
import time
|
|
|
|
|
from backupUtilities import backupUtilities
|
|
|
|
|
|
|
|
|
|
class backupSchedule:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2017-11-05 21:07:12 +05:00
|
|
|
def createBackup(virtualHost, ipAddress,writeToFile,port):
|
2017-10-24 19:16:36 +05:00
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
writeToFile.writelines("["+time.strftime("%I-%M-%S-%a-%b-%Y")+"]"+" Preparing to create backup for: "+virtualHost+"\n")
|
|
|
|
|
writeToFile.writelines("[" + time.strftime(
|
|
|
|
|
"%I-%M-%S-%a-%b-%Y") + "]" + " Backup started for: " + virtualHost + "\n")
|
|
|
|
|
|
|
|
|
|
finalData = json.dumps({'websiteToBeBacked': virtualHost})
|
|
|
|
|
r = requests.post("http://localhost:5003/backup/submitBackupCreation", data=finalData)
|
|
|
|
|
data = json.loads(r.text)
|
|
|
|
|
backupPath = data['tempStorage']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (1):
|
|
|
|
|
r = requests.post("http://localhost:5003/backup/backupStatus", data= finalData)
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
data = json.loads(r.text)
|
|
|
|
|
|
|
|
|
|
if data['status'] == 0:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
writeToFile.writelines("[" + time.strftime(
|
|
|
|
|
"%I-%M-%S-%a-%b-%Y") + "]" + " Backup created for:" + virtualHost + "\n")
|
|
|
|
|
|
|
|
|
|
writeToFile.writelines("[" + time.strftime(
|
|
|
|
|
"%I-%M-%S-%a-%b-%Y") + "]" + " Preparing to send backup for: " + virtualHost +" to "+ipAddress+ "\n")
|
|
|
|
|
writeToFile.flush()
|
|
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
backupSchedule.sendBackup(backupPath+".tar.gz", ipAddress,writeToFile,port)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
writeToFile.writelines("[" + time.strftime(
|
|
|
|
|
"%I-%M-%S-%a-%b-%Y") + "]" + " Backup for: " + virtualHost + " is sent to " + ipAddress + "\n")
|
|
|
|
|
|
|
|
|
|
writeToFile.writelines("\n")
|
|
|
|
|
writeToFile.writelines("\n")
|
|
|
|
|
|
|
|
|
|
writeToFile.writelines("#####################################")
|
|
|
|
|
|
|
|
|
|
writeToFile.writelines("\n")
|
|
|
|
|
writeToFile.writelines("\n")
|
|
|
|
|
|
|
|
|
|
except BaseException,msg:
|
2017-11-05 21:07:12 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [createBackup]")
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
@staticmethod
|
2017-11-05 21:07:12 +05:00
|
|
|
def sendBackup(backupPath, IPAddress, writeToFile,port):
|
2017-10-24 19:16:36 +05:00
|
|
|
try:
|
2017-11-05 21:07:12 +05:00
|
|
|
command = 'rsync -avz -e "ssh -i /root/.ssh/cyberpanel -o StrictHostKeyChecking=no -p '+port+'" ' + backupPath + ' root@' + IPAddress + ':/home/backup/' + time.strftime(
|
|
|
|
|
"%a-%b") + "/"
|
|
|
|
|
subprocess.call(shlex.split(command), stdout=writeToFile)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
except BaseException, msg:
|
2017-10-24 19:16:36 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startBackup]")
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def prepare():
|
|
|
|
|
try:
|
|
|
|
|
path = "/usr/local/CyberCP/backup/"
|
|
|
|
|
destinations = path + "destinations"
|
|
|
|
|
|
|
|
|
|
backupLogPath = "/usr/local/lscp/logs/backup_log."+time.strftime("%I-%M-%S-%a-%b-%Y")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
writeToFile = open(backupLogPath,"a")
|
|
|
|
|
|
|
|
|
|
writeToFile.writelines("#################################################\n")
|
|
|
|
|
writeToFile.writelines(" Backup log for: " +time.strftime("%I-%M-%S-%a-%b-%Y")+"\n")
|
|
|
|
|
writeToFile.writelines("#################################################\n")
|
|
|
|
|
|
|
|
|
|
writeToFile.writelines("\n")
|
|
|
|
|
writeToFile.writelines("\n")
|
|
|
|
|
|
2017-11-05 21:07:12 +05:00
|
|
|
data = open(destinations,'r').readlines()
|
|
|
|
|
ipAddress = data[0].strip("\n")
|
|
|
|
|
port = data[1].strip("\n")
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if backupUtilities.checkIfHostIsUp(ipAddress) == 1:
|
2017-11-05 21:07:12 +05:00
|
|
|
checkConn = backupUtilities.checkConnection(ipAddress)
|
|
|
|
|
if checkConn[0] == 0:
|
2017-10-24 19:16:36 +05:00
|
|
|
writeToFile.writelines("[" + time.strftime(
|
|
|
|
|
"%I-%M-%S-%a-%b-%Y") + "]" + " Connection to:" + ipAddress+" Failed, please resetup this destination from CyberPanel, aborting." + "\n")
|
|
|
|
|
return 0
|
|
|
|
|
else:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
writeToFile.writelines("[" + time.strftime(
|
2017-11-05 21:07:12 +05:00
|
|
|
"%I-%M-%S-%a-%b-%Y") + "]" + " Host: " + ipAddress + " is down, aborting." + "\n")
|
2017-10-24 19:16:36 +05:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
for virtualHost in os.listdir("/home"):
|
2017-11-05 21:07:12 +05:00
|
|
|
if virtualHost == "vmail" or virtualHost == "cyberpanel" or virtualHost =="backup":
|
|
|
|
|
continue
|
|
|
|
|
backupSchedule.createBackup(virtualHost,ipAddress,writeToFile,port)
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
except BaseException,msg:
|
2017-11-05 21:07:12 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [prepare]")
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
backupSchedule.prepare()
|