2015-06-25 72 views
0

我想從我的本地使用python腳本恢復遠程機器上的mongodump。我的腳本經過很好,但我沒有看到恢復的數據庫。使用paramiko在遠程主機Mongorestore

蒙戈轉儲遠程

/root/dump 

我的腳本終端

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]ssh.com', 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 

#!/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() 

登錄你可以從日誌標準輸出通知說,連接到127.0.0.1,但沒有還原發生。

任何建議請

回答

0

我發現了什麼問題。我在腳本中有兩個ssh_exec語句,第一個是將我的PWD定位到mongo轉儲文件夾,第二個是恢復轉儲。我發現當第二個命令執行時,PWD不是一個集合,它是默認的$ HOME目錄。所以我改變了我的腳本將兩個exec語句合併爲一個

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

這適用於我。