Skip to content

Commit

Permalink
use website level user for restic backups
Browse files Browse the repository at this point in the history
  • Loading branch information
usmannasir committed Nov 23, 2021
1 parent 8d3eac6 commit b6775c8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
64 changes: 37 additions & 27 deletions IncBackups/IncBackupsControl.py
Expand Up @@ -69,12 +69,12 @@ def getRemoteBackups(self):
path = '/home/backup/%s' % (self.website)
command = 'export RESTIC_PASSWORD=%s PATH=${PATH}:/usr/bin && restic -r %s:%s snapshots' % (
self.passwordFile, self.backupDestinations, path)
return ProcessUtilities.outputExecutioner(command).split('\n')
return ProcessUtilities.outputExecutioner(command, self.externalApp).split('\n')
else:
key, secret = self.getAWSData()
command = 'export RESTIC_PASSWORD=%s AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s && restic -r s3:s3.amazonaws.com/%s snapshots' % (
self.passwordFile, key, secret, self.website)
return ProcessUtilities.outputExecutioner(command).split('\n')
return ProcessUtilities.outputExecutioner(command, self.externalApp).split('\n')

def fetchCurrentBackups(self):
try:
Expand Down Expand Up @@ -152,7 +152,7 @@ def awsFunction(self, fType, backupPath=None, snapshotID=None, bType=None, delet
# If /home/%s/backup-exclude.conf file exists lets pass this to restic by appending the command to end.
if os.path.isfile(backupExcludesFile):
command = command + resticBackupExcludeCMD
result = ProcessUtilities.outputExecutioner(command)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)

if result.find('saved') == -1:
logging.statusWriter(self.statusPath, '%s. [5009].' % (result), 1)
Expand Down Expand Up @@ -181,7 +181,7 @@ def awsFunction(self, fType, backupPath=None, snapshotID=None, bType=None, delet
self.passwordFile,
key, secret, self.website, snapshotID, self.restoreTarget)

result = ProcessUtilities.outputExecutioner(command)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)

if result.find('restoring') == -1:
logging.statusWriter(self.statusPath, 'Failed: %s. [5009]' % (result), 1)
Expand All @@ -195,7 +195,7 @@ def awsFunction(self, fType, backupPath=None, snapshotID=None, bType=None, delet
command = 'export AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s && restic -r s3:s3.amazonaws.com/%s forget %s --password-file %s' % (
key, secret, self.website, snapshotID, self.passwordFile)

result = ProcessUtilities.outputExecutioner(command)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)

if result.find('removed snapshot') > -1 or result.find('deleted') > -1:
pass
Expand All @@ -206,7 +206,7 @@ def awsFunction(self, fType, backupPath=None, snapshotID=None, bType=None, delet
command = 'export AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s && restic -r s3:s3.amazonaws.com/%s prune --password-file %s' % (
key, secret, self.website, self.passwordFile)

ProcessUtilities.outputExecutioner(command)
ProcessUtilities.outputExecutioner(command, self.externalApp)
else:
self.backupDestinations = self.jobid.destination

Expand All @@ -215,15 +215,13 @@ def awsFunction(self, fType, backupPath=None, snapshotID=None, bType=None, delet
command = 'export AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s && restic -r s3:s3.amazonaws.com/%s restore %s --password-file %s --target %s' % (
key, secret, self.website, snapshotID, self.passwordFile, self.restoreTarget)

result = ProcessUtilities.outputExecutioner(command)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)

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

return 1


except BaseException as msg:
logging.statusWriter(self.statusPath, "%s [88][5009]" % (str(msg)), 1)
return 0
Expand All @@ -242,7 +240,7 @@ def localFunction(self, backupPath, type, restore=None, delete=None):
# If /home/%s/backup-exclude.conf file exists lets pass this to restic by appending the command to end.
if os.path.isfile(backupExcludesFile):
command = command + resticBackupExcludeCMD
result = ProcessUtilities.outputExecutioner(command)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)

if result.find('saved') == -1:
logging.statusWriter(self.statusPath, '%s. [5009].' % (result), 1)
Expand All @@ -265,7 +263,7 @@ def localFunction(self, backupPath, type, restore=None, delete=None):
repoLocation = '/home/%s/incbackup' % (self.website)

command = 'restic -r %s forget %s --password-file %s' % (repoLocation, self.jobid.snapshotid, self.passwordFile)
result = ProcessUtilities.outputExecutioner(command)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)

