Skip to content

Commit 4b1ebda

Browse files
committed
complete local scheduled backups
1 parent 2d4c0a5 commit 4b1ebda

File tree

3 files changed

+115
-13
lines changed

3 files changed

+115
-13
lines changed

backup/backupManager.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import googleapiclient.discovery
2828
from googleapiclient.discovery import build
2929
from websiteFunctions.models import NormalBackupDests, NormalBackupJobs, NormalBackupSites
30-
30+
from plogical.IncScheduler import IncScheduler
3131

3232
class BackupManager:
3333
localBackupPath = '/home/cyberpanel/localBackupPath'
@@ -1512,22 +1512,22 @@ def fetchgNormalSites(self, request=None, userID=None, data=None):
15121512
config = json.loads(nbd.config)
15131513

15141514
try:
1515-
lastRun = config['lastRun']
1515+
lastRun = config[IncScheduler.lastRun]
15161516
except:
15171517
lastRun = 'Never'
15181518

15191519
try:
1520-
allSites = config['allSites']
1520+
allSites = config[IncScheduler.allSites]
15211521
except:
15221522
allSites = 'Selected Only'
15231523

15241524
try:
1525-
frequency = config['frequency']
1525+
frequency = config[IncScheduler.frequency]
15261526
except:
15271527
frequency = 'Never'
15281528

15291529
try:
1530-
currentStatus = config['currentStatus']
1530+
currentStatus = config[IncScheduler.currentStatus]
15311531
except:
15321532
currentStatus = 'Not running'
15331533

@@ -1574,7 +1574,6 @@ def fetchNormalJobs(self, request=None, userID=None, data=None):
15741574
json_data = json.dumps(data_ret)
15751575
return HttpResponse(json_data)
15761576

1577-
15781577
except BaseException as msg:
15791578
data_ret = {'status': 0, 'error_message': str(msg)}
15801579
json_data = json.dumps(data_ret)
@@ -1600,16 +1599,16 @@ def addSiteNormal(self, request=None, userID=None, data=None):
16001599
config = json.loads(nbj.config)
16011600

16021601
try:
1603-
if config['allSites'] == 'all':
1604-
config['allSites'] = 'Selected Only'
1602+
if config[IncScheduler.allSites] == 'all':
1603+
config[IncScheduler.allSites] = 'Selected Only'
16051604
nbj.config = json.dumps(config)
16061605
nbj.save()
16071606
data_ret = {'status': 1}
16081607
json_data = json.dumps(data_ret)
16091608
return HttpResponse(json_data)
16101609
except:
16111610
pass
1612-
config['allSites'] = type
1611+
config[IncScheduler.allSites] = type
16131612
nbj.config = json.dumps(config)
16141613
nbj.save()
16151614

@@ -1684,7 +1683,7 @@ def changeAccountFrequencyNormal(self, request=None, userID=None, data=None):
16841683
return ACLManager.loadErrorJson('scheduleStatus', 0)
16851684

16861685
config = json.loads(nbj.config)
1687-
config['frequency'] = backupFrequency
1686+
config[IncScheduler.frequency] = backupFrequency
16881687

16891688
nbj.config = json.dumps(config)
16901689
nbj.save()

backup/templates/backup/backupSchedule.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ <h3 class="title-hero">
126126
</div>
127127

128128
<div ng-hide="driveHidden" class="form-group">
129-
<div class="col-sm-12">
129+
<div style="border: 1px solid green;" class="col-sm-12">
130130
<table style="margin-top: 2%" class="table">
131131
<thead>
132132
<tr>
@@ -156,7 +156,7 @@ <h3 class="title-hero">
156156
</div>
157157
</div>
158158

159-
<div ng-hide="driveHidden" class="form-group">
159+
<div style="border: 1px solid green;" ng-hide="driveHidden" class="form-group">
160160
<div class="row">
161161
<div style="margin-left: 2%" class="col-sm-3">
162162
<button data-toggle="modal" data-target="#backupLogs" ng-hide="driveHidden"
@@ -169,7 +169,7 @@ <h3 class="title-hero">
169169
<div class="modal-header">
170170
<button type="button" class="close" data-dismiss="modal">&times;
171171
</button>
172-
<h4 class="modal-title">{% trans "Git Logs" %} <img
172+
<h4 class="modal-title">{% trans "Backup logs" %} <img
173173
ng-hide="cyberPanelLoading"
174174
src="{% static 'images/loading.gif' %}">
175175
</h4>

