Mongorestore in remote host using paramiko

752 views Asked by At

I am trying to restore a mongodump in remote machine from my local using a python script. My script goes through fine but I don't see restored db.

Mongo dump in remote

/root/dump

My script

#!/usr/bin/python

import paramiko
import sys
import os

ssh = paramiko.SSHClient()
ssh.connect('192.168.3.54',username='xx',password='xx')
ssh.exec_command('cd /root')
stdin,stdout,stderr = ssh.exec_command('mongorestore --db mydatabase')
print stdout.read()
ssh.close()

Log from terminal

DEBUG:root:Excecuting Command in Remote:mongorestore --db mydatabase
DEBUG:root:Making SSH Connection to host
DEBUG:paramiko.transport:starting thread (client mode): 0x882e50L
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_5.3)
DEBUG:paramiko.transport:kex algos:[u'diffie-hellman-group-exchange-sha256', u'diffie-hellman-group-exchange-sha1', u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1'] server key:[u'ssh-rsa', u'ssh-dss'] client encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'[email protected]'] server encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'[email protected]'] client mac:[u'hmac-md5', u'hmac-sha1', u'[email protected]', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-ripemd160', u'[email protected]', u'hmac-sha1-96', u'hmac-md5-96'] server mac:[u'hmac-md5', u'hmac-sha1', u'[email protected]', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-ripemd160', u'[email protected]', u'hmac-sha1-96', u'hmac-md5-96'] client compress:[u'none', u'[email protected]'] server compress:[u'none', u'[email protected]'] client lang:[u''] server lang:[u''] kex follows?False
DEBUG:paramiko.transport:Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
DEBUG:paramiko.transport:using kex diffie-hellman-group14-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, remote none
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ssh-rsa host key for 192.168.3.54: 6a1fb59780077de6b0f1a11fb8a3655c
DEBUG:paramiko.transport:Trying discovered key 7b2147c9ba61f08f0680606e6c92c17e in /Users/BharathMBA/.ssh/id_rsa
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (publickey) failed.
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
INFO:paramiko.transport.sftp:[chan 0] Opened sftp connection (server version 3)
DEBUG:paramiko.transport:[chan 1] Max packet in: 32768 bytes
DEBUG:paramiko.transport:[chan 1] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 1 opened.
DEBUG:paramiko.transport:[chan 1] Sesch channel 1 request ok
DEBUG:paramiko.transport:[chan 1] EOF received (1)
DEBUG:root:stdout:connected to: 127.0.0.1

DEBUG:root:Closing SSH and FTP Connections
INFO:paramiko.transport.sftp:[chan 0] sftp session closed.
DEBUG:paramiko.transport:[chan 1] EOF sent (1)
DEBUG:paramiko.transport:[chan 0] EOF sent (0)
DEBUG:paramiko.transport:EOF in transport thread

As you can notice from log the stdout says connected to 127.0.0.1 but no restore happens.

Any suggestions please

1

There are 1 answers

0
slysid On

I found out what the problem is. I have two ssh_exec statements in my script, the first is to position my PWD to mongo dump folder and second one is to restore the dump. What I found is when the second command is executed, the PWD is not the one set and it is the default $HOME directory. so i changed my script to combine both exec statements into one

stdin,stdout,stderr = ssh.exec_command('mongorestore --db mydatabase /root/dump')

This works for me.