2015-06-25 49 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') 

这适用于我。