Skip to content

Commit

Permalink
bug fix: wp backups to s3 arae improved using paramiko
Browse files Browse the repository at this point in the history
  • Loading branch information
usmannasir committed Oct 1, 2024
1 parent ef5d8c3 commit 37d51ae
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 37 deletions.
51 changes: 35 additions & 16 deletions plogical/IncScheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,18 +1117,18 @@ def WPUpdates():
@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':
Expand All @@ -1140,12 +1140,12 @@ def RemoteBackup(function):
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)

Expand Down Expand Up @@ -1401,23 +1401,42 @@ def SendTORemote(FileName, RemoteBackupID):
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
Expand Down
82 changes: 63 additions & 19 deletions plogical/applicationInstaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2643,7 +2643,7 @@ def WPCreateBackup(self):
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)
Expand Down Expand Up @@ -2948,6 +2948,7 @@ def WPCreateBackup(self):
ProcessUtilities.executioner(command)
except:
pass

logging.statusWriter(self.tempStatusPath, f'{str(msg)}. [404]')
return 0, str(msg), None

Expand Down Expand Up @@ -3002,18 +3003,44 @@ def RestoreWPbackupNow(self):
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:
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
# if BackupType == 'DataBase Backup':
Expand Down Expand Up @@ -5280,6 +5307,14 @@ def RestoreWPbackupNow(self):
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
Expand All @@ -5295,6 +5330,15 @@ def RestoreWPbackupNow(self):
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):
Expand Down Expand Up @@ -5870,10 +5914,10 @@ def RestoreWPbackupNow(self):

### 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)
Expand Down Expand Up @@ -6308,21 +6352,21 @@ def RestoreWPbackupNow(self):

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
Expand All @@ -6343,7 +6387,7 @@ def RestoreWPbackupNow(self):
logging.writeToFile("Error RestoreWPbackupNow ....... %s" % str(msg))
try:
command = f'rm -rf {self.tempPath}'
#ProcessUtilities.executioner(command)
ProcessUtilities.executioner(command)

except:
pass
Expand Down
4 changes: 2 additions & 2 deletions websiteFunctions/static/websiteFunctions/websiteFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1967,7 +1967,7 @@ app.controller('RestoreWPBackup', function ($scope, $http, $timeout, $window) {


function ListInitialDatas(response) {
$('#wordpresshomeloading').hide();
//$('#wordpresshomeloading').hide();

if (response.data.abort === 1) {

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 37d51ae

Please sign in to comment.