From 9ed61ffb59814110efb1a47ad3dfa376a6c506ad Mon Sep 17 00:00:00 2001 From: Usman Nasir Date: Sun, 20 Sep 2020 19:52:02 +0500 Subject: [PATCH] refactor how the schedule backup works --- backup/backupManager.py | 155 +++---- backup/static/backup/backup.js | 436 ++++++++---------- .../templates/backup/backupDestinations.html | 314 ++++++++----- websiteFunctions/models.py | 9 +- 4 files changed, 451 insertions(+), 463 deletions(-) diff --git a/backup/backupManager.py b/backup/backupManager.py index 94ae8c45b..ac8a5e090 100755 --- a/backup/backupManager.py +++ b/backup/backupManager.py @@ -10,7 +10,7 @@ django.setup() import json from plogical.acl import ACLManager import plogical.CyberCPLogFileWriter as logging -from websiteFunctions.models import Websites, Backups, dest, backupSchedules, BackupJob, BackupJobLogs, GDrive, GDriveSites, GDriveJobLogs +from websiteFunctions.models import Websites, Backups, dest, backupSchedules, BackupJob, GDrive, GDriveSites from plogical.virtualHostUtilities import virtualHostUtilities import subprocess import shlex @@ -26,6 +26,7 @@ import requests import google.oauth2.credentials import googleapiclient.discovery from googleapiclient.discovery import build +from websiteFunctions.models import NormalBackupDests class BackupManager: @@ -710,35 +711,23 @@ class BackupManager: if ACLManager.currentContextPermission(currentACL, 'addDeleteDestinations') == 0: return ACLManager.loadErrorJson('destStatus', 0) - destinations = backupUtil.backupUtilities.destinationsPath - finalDic = {} - finalDic['ipAddress'] = data['IPAddress'] - finalDic['password'] = data['password'] + if data['type'] == 'SFTP': - try: - finalDic['port'] = data['backupSSHPort'] - except: - finalDic['port'] = "22" + finalDic['ipAddress'] = data['IPAddress'] + finalDic['password'] = data['password'] - try: - finalDic['user'] = data['user'] - except: - finalDic['user'] = "root" + try: + finalDic['port'] = data['backupSSHPort'] + except: + finalDic['port'] = "22" - if dest.objects.all().count() == 2: - final_dic = {'destStatus': 0, - 'error_message': "Currently only one remote destination is allowed."} - final_json = json.dumps(final_dic) - return HttpResponse(final_json) + try: + finalDic['user'] = data['user'] + except: + finalDic['user'] = "root" - try: - d = dest.objects.get(destLoc=finalDic['password']) - final_dic = {'destStatus': 0, 'error_message': "This destination already exists."} - final_json = json.dumps(final_dic) - return HttpResponse(final_json) - except: execPath = "/usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/backupUtilities.py" execPath = execPath + " submitDestinationCreation --ipAddress " + finalDic['ipAddress'] + " --password " \ @@ -753,56 +742,63 @@ class BackupManager: logging.CyberCPLogFileWriter.writeToFile(output) if output.find('1,') > -1: - try: - writeToFile = open(destinations, "w") - writeToFile.write(json.dumps(finalDic)) - writeToFile.close() - newDest = dest(destLoc=finalDic['ipAddress']) - newDest.save() - final_dic = {'destStatus': 1, 'error_message': "None"} - final_json = json.dumps(final_dic) - return HttpResponse(final_json) - except: - writeToFile = open(destinations, "w") - writeToFile.write(json.dumps(finalDic)) - writeToFile.close() + config = {'type': data['type'], 'ip': data['IPAddress'], 'username': data['userName'], 'port': data['backupSSHPort'], 'path': data['path']} + nd = NormalBackupDests(name=data['name'], config = json.dumps(config)) + nd.save() - newDest = dest(destLoc=finalDic['ipAddress']) - newDest.save() - final_dic = {'destStatus': 1, 'error_message': "None"} - final_json = json.dumps(final_dic) - return HttpResponse(final_json) - else: - final_dic = {'destStatus': 0, 'error_message': output} + final_dic = {'status' : 1, 'destStatus': 1, 'error_message': "None"} final_json = json.dumps(final_dic) return HttpResponse(final_json) + else: + final_dic = {'status' : 0, 'destStatus': 0, 'error_message': output} + final_json = json.dumps(final_dic) + return HttpResponse(final_json) + else: + config = {'type': data['type'], 'path': data['path']} + nd = NormalBackupDests(name='local', config=json.dumps(config)) + nd.save() + + final_dic = {'status' : 1, 'destStatus': 1, 'error_message': "None"} + final_json = json.dumps(final_dic) + return HttpResponse(final_json) + except BaseException as msg: - final_dic = {'destStatus': 0, 'error_message': str(msg)} + final_dic = {'status' : 0, 'destStatus': 0, 'error_message': str(msg)} final_json = json.dumps(final_dic) return HttpResponse(final_json) def getCurrentBackupDestinations(self, userID=None, data=None): try: - currentACL = ACLManager.loadedACL(userID) if ACLManager.currentContextPermission(currentACL, 'addDeleteDestinations') == 0: return ACLManager.loadErrorJson('fetchStatus', 0) - records = dest.objects.all() + destinations = NormalBackupDests.objects.all() json_data = "[" checker = 0 - for items in records: - if items.destLoc == "Home": - continue - dic = {'id': items.id, - 'ip': items.destLoc, - } + for items in destinations: + + config = json.loads(items.config) + + if config['type'] == data['type'] and data['type'] == 'SFTP': + dic = { + 'name': items.name, + 'ip': config['ip'], + 'username': config['username'], + 'path': config['path'], + 'port': config['port'], + } + else: + dic = { + 'name': items.name, + 'path': config['path'], + } if checker == 0: json_data = json_data + json.dumps(dic) @@ -811,11 +807,11 @@ class BackupManager: json_data = json_data + ',' + json.dumps(dic) json_data = json_data + ']' - final_json = json.dumps({'fetchStatus': 1, 'error_message': "None", "data": json_data}) + final_json = json.dumps({'status': 1, 'error_message': "None", "data": json_data}) return HttpResponse(final_json) except BaseException as msg: - final_dic = {'fetchStatus': 0, 'error_message': str(msg)} + final_dic = {'status': 0, 'error_message': str(msg)} final_json = json.dumps(final_dic) return HttpResponse(final_json) @@ -855,48 +851,25 @@ class BackupManager: if ACLManager.currentContextPermission(currentACL, 'addDeleteDestinations') == 0: return ACLManager.loadErrorJson('delStatus', 0) - ipAddress = data['IPAddress'] + nameOrPath = data['nameOrPath'] + type = data['type'] - delDest = dest.objects.get(destLoc=ipAddress) - delDest.delete() + if type == 'SFTP': + NormalBackupDests.objects.get(name=nameOrPath).delete() + else: + dests = NormalBackupDests.objects.filter(name='local') + for items in dests: + config = json.loads(items.config) + if config['path'] == nameOrPath: + items.delete() + break - path = "/usr/local/CyberCP/backup/" - destinations = path + "destinations" - - data = open(destinations, 'r').readlines() - - writeToFile = open(destinations, 'r') - - for items in data: - if items.find(ipAddress) > -1: - continue - else: - writeToFile.writelines(items) - - writeToFile.close() - - ## Deleting Cron Tab Entries for this destination - - path = "/etc/crontab" - - data = open(path, 'r').readlines() - - writeToFile = open(path, 'w') - - for items in data: - if items.find("backupSchedule.py") > -1: - continue - else: - writeToFile.writelines(items) - - writeToFile.close() - - final_dic = {'delStatus': 1, 'error_message': "None"} + final_dic = {'status': 1, 'delStatus': 1, 'error_message': "None"} final_json = json.dumps(final_dic) return HttpResponse(final_json) except BaseException as msg: - final_dic = {'delStatus': 1, 'error_message': str(msg)} + final_dic = {'status': 0, 'delStatus': 1, 'error_message': str(msg)} final_json = json.dumps(final_dic) return HttpResponse(final_json) diff --git a/backup/static/backup/backup.js b/backup/static/backup/backup.js index 2533712bd..67e7cc3ef 100755 --- a/backup/static/backup/backup.js +++ b/backup/static/backup/backup.js @@ -415,254 +415,6 @@ app.controller('restoreWebsiteControl', function ($scope, $http, $timeout) { //*** Restore site ends here ***/// -///** Backup Destination ***// - -app.controller('backupDestinations', function ($scope, $http, $timeout) { - - $scope.destinationLoading = true; - $scope.connectionFailed = true; - $scope.connectionSuccess = true; - $scope.canNotAddDestination = true; - $scope.destinationAdded = true; - $scope.couldNotConnect = true; - - populateCurrentRecords(); - - $scope.addDestination = function () { - - $scope.destinationLoading = false; - $scope.connectionFailed = true; - $scope.connectionSuccess = true; - $scope.canNotAddDestination = true; - $scope.destinationAdded = true; - $scope.couldNotConnect = true; - - url = "/backup/submitDestinationCreation"; - - - var data = { - IPAddress: $scope.IPAddress, - password: $scope.password, - user: $scope.user, - backupSSHPort: $scope.backupSSHPort, - }; - - var config = { - headers: { - 'X-CSRFToken': getCookie('csrftoken') - } - }; - - - $http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas); - - - function ListInitialDatas(response) { - - - if (response.data.destStatus == 1) { - - $scope.destinationLoading = true; - $scope.connectionFailed = true; - $scope.connectionSuccess = true; - $scope.canNotAddDestination = true; - $scope.destinationAdded = false; - $scope.couldNotConnect = true; - - populateCurrentRecords(); - - } - else { - $scope.destinationLoading = true; - $scope.connectionFailed = true; - $scope.connectionSuccess = true; - $scope.canNotAddDestination = false; - $scope.destinationAdded = true; - $scope.couldNotConnect = true; - - $scope.errorMessage = response.data.error_message; - } - } - - function cantLoadInitialDatas(response) { - $scope.destinationLoading = true; - $scope.connectionFailed = true; - $scope.connectionSuccess = true; - $scope.canNotAddDestination = true; - $scope.destinationAdded = true; - $scope.couldNotConnect = false; - } - - }; - - $scope.checkConn = function (ip) { - - $scope.destinationLoading = false; - $scope.connectionFailed = true; - $scope.connectionSuccess = true; - $scope.canNotAddDestination = true; - $scope.destinationAdded = true; - $scope.couldNotConnect = true; - - url = "/backup/getConnectionStatus"; - - - var data = { - IPAddress: ip, - }; - - var config = { - headers: { - 'X-CSRFToken': getCookie('csrftoken') - } - }; - - - $http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas); - - - function ListInitialDatas(response) { - - - if (response.data.connStatus == 1) { - - $scope.destinationLoading = true; - $scope.connectionFailed = true; - $scope.connectionSuccess = false; - $scope.canNotAddDestination = true; - $scope.destinationAdded = true; - $scope.couldNotConnect = true; - - $scope.IPAddress = ip; - - } - else { - $scope.destinationLoading = true; - $scope.connectionFailed = false; - $scope.connectionSuccess = true; - $scope.canNotAddDestination = true; - $scope.destinationAdded = true; - $scope.couldNotConnect = true; - - $scope.errorMessage = response.data.error_message; - $scope.IPAddress = ip; - } - } - - function cantLoadInitialDatas(response) { - $scope.destinationLoading = true; - $scope.connectionFailed = true; - $scope.connectionSuccess = true; - $scope.canNotAddDestination = true; - $scope.destinationAdded = true; - $scope.couldNotConnect = false; - } - - }; - - $scope.delDest = function (ip) { - - $scope.destinationLoading = false; - $scope.connectionFailed = true; - $scope.connectionSuccess = true; - $scope.canNotAddDestination = true; - $scope.destinationAdded = true; - $scope.couldNotConnect = true; - - url = "/backup/deleteDestination"; - - - var data = { - IPAddress: ip, - }; - - var config = { - headers: { - 'X-CSRFToken': getCookie('csrftoken') - } - }; - - - $http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas); - - - function ListInitialDatas(response) { - - - if (response.data.delStatus == 1) { - - $scope.destinationLoading = true; - $scope.connectionFailed = true; - $scope.connectionSuccess = true; - $scope.canNotAddDestination = true; - $scope.destinationAdded = true; - $scope.couldNotConnect = true; - populateCurrentRecords(); - - $scope.IPAddress = ip; - - } - else { - $scope.destinationLoading = true; - $scope.connectionFailed = true; - $scope.connectionSuccess = true; - $scope.canNotAddDestination = true; - $scope.destinationAdded = true; - $scope.couldNotConnect = true; - - $scope.errorMessage = response.data.error_message; - $scope.IPAddress = ip; - } - } - - function cantLoadInitialDatas(response) { - $scope.destinationLoading = true; - $scope.connectionFailed = true; - $scope.connectionSuccess = true; - $scope.canNotAddDestination = true; - $scope.destinationAdded = true; - $scope.couldNotConnect = false; - } - - }; - - - function populateCurrentRecords() { - - url = "/backup/getCurrentBackupDestinations"; - - var data = {}; - - var config = { - headers: { - 'X-CSRFToken': getCookie('csrftoken') - } - }; - - - $http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas); - - - function ListInitialDatas(response) { - - - if (response.data.fetchStatus == 1) { - - $scope.records = JSON.parse(response.data.data); - - } - } - - function cantLoadInitialDatas(response) { - $scope.couldNotConnect = false; - } - - }; - -}); - -//*** Backup destination ***/// - ///** Schedule Backup ***// app.controller('scheduleBackup', function ($scope, $http, $timeout) { @@ -1911,3 +1663,191 @@ app.controller('googleDrive', function ($scope, $http) { }; }); + +/// + +app.controller('backupDestinations', function ($scope, $http) { + $scope.cyberpanelLoading = true; + $scope.sftpHide = true; + $scope.localHide = true; + + $scope.fetchDetails = function () { + + if ($scope.destinationType === 'SFTP') { + $scope.sftpHide = false; + $scope.localHide = true; + $scope.populateCurrentRecords(); + } else { + $scope.sftpHide = true; + $scope.localHide = false; + $scope.populateCurrentRecords(); + } + }; + + $scope.populateCurrentRecords = function () { + + $scope.cyberpanelLoading = false; + + url = "/backup/getCurrentBackupDestinations"; + + var type = 'SFTP'; + if ($scope.destinationType === 'SFTP') { + type = 'SFTP'; + } else { + type = 'local'; + } + + var data = { + type: type + }; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + + + $http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas); + + + function ListInitialDatas(response) { + $scope.cyberpanelLoading = true; + if (response.data.status === 1) { + $scope.records = JSON.parse(response.data.data); + } else { + new PNotify({ + title: 'Operation Failed!', + text: response.data.error_message, + type: 'error' + }); + } + + } + + function cantLoadInitialDatas(response) { + $scope.cyberpanelLoading = true; + new PNotify({ + title: 'Operation Failed!', + text: 'Could not connect to server, please refresh this page', + type: 'error' + }); + } + + }; + + $scope.addDestination = function (type) { + $scope.cyberpanelLoading = false; + + url = "/backup/submitDestinationCreation"; + + if (type === 'SFTP') { + var data = { + type: type, + name: $scope.name, + IPAddress: $scope.IPAddress, + userName: $scope.userName, + password: $scope.password, + backupSSHPort: $scope.backupSSHPort, + path: $scope.path + }; + } else { + var data = { + type: type, + path: $scope.localPath, + }; + } + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + + + $http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas); + + + function ListInitialDatas(response) { + $scope.cyberpanelLoading = true; + $scope.populateCurrentRecords(); + if (response.data.status === 1) { + new PNotify({ + title: 'Success!', + text: 'Destination successfully added.', + type: 'success' + }); + } else { + new PNotify({ + title: 'Operation Failed!', + text: response.data.error_message, + type: 'error' + }); + } + + } + + function cantLoadInitialDatas(response) { + $scope.cyberpanelLoading = true; + new PNotify({ + title: 'Operation Failed!', + text: 'Could not connect to server, please refresh this page', + type: 'error' + }); + } + + }; + + $scope.removeDestination = function (type, nameOrPath) { + $scope.cyberpanelLoading = false; + + + url = "/backup/deleteDestination"; + + var data = { + type: type, + nameOrPath: nameOrPath, + }; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + + + $http.post(url, data, config).then(ListInitialDatas, cantLoadInitialDatas); + + + function ListInitialDatas(response) { + $scope.cyberpanelLoading = true; + $scope.populateCurrentRecords(); + if (response.data.status === 1) { + new PNotify({ + title: 'Success!', + text: 'Destination successfully removed.', + type: 'success' + }); + } else { + new PNotify({ + title: 'Operation Failed!', + text: response.data.error_message, + type: 'error' + }); + } + + } + + function cantLoadInitialDatas(response) { + $scope.cyberpanelLoading = true; + new PNotify({ + title: 'Operation Failed!', + text: 'Could not connect to server, please refresh this page', + type: 'error' + }); + } + + }; + + +}); diff --git a/backup/templates/backup/backupDestinations.html b/backup/templates/backup/backupDestinations.html index 5ab7e5ad9..8107e5c2f 100755 --- a/backup/templates/backup/backupDestinations.html +++ b/backup/templates/backup/backupDestinations.html @@ -3,134 +3,202 @@ {% block title %}{% trans "Set up Back up Destinations" %}{% endblock %} {% block content %} -{% load static %} - - -{% get_current_language as LANGUAGE_CODE %} - - -
-
-

