From 8d78715c86630def799a495e4021582dda576b2b Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 19 Mar 2023 15:03:34 +0500 Subject: [PATCH] backupv2 setup and remotewpbackup bug fix --- IncBackups/static/IncBackups/IncBackups.js | 159 +++++++++++++++++- .../IncBackups/ConfigureV2Backup.html | 60 +++++++ .../templates/IncBackups/CreateV2Backup.html | 41 +++++ IncBackups/tests.py | 6 - IncBackups/urls.py | 3 + IncBackups/views.py | 156 +++++++++++++++-- backup/backupManager.py | 49 ++++++ plogical/Backupsv2.py | 7 +- plogical/IncScheduler.py | 15 +- websiteFunctions/website.py | 2 +- 10 files changed, 457 insertions(+), 41 deletions(-) diff --git a/IncBackups/static/IncBackups/IncBackups.js b/IncBackups/static/IncBackups/IncBackups.js index d8e1b7652..153de3aa5 100644 --- a/IncBackups/static/IncBackups/IncBackups.js +++ b/IncBackups/static/IncBackups/IncBackups.js @@ -1438,12 +1438,17 @@ app.controller('CreateV2Backup', function ($scope, $http, $timeout, $compile) { $scope.backupLoading = true; + $scope.installationProgress = true; + $scope.errorMessageBox = true; + $scope.success = true; + $scope.couldNotConnect = true; + $scope.goBackDisable = true; $scope.selectwebsite = function () { document.getElementById('reposelectbox').innerHTML = ""; $scope.backupLoading = false; // document.getElementById('CreateV2BackupButton').style.display = "block"; - var url = "/IncrementalBackups/selectwebsiteRetorev2"; + var url = "/IncrementalBackups/selectwebsiteCreatev2"; var data = { Selectedwebsite: $scope.selwebsite, @@ -1465,7 +1470,6 @@ app.controller('CreateV2Backup', function ($scope, $http, $timeout, $compile) { $scope.backupLoading = true; if (response.data.status === 1) { - const selectBox = document.getElementById('reposelectbox'); @@ -1519,9 +1523,7 @@ app.controller('CreateV2Backup', function ($scope, $http, $timeout, $compile) { } } - - - + var Domain; $scope.CreateV2BackupButton = function () { $scope.backupLoading = false; @@ -1533,7 +1535,6 @@ app.controller('CreateV2Backup', function ($scope, $http, $timeout, $compile) { Selectedrepo: $('#reposelectbox').val(), }; - var config = { headers: { 'X-CSRFToken': getCookie('csrftoken') @@ -1548,11 +1549,17 @@ app.controller('CreateV2Backup', function ($scope, $http, $timeout, $compile) { $scope.backupLoading = true; if (response.data.status === 1) { - alert("....................."+response.data.status); + Domain = $scope.selwebsite; + getCreationStatus(); } else { - alert('fail..........'+response.data.status); + $scope.goBackDisable = false; + new PNotify({ + title: 'Operation Failed!', + text: response.data.error_message, + type: 'error' + }); } } @@ -1568,6 +1575,87 @@ app.controller('CreateV2Backup', function ($scope, $http, $timeout, $compile) { } + function getCreationStatus() { + + url = "/IncrementalBackups/CreateV2BackupStatus"; + + var data = { + domain: Domain + }; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + + + $http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas); + + + function ListInitialDatas(response) { + + if (response.data.abort === 1) { + + if (response.data.installStatus === 1) { + + $scope.webSiteCreationLoading = true; + $scope.installationDetailsForm = true; + $scope.installationProgress = false; + $scope.errorMessageBox = true; + $scope.success = false; + $scope.couldNotConnect = true; + $scope.goBackDisable = false; + + $("#installProgress").css("width", "100%"); + $scope.installPercentage = "100"; + $scope.currentStatus = response.data.currentStatus; + $timeout.cancel(); + + } else { + + $scope.webSiteCreationLoading = true; + $scope.installationDetailsForm = true; + $scope.installationProgress = false; + $scope.errorMessageBox = false; + $scope.success = true; + $scope.couldNotConnect = true; + $scope.goBackDisable = false; + + $scope.errorMessage = response.data.error_message; + + $("#installProgress").css("width", "0%"); + $scope.installPercentage = "0"; + $scope.goBackDisable = false; + + } + + } else { + $scope.webSiteCreationLoading = false; + $("#installProgress").css("width", response.data.installationProgress + "%"); + $scope.installPercentage = response.data.installationProgress; + $scope.currentStatus = response.data.currentStatus; + $timeout(getCreationStatus, 1000); + } + + } + + function cantLoadInitialDatas(response) { + + $scope.webSiteCreationLoading = true; + $scope.installationDetailsForm = true; + $scope.installationProgress = false; + $scope.errorMessageBox = true; + $scope.success = true; + $scope.couldNotConnect = false; + $scope.goBackDisable = false; + + } + + + } + + }); app.controller('ConfigureV2Backup', function ($scope, $http, $timeout){ @@ -1582,12 +1670,65 @@ app.controller('ConfigureV2Backup', function ($scope, $http, $timeout){ $scope.cyberpanelLoading = true; $('#GdriveModal').modal('show'); } + else if(backuptype === 'SFTP') + { + $scope.cyberpanelLoading = true; + $('#SFTPModal').modal('show'); + } } + $scope.setupAccount = function(){ - window.open("https://platform.cyberpersons.com/gDrive?name=" + $scope.accountName + '&server=' + window.location.href + 'Setup'); + window.open("https://platform.cyberpersons.com/gDrive?name=" + $scope.accountName + '&server=' + window.location.href + 'Setup&domain=' + $scope.selwebsite); }; + + $scope.ConfigerSFTP = function (){ + $scope.cyberpanelLoading = false; + var url = "/IncrementalBackups/ConfigureSftpV2Backup"; + + var data = { + Selectedwebsite: $scope.selwebsite, + sfptpasswd: $scope.sfptpasswd, + hostName: $scope.hostName, + }; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + + + $http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas); + + + function ListInitialDatas(response) { + $scope.backupLoading = true; + if (response.data.status === 1) { + location.reload() + + } + else { + $scope.goBackDisable = false; + new PNotify({ + title: 'Operation Failed!', + text: response.data.error_message, + type: 'error' + }); + } + + } + + function cantLoadInitialDatas(response) { + $scope.backupLoading = true; + new PNotify({ + title: 'Operation Failed!', + text: 'Could not connect to server, please refresh this page', + type: 'error' + }); + } + } }); function listpaths(pathid, button) { diff --git a/IncBackups/templates/IncBackups/ConfigureV2Backup.html b/IncBackups/templates/IncBackups/ConfigureV2Backup.html index d457858e8..f79c562c1 100644 --- a/IncBackups/templates/IncBackups/ConfigureV2Backup.html +++ b/IncBackups/templates/IncBackups/ConfigureV2Backup.html @@ -21,6 +21,17 @@
+
+ +
+ +
+
+
@@ -73,6 +84,55 @@
+ + +
diff --git a/IncBackups/templates/IncBackups/CreateV2Backup.html b/IncBackups/templates/IncBackups/CreateV2Backup.html index b51231750..59c17e4ac 100644 --- a/IncBackups/templates/IncBackups/CreateV2Backup.html +++ b/IncBackups/templates/IncBackups/CreateV2Backup.html @@ -52,6 +52,47 @@ + +
+ +
+ +
+

{$ currentStatus $}

+
+ +
+
+ 70% Complete +
+
+ +
+

{% trans "Error message:" %} {$ errorMessage $}

+
+ +
+

{% trans "Website succesfully created." %}

+
+ + +
+

{% trans "Could not connect to server. Please refresh this page." %}

+
+ + +
+
+ +
+ +
+ +
+
+ diff --git a/IncBackups/tests.py b/IncBackups/tests.py index f067dcaac..e69de29bb 100644 --- a/IncBackups/tests.py +++ b/IncBackups/tests.py @@ -1,6 +0,0 @@ -# -*- coding: utf-8 -*- - - -from django.test import TestCase - -# Create your tests here. diff --git a/IncBackups/urls.py b/IncBackups/urls.py index 059cdf572..3354254d4 100644 --- a/IncBackups/urls.py +++ b/IncBackups/urls.py @@ -32,5 +32,8 @@ urlpatterns = [ url(r'^selectreporestorev2$', views.selectreporestorev2, name='selectreporestorev2'), url(r'^RestorePathV2$', views.RestorePathV2, name='RestorePathV2'), url(r'^CreateV2BackupButton$', views.CreateV2BackupButton, name='CreateV2BackupButton'), + url(r'^selectwebsiteCreatev2$', views.selectwebsiteCreatev2, name='selectwebsiteCreatev2'), + url(r'^CreateV2BackupStatus$', views.CreateV2BackupStatus, name='CreateV2BackupStatus'), + url(r'^ConfigureSftpV2Backup$', views.ConfigureSftpV2Backup, name='ConfigureSftpV2Backup'), ] \ No newline at end of file diff --git a/IncBackups/views.py b/IncBackups/views.py index 6960aced7..5255196a8 100644 --- a/IncBackups/views.py +++ b/IncBackups/views.py @@ -721,10 +721,10 @@ def ConfigureV2Backup(request): return ACLManager.loadError() - # websites = ACLManager.findAllSites(current_acl, user_id) + websites = ACLManager.findAllSites(current_acl, user_id) # # destinations = _get_destinations(local=True) - proc = httpProc(request, 'IncBackups/ConfigureV2Backup.html') + proc = httpProc(request, 'IncBackups/ConfigureV2Backup.html', {'websiteList': websites}) return proc.render() @@ -752,23 +752,17 @@ def createV2BackupSetup(request): req_data['token_uri'] = request.GET.get('to') req_data['scopes'] = request.GET.get('s') req_data['accountname'] = request.GET.get('n') + website = request.GET.get('d') + # logging.writeToFile('domainname is ====%s'%(request.GET.get)) cpbuv2 = CPBackupsV2( - {'domain': 'cyberpanel.net', 'BasePath': '/home/backup', 'BackupDatabase': 1, 'BackupData': 1, + {'domain': website, 'BasePath': '/home/backup', 'BackupDatabase': 1, 'BackupData': 1, 'BackupEmails': 1, 'BackendName': 'testremote'}) - # RcloneData = {"name": 'testremote', "host": "staging.cyberpanel.net", "user": "abcds2751", "port": "22", - # "password": "hosting", } cpbuv2.SetupRcloneBackend(CPBackupsV2.GDrive, req_data) return ConfigureV2Backup(request) - # wm = BackupManager() - # return wm.gDriveSetup(userID, request) - # currentACL = ACLManager.loadedACL(userID) - # websitesName = ACLManager.findAllSites(currentACL, userID) - # proc = httpProc(request, 'IncBackups/RestoreV2Backup.html', {'websiteList': websitesName}, 'createBackup') - # return proc.render() except KeyError: return redirect(loadLoginPage) @@ -781,14 +775,37 @@ def CreateV2BackupButton(request): Selectedwebsite = data['Selectedwebsite'] Selectedrepo = data['Selectedrepo'] - final_dic = {'status': 1, 'data': None} - final_json = json.dumps(final_dic) - return HttpResponse(final_json) + extraArgs = {} + + extraArgs['function'] = 'InitiateBackup' + extraArgs['BackendName'] = Selectedrepo + extraArgs['domain'] = Selectedwebsite + extraArgs['BasePath']= '/home/backup' + extraArgs['tempStatusPath'] = "/home/cyberpanel/" + str(randint(1000, 9999)) + + background = CPBackupsV2(extraArgs) + background.start() + + time.sleep(2) + + data_ret = {'status': 1, 'installStatus': 1, 'error_message': 'None', + 'tempStatusPath': extraArgs['tempStatusPath']} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) except BaseException as msg: - final_dic = {'status': 0, 'error_message': str(msg)} - final_json = json.dumps(final_dic) - return HttpResponse(final_json) + data_ret = {'status': 0, 'installStatus': 0, 'error_message': str(msg)} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + +def CreateV2BackupStatus(request): + try: + userID = request.session['userID'] + bm = BackupManager() + return bm.CreateV2BackupStatus(userID, json.loads(request.body)) + except KeyError: + return redirect(loadLoginPage) def RestoreV2backupSite(request): try: @@ -830,7 +847,7 @@ def selectwebsiteRetorev2(request): command = 'cat %s'%(path) result = pu.outputExecutioner(command) - if result.find('host') > -1: + if result.find('type') > -1: pattern = r'\[(.*?)\]' matches = re.findall(pattern, result) final_json = json.dumps({'status': 1, 'fetchStatus': 1, 'error_message': "None", "data": matches}) @@ -848,6 +865,109 @@ def selectwebsiteRetorev2(request): return HttpResponse(final_json) + +def ConfigureSftpV2Backup(request): + try: + userID = request.session['userID'] + data = json.loads(request.body) + Selectedwebsite = data['Selectedwebsite'] + sfptpasswd = data['sfptpasswd'] + hostName = data['hostName'] + admin = Administrator.objects.get(pk=userID) + + req_data = {} + req_data['name'] = 'SFTP' + req_data['host'] = Selectedwebsite + req_data['user'] = hostName + req_data['password'] = sfptpasswd + + + cpbuv2 = CPBackupsV2( + {'domain': Selectedwebsite, 'BasePath': '/home/backup', 'BackupDatabase': 1, 'BackupData': 1, + 'BackupEmails': 1, 'BackendName': 'SFTP', 'function': None}) + + cpbuv2.SetupRcloneBackend(CPBackupsV2.SFTP, req_data) + + return ConfigureV2Backup(request) + + # final_json = json.dumps({'status': 1, 'fetchStatus': 1, 'error_message': "None", "data": None}) + # return HttpResponse(final_json) + + + except BaseException as msg: + final_dic = {'status': 0, 'fetchStatus': 0, 'error_message': str(msg)} + final_json = json.dumps(final_dic) + return HttpResponse(final_json) + + +def selectwebsiteCreatev2(request): + import re + try: + userID = request.session['userID'] + data = json.loads(request.body) + Selectedwebsite = data['Selectedwebsite'] + admin = Administrator.objects.get(pk=userID) + + obj = Websites.objects.get(domain = str(Selectedwebsite), admin = admin) + #/home/cyberpanel.net/.config/rclone/rclone.conf + path = '/home/%s/.config/rclone/rclone.conf' %(obj.domain) + + command = 'cat %s'%(path) + CurrentContent = pu.outputExecutioner(command) + + + if CurrentContent.find('No such file or directory') > -1: + LocalRclonePath = f'/home/{obj.domain}/.config/rclone' + command = f'mkdir -p {LocalRclonePath}' + pu.executioner(command, obj.externalApp) + + content = ''' + [local] + type = local + ''' + command = f"echo '{content}' > {path}" + pu.executioner(command, obj.externalApp, True) + + command = 'cat %s' % (path) + result = pu.outputExecutioner(command) + + if result.find('type') > -1: + pattern = r'\[(.*?)\]' + matches = re.findall(pattern, result) + final_json = json.dumps({'status': 1, 'fetchStatus': 1, 'error_message': "None", "data": matches}) + return HttpResponse(final_json) + else: + final_json = json.dumps({'status': 0, 'fetchStatus': 0, 'error_message': 'Could not Find repo'}) + return HttpResponse(final_json) + else: + command = 'cat %s' % (path) + result = pu.outputExecutioner(command) + + if result.find('type') > -1: + pattern = r'\[(.*?)\]' + matches = re.findall(pattern, result) + final_json = json.dumps({'status': 1, 'fetchStatus': 1, 'error_message': "None", "data": matches}) + return HttpResponse(final_json) + else: + final_json = json.dumps({'status': 0, 'fetchStatus': 0, 'error_message': 'Could not Find repo'}) + return HttpResponse(final_json) + + + + + + # logging.writeToFile(str(CurrentContent)) + # final_json = json.dumps({'status': 1, 'fetchStatus': 1, 'error_message': "None", "data": None}) + # return HttpResponse(final_json) + # + + + except BaseException as msg: + final_dic = {'status': 0, 'fetchStatus': 0, 'error_message': str(msg)} + final_json = json.dumps(final_dic) + return HttpResponse(final_json) + + def selectreporestorev2(request): try: userID = request.session['userID'] diff --git a/backup/backupManager.py b/backup/backupManager.py index acea516ef..d69de1561 100755 --- a/backup/backupManager.py +++ b/backup/backupManager.py @@ -1795,3 +1795,52 @@ class BackupManager: data_ret = {'status': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) return HttpResponse(json_data) + + def CreateV2BackupStatus(self, userID=None, data=None): + try: + domain = data['domain'] + + statusFile = f'/home/cyberpanel/{domain}_rustic_backup_log' + + if ACLManager.CheckStatusFilleLoc(statusFile): + pass + else: + data_ret = {'abort': 1, 'installStatus': 0, 'installationProgress': "100", + 'currentStatus': 'Invalid status file.'} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + #currentStatus:"cat: /home/cyberpanel/9219: No such file or directory" + + statusData = ProcessUtilities.outputExecutioner("cat " + statusFile).splitlines() + + lastLine = statusData[-1] + + if lastLine.find('[200]') > -1: + command = 'rm -f ' + statusFile + subprocess.call(shlex.split(command)) + data_ret = {'abort': 1, 'installStatus': 1, 'installationProgress': "100", + 'currentStatus': 'Successfully Installed.'} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + elif lastLine.find('[404]') > -1: + data_ret = {'abort': 1, 'installStatus': 0, 'installationProgress': "0", + 'error_message': ProcessUtilities.outputExecutioner("cat " + statusFile).splitlines()} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + else: + progress = lastLine.split(',') + currentStatus = progress[0] + try: + installationProgress = progress[1] + except: + installationProgress = 0 + data_ret = {'abort': 0, 'installStatus': 0, 'installationProgress': installationProgress, + 'currentStatus': currentStatus} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + except BaseException as msg: + data_ret = {'abort': 0, 'installStatus': 0, 'installationProgress': "0", 'error_message': str(msg)} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) \ No newline at end of file diff --git a/plogical/Backupsv2.py b/plogical/Backupsv2.py index 605f09373..8e7784f8f 100644 --- a/plogical/Backupsv2.py +++ b/plogical/Backupsv2.py @@ -4,6 +4,7 @@ import os import sys import time import requests +from django.http import HttpResponse sys.path.append('/usr/local/CyberCP') import django @@ -112,12 +113,14 @@ pass = {ObsecurePassword} ''' command = f"echo '{content}' > {self.ConfigFilePath}" - #ProcessUtilities.executioner(command, self.website.externalApp, True) + ProcessUtilities.executioner(command, self.website.externalApp, True) command = f"chmod 600 {self.ConfigFilePath}" ProcessUtilities.executioner(command, self.website.externalApp) + + final_json = json.dumps({'status': 1, 'fetchStatus': 1, 'error_message': "None", "data": None}) + return HttpResponse(final_json) elif type == CPBackupsV2.GDrive: - logging.CyberCPLogFileWriter.writeToFile('tes 2,...gdive..........in') token = """{"access_token":"%s","token_type":"Bearer","refresh_token":"%s"}""" %(config["token"], config["refresh_token"]) diff --git a/plogical/IncScheduler.py b/plogical/IncScheduler.py index 2866825a7..2077b8d1c 100644 --- a/plogical/IncScheduler.py +++ b/plogical/IncScheduler.py @@ -34,7 +34,6 @@ except: pass import threading as multi - class IncScheduler(multi.Thread): logPath = '/home/cyberpanel/incbackuplogs' gitFolder = '/home/cyberpanel/git' @@ -122,7 +121,7 @@ class IncScheduler(multi.Thread): break except BaseException as msg: - logging.writeToFile(str(msg)) + logging.writeToFile( "%s [startBackup]"%str(msg)) @staticmethod def git(type): @@ -964,29 +963,35 @@ Automatic backup failed for %s on %s. @staticmethod def RemoteBackup(function): try: + # print("....start remote backup...............") from websiteFunctions.models import RemoteBackupSchedule, RemoteBackupsites, WPSites from loginSystem.models import Administrator import json import time from plogical.applicationInstaller import ApplicationInstaller for config in RemoteBackupSchedule.objects.all(): + # print("....start remote backup........site.......%s"%config.Name) try: configbakup = json.loads(config.config) backuptype = configbakup['BackupType'] + # print("....start remote backup........site.......%s.. and bakuptype...%s" % (config.Name, backuptype)) if backuptype == 'Only DataBase': Backuptype = "3" elif backuptype == 'Only Website': Backuptype = "2" else: Backuptype = "1" - except: + except BaseException as msg: + print("....backup config type Error.%s" % str(msg)) continue try: - allRemoteBackupsiteobj = RemoteBackupsites.objects.filter(owner=config.pk) + allRemoteBackupsiteobj = RemoteBackupsites.objects.filter(owner=config) + # print("store site id.....%s"%str(allRemoteBackupsiteobj)) for i in allRemoteBackupsiteobj: try: backupsiteID = i.WPsites wpsite = WPSites.objects.get(pk=backupsiteID) + # print("site name.....%s"%wpsite.title) AdminID = wpsite.owner.admin_id Admin = Administrator.objects.get(pk=AdminID) @@ -1375,7 +1380,7 @@ def main(): IncScheduler.CalculateAndUpdateDiskUsage() return 0 - if args.function == '30 Minutes' or args.function == '30 Minutes' or args.function == '1 Hour' or args.function == '6 Hours' or args.function == '12 Hours' or args.function == '1 Day' or args.function == '3 Days' or args.function == '1 Week': + if args.function == '30 Minutes' or args.function == '30 Minutes'or args.function == 'Daily' or args.function == '1 Hour' or args.function == '6 Hours' or args.function == '12 Hours' or args.function == '1 Day' or args.function == '3 Days' or args.function == '1 Week': IncScheduler.RemoteBackup(args.function) return 0 diff --git a/websiteFunctions/website.py b/websiteFunctions/website.py index fe116ea1e..283cca05f 100755 --- a/websiteFunctions/website.py +++ b/websiteFunctions/website.py @@ -1326,7 +1326,7 @@ class WebsiteManager: 'BucketName': BucketName } except BaseException as msg: - config = {} + config = {'BackupType': BackupType} pass svobj = RemoteBackupSchedule(RemoteBackupConfig=RemoteBackupConfigobj, Name=ScheduleName,