Skip to content

Commit a9820ac

Browse files
committed
restore s3 backups
1 parent f0c1dfb commit a9820ac

5 files changed

Lines changed: 401 additions & 111 deletions

File tree

‎cloudAPI/cloudManager.py‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,3 +1791,106 @@ def SubmitCloudBackupRestore(self):
17911791

17921792
except BaseException as msg:
17931793
return self.ajaxPre(0, str(msg))
1794+
1795+
def fetchAWSKeys(self):
1796+
path = '/home/cyberpanel/.aws'
1797+
credentials = path + '/credentials'
1798+
1799+
data = open(credentials, 'r').readlines()
1800+
1801+
aws_access_key_id = data[1].split(' ')[2].strip(' ').strip('\n')
1802+
aws_secret_access_key = data[2].split(' ')[2].strip(' ').strip('\n')
1803+
region = data[3].split(' ')[2].strip(' ').strip('\n')
1804+
1805+
return aws_access_key_id, aws_secret_access_key, region
1806+
1807+
def getCurrentS3Backups(self):
1808+
try:
1809+
1810+
import boto3
1811+
from s3Backups.models import BackupPlan, BackupLogs
1812+
plan = BackupPlan.objects.get(name=self.data['planName'])
1813+
1814+
aws_access_key_id, aws_secret_access_key, region = self.fetchAWSKeys()
1815+
1816+
s3 = boto3.resource(
1817+
's3',
1818+
aws_access_key_id=aws_access_key_id,
1819+
aws_secret_access_key=aws_secret_access_key
1820+
)
1821+
bucket = s3.Bucket(plan.bucket)
1822+
key = '%s/%s/' % (plan.name, self.data['domainName'])
1823+
1824+
backups = []
1825+
1826+
for file in bucket.objects.filter(Prefix=key):
1827+
backups.append({'key': file.key, 'size': file.size})
1828+
1829+
json_data = "["
1830+
checker = 0
1831+
1832+
counter = 1
1833+
for items in backups:
1834+
1835+
dic = {'id': counter,
1836+
'file': items['key'],
1837+
'size': items['size'],
1838+
}
1839+
counter = counter + 1
1840+
1841+
if checker == 0:
1842+
json_data = json_data + json.dumps(dic)
1843+
checker = 1
1844+
else:
1845+
json_data = json_data + ',' + json.dumps(dic)
1846+
1847+
json_data = json_data + ']'
1848+
final_json = json.dumps({'status': 1, 'fetchStatus': 1, 'error_message': "None", "data": json_data})
1849+
return HttpResponse(final_json)
1850+
except BaseException as msg:
1851+
final_dic = {'status': 0, 'fetchStatus': 0, 'error_message': str(msg)}
1852+
final_json = json.dumps(final_dic)
1853+
return HttpResponse(final_json)
1854+
1855+
def deleteS3Backup(self):
1856+
try:
1857+
1858+
import boto3
1859+
from s3Backups.models import BackupPlan, BackupLogs
1860+
plan = BackupPlan.objects.get(name=self.data['planName'])
1861+
1862+
aws_access_key_id, aws_secret_access_key, region = self.fetchAWSKeys()
1863+
1864+
s3 = boto3.resource(
1865+
's3',
1866+
aws_access_key_id=aws_access_key_id,
1867+
aws_secret_access_key=aws_secret_access_key
1868+
)
1869+
s3.Object(plan.bucket, self.data['backupFile']).delete()
1870+
1871+
final_json = json.dumps({'status': 1, 'fetchStatus': 1, 'error_message': "None"})
1872+
return HttpResponse(final_json)
1873+
except BaseException as msg:
1874+
final_dic = {'status': 0, 'fetchStatus': 0, 'error_message': str(msg)}
1875+
final_json = json.dumps(final_dic)
1876+
return HttpResponse(final_json)
1877+
1878+
def SubmitS3BackupRestore(self):
1879+
try:
1880+
1881+
tempStatusPath = "/home/cyberpanel/" + str(randint(1000, 9999))
1882+
1883+
writeToFile = open(tempStatusPath, 'w')
1884+
writeToFile.write('Starting..,0')
1885+
writeToFile.close()
1886+
1887+
execPath = "/usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/backupUtilities.py"
1888+
execPath = execPath + " SubmitS3BackupRestore --backupDomain %s --backupFile '%s' --tempStoragePath %s --planName %s" % (self.data['domain'], self.data['backupFile'], tempStatusPath, self.data['planName'])
1889+
ProcessUtilities.popenExecutioner(execPath)
1890+
1891+
final_dic = {'status': 1, 'tempStatusPath': tempStatusPath}
1892+
final_json = json.dumps(final_dic)
1893+
return HttpResponse(final_json)
1894+
1895+
except BaseException as msg:
1896+
return self.ajaxPre(0, str(msg))

