From c6483559695237880370b129bc91a2cdafeb3fb4 Mon Sep 17 00:00:00 2001 From: "usman@cyberpersons.com" Date: Wed, 8 Mar 2023 11:39:36 +0500 Subject: [PATCH] add function to list repo snapshots --- plogical/Backupsv2.py | 11 ++++++++ plogical/mysqlUtilities.py | 52 +++++++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/plogical/Backupsv2.py b/plogical/Backupsv2.py index 3854c0813..69f8f258f 100644 --- a/plogical/Backupsv2.py +++ b/plogical/Backupsv2.py @@ -44,6 +44,16 @@ class CPBackupsV2: self.repo = f"rclone:{self.data['BackendName']}:{self.data['domain']}" + + def FetchSnapShots(self): + try: + command = f'rustic -r {self.repo} snapshots --password "" --json 2>/dev/null' + result = json.loads( + ProcessUtilities.outputExecutioner(command, self.website.externalApp, True).rstrip('\n')) + return 1, result + except BaseException as msg: + return 0, str(msg) + def SetupRcloneBackend(self, type, config): self.LocalRclonePath = f'/home/{self.website.domain}/.config/rclone' self.ConfigFilePath = f'{self.LocalRclonePath}/rclone.conf' @@ -509,6 +519,7 @@ pass = {ObsecurePassword} command = f'rustic -r {self.repo} backup {source} {self.FinalPathRuctic}/config.json --password "" {exclude} --json 2>/dev/null' result = json.loads(ProcessUtilities.outputExecutioner(command, self.website.externalApp, True).rstrip('\n')) + try: SnapShotID = result['id'] ## snapshot id that we need to store in db files_new = result['summary']['files_new'] ## basically new files in backup diff --git a/plogical/mysqlUtilities.py b/plogical/mysqlUtilities.py index f09354c54..cda8ebb27 100755 --- a/plogical/mysqlUtilities.py +++ b/plogical/mysqlUtilities.py @@ -245,7 +245,7 @@ class mysqlUtilities: return str(msg) @staticmethod - def createDatabaseBackup(databaseName, tempStoragePath): + def createDatabaseBackup(databaseName, tempStoragePath, rustic=0, RusticRepoName = None, RusticConfigPath = None): try: passFile = "/etc/cyberpanel/mysqlPassword" @@ -284,26 +284,42 @@ password=%s os.chmod(cnfPath, 0o600) - command = 'mysqldump --defaults-extra-file=/home/cyberpanel/.my.cnf -u %s --host=%s --port %s %s' % (mysqluser, mysqlhost, mysqlport, databaseName) + SHELL = False + + if rustic == 0: + + command = 'mysqldump --defaults-extra-file=/home/cyberpanel/.my.cnf -u %s --host=%s --port %s %s' % (mysqluser, mysqlhost, mysqlport, databaseName) + + if os.path.exists(ProcessUtilities.debugPath): + logging.CyberCPLogFileWriter.writeToFile(command) + + cmd = shlex.split(command) + + try: + errorPath = '/home/cyberpanel/error-logs.txt' + errorLog = open(errorPath, 'a') + with open(tempStoragePath + "/" + databaseName + '.sql', 'w') as f: + res = subprocess.call(cmd, stdout=f, stderr=errorLog, shell=SHELL) + if res != 0: + logging.CyberCPLogFileWriter.writeToFile( + "Database: " + databaseName + "could not be backed! [createDatabaseBackup]") + return 0 + except subprocess.CalledProcessError as msg: + logging.CyberCPLogFileWriter.writeToFile( + "Database: " + databaseName + "could not be backed! Error: %s. [createDatabaseBackup]" % ( + str(msg))) + return 0 + + else: + SHELL = True + + command = f'mysqldump --defaults-extra-file=/home/cyberpanel/.my.cnf -u {mysqluser} --host={mysqlhost} --port {mysqlport} --add-drop-table --allow-keywords --complete-insert --quote-names --skip-comments {databaseName} | rustic -r {RusticRepoName} backup --stdin-filename {databaseName}.sql {RusticConfigPath} --password "" --json 2>/dev/null' + + if os.path.exists(ProcessUtilities.debugPath): + logging.CyberCPLogFileWriter.writeToFile(command) - if os.path.exists(ProcessUtilities.debugPath): - logging.CyberCPLogFileWriter.writeToFile(command) - cmd = shlex.split(command) - try: - errorPath = '/home/cyberpanel/error-logs.txt' - errorLog = open(errorPath, 'a') - with open(tempStoragePath+"/"+databaseName+'.sql', 'w') as f: - res = subprocess.call(cmd,stdout=f, stderr=errorLog) - if res != 0: - logging.CyberCPLogFileWriter.writeToFile( - "Database: " + databaseName + "could not be backed! [createDatabaseBackup]") - return 0 - except subprocess.CalledProcessError as msg: - logging.CyberCPLogFileWriter.writeToFile( - "Database: " + databaseName + "could not be backed! Error: %s. [createDatabaseBackup]" % (str(msg))) - return 0 return 1 except BaseException as msg: logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[createDatabaseBackup]")