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
def RemoteBackup(function):
try:
# print("....start remote backup...............")
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)
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))
print("....start remote backup........site.......%s.. and bakuptype...%s" % (config.Name, backuptype))
if backuptype == 'Only DataBase':
Backuptype = "3"
elif backuptype == 'Only Website':
@@ -1140,12 +1140,12 @@ Automatic backup failed for %s on %s.
continue
try:
allRemoteBackupsiteobj = RemoteBackupsites.objects.filter(owner=config)
# print("store site id.....%s"%str(allRemoteBackupsiteobj))
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)
print("site name.....%s"%wpsite.title)
AdminID = wpsite.owner.admin_id
Admin = Administrator.objects.get(pk=AdminID)
@@ -1401,23 +1401,42 @@ Automatic backup failed for %s on %s.
Password = config['Password']
Path = config['Path']
cnopts = sftp.CnOpts()
cnopts.hostkeys = None
# Connect to the remote server using the private key
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
with pysftp.Connection(HostName, username=Username, password=Password, cnopts=cnopts) as sftp:
print("Connection succesfully stablished ... ")
# Connect to the server using the private key
ssh.connect(HostName, username=Username, password=Password)
sftp = ssh.open_sftp()
ssh.exec_command(f'mkdir -p {Path}')
try:
with sftp.cd(Path):
sftp.put(FileName)
except:
sftp.mkdir(Path)
with sftp.cd(Path):
sftp.put(FileName)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(f"Filename: {FileName}, Path {Path}/{FileName.split('/')[-1]}. [SendTORemote]")
sftp.put(FileName, f"{Path}/{FileName.split('/')[-1]}")
# sftp.get(str(remotepath), str(loaclpath),
# 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:
print('%s. [SendTORemote]' % (str(msg)))
logging.writeToFile('%s. [SendTORemote]' % (str(msg)))
@staticmethod

View File

@@ -2643,7 +2643,7 @@ class ApplicationInstaller(multi.Thread):
backupobj.save()
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:
raise BaseException(stdout)
@@ -2948,6 +2948,7 @@ class ApplicationInstaller(multi.Thread):
ProcessUtilities.executioner(command)
except:
pass
logging.statusWriter(self.tempStatusPath, f'{str(msg)}. [404]')
return 0, str(msg), None
@@ -3002,19 +3003,45 @@ class ApplicationInstaller(multi.Thread):
Username = RemoteBackupconf['Username']
Password = RemoteBackupconf['Password']
Path = RemoteBackupconf['Path']
cnopts = sftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(HostName, username=Username, password=Password, cnopts=cnopts) as sftp:
####
# 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))
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
# if BackupType == 'DataBase Backup':
# if (DomainName == "" and int(self.extraArgs['DesSiteID']) != -1):
@@ -5280,6 +5307,14 @@ class ApplicationInstaller(multi.Thread):
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')
#### Make temp dir ab for unzip
@@ -5295,6 +5330,15 @@ class ApplicationInstaller(multi.Thread):
if result == 0:
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
if BackupType == 'DataBase Backup':
if (DomainName == "" and int(self.extraArgs['DesSiteID']) != -1):
@@ -5870,10 +5914,10 @@ class ApplicationInstaller(multi.Thread):
### Remove temppath
command = f'rm -rf {self.tempPath}'
# result, stdout = ProcessUtilities.outputExecutioner(command, None, None, None, 1)
#
# if result == 0:
# raise BaseException(stdout)
result, stdout = ProcessUtilities.outputExecutioner(command, None, None, None, 1)
if result == 0:
raise BaseException(stdout)
# try:
# WPobj = WPSites.objects.get(FinalURL=finalurl, owner=webobj)
@@ -6308,21 +6352,21 @@ class ApplicationInstaller(multi.Thread):
logging.statusWriter(self.tempStatusPath, 'Replacing URLs...,90')
########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)
result, stdout = ProcessUtilities.outputExecutioner(command, None, None, None, 1)
if stdout.find('Error:') > -1:
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)
result, stdout = ProcessUtilities.outputExecutioner(command, None, None, None, 1)
if stdout.find('Error:') > -1:
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)
##Remove temppath
@@ -6343,7 +6387,7 @@ class ApplicationInstaller(multi.Thread):
logging.writeToFile("Error RestoreWPbackupNow ....... %s" % str(msg))
try:
command = f'rm -rf {self.tempPath}'
#ProcessUtilities.executioner(command)
ProcessUtilities.executioner(command)
except:
pass

View File

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