Files
CyberPanel/plogical/backupScheduleLocal.py

120 lines
4.8 KiB
Python
Raw Normal View History

2020-01-14 23:20:57 +05:00
#!/usr/local/CyberCP/bin/python
import os
import os.path
import sys
import django
sys.path.append('/usr/local/CyberCP')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
try:
django.setup()
except:
pass
2019-12-15 13:30:40 +05:00
from plogical import CyberCPLogFileWriter as logging
2017-10-24 19:16:36 +05:00
import os
import time
2019-12-15 13:30:40 +05:00
from plogical.backupSchedule import backupSchedule
from plogical.processUtilities import ProcessUtilities
from re import match,I,M
2019-12-14 16:23:47 +05:00
import signal
from datetime import datetime
2020-05-20 22:23:21 +05:00
from websiteFunctions.models import BackupJob, BackupJobLogs
2017-10-24 19:16:36 +05:00
class backupScheduleLocal:
localBackupPath = '/home/cyberpanel/localBackupPath'
runningPath = '/home/cyberpanel/localBackupPID'
2019-12-14 16:23:47 +05:00
now = datetime.now()
2017-10-24 19:16:36 +05:00
@staticmethod
def prepare():
try:
if os.path.exists(backupScheduleLocal.runningPath):
pid = open(backupScheduleLocal.runningPath, 'r').read()
2020-05-22 16:57:44 +05:00
print('\n\nLocal backup is already running with PID: %s. If you want to run again kindly kill the backup process: \n\n kill -9 %s.\n\n' % (pid, pid))
return 0
writeToFile = open(backupScheduleLocal.runningPath, 'w')
writeToFile.write(str(os.getpid()))
writeToFile.close()
backupRunTime = time.strftime("%m.%d.%Y_%H-%M-%S")
backupLogPath = "/usr/local/lscp/logs/local_backup_log." + backupRunTime
2017-10-24 19:16:36 +05:00
2020-05-21 23:21:36 +05:00
jobSuccessSites = 0
jobFailedSites = 0
backupSchedule.backupLog = BackupJob(logFile=backupLogPath, location=backupSchedule.LOCAL, jobSuccessSites=jobSuccessSites, jobFailedSites=jobFailedSites)
backupSchedule.backupLog.save()
2017-10-24 19:16:36 +05:00
writeToFile = open(backupLogPath, "a")
2018-08-18 03:56:25 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "#################################################")
backupSchedule.remoteBackupLogging(backupLogPath," Local Backup log for: " + backupRunTime)
2018-08-18 03:56:25 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "#################################################\n")
2017-10-24 19:16:36 +05:00
2018-08-18 03:56:25 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "")
backupSchedule.remoteBackupLogging(backupLogPath, "")
2017-10-24 19:16:36 +05:00
for virtualHost in os.listdir("/home"):
2020-02-27 16:51:17 +05:00
if match(r'^[a-zA-Z0-9-]*[a-zA-Z0-9-]{0,61}[a-zA-Z0-9-](?:\.[a-zA-Z0-9-]{2,})+$', virtualHost, M | I):
try:
retValues = backupSchedule.createLocalBackup(virtualHost, backupLogPath)
2020-05-21 23:21:36 +05:00
if retValues[0] == 0:
continue
if os.path.exists(backupScheduleLocal.localBackupPath):
backupPath = retValues[1] + ".tar.gz"
localBackupPath = '%s/%s' % (open(backupScheduleLocal.localBackupPath, 'r').read().rstrip('/'), backupRunTime)
command = 'mkdir -p %s' % (localBackupPath)
ProcessUtilities.normalExecutioner(command)
command = 'mv %s %s' % (backupPath, localBackupPath)
ProcessUtilities.normalExecutioner(command)
2020-05-21 23:21:36 +05:00
jobSuccessSites = jobSuccessSites + 1
2020-01-14 23:14:23 +05:00
except BaseException as msg:
2020-05-21 23:21:36 +05:00
jobFailedSites = jobFailedSites + 1
backupSchedule.remoteBackupLogging(backupLogPath,
'[ERROR] Backup failed for %s, error: %s moving on..' % (virtualHost, str(msg)), backupSchedule.ERROR)
2018-08-18 03:56:25 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "")
backupSchedule.remoteBackupLogging(backupLogPath, "")
backupSchedule.remoteBackupLogging(backupLogPath, "#################################################")
backupSchedule.remoteBackupLogging(backupLogPath, "")
backupSchedule.remoteBackupLogging(backupLogPath, "")
2017-10-24 19:16:36 +05:00
2018-08-18 03:56:25 +05:00
backupSchedule.remoteBackupLogging(backupLogPath, "Local backup job completed.\n")
2017-10-24 19:16:36 +05:00
writeToFile.close()
2020-05-21 23:21:36 +05:00
job = BackupJob.objects.get(logFile=backupLogPath)
job.jobFailedSites = jobFailedSites
job.jobSuccessSites = jobSuccessSites
job.save()
if os.path.exists(backupScheduleLocal.runningPath):
os.remove(backupScheduleLocal.runningPath)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2019-07-18 14:08:00 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [214:startBackup]")
2017-10-24 19:16:36 +05:00
2018-08-18 15:16:15 +05:00
def main():
backupScheduleLocal.prepare()
2017-10-24 19:16:36 +05:00
2018-08-18 15:16:15 +05:00
2019-12-14 16:23:47 +05:00
def handler(signum, frame):
diff = datetime.now() - backupScheduleLocal.now
logging.CyberCPLogFileWriter.writeToFile('Signal: %s, time spent: %s' % (str(signum), str(diff.total_seconds())))
2018-08-18 15:16:15 +05:00
if __name__ == "__main__":
2019-12-14 16:23:47 +05:00
for i in range(1,32):
if i == 9 or i == 19 or i == 32:
continue
signal.signal(i, handler)
2018-08-18 15:16:15 +05:00
main()