added mysqldump support

This commit is contained in:
christian
2021-11-02 21:36:38 +01:00
parent 30f4228f24
commit 33749c149c
6 changed files with 47 additions and 8 deletions
+15 -2
View File
@@ -3,6 +3,7 @@ from paramiko.ssh_exception import SSHException, NoValidConnectionsError
import time
import hashlib
import stat
import sys
from datetime import datetime, timedelta
import os
@@ -43,11 +44,18 @@ class sftp_remote:
def ssh_disconnect(self):
self.ssh.close()
def zip_remote_directory(self, job):
def remote_channel_command(self, job, command_state):
zipname = f'{job["zipname"]}-{str(datetime.now()).split(" ")[0]}.zip'
self.ssh_connect()
channel = self.ssh.get_transport().open_session(timeout=120)
exec_cmd = f'cd /home/{self.username}/; zip -r {zipname} {self.path}'
if 'zip_recursive' == command_state:
exec_command_state = f'zip -r {zipname} {self.path}'
elif 'mysqldump' == command_state:
#check README for my.cnf regarding how this works
exec_command_state = f'mysqldump -u root --all-databases > mysqldump.sql'
else: #zip_mysqldump
exec_command_state = f'zip {zipname} /home/{self.username}/mysqldump.sql'
exec_cmd = f'cd /home/{self.username}/; {exec_command_state}'
keyboard_interrupt = False
try:
channel.exec_command(exec_cmd)
@@ -63,6 +71,11 @@ class sftp_remote:
keyboard_interrupt = True
finally:
channel.close()
if 'zip_mysqldump' == command_state:
file_path = f'/home/{self.username}/mysqldump.sql'
self.connect()
self.sftp.remove(file_path)
self.disconnect()
self.ssh_disconnect()
if keyboard_interrupt:
print("manually interrupted")