mirror of
https://github.com/usmannasir/cyberpanel.git
synced 2025-11-07 22:06:05 +01:00
complete local scheduled backups
This commit is contained in:
@@ -27,7 +27,7 @@ import google.oauth2.credentials
|
|||||||
import googleapiclient.discovery
|
import googleapiclient.discovery
|
||||||
from googleapiclient.discovery import build
|
from googleapiclient.discovery import build
|
||||||
from websiteFunctions.models import NormalBackupDests, NormalBackupJobs, NormalBackupSites
|
from websiteFunctions.models import NormalBackupDests, NormalBackupJobs, NormalBackupSites
|
||||||
|
from plogical.IncScheduler import IncScheduler
|
||||||
|
|
||||||
class BackupManager:
|
class BackupManager:
|
||||||
localBackupPath = '/home/cyberpanel/localBackupPath'
|
localBackupPath = '/home/cyberpanel/localBackupPath'
|
||||||
@@ -1512,22 +1512,22 @@ class BackupManager:
|
|||||||
config = json.loads(nbd.config)
|
config = json.loads(nbd.config)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
lastRun = config['lastRun']
|
lastRun = config[IncScheduler.lastRun]
|
||||||
except:
|
except:
|
||||||
lastRun = 'Never'
|
lastRun = 'Never'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
allSites = config['allSites']
|
allSites = config[IncScheduler.allSites]
|
||||||
except:
|
except:
|
||||||
allSites = 'Selected Only'
|
allSites = 'Selected Only'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
frequency = config['frequency']
|
frequency = config[IncScheduler.frequency]
|
||||||
except:
|
except:
|
||||||
frequency = 'Never'
|
frequency = 'Never'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
currentStatus = config['currentStatus']
|
currentStatus = config[IncScheduler.currentStatus]
|
||||||
except:
|
except:
|
||||||
currentStatus = 'Not running'
|
currentStatus = 'Not running'
|
||||||
|
|
||||||
@@ -1574,7 +1574,6 @@ class BackupManager:
|
|||||||
json_data = json.dumps(data_ret)
|
json_data = json.dumps(data_ret)
|
||||||
return HttpResponse(json_data)
|
return HttpResponse(json_data)
|
||||||
|
|
||||||
|
|
||||||
except BaseException as msg:
|
except BaseException as msg:
|
||||||
data_ret = {'status': 0, 'error_message': str(msg)}
|
data_ret = {'status': 0, 'error_message': str(msg)}
|
||||||
json_data = json.dumps(data_ret)
|
json_data = json.dumps(data_ret)
|
||||||
@@ -1600,8 +1599,8 @@ class BackupManager:
|
|||||||
config = json.loads(nbj.config)
|
config = json.loads(nbj.config)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if config['allSites'] == 'all':
|
if config[IncScheduler.allSites] == 'all':
|
||||||
config['allSites'] = 'Selected Only'
|
config[IncScheduler.allSites] = 'Selected Only'
|
||||||
nbj.config = json.dumps(config)
|
nbj.config = json.dumps(config)
|
||||||
nbj.save()
|
nbj.save()
|
||||||
data_ret = {'status': 1}
|
data_ret = {'status': 1}
|
||||||
@@ -1609,7 +1608,7 @@ class BackupManager:
|
|||||||
return HttpResponse(json_data)
|
return HttpResponse(json_data)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
config['allSites'] = type
|
config[IncScheduler.allSites] = type
|
||||||
nbj.config = json.dumps(config)
|
nbj.config = json.dumps(config)
|
||||||
nbj.save()
|
nbj.save()
|
||||||
|
|
||||||
@@ -1684,7 +1683,7 @@ class BackupManager:
|
|||||||
return ACLManager.loadErrorJson('scheduleStatus', 0)
|
return ACLManager.loadErrorJson('scheduleStatus', 0)
|
||||||
|
|
||||||
config = json.loads(nbj.config)
|
config = json.loads(nbj.config)
|
||||||
config['frequency'] = backupFrequency
|
config[IncScheduler.frequency] = backupFrequency
|
||||||
|
|
||||||
nbj.config = json.dumps(config)
|
nbj.config = json.dumps(config)
|
||||||
nbj.save()
|
nbj.save()
|
||||||
|
|||||||
@@ -126,7 +126,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div ng-hide="driveHidden" class="form-group">
|
<div ng-hide="driveHidden" class="form-group">
|
||||||
<div class="col-sm-12">
|
<div style="border: 1px solid green;" class="col-sm-12">
|
||||||
<table style="margin-top: 2%" class="table">
|
<table style="margin-top: 2%" class="table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -156,7 +156,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div ng-hide="driveHidden" class="form-group">
|
<div style="border: 1px solid green;" ng-hide="driveHidden" class="form-group">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div style="margin-left: 2%" class="col-sm-3">
|
<div style="margin-left: 2%" class="col-sm-3">
|
||||||
<button data-toggle="modal" data-target="#backupLogs" ng-hide="driveHidden"
|
<button data-toggle="modal" data-target="#backupLogs" ng-hide="driveHidden"
|
||||||
@@ -169,7 +169,7 @@
|
|||||||
<div class="modal-header">
|
<div class="modal-header">
|
||||||
<button type="button" class="close" data-dismiss="modal">×
|
<button type="button" class="close" data-dismiss="modal">×
|
||||||
</button>
|
</button>
|
||||||
<h4 class="modal-title">{% trans "Git Logs" %} <img
|
<h4 class="modal-title">{% trans "Backup logs" %} <img
|
||||||
ng-hide="cyberPanelLoading"
|
ng-hide="cyberPanelLoading"
|
||||||
src="{% static 'images/loading.gif' %}">
|
src="{% static 'images/loading.gif' %}">
|
||||||
</h4>
|
</h4>
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ from googleapiclient.discovery import build
|
|||||||
from googleapiclient.http import MediaFileUpload
|
from googleapiclient.http import MediaFileUpload
|
||||||
from plogical.backupSchedule import backupSchedule
|
from plogical.backupSchedule import backupSchedule
|
||||||
import requests
|
import requests
|
||||||
|
from websiteFunctions.models import NormalBackupJobs, NormalBackupSites, NormalBackupDests, NormalBackupJobLogs
|
||||||
try:
|
try:
|
||||||
from plogical.virtualHostUtilities import virtualHostUtilities
|
from plogical.virtualHostUtilities import virtualHostUtilities
|
||||||
from plogical.mailUtilities import mailUtilities
|
from plogical.mailUtilities import mailUtilities
|
||||||
@@ -29,6 +30,15 @@ class IncScheduler():
|
|||||||
logPath = '/home/cyberpanel/incbackuplogs'
|
logPath = '/home/cyberpanel/incbackuplogs'
|
||||||
gitFolder = '/home/cyberpanel/git'
|
gitFolder = '/home/cyberpanel/git'
|
||||||
|
|
||||||
|
timeFormat = time.strftime("%m.%d.%Y_%H-%M-%S")
|
||||||
|
|
||||||
|
### Normal scheduled backups constants
|
||||||
|
|
||||||
|
frequency = 'frequency'
|
||||||
|
allSites = 'allSites'
|
||||||
|
currentStatus = 'currentStatus'
|
||||||
|
lastRun = 'lastRun'
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def startBackup(type):
|
def startBackup(type):
|
||||||
try:
|
try:
|
||||||
@@ -332,6 +342,99 @@ class IncScheduler():
|
|||||||
GDriveJobLogs(owner=items, status=backupSchedule.ERROR,
|
GDriveJobLogs(owner=items, status=backupSchedule.ERROR,
|
||||||
message='[Completely] Job failed, Error message: %s.' % (str(msg))).save()
|
message='[Completely] Job failed, Error message: %s.' % (str(msg))).save()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def startNormalBackups(type):
|
||||||
|
from plogical.processUtilities import ProcessUtilities
|
||||||
|
from plogical.backupSchedule import backupSchedule
|
||||||
|
import socket
|
||||||
|
|
||||||
|
## SFTP Destination Config sample
|
||||||
|
## {"type": "SFTP", "ip": "ip", "username": "root", "port": "22", "path": "/home/backup"}
|
||||||
|
|
||||||
|
## Local Destination config sample
|
||||||
|
## {"type": "local", "path": "/home/backup"}
|
||||||
|
|
||||||
|
## Backup jobs config
|
||||||
|
|
||||||
|
## {"frequency": "Daily", "allSites": "Selected Only"}
|
||||||
|
## {"frequency": "Daily"}
|
||||||
|
|
||||||
|
|
||||||
|
for backupjob in NormalBackupJobs.objects.all():
|
||||||
|
jobConfig = json.loads(backupjob.config)
|
||||||
|
destinationConfig = json.loads(backupjob.owner.config)
|
||||||
|
|
||||||
|
currentTime = time.strftime("%m.%d.%Y_%H-%M-%S")
|
||||||
|
|
||||||
|
if destinationConfig['type'] == 'local':
|
||||||
|
finalPath = '%s/%s' % (destinationConfig['path'].rstrip('/'), currentTime)
|
||||||
|
command = 'mkdir %s' % (finalPath)
|
||||||
|
ProcessUtilities.executioner(command)
|
||||||
|
|
||||||
|
|
||||||
|
if jobConfig[IncScheduler.frequency] == type:
|
||||||
|
NormalBackupJobLogs.objects.filter(owner=backupjob).delete()
|
||||||
|
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
|
||||||
|
message='Starting %s backup on %s..' % (type, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
|
||||||
|
|
||||||
|
if jobConfig[IncScheduler.allSites] == 'all':
|
||||||
|
websites = Websites.objects.all()
|
||||||
|
|
||||||
|
else:
|
||||||
|
websites = backupjob.normalbackupsites_set.all()
|
||||||
|
|
||||||
|
|
||||||
|
for site in websites:
|
||||||
|
try:
|
||||||
|
domain = site.domain
|
||||||
|
except:
|
||||||
|
domain = site.domain.domain
|
||||||
|
|
||||||
|
retValues = backupSchedule.createLocalBackup(domain, '/dev/null')
|
||||||
|
|
||||||
|
if retValues[0] == 0:
|
||||||
|
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.ERROR,
|
||||||
|
message='Backup failed for %s on %s.' % (
|
||||||
|
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
|
||||||
|
|
||||||
|
SUBJECT = "Automatic backup failed for %s on %s." % (domain, currentTime)
|
||||||
|
adminEmailPath = '/home/cyberpanel/adminEmail'
|
||||||
|
adminEmail = open(adminEmailPath, 'r').read().rstrip('\n')
|
||||||
|
sender = 'root@%s' % (socket.gethostname())
|
||||||
|
TO = [adminEmail]
|
||||||
|
message = """\
|
||||||
|
From: %s
|
||||||
|
To: %s
|
||||||
|
Subject: %s
|
||||||
|
|
||||||
|
Automatic backup failed for %s on %s.
|
||||||
|
""" % (sender, ", ".join(TO), SUBJECT, domain, currentTime)
|
||||||
|
|
||||||
|
logging.SendEmail(sender, TO, message)
|
||||||
|
else:
|
||||||
|
backupPath = retValues[1] + ".tar.gz"
|
||||||
|
|
||||||
|
command = 'mv %s %s' % (backupPath, finalPath)
|
||||||
|
ProcessUtilities.executioner(command)
|
||||||
|
|
||||||
|
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
|
||||||
|
message='Backup completed for %s on %s.' % (
|
||||||
|
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user