Skip to content
Permalink
Browse files
better logging and error reporting for inc backups
  • Loading branch information
usmannasir committed Jan 23, 2020
1 parent c1eed3a commit 8aa4b84c250b99c470002855f5805de36196336c
Showing with 66 additions and 35 deletions.
  1. +64 −31 IncBackups/IncBackupsControl.py
  2. +0 −2 IncBackups/views.py
  3. +1 −1 install/install.py
  4. +1 −1 plogical/upgrade.py
@@ -133,7 +133,11 @@ def awsFunction(self, fType, backupPath=None, snapshotID=None, bType=None):
command = 'export AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s && restic -r s3:s3.amazonaws.com/%s backup %s --password-file %s' % (
key, secret, self.website.domain, backupPath, self.passwordFile)
result = ProcessUtilities.outputExecutioner(command)
logging.statusWriter(self.statusPath, result, 1)

if result.find('saved') == -1:
logging.statusWriter(self.statusPath, '%s. [5009].' % (result), 1)
return 0

snapShotid = result.split(' ')[-2]
if bType == 'database':
newSnapshot = JobSnapshots(job=self.jobid,
@@ -172,7 +176,11 @@ def localFunction(self, backupPath, type, restore=None):
command = 'restic -r %s backup %s --password-file %s --exclude %s' % (
self.repoPath, backupPath, self.passwordFile, self.repoPath)
result = ProcessUtilities.outputExecutioner(command)
logging.statusWriter(self.statusPath, result, 1)

if result.find('saved') == -1:
logging.statusWriter(self.statusPath, '%s. [5009].' % (result), 1)
return 0

snapShotid = result.split(' ')[-2]

if type == 'database':
@@ -191,13 +199,20 @@ def localFunction(self, backupPath, type, restore=None):
result = ProcessUtilities.outputExecutioner(command)
logging.statusWriter(self.statusPath, result, 1)

return 1


def sftpFunction(self, backupPath, type, restore=None):
if restore == None:
remotePath = '/home/backup/%s' % (self.website.domain)
command = 'export PATH=${PATH}:/usr/bin && restic -r %s:%s backup %s --password-file %s --exclude %s' % (
self.backupDestinations, remotePath, backupPath, self.passwordFile, self.repoPath)
result = ProcessUtilities.outputExecutioner(command)
logging.statusWriter(self.statusPath, result, 1)

if result.find('saved') == -1:
logging.statusWriter(self.statusPath, '%s. [5009].' % (result), 1)
return 0

snapShotid = result.split(' ')[-2]

if type == 'database':
@@ -586,9 +601,11 @@ def backupData(self):
backupPath = '/home/%s' % (self.website.domain)

if self.backupDestinations == 'local':
self.localFunction(backupPath, 'data')
if self.localFunction(backupPath, 'data') == 0:
return 0
elif self.backupDestinations[:4] == 'sftp':
self.sftpFunction(backupPath, 'data')
if self.sftpFunction(backupPath, 'data') == 0:
return 0
else:
self.awsFunction('backup', backupPath, '', 'data')

@@ -676,20 +693,23 @@ def initiateRepo(self):
if self.backupDestinations == 'local':
command = 'restic init --repo %s --password-file %s' % (self.repoPath, self.passwordFile)
result = ProcessUtilities.outputExecutioner(command)
logging.statusWriter(self.statusPath, result, 1)
if result.find('config file already exists') == -1:
logging.statusWriter(self.statusPath, result, 1)

elif self.backupDestinations[:4] == 'sftp':
remotePath = '/home/backup/%s' % (self.website.domain)
command = 'export PATH=${PATH}:/usr/bin && restic init --repo %s:%s --password-file %s' % (
self.backupDestinations, remotePath, self.passwordFile)
result = ProcessUtilities.outputExecutioner(command)
logging.statusWriter(self.statusPath, result, 1)
if result.find('config file already exists') == -1:
logging.statusWriter(self.statusPath, result, 1)
else:
key, secret = self.getAWSData()
command = 'export AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s && restic -r s3:s3.amazonaws.com/%s init --password-file %s' % (
key, secret, self.website.domain, self.passwordFile)
result = ProcessUtilities.outputExecutioner(command)
logging.statusWriter(self.statusPath, result, 1)
if result.find('config file already exists') == -1:
logging.statusWriter(self.statusPath, result, 1)
return 1

logging.statusWriter(self.statusPath,
@@ -699,6 +719,23 @@ def initiateRepo(self):
logging.statusWriter(self.statusPath, '%s. [IncJobs.initiateRepo.47][5009]' % str(msg), 1)
return 0

def sendEmail(self, password):
SUBJECT = "Backup Repository password for %s" % (self.website.domain)
text = """Password: %s
This is password for your incremental backup repository, please save it in safe place as it will be required when you want to restore backup for this site on remote server.
""" % (password)

sender = 'cyberpanel@%s' % (self.website.domain)
TO = [self.website.adminEmail]
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (sender, ", ".join(TO), SUBJECT, text)
mailUtilities.SendEmail(sender, TO, message)

def createBackup(self):
self.statusPath = self.extraArgs['tempPath']
website = self.extraArgs['website']
@@ -708,12 +745,21 @@ def createBackup(self):
websiteSSLs = self.extraArgs['websiteSSLs']
websiteDatabases = self.extraArgs['websiteDatabases']

self.website = Websites.objects.get(domain=website)
### Checking if restic is installed before moving on

command = 'restic'
if ProcessUtilities.outputExecutioner(command).find('restic is a backup program which') == -1:
logging.statusWriter(self.statusPath, 'It seems restic is not installed, for incremental backups to work '
'restic must be installed. You can manually install restic using this '
'guide -> http://go.cyberpanel.net/restic. [5009]', 1)
return 0

## Restic check completed.

newJob = IncJob(website=self.website)
newJob.save()
self.website = Websites.objects.get(domain=website)

self.jobid = newJob
self.jobid = IncJob(website=self.website)
self.jobid.save()

self.passwordFile = '/home/%s/%s' % (self.website.domain, self.website.domain)

@@ -726,39 +772,26 @@ def createBackup(self):

command = 'chmod 600 %s' % (self.passwordFile)
ProcessUtilities.executioner(command)
SUBJECT = "Backup Repository password for %s" % (self.website.domain)
text = """Password: %s
This is password for your incremental backup repository, please save it in safe place as it will be required when you want to restore backup for this site on remote server.
""" % (password)

sender = 'cyberpanel@%s' % (self.website.domain)
TO = [self.website.adminEmail]
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (sender, ", ".join(TO), SUBJECT, text)
mailUtilities.SendEmail(sender, TO, message)
self.sendEmail(password)

if self.initiateRepo() == 0:
return
return 0

if self.prepareBackupMeta() == 0:
return
return 0

if websiteData:
if self.backupData() == 0:
return
return 0

if websiteDatabases:
if self.backupDatabases() == 0:
return
return 0

if websiteEmails:
if self.emailBackup() == 0:
return
return 0

self.metaBackup()

@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-


from django.shortcuts import render
from plogical.acl import ACLManager
from django.shortcuts import HttpResponse, redirect
@@ -392,7 +391,6 @@ def submitBackupCreation(request):
startJob = IncJobs('createBackup', extraArgs)
startJob.start()


time.sleep(2)

final_json = json.dumps({'status': 1, 'error_message': "None", 'tempPath': tempPath})
@@ -2133,7 +2133,7 @@ def installRestic(self):
writeToFile = open(cronTab, 'a')
writeToFile.writelines(cronJob)

cronJob = '0 0 * * 0 root /usr/local/CyberCP/bin/python /usr/local/CyberCP/IncBackups/IncScheduler.py Daily\n'
cronJob = '0 0 * * 0 root /usr/local/CyberCP/bin/python /usr/local/CyberCP/IncBackups/IncScheduler.py Weekly\n'
writeToFile.writelines(cronJob)
writeToFile.close()
except:
@@ -1955,7 +1955,7 @@ def installRestic():
writeToFile = open(cronTab, 'a')
writeToFile.writelines(cronJob)

cronJob = '0 0 * * 0 root /usr/local/CyberPanel/bin/python /usr/local/CyberCP/IncBackups/IncScheduler.py Daily\n'
cronJob = '0 0 * * 0 root /usr/local/CyberPanel/bin/python /usr/local/CyberCP/IncBackups/IncScheduler.py Weekly\n'
writeToFile.writelines(cronJob)
writeToFile.close()

0 comments on commit 8aa4b84

Please sign in to comment.