bug fix: use paramiko for ssh operations

This commit is contained in:
usmannasir
2024-02-11 14:42:16 +05:00
parent 9de6243c26
commit 670e46c309
6 changed files with 63 additions and 21 deletions

View File

@@ -1754,6 +1754,7 @@ class BackupManager:
except:
pass
nbj.config = json.dumps(config)
nbj.save()

View File

@@ -12,7 +12,7 @@
<div class="container">
<div id="page-title">
<h2>{% trans "Set up Backup Destinations" %} - <a target="_blank"
href="https://go.cyberpanel.net/remote-backup"
href="https://cyberpanel.net/KnowledgeBase/home/add-destination-scheduled-local-sftp-remote-backups/"
style="height: 23px;line-height: 21px;"
class="btn btn-border btn-alt border-red btn-link font-red"
title=""><span>{% trans "Remote Backups" %}</span></a>

View File

@@ -12,7 +12,7 @@
<div ng-controller="scheduleBackup" class="container">
<div id="page-title">
<h2>{% trans "Schedule Backup" %} - <a target="_blank"
href="https://go.cyberpanel.net/remote-backup"
href="https://cyberpanel.net/KnowledgeBase/home/schedule-backups-local-or-sftp/"
style="height: 23px;line-height: 21px; text-decoration: underline"
class="btn btn-border btn-alt border-red btn-link font-red"
title=""><span>{% trans "Remote Backups" %}</span></a>

View File

@@ -1,6 +1,9 @@
#!/usr/local/CyberCP/bin/python
import os.path
import sys
import paramiko
sys.path.append('/usr/local/CyberCP')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
import django
@@ -509,6 +512,7 @@ class IncScheduler(multi.Thread):
destinationConfig = json.loads(backupjob.owner.config)
currentTime = time.strftime("%m.%d.%Y_%H-%M-%S")
print(destinationConfig['type'])
if destinationConfig['type'] == 'local':
@@ -620,18 +624,51 @@ Automatic backup failed for %s on %s.
backupjob.config = json.dumps(jobConfig)
backupjob.save()
else:
if jobConfig[IncScheduler.frequency] == type:
import subprocess
import shlex
print(jobConfig[IncScheduler.frequency])
finalPath = '%s/%s' % (destinationConfig['path'].rstrip('/'), currentTime)
command = "ssh -o StrictHostKeyChecking=no -p " + destinationConfig[
'port'] + " -i /root/.ssh/cyberpanel " + destinationConfig['username'] + "@" + \
destinationConfig[
'ip'] + " mkdir -p %s" % (finalPath)
subprocess.call(shlex.split(command))
# import subprocess
# import shlex
# command = "ssh -o StrictHostKeyChecking=no -p " + destinationConfig[
# 'port'] + " -i /root/.ssh/cyberpanel " + destinationConfig['username'] + "@" + \
# destinationConfig[
# 'ip'] + " mkdir -p %s" % (finalPath)
# subprocess.call(shlex.split(command))
### improved paramiko code
private_key_path = '/root/.ssh/cyberpanel'
# Create an SSH client
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Load the private key
private_key = paramiko.RSAKey.from_private_key_file(private_key_path)
# Connect to the server using the private key
try:
ssh.connect(destinationConfig['ip'], port=int(destinationConfig['port']), username=destinationConfig['username'], pkey=private_key)
except BaseException as msg:
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
message=f'Failed to make sftp connection {str(msg)}').save()
print(str(msg))
continue
# Execute the command to create the remote directory
command = f'mkdir -p {finalPath}'
stdin, stdout, stderr = ssh.exec_command(command)
# Wait for the command to finish and check for any errors
stdout.channel.recv_exit_status()
error_message = stderr.read().decode('utf-8')
print(error_message)
if error_message:
NormalBackupJobLogs(owner=backupjob, status=backupSchedule.INFO,
message=f'Error while creating directory on remote server {error_message.strip()}').save()
continue
else:
pass
### Check if an old job prematurely killed, then start from there.
# try:
@@ -1550,5 +1587,6 @@ def main():
ib.join()
if __name__ == "__main__":
main()

View File

@@ -1316,6 +1316,7 @@ class backupUtilities:
return [1, "None"]
except paramiko.AuthenticationException:
return [0, 'Authentication failed. [sendKey]']
except paramiko.SSHException as e:

View File

@@ -5,16 +5,17 @@ Babel==2.8.0
backports.ssl-match-hostname==3.7.0.1
bcrypt==3.2.0
beautifulsoup4==4.9.3
boto3==1.18.26
botocore==1.21.26
boto3==1.34.39
botocore==1.34.39
cachetools==4.1.1
certifi==2020.11.8
cffi==1.14.3
chardet==3.0.4
charset-normalizer==2.0.12
cloudflare==2.8.13
ConfigArgParse==1.2.3
configobj==5.0.6
cryptography==3.2.1
cryptography==42.0.2
decorator==4.4.2
Django==3.1.3
docker==4.3.1
@@ -44,10 +45,9 @@ MarkupSafe==1.1.1
mock==4.0.2
mysqlclient==2.0.1
oauthlib==3.1.0
paramiko==2.7.2
paramiko==3.4.0
parsedatetime==2.6
pbr==5.5.1
pysftp
pexpect==4.8.0
prettytable==1.0.1
protobuf==3.13.0
@@ -58,20 +58,22 @@ pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.20
pygpgme==0.3
PyNaCl==1.4.0
pyOpenSSL==19.1.0
PyNaCl==1.5.0
pyOpenSSL==24.0.0
pyotp==2.4.1
pyRFC3339==1.1
pysftp==0.2.9
python-dateutil==2.8.1
pytz==2020.4
pyudev==0.22.0
pyxattr==0.7.1
PyYAML==6.0.1
requests==2.26.0
requests-file==1.5.1
requests-oauthlib==1.3.0
requests-toolbelt==0.9.1
rsa==4.6
s3transfer==0.5.0
s3transfer==0.10.0
six==1.15.0
soupsieve==2.0.1
sqlparse==0.4.2