{% trans "Set up Back up Destinations" %} - {% trans "Remote Backups" %}

-

{% trans "On this page you can set up your Back up destinations. (SFTP)" %}

-
- -
-
-

- {% trans "Set up Back up Destinations (SSH port should be 22 on backup server)" %} -

-
- - -
- - -
- -
- -
-
- -
- -
- -
-
- - -
- -
- -
-
- -
- -
- -
-
- -
- -
- - -
-
- - - - - -
- -
-
-

{% trans "Connection to" %} {$ IPAddress $} {% trans "failed. Please delete and re-add. " %} {$ errorMessage $}

-
- -
-

{% trans "Connection to" %} {$ IPAddress $} {% trans "successful." %}

-
- -
-

{% trans "Cannot add destination. Error message:" %} {$ errorMessage $}

-
- -
-

{% trans "Destination Added." %}

-
- -
-

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

-
-
-
- -
- -
- - - - - - - - - - - - - - - - - - - -
{% trans "ID" %}{% trans "IP" %}{% trans "Check Connection" %}{% trans "Delete" %}
-
-
- - - - -
+ {% load static %} + {% get_current_language as LANGUAGE_CODE %} + +
+
+

{% trans "Set up Back up Destinations" %} - {% trans "Remote Backups" %} +

+

