bug fix: wp backups to s3 arae improved using paramiko

This commit is contained in:
usmannasir
2024-10-01 12:02:45 +05:00
parent ef5d8c3c15
commit 37d51aed41
3 changed files with 100 additions and 37 deletions

View File

@@ -1117,18 +1117,18 @@ Automatic backup failed for %s on %s.
@staticmethod @staticmethod
def RemoteBackup(function): def RemoteBackup(function):
try: try:
# print("....start remote backup...............") print("....start remote backup...............")
from websiteFunctions.models import RemoteBackupSchedule, RemoteBackupsites, WPSites from websiteFunctions.models import RemoteBackupSchedule, RemoteBackupsites, WPSites
from loginSystem.models import Administrator from loginSystem.models import Administrator
import json import json
import time import time
from plogical.applicationInstaller import ApplicationInstaller from plogical.applicationInstaller import ApplicationInstaller
for config in RemoteBackupSchedule.objects.all(): for config in RemoteBackupSchedule.objects.all():
# print("....start remote backup........site.......%s"%config.Name) print("....start remote backup........site.......%s"%config.Name)
try: try:
configbakup = json.loads(config.config) configbakup = json.loads(config.config)
backuptype = configbakup['BackupType'] backuptype = configbakup['BackupType']
# print("....start remote backup........site.......%s.. and bakuptype...%s" % (config.Name, backuptype)) print("....start remote backup........site.......%s.. and bakuptype...%s" % (config.Name, backuptype))
if backuptype == 'Only DataBase': if backuptype == 'Only DataBase':
Backuptype = "3" Backuptype = "3"
elif backuptype == 'Only Website': elif backuptype == 'Only Website':
@@ -1140,12 +1140,12 @@ Automatic backup failed for %s on %s.
continue continue
try: try:
allRemoteBackupsiteobj = RemoteBackupsites.objects.filter(owner=config) allRemoteBackupsiteobj = RemoteBackupsites.objects.filter(owner=config)
# print("store site id.....%s"%str(allRemoteBackupsiteobj)) print("store site id.....%s"%str(allRemoteBackupsiteobj))
for i in allRemoteBackupsiteobj: for i in allRemoteBackupsiteobj:
try: try:
backupsiteID = i.WPsites backupsiteID = i.WPsites
wpsite = WPSites.objects.get(pk=backupsiteID) wpsite = WPSites.objects.get(pk=backupsiteID)
# print("site name.....%s"%wpsite.title) print("site name.....%s"%wpsite.title)
AdminID = wpsite.owner.admin_id AdminID = wpsite.owner.admin_id
Admin = Administrator.objects.get(pk=AdminID) Admin = Administrator.objects.get(pk=AdminID)
@@ -1401,23 +1401,42 @@ Automatic backup failed for %s on %s.
Password = config['Password'] Password = config['Password']
Path = config['Path'] Path = config['Path']
cnopts = sftp.CnOpts() # Connect to the remote server using the private key
cnopts.hostkeys = None ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
with pysftp.Connection(HostName, username=Username, password=Password, cnopts=cnopts) as sftp: # Connect to the server using the private key
print("Connection succesfully stablished ... ") ssh.connect(HostName, username=Username, password=Password)
sftp = ssh.open_sftp()
ssh.exec_command(f'mkdir -p {Path}')
try: if os.path.exists(ProcessUtilities.debugPath):
with sftp.cd(Path): logging.writeToFile(f"Filename: {FileName}, Path {Path}/{FileName.split('/')[-1]}. [SendTORemote]")
sftp.put(FileName)
except: sftp.put(FileName, f"{Path}/{FileName.split('/')[-1]}")
sftp.mkdir(Path)
with sftp.cd(Path): # sftp.get(str(remotepath), str(loaclpath),
sftp.put(FileName) # callback=self.UpdateDownloadStatus)
#
# cnopts = sftp.CnOpts()
# cnopts.hostkeys = None
#
# with pysftp.Connection(HostName, username=Username, password=Password, cnopts=cnopts) as sftp:
# print("Connection succesfully stablished ... ")
#
# try:
# with sftp.cd(Path):
# sftp.put(FileName)
# except BaseException as msg:
# print(f'Error on {str(msg)}')
# sftp.mkdir(Path)
# with sftp.cd(Path):
# sftp.put(FileName)
except BaseException as msg: except BaseException as msg:
print('%s. [SendTORemote]' % (str(msg)))
logging.writeToFile('%s. [SendTORemote]' % (str(msg))) logging.writeToFile('%s. [SendTORemote]' % (str(msg)))
@staticmethod @staticmethod

View File

