@@ -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 ))
0 commit comments