‎cloudAPI/views.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ def router(request):
6363
return cm.deleteCloudBackup()
6464
elif controller == 'SubmitCloudBackupRestore':
6565
return cm.SubmitCloudBackupRestore()
66+
elif controller == 'getCurrentS3Backups':
67+
return cm.getCurrentS3Backups()
68+
elif controller == 'deleteS3Backup':
69+
return cm.deleteS3Backup()
70+
elif controller == 'SubmitS3BackupRestore':
71+
return cm.SubmitS3BackupRestore()
6672
elif controller == 'fetchWebsites':
6773
return cm.fetchWebsites()
6874
elif controller == 'fetchWebsiteDataJSON':

‎plogical/IncScheduler.py‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -611,15 +611,37 @@ def fetchAWSKeys():
611611
def forceRunAWSBackup(planName):
612612
try:
613613

614+
614615
plan = BackupPlan.objects.get(name=planName)
615616
bucketName = plan.bucket.strip('\n').strip(' ')
616617
runTime = time.strftime("%d:%m:%Y")
617618

618619
config = TransferConfig(multipart_threshold=1024 * 25, max_concurrency=10,
619620
multipart_chunksize=1024 * 25, use_threads=True)
620621

622+
##
623+
621624
aws_access_key_id, aws_secret_access_key, region = IncScheduler.fetchAWSKeys()
622625

626+
ts = time.time()
627+
retentionSeconds = 86400 * plan.retention
628+
s3 = boto3.resource(
629+
's3',
630+
aws_access_key_id=aws_access_key_id,
631+
aws_secret_access_key=aws_secret_access_key
632+
)
633+
bucket = s3.Bucket(plan.bucket)
634+
635+
for file in bucket.objects.all():
636+
result = float(ts - file.last_modified.timestamp())
637+
if result > retentionSeconds:
638+
BackupLogs(owner=plan, level='INFO', timeStamp=time.strftime("%b %d %Y, %H:%M:%S"),
639+
msg='File %s expired and deleted according to your retention settings.' % (
640+
file.key)).save()
641+
file.delete()
642+
643+
###
644+
623645
client = boto3.client(
624646
's3',
625647
aws_access_key_id = aws_access_key_id,
@@ -629,7 +651,6 @@ def forceRunAWSBackup(planName):
629651

630652
##
631653

632-
633654
BackupLogs(owner=plan, level='INFO', timeStamp=time.strftime("%b %d %Y, %H:%M:%S"),
634655
msg='Starting backup process..').save()
635656

@@ -652,7 +673,7 @@ def forceRunAWSBackup(planName):
652673
finalResult = open(tempStatusPath, 'r').read()
653674

654675
if result == 1:
655-
key = plan.name + '/' + runTime + '/' + fileName.split('/')[-1]
676+
key = plan.name + '/' + items.domain + '/' + fileName.split('/')[-1]
656677
client.upload_file(
657678
fileName,
658679
bucketName,
@@ -675,26 +696,6 @@ def forceRunAWSBackup(planName):
675696
BackupLogs(owner=plan, level='INFO', timeStamp=time.strftime("%b %d %Y, %H:%M:%S"),
676697
msg='Backup Process Finished.').save()
677698

678-
###
679-
680-
s3 = boto3.resource(
681-
's3',
682-
aws_access_key_id=aws_access_key_id,
683-
aws_secret_access_key=aws_secret_access_key,
684-
region_name=region
685-
)
686-
687-
ts = time.time()
688-
689-
retentionSeconds = 86400 * plan.retention
690-
691-
for bucket in s3.buckets.all():
692-
if bucket.name == plan.bucket:
693-
for file in bucket.objects.all():
694-
result = float(ts - file.last_modified.timestamp())
695-
if result > retentionSeconds:
696-
file.delete()
697-
break
698699

699700
except BaseException as msg:
700701
logging.writeToFile(str(msg) + ' [S3Backups.runBackupPlan]')

0 commit comments

Comments
 (0)