| 
									
										
										
										
											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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             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: | 
					
						
							| 
									
										
										
										
											2018-02-19 23:42:57 +05:00
										 |  |  |             command = "sudo scp -P "+port+" -i /root/.ssh/cyberpanel " + backupPath + " root@"+IPAddress+":/home/backup/"+ time.strftime("%a-%b") + "/" | 
					
						
							| 
									
										
										
										
											2017-11-05 21:07:12 +05:00
										 |  |  |             subprocess.call(shlex.split(command), stdout=writeToFile) | 
					
						
							|  |  |  |         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") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             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: | 
					
						
							| 
									
										
										
										
											2018-02-19 23:42:57 +05:00
										 |  |  |                     ## Create backup dir on remote server | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     command = "sudo ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel root@" + ipAddress + " mkdir /home/backup/" + time.strftime("%a-%b") | 
					
						
							|  |  |  |                     subprocess.call(shlex.split(command)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 19:16:36 +05:00
										 |  |  |                     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() |