{% trans "On this page you can set up your Back up destinations. (SFTP)" %}

+ +
+
+

+ {% trans "Set up Back up Destinations." %} +

+
+ + +
+ +
+ +
+ +
+
+ + + +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ + +
+
+ + + + + + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
{% trans "Name" %}{% trans "IP" %}{% trans "Username" %}{% trans "Path" %}{% trans "Port" %}{% trans "Delete" %}
+
+
+
+ + + + + + + + +
+ +
+ +
+
+ +
+ +
+ + +
+
+ + + + + + + + +
+ +
+ + + + + + + + + + + + + + + +
{% trans "Path" %}{% trans "Delete" %}
+
+
+
+ + + + + + + +
+ + +
+
+
+ +
-
- - -
{% endblock %} \ No newline at end of file diff --git a/websiteFunctions/models.py b/websiteFunctions/models.py index db9e6a516..bf1231638 100755 --- a/websiteFunctions/models.py +++ b/websiteFunctions/models.py @@ -75,4 +75,11 @@ class GDriveSites(models.Model): class GDriveJobLogs(models.Model): owner = models.ForeignKey(GDrive, on_delete=models.CASCADE) status = models.IntegerField() - message = models.TextField() \ No newline at end of file + message = models.TextField() + + +### Normal backup models + +class NormalBackupDests(models.Model): + name = models.CharField(max_length=25) + config = models.TextField()