plogical/IncScheduler.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from googleapiclient.http import MediaFileUpload
1919
from plogical.backupSchedule import backupSchedule
2020
import requests
21+
from websiteFunctions.models import NormalBackupJobs, NormalBackupSites, NormalBackupDests, NormalBackupJobLogs
2122
try:
2223
from plogical.virtualHostUtilities import virtualHostUtilities
2324
from plogical.mailUtilities import mailUtilities
@@ -29,6 +30,15 @@ class IncScheduler():
2930
logPath = '/home/cyberpanel/incbackuplogs'
3031
gitFolder = '/home/cyberpanel/git'
3132

33+
timeFormat = time.strftime("%m.%d.%Y_%H-%M-%S")
34+
35+
### Normal scheduled backups constants
36+
37+
frequency = 'frequency'
38+
allSites = 'allSites'
39+
currentStatus = 'currentStatus'
40+
lastRun = 'lastRun'
41+
3242
@staticmethod
3343
def startBackup(type):
3444
try:
@@ -332,6 +342,99 @@ def runGoogleDriveBackups(type):
332342
GDriveJobLogs(owner=items, status=backupSchedule.ERROR,
333343
message='[Completely] Job failed, Error message: %s.' % (str(msg))).save()
334344

345+
@staticmethod
346+
def startNormalBackups(type):
347+
from plogical.processUtilities import ProcessUtilities
348+
from plogical.backupSchedule import backupSchedule
349+
import socket
350+
351+
## SFTP Destination Config sample
352+
## {"type": "SFTP", "ip": "ip", "username": "root", "port": "22", "path": "/home/backup"}
353+
354+
## Local Destination config sample
355+
## {"type": "local", "path": "/home/backup"}
356+
357+
## Backup jobs config
358+
359+
## {"frequency": "Daily", "allSites": "Selected Only"}
360+
## {"frequency": "Daily"}
361+
362+
363+
for backupjob in NormalBackupJobs.objects.all():
364+
jobConfig = json.loads(backupjob.config)
365+
destinationConfig = json.loads(backupjob.owner.config)
366+
367+
currentTime = time.strftime("%m.%d.%Y_%H-%M-%S")
368+
369+
if destinationConfig['type'] == 'local':
370+
finalPath = '%s/%s' % (destinationConfig['path'].rstrip('/'), currentTime)
371+
command = 'mkdir %s' % (finalPath)
372+
ProcessUtilities.executioner(command)
373+
374+
375+
if jobConfig[IncScheduler.frequency] == type:
376+
NormalBackupJobLogs.objects.filter(owner=backupjob).delete()
377+
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
378+
message='Starting %s backup on %s..' % (type, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
379+
380+
if jobConfig[IncScheduler.allSites] == 'all':
381+
websites = Websites.objects.all()
382+
383+
else:
384+
websites = backupjob.normalbackupsites_set.all()
385+
386+
387+
for site in websites:
388+
try:
389+
domain = site.domain
390+
except:
391+
domain = site.domain.domain
392+
393+
retValues = backupSchedule.createLocalBackup(domain, '/dev/null')
394+
395+
if retValues[0] == 0:
396+
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.ERROR,
397+
message='Backup failed for %s on %s.' % (
398+
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
399+
400+
SUBJECT = "Automatic backup failed for %s on %s." % (domain, currentTime)
401+
adminEmailPath = '/home/cyberpanel/adminEmail'
402+
adminEmail = open(adminEmailPath, 'r').read().rstrip('\n')
403+
sender = 'root@%s' % (socket.gethostname())
404+
TO = [adminEmail]
405+
message = """\
406+
From: %s
407+
To: %s
408+
Subject: %s
409+
410+
Automatic backup failed for %s on %s.
411+
""" % (sender, ", ".join(TO), SUBJECT, domain, currentTime)
412+
413+
logging.SendEmail(sender, TO, message)
414+
else:
415+
backupPath = retValues[1] + ".tar.gz"
416+
417+
command = 'mv %s %s' % (backupPath, finalPath)
418+
ProcessUtilities.executioner(command)
419+
420+
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
421+
message='Backup completed for %s on %s.' % (
422+
domain, time.strftime("%m.%d.%Y_%H-%M-%S"))).save()
423+
424+
425+
426+
427+
428+
429+
430+
431+
432+
433+
434+
435+
436+
437+
335438

336439
def main():
337440

0 commit comments

Comments
 (0)