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: except:
pass pass
nbj.config = json.dumps(config) nbj.config = json.dumps(config)
nbj.save() nbj.save()

View File

@@ -12,7 +12,7 @@
<div class="container"> <div class="container">
<div id="page-title"> <div id="page-title">
<h2>{% trans "Set up Backup Destinations" %} - <a target="_blank" <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;" style="height: 23px;line-height: 21px;"
class="btn btn-border btn-alt border-red btn-link font-red" class="btn btn-border btn-alt border-red btn-link font-red"
title=""><span>{% trans "Remote Backups" %}</span></a> title=""><span>{% trans "Remote Backups" %}</span></a>

View File

@@ -12,7 +12,7 @@
<div ng-controller="scheduleBackup" class="container"> <div ng-controller="scheduleBackup" class="container">
<div id="page-title"> <div id="page-title">
<h2>{% trans "Schedule Backup" %} - <a target="_blank" <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" style="height: 23px;line-height: 21px; text-decoration: underline"
class="btn btn-border btn-alt border-red btn-link font-red" class="btn btn-border btn-alt border-red btn-link font-red"
title=""><span>{% trans "Remote Backups" %}</span></a> title=""><span>{% trans "Remote Backups" %}</span></a>

View File

@@ -1,6 +1,9 @@
#!/usr/local/CyberCP/bin/python #!/usr/local/CyberCP/bin/python
import os.path import os.path
import sys import sys
import paramiko
sys.path.append('/usr/local/CyberCP') sys.path.append('/usr/local/CyberCP')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
import django import django
@@ -509,6 +512,7 @@ class IncScheduler(multi.Thread):
destinationConfig = json.loads(backupjob.owner.config) destinationConfig = json.loads(backupjob.owner.config)
currentTime = time.strftime("%m.%d.%Y_%H-%M-%S") currentTime = time.strftime("%m.%d.%Y_%H-%M-%S")
print(destinationConfig['type'])
if destinationConfig['type'] == 'local': if destinationConfig['type'] == 'local':
@@ -620,18 +624,51 @@ Automatic backup failed for %s on %s.
backupjob.config = json.dumps(jobConfig) backupjob.config = json.dumps(jobConfig)
backupjob.save() backupjob.save()
else: else:
if jobConfig[IncScheduler.frequency] == type: if jobConfig[IncScheduler.frequency] == type:
print(jobConfig[IncScheduler.frequency])
import subprocess
import shlex
finalPath = '%s/%s' % (destinationConfig['path'].rstrip('/'), currentTime) finalPath = '%s/%s' % (destinationConfig['path'].rstrip('/'), currentTime)
command = "ssh -o StrictHostKeyChecking=no -p " + destinationConfig[
'port'] + " -i /root/.ssh/cyberpanel " + destinationConfig['username'] + "@" + \ # import subprocess
destinationConfig[ # import shlex
'ip'] + " mkdir -p %s" % (finalPath) # command = "ssh -o StrictHostKeyChecking=no -p " + destinationConfig[
subprocess.call(shlex.split(command)) # '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. ### Check if an old job prematurely killed, then start from there.
# try: # try:
@@ -1550,5 +1587,6 @@ def main():
ib.join() ib.join()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

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

View File

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