mirror of
https://github.com/usmannasir/cyberpanel.git
synced 2025-11-07 05:45:59 +01:00
complete local scheduled backups
This commit is contained in:
@@ -27,7 +27,7 @@ import google.oauth2.credentials
|
||||
import googleapiclient.discovery
|
||||
from googleapiclient.discovery import build
|
||||
from websiteFunctions.models import NormalBackupDests, NormalBackupJobs, NormalBackupSites
|
||||
|
||||
from plogical.IncScheduler import IncScheduler
|
||||
|
||||
class BackupManager:
|
||||
localBackupPath = '/home/cyberpanel/localBackupPath'
|
||||
@@ -1512,22 +1512,22 @@ class BackupManager:
|
||||
config = json.loads(nbd.config)
|
||||
|
||||
try:
|
||||
lastRun = config['lastRun']
|
||||
lastRun = config[IncScheduler.lastRun]
|
||||
except:
|
||||
lastRun = 'Never'
|
||||
|
||||
try:
|
||||
allSites = config['allSites']
|
||||
allSites = config[IncScheduler.allSites]
|
||||
except:
|
||||
allSites = 'Selected Only'
|
||||
|
||||
try:
|
||||
frequency = config['frequency']
|
||||
frequency = config[IncScheduler.frequency]
|
||||
except:
|
||||
frequency = 'Never'
|
||||
|
||||
try:
|
||||
currentStatus = config['currentStatus']
|
||||
currentStatus = config[IncScheduler.currentStatus]
|
||||
except:
|
||||
currentStatus = 'Not running'
|
||||
|
||||
@@ -1574,7 +1574,6 @@ class BackupManager:
|
||||
json_data = json.dumps(data_ret)
|
||||
return HttpResponse(json_data)
|
||||
|
||||
|
||||
except BaseException as msg:
|
||||
data_ret = {'status': 0, 'error_message': str(msg)}
|
||||
json_data = json.dumps(data_ret)
|
||||
@@ -1600,8 +1599,8 @@ class BackupManager:
|
||||
config = json.loads(nbj.config)
|
||||
|
||||
try:
|
||||
if config['allSites'] == 'all':
|
||||
config['allSites'] = 'Selected Only'
|
||||
if config[IncScheduler.allSites] == 'all':
|
||||
config[IncScheduler.allSites] = 'Selected Only'
|
||||
nbj.config = json.dumps(config)
|
||||
nbj.save()
|
||||
data_ret = {'status': 1}
|
||||
@@ -1609,7 +1608,7 @@ class BackupManager:
|
||||
return HttpResponse(json_data)
|
||||
except:
|
||||
pass
|
||||
config['allSites'] = type
|
||||
config[IncScheduler.allSites] = type
|
||||
nbj.config = json.dumps(config)
|
||||
nbj.save()
|
||||
|
||||
@@ -1684,7 +1683,7 @@ class BackupManager:
|
||||
return ACLManager.loadErrorJson('scheduleStatus', 0)
|
||||
|
||||
config = json.loads(nbj.config)
|
||||
config['frequency'] = backupFrequency
|
||||
config[IncScheduler.frequency] = backupFrequency
|
||||
|
||||
nbj.config = json.dumps(config)
|
||||
nbj.save()
|
||||
|
||||
@@ -126,7 +126,7 @@
|
||||
</div>
|
||||
|
||||
<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">
|
||||
<thead>
|
||||
<tr>
|
||||
@@ -156,7 +156,7 @@
|
||||
</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 style="margin-left: 2%" class="col-sm-3">
|
||||
<button data-toggle="modal" data-target="#backupLogs" ng-hide="driveHidden"
|
||||
@@ -169,7 +169,7 @@
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal">×
|
||||
</button>
|
||||
<h4 class="modal-title">{% trans "Git Logs" %} <img
|
||||
<h4 class="modal-title">{% trans "Backup logs" %} <img
|
||||
ng-hide="cyberPanelLoading"
|
||||
src="{% static 'images/loading.gif' %}">
|
||||
</h4>
|
||||
|
||||
@@ -18,6 +18,7 @@ from googleapiclient.discovery import build
|
||||
from googleapiclient.http import MediaFileUpload
|
||||
from plogical.backupSchedule import backupSchedule
|
||||
import requests
|
||||
from websiteFunctions.models import NormalBackupJobs, NormalBackupSites, NormalBackupDests, NormalBackupJobLogs
|
||||
try:
|
||||
from plogical.virtualHostUtilities import virtualHostUtilities
|
||||
from plogical.mailUtilities import mailUtilities
|
||||
@@ -29,6 +30,15 @@ class IncScheduler():
|
||||
logPath = '/home/cyberpanel/incbackuplogs'
|
||||
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
|
||||
def startBackup(type):
|
||||
try:
|
||||
@@ -332,6 +342,99 @@ class IncScheduler():
|
||||
GDriveJobLogs(owner=items, status=backupSchedule.ERROR,
|
||||
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():
|
||||
|
||||
|
||||
Reference in New Issue
Block a user