Running remote mysqldump over SSH all in python -
hi im trying write python script ssh remote server , execute mysqldump. method far establish portforwarding run backup script had written. suspect issue in portforwarding backup script pretty straightforward. here portforwarding:
import getpass import os import socket import select import socketserver import sys import paramiko username = 'myusername' remote_server = 'remote.servername' remote_port = 3306 local_server = '127.0.0.1' local_port = 3307 ssh_port = 22 password = none keyfile = 'hosting.pem' g_verbose = true class forwardserver (socketserver.threadingtcpserver): daemon_threads = true allow_reuse_address = true class handler (socketserver.baserequesthandler): def handle(self): try: chan = self.ssh_transport.open_channel('direct-tcpip', (self.chain_host, self.chain_port), self.request.getpeername()) except exception, e: verbose('incoming request %s:%d failed: %s' % (self.chain_host, self.chain_port, repr(e))) return if chan none: verbose('incoming request %s:%d rejected ssh server.' % (self.chain_host, self.chain_port)) return verbose('connected! tunnel open %r -> %r -> %r' % (self.request.getpeername(), chan.getpeername(), (self.chain_host, self.chain_port))) while true: r, w, x = select.select([self.request, chan], [], []) if self.request in r: data = self.request.recv(1024) if len(data) == 0: break chan.send(data) if chan in r: data = chan.recv(1024) if len(data) == 0: break self.request.send(data) chan.close() self.request.close() verbose('tunnel closed %r' % (self.request.getpeername(),)) def forward_tunnel(local_port, remote_host, remote_port, transport): # little convoluted, lets me configure things handler # object. (socketserver doesn't give handlers way access outer # server normally.) class subhander (handler): chain_host = remote_host chain_port = remote_port ssh_transport = transport forwardserver(('', local_port), subhander).serve_forever() def verbose(s): if g_verbose: print s def pforward(): paramiko.util.log_to_file('demo.log') client = paramiko.sshclient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.warningpolicy()) try: client.connect(remote_server, ssh_port, username, key_filename=keyfile, look_for_keys=true) except exception, e: print '*** failed connect %s:%d: %r' % (remote_server, ssh_port, e) sys.exit(1) verbose('now forwarding port %d %s:%d ...' % (local_port, remote_server, remote_port)) try: forward_tunnel(local_port, remote_server, remote_port, client.get_transport()) except keyboardinterrupt: print 'c-c: port forwarding stopped.' sys.exit(0) pforward()
then once thats run mysql backup script:
import mysqldb import os conn = mysqldb.connect ( host = "127.0.0.1", user = "root", passwd = "password" port = 3307) cursor = conn.cursor() cursor.execute("show databases") results = cursor.fetchall() cursor.close() conn.close() print results result in results: backupfile=result[0]+".sql.gz" cmd="echo 'back "+result[0]+" database yourlocation/"+backupfile+"'" os.system(cmd) cmd="mysqldump -u "+user+" -h "+host+" -p"+passwd+" --opt --routines --flush-privileges --single-transaction --database "+result[0]+" | gzip -9 --rsyncable > yourlocation/"+backupfile
this error get:
_mysql_exceptions.operationalerror: (2013, "lost connection mysql server @ 'waiting initial communication packet', system error: 0")
ok- guess over-thinking it, no need set port forwarding , mess. here solution>
# -*- coding: utf-8 -*- # <nbformat>3.0</nbformat> import paramiko import os savefile = 'dump.sql' mykey = paramiko.rsakey.from_private_key_file("/users/me/my-host.pem") client = paramiko.sshclient() client.load_system_host_keys() client.connect('hungry.turtles.com', username = "turtles", pkey = mykey) def ssh(cmd): out = [] msg = [stdin, stdout, stderr] = client.exec_command(cmd) item in msg: try: line in item: out.append(line.strip('\n')) except: pass return(list(out)) dump = ssh('mysqldump -u root -ppassword turtleturds') file = open(savefile, 'w') file.write(str(dump)) file.close() print 'the dump had '+ str(len(dump))+ ' lines , saved '+ str(os.path.realpath('dump.sql'))
Comments
Post a Comment