Files
CyberPanel/plogical/backupSchedule.py

145 lines
5.6 KiB
Python
Raw Normal View History

2017-10-24 19:16:36 +05:00
import CyberCPLogFileWriter as logging
import subprocess
import shlex
import os
import requests
import json
import time
from backupUtilities import backupUtilities
from re import match,I,M
2017-10-24 19:16:36 +05:00
class backupSchedule:
2018-04-23 19:23:03 +05:00
@staticmethod
def remoteBackupLogging(fileName, message):
try:
file = open(fileName,'a')
file.writelines("[" + time.strftime("%I-%M-%S-%a-%b-%Y") + "] "+ message + "\n")
file.close()
except IOError,msg:
return "Can not write to error file."
2017-10-24 19:16:36 +05:00
@staticmethod
2018-04-23 19:23:03 +05:00
def createBackup(virtualHost, ipAddress, backupLogPath , port):
2017-10-24 19:16:36 +05:00
try:
backupSchedule.remoteBackupLogging(backupLogPath, "Preparing to create backup for: " + virtualHost)
2018-04-23 19:23:03 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "Backup started for: " + virtualHost)
2017-10-24 19:16:36 +05:00
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)
2018-02-19 23:42:57 +05:00
if data['backupStatus'] == 0:
break
elif data['abort'] == 1:
2017-10-24 19:16:36 +05:00
break
2018-04-23 19:23:03 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "Backup created for: " + virtualHost)
## Prepping to send backup.
backupSchedule.remoteBackupLogging(backupLogPath, "Preparing to send backup for: " + virtualHost +" to " + ipAddress)
backupSchedule.sendBackup(backupPath+".tar.gz", ipAddress, backupLogPath, port)
2017-10-24 19:16:36 +05:00
2018-04-23 19:23:03 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "Backup for: " + virtualHost + " is sent to " + ipAddress)
2017-10-24 19:16:36 +05:00
2018-04-23 19:23:03 +05:00
## Backup sent.
2017-10-24 19:16:36 +05:00
2018-04-23 19:23:03 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "")
backupSchedule.remoteBackupLogging(backupLogPath, "")
2017-10-24 19:16:36 +05:00
2018-04-23 19:23:03 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "#################################################")
2017-10-24 19:16:36 +05:00
2018-04-23 19:23:03 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "")
backupSchedule.remoteBackupLogging(backupLogPath, "")
2017-10-24 19:16:36 +05:00
except BaseException,msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [createBackup]")
2017-10-24 19:16:36 +05:00
@staticmethod
2018-04-23 19:23:03 +05:00
def sendBackup(backupPath, IPAddress, backupLogPath , port):
2017-10-24 19:16:36 +05:00
try:
2018-04-23 19:23:03 +05:00
## IPAddress of local server
ipFile = "/etc/cyberpanel/machineIP"
f = open(ipFile)
ipData = f.read()
ipAddressLocal = ipData.split('\n', 1)[0]
##
writeToFile = open(backupLogPath, "a")
command = "sudo scp -o StrictHostKeyChecking=no -P "+port+" -i /root/.ssh/cyberpanel " + backupPath + " root@"+IPAddress+":/home/backup/" + ipAddressLocal + "/" + time.strftime("%a-%b") + "/"
subprocess.call(shlex.split(command), stdout=writeToFile)
2018-07-29 01:20:46 +05:00
## Remove backups already sent to remote destinations
os.remove(backupPath)
except BaseException, msg:
2017-10-24 19:16:36 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [startBackup]")
@staticmethod
def prepare():
try:
2017-12-09 22:30:10 +05:00
destinations = backupUtilities.destinationsPath
2017-10-24 19:16:36 +05:00
backupLogPath = "/usr/local/lscp/logs/backup_log."+time.strftime("%I-%M-%S-%a-%b-%Y")
2018-04-23 19:23:03 +05:00
backupSchedule.remoteBackupLogging(backupLogPath,"#################################################")
backupSchedule.remoteBackupLogging(backupLogPath," Backup log for: " +time.strftime("%I-%M-%S-%a-%b-%Y"))
backupSchedule.remoteBackupLogging(backupLogPath,"#################################################\n")
2017-10-24 19:16:36 +05:00
2018-04-23 19:23:03 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "")
backupSchedule.remoteBackupLogging(backupLogPath, "")
2017-10-24 19:16:36 +05:00
2018-04-23 19:23:03 +05:00
## IP of Remote server.
2017-10-24 19:16:36 +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
2018-04-23 19:23:03 +05:00
## IPAddress of local server
ipFile = "/etc/cyberpanel/machineIP"
f = open(ipFile)
ipData = f.read()
ipAddressLocal = ipData.split('\n', 1)[0]
2017-10-24 19:16:36 +05:00
if backupUtilities.checkIfHostIsUp(ipAddress) == 1:
checkConn = backupUtilities.checkConnection(ipAddress)
if checkConn[0] == 0:
2018-04-23 19:23:03 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "Connection to: " + ipAddress+ " Failed, please resetup this destination from CyberPanel, aborting.")
2017-10-24 19:16:36 +05:00
return 0
else:
2018-02-19 23:42:57 +05:00
## Create backup dir on remote server
2018-04-23 19:23:03 +05:00
command = "sudo ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel root@" + ipAddress + " mkdir -p /home/backup/"+ ipAddressLocal + "/" + time.strftime("%a-%b")
2018-02-19 23:42:57 +05:00
subprocess.call(shlex.split(command))
2017-10-24 19:16:36 +05:00
pass
else:
2018-04-23 19:23:03 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "Host: " + ipAddress + " is down, aborting.")
2017-10-24 19:16:36 +05:00
return 0
for virtualHost in os.listdir("/home"):
if match(r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', virtualHost, M | I):
backupSchedule.createBackup(virtualHost, ipAddress, backupLogPath, port)
2017-10-24 19:16:36 +05:00
except BaseException,msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [prepare]")
2017-10-24 19:16:36 +05:00
backupSchedule.prepare()