@@ -2643,7 +2643,7 @@ class ApplicationInstaller(multi.Thread):
backupobj.save() backupobj.save()
command = f'rm -rf {self.tempPath}' command = f'rm -rf {self.tempPath}'
result, stdout = ProcessUtilities.outputExecutioner(command, None, None, None, 1) #result, stdout = ProcessUtilities.outputExecutioner(command, None, None, None, 1)
if result == 0: if result == 0:
raise BaseException(stdout) raise BaseException(stdout)
@@ -2948,6 +2948,7 @@ class ApplicationInstaller(multi.Thread):
ProcessUtilities.executioner(command) ProcessUtilities.executioner(command)
except: except:
pass pass
logging.statusWriter(self.tempStatusPath, f'{str(msg)}. [404]') logging.statusWriter(self.tempStatusPath, f'{str(msg)}. [404]')
return 0, str(msg), None return 0, str(msg), None
@@ -3002,18 +3003,44 @@ class ApplicationInstaller(multi.Thread):
Username = RemoteBackupconf['Username'] Username = RemoteBackupconf['Username']
Password = RemoteBackupconf['Password'] Password = RemoteBackupconf['Password']
Path = RemoteBackupconf['Path'] Path = RemoteBackupconf['Path']
cnopts = sftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(HostName, username=Username, password=Password, cnopts=cnopts) as sftp:
logging.statusWriter(self.tempStatusPath, 'Downloading Backups...,15')
loaclpath = "/home/cyberpanel/%s.tar.gz" % BackUpFileName
remotepath = "%s/%s.tar.gz" % (Path, BackUpFileName)
logging.writeToFile("Downloading start")
sftp.get(str(remotepath), str(loaclpath))
command = "mv %s /home/backup/" % loaclpath ####
ProcessUtilities.executioner(command)
# Connect to the remote server using the private key
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the server using the private key
ssh.connect(HostName, username=Username, password=Password)
sftp = ssh.open_sftp()
logging.statusWriter(self.tempStatusPath, 'Downloading Backups...,15')
loaclpath = "/home/cyberpanel/%s.tar.gz" % BackUpFileName
remotepath = "%s/%s.tar.gz" % (Path, BackUpFileName)
logging.writeToFile("Downloading start")
sftp.get(str(remotepath), str(loaclpath),
callback=self.UpdateDownloadStatus)
command = "mv %s /home/backup/" % loaclpath
ProcessUtilities.executioner(command)
##
# cnopts = sftp.CnOpts()
# cnopts.hostkeys = None
#
# with pysftp.Connection(HostName, username=Username, password=Password, cnopts=cnopts) as sftp:
# logging.statusWriter(self.tempStatusPath, 'Downloading Backups...,15')
# loaclpath = "/home/cyberpanel/%s.tar.gz" % BackUpFileName
# remotepath = "%s/%s.tar.gz" % (Path, BackUpFileName)
# logging.writeToFile("Downloading start")
# sftp.get(str(remotepath), str(loaclpath))
#
# command = "mv %s /home/backup/" % loaclpath
# ProcessUtilities.executioner(command)
# ##### CHeck if Backup type is Only Database # ##### CHeck if Backup type is Only Database
# if BackupType == 'DataBase Backup': # if BackupType == 'DataBase Backup':
@@ -5280,6 +5307,14 @@ class ApplicationInstaller(multi.Thread):
raise BaseException(stdout) raise BaseException(stdout)
if os.path.exists(ProcessUtilities.debugPath):
if os.path.exists(f'/home/backup/{BackUpFileName}'):
logging.writeToFile(f'Backup file is present and downloaded {str(stdout)}')
logging.writeToFile(f'Extracting to {str(self.tempPath)}')
logging.statusWriter(self.tempStatusPath, 'Extracting Backup File...,30') logging.statusWriter(self.tempStatusPath, 'Extracting Backup File...,30')
#### Make temp dir ab for unzip #### Make temp dir ab for unzip
@@ -5295,6 +5330,15 @@ class ApplicationInstaller(multi.Thread):
if result == 0: if result == 0:
raise BaseException(stdout) raise BaseException(stdout)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(f'Output of archive {str(stdout)}')
command = f"ls -lh {self.tempPath}/ab"
result, stdout = ProcessUtilities.outputExecutioner(command, None, None, None, 1)
logging.writeToFile(f'Listing files {str(stdout)}')
##### Check if Backup type is Only Database ##### Check if Backup type is Only Database
if BackupType == 'DataBase Backup': if BackupType == 'DataBase Backup':
if (DomainName == "" and int(self.extraArgs['DesSiteID']) != -1): if (DomainName == "" and int(self.extraArgs['DesSiteID']) != -1):
@@ -5870,10 +5914,10 @@ class ApplicationInstaller(multi.Thread):
### Remove temppath ### Remove temppath
command = f'rm -rf {self.tempPath}' command = f'rm -rf {self.tempPath}'
# result, stdout = ProcessUtilities.outputExecutioner(command, None, None, None, 1) result, stdout = ProcessUtilities.outputExecutioner(command, None, None, None, 1)
#
# if result == 0: if result == 0:
# raise BaseException(stdout) raise BaseException(stdout)
# try: # try:
# WPobj = WPSites.objects.get(FinalURL=finalurl, owner=webobj) # WPobj = WPSites.objects.get(FinalURL=finalurl, owner=webobj)
@@ -6308,21 +6352,21 @@ class ApplicationInstaller(multi.Thread):
logging.statusWriter(self.tempStatusPath, 'Replacing URLs...,90') logging.statusWriter(self.tempStatusPath, 'Replacing URLs...,90')
########Now Replace URL's ########Now Replace URL's
command = 'sudo -u %s /usr/local/lsws/lsphp74/bin/php -d error_reporting=0 /usr/bin/wp search-replace --skip-plugins --skip-themes --path=%s "%s" "%s"' % ( command = f'sudo -u %s {FinalPHPPath} -d error_reporting=0 /usr/bin/wp search-replace --skip-plugins --skip-themes --path=%s "%s" "%s"' % (
VHuser, newWPpath, oldurl, newurl) VHuser, newWPpath, oldurl, newurl)
result, stdout = ProcessUtilities.outputExecutioner(command, None, None, None, 1) result, stdout = ProcessUtilities.outputExecutioner(command, None, None, None, 1)
if stdout.find('Error:') > -1: if stdout.find('Error:') > -1:
raise BaseException(stdout) raise BaseException(stdout)
command = 'sudo -u %s /usr/local/lsws/lsphp74/bin/php -d error_reporting=0 /usr/bin/wp search-replace --skip-plugins --skip-themes --allow-root --path=%s "https://www.%s" "http://%s"' % ( command = f'sudo -u %s {FinalPHPPath} -d error_reporting=0 /usr/bin/wp search-replace --skip-plugins --skip-themes --allow-root --path=%s "https://www.%s" "http://%s"' % (
VHuser, newWPpath, newurl, newurl) VHuser, newWPpath, newurl, newurl)
result, stdout = ProcessUtilities.outputExecutioner(command, None, None, None, 1) result, stdout = ProcessUtilities.outputExecutioner(command, None, None, None, 1)
if stdout.find('Error:') > -1: if stdout.find('Error:') > -1:
raise BaseException(stdout) raise BaseException(stdout)
command = f'sudo -u {VHuser} /usr/local/lsws/lsphp74/bin/php -d error_reporting=0 /usr/bin/wp litespeed-purge all --path={newWPpath}' command = f'sudo -u {VHuser} {FinalPHPPath} -d error_reporting=0 /usr/bin/wp litespeed-purge all --path={newWPpath}'
ProcessUtilities.outputExecutioner(command, None, None, None, 1) ProcessUtilities.outputExecutioner(command, None, None, None, 1)
##Remove temppath ##Remove temppath
@@ -6343,7 +6387,7 @@ class ApplicationInstaller(multi.Thread):
logging.writeToFile("Error RestoreWPbackupNow ....... %s" % str(msg)) logging.writeToFile("Error RestoreWPbackupNow ....... %s" % str(msg))
try: try:
command = f'rm -rf {self.tempPath}' command = f'rm -rf {self.tempPath}'
#ProcessUtilities.executioner(command) ProcessUtilities.executioner(command)
except: except:
pass pass

View File

@@ -1967,7 +1967,7 @@ app.controller('RestoreWPBackup', function ($scope, $http, $timeout, $window) {
function ListInitialDatas(response) { function ListInitialDatas(response) {
$('#wordpresshomeloading').hide(); //$('#wordpresshomeloading').hide();
if (response.data.abort === 1) { if (response.data.abort === 1) {
@@ -2023,7 +2023,7 @@ app.controller('RestoreWPBackup', function ($scope, $http, $timeout, $window) {
} }
function cantLoadInitialDatas(response) { function cantLoadInitialDatas(response) {
$('#wordpresshomeloading').hide(); //$('#wordpresshomeloading').hide();
$scope.wordpresshomeloading = true; $scope.wordpresshomeloading = true;
$scope.stagingDetailsForm = true; $scope.stagingDetailsForm = true;
$scope.installationProgress = false; $scope.installationProgress = false;