if result.find('removed snapshot') > -1 or result.find('deleted') > -1:
pass
Expand All @@ -274,15 +272,15 @@ def localFunction(self, backupPath, type, restore=None, delete=None):
return 0

command = 'restic -r %s prune --password-file %s' % (repoLocation, self.passwordFile)
ProcessUtilities.outputExecutioner(command)
ProcessUtilities.outputExecutioner(command, self.externalApp)

return 1
else:
repoLocation = '/home/%s/incbackup' % (self.website)
command = 'restic -r %s restore %s --target %s --password-file %s' % (
repoLocation, self.jobid.snapshotid, self.restoreTarget, self.passwordFile)

result = ProcessUtilities.outputExecutioner(command)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)

if result.find('restoring') == -1:
logging.statusWriter(self.statusPath, 'Failed: %s. [5009]' % (result), 1)
Expand All @@ -303,7 +301,7 @@ def sftpFunction(self, backupPath, type, restore=None, delete=None):
# If /home/%s/backup-exclude.conf file exists lets pass this to restic by appending the command to end.
if os.path.isfile(backupExcludesFile):
command = command + resticBackupExcludeCMD
result = ProcessUtilities.outputExecutioner(command)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)

if result.find('saved') == -1:
logging.statusWriter(self.statusPath, '%s. [5009].' % (result), 1)
Expand All @@ -325,7 +323,7 @@ def sftpFunction(self, backupPath, type, restore=None, delete=None):
repoLocation = '/home/backup/%s' % (self.website)
command = 'export PATH=${PATH}:/usr/bin && restic -r %s:%s forget %s --password-file %s' % (
self.jobid.destination, repoLocation, self.jobid.snapshotid, self.passwordFile)
result = ProcessUtilities.outputExecutioner(command)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)

if result.find('removed snapshot') > -1 or result.find('deleted') > -1:
pass
Expand All @@ -334,22 +332,22 @@ def sftpFunction(self, backupPath, type, restore=None, delete=None):
return 0

command = 'export PATH=${PATH}:/usr/bin && restic -r %s:%s prune --password-file %s' % (self.jobid.destination, repoLocation, self.passwordFile)
ProcessUtilities.outputExecutioner(command)
ProcessUtilities.outputExecutioner(command, self.externalApp)
else:
if self.reconstruct == 'remote':
repoLocation = '/home/backup/%s' % (self.website)
command = 'export RESTIC_PASSWORD=%s PATH=${PATH}:/usr/bin && restic -r %s:%s restore %s --target %s' % (
self.passwordFile,
self.backupDestinations, repoLocation, self.jobid, self.restoreTarget)
result = ProcessUtilities.outputExecutioner(command)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)
if result.find('restoring') == -1:
logging.statusWriter(self.statusPath, 'Failed: %s. [5009]' % (result), 1)
return 0
else:
repoLocation = '/home/backup/%s' % (self.website)
command = 'export PATH=${PATH}:/usr/bin && restic -r %s:%s restore %s --target %s --password-file %s' % (
self.jobid.destination, repoLocation, self.jobid.snapshotid, self.restoreTarget, self.passwordFile)
result = ProcessUtilities.outputExecutioner(command)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)
if result.find('restoring') == -1:
logging.statusWriter(self.statusPath, 'Failed: %s. [5009]' % (result), 1)
return 0
Expand Down Expand Up @@ -502,6 +500,10 @@ def restorePoint(self):
jobid = self.extraArgs['jobid']
self.reconstruct = self.extraArgs['reconstruct']

WebsiteObject = Websites.objects.get(domain=self.website)

self.externalApp = WebsiteObject.externalApp

if self.reconstruct == 'remote':

self.findRestorePath()
Expand Down Expand Up @@ -588,7 +590,6 @@ def prepareBackupMeta(self):

## Use the meta function from backup utils for future improvements.


if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile('Creating meta for %s. [IncBackupsControl.py]' % (self.website.domain))

Expand All @@ -600,8 +601,12 @@ def prepareBackupMeta(self):
if status == 1:
logging.statusWriter(self.statusPath, 'Meta data is ready..', 1)
metaPathNew = '/home/%s/meta.xml' % (self.website.domain)
command = 'mv %s %s' % (metaPath, metaPathNew)

command = 'chown %s:%s %s' % (self.externalApp, self.externalApp, metaPath)
ProcessUtilities.executioner(command)

command = 'mv %s %s' % (metaPath, metaPathNew)
ProcessUtilities.executioner(command, self.externalApp)
return 1
else:
logging.statusWriter(self.statusPath, "%s [544][5009]" % (message), 1)
Expand Down Expand Up @@ -720,7 +725,7 @@ def initiateRepo(self):

if self.backupDestinations == 'local':
command = 'restic init --repo %s --password-file %s' % (self.repoPath, self.passwordFile)
result = ProcessUtilities.outputExecutioner(command)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)

if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(result)
Expand All @@ -732,7 +737,7 @@ def initiateRepo(self):
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)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)

if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(result)
Expand All @@ -743,7 +748,7 @@ def initiateRepo(self):
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)
result = ProcessUtilities.outputExecutioner(command, self.externalApp)

if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(result)
Expand Down Expand Up @@ -820,6 +825,7 @@ def createBackup(self):
## Restic check completed.

self.website = Websites.objects.get(domain=website)
self.externalApp = self.website.externalApp

self.jobid = IncJob(website=self.website)
self.jobid.save()
Expand All @@ -828,13 +834,16 @@ def createBackup(self):

self.repoPath = '/home/%s/incbackup' % (self.website.domain)

if not os.path.exists(self.passwordFile):
command = 'ls -la %s' % (self.passwordFile)
output = ProcessUtilities.outputExecutioner(command, self.externalApp)

if output.find('No such file or directory') > -1:
password = randomPassword.generate_pass()
command = 'echo "%s" > %s' % (password, self.passwordFile)
ProcessUtilities.executioner(command, self.website.externalApp, True)
ProcessUtilities.executioner(command, self.externalApp, True)

command = 'chmod 600 %s' % (self.passwordFile)
ProcessUtilities.executioner(command)
ProcessUtilities.executioner(command, self.externalApp)

self.sendEmail(password)

Expand Down Expand Up @@ -868,7 +877,7 @@ def createBackup(self):

try:
command = 'rm -f %s' % (metaPathNew)
ProcessUtilities.executioner(command)
#ProcessUtilities.executioner(command)
except BaseException as msg:
logging.statusWriter(self.statusPath,
'Failed to delete meta file: %s. [IncJobs.createBackup.591]' % str(msg), 1)
Expand All @@ -887,6 +896,7 @@ def DeleteSnapShot(self, inc_job):
### Fetch the website name from JobSnapshot object and set these variable as they are needed in called functions below

self.website = job_snapshots[0].job.website.domain
self.externalApp = job_snapshots[0].job.website.externalApp
self.passwordFile = '/home/%s/%s' % (self.website, self.website)

for job_snapshot in job_snapshots:
Expand Down
6 changes: 0 additions & 6 deletions IncBackups/views.py
Expand Up @@ -212,7 +212,6 @@ def populate_current_records(request):
final_json = json.dumps(final_dic)
return HttpResponse(final_json)


def remove_destination(request):
try:
user_id, current_acl = _get_user_acl(request)
Expand Down Expand Up @@ -240,7 +239,6 @@ def remove_destination(request):
final_json = json.dumps(final_dic)
return HttpResponse(final_json)


def fetch_current_backups(request):
try:
user_id, current_acl = _get_user_acl(request)
Expand Down Expand Up @@ -286,7 +284,6 @@ def fetch_current_backups(request):
final_json = json.dumps(final_dic)
return HttpResponse(final_json)


def submit_backup_creation(request):
try:
user_id, current_acl = _get_user_acl(request)
Expand Down Expand Up @@ -325,7 +322,6 @@ def submit_backup_creation(request):
final_json = json.dumps(final_dic)
return HttpResponse(final_json)


def get_backup_status(request):
try:
data = json.loads(request.body)
Expand Down Expand Up @@ -389,7 +385,6 @@ def get_backup_status(request):
logging.writeToFile(str(msg) + " [backupStatus]")
return HttpResponse(final_json)


def delete_backup(request):
try:
user_id, current_acl = _get_user_acl(request)
Expand Down Expand Up @@ -419,7 +414,6 @@ def delete_backup(request):
final_json = json.dumps(final_dic)
return HttpResponse(final_json)


def fetch_restore_points(request):
try:
user_id, current_acl = _get_user_acl(request)
Expand Down

0 comments on commit b6775c8

Please sign in to comment.