Mongorestore在远程主机上使用paramiko

时间:2015-06-25 11:02:39

标签: python mongodb ssh paramiko mongorestore

我正在尝试使用python脚本从本地恢复远程机器中的mongodump。我的脚本运行良好,但我没有看到恢复数据库。

远程转储中的Mongo转储

/root/dump

我的剧本

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

从终端登录

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'rijndael-cbc@lysator.liu.se'] 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'rijndael-cbc@lysator.liu.se'] client mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] server mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] client compress:[u'none', u'zlib@openssh.com'] server compress:[u'none', u'zlib@openssh.com'] 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

正如您可以从日志中注意到的那样,stdout表示连接到127.0.0.1但没有恢复。

请提出任何建议

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。我的脚本中有两个ssh_exec语句,第一个是将我的PWD定位到mongo dump文件夹,第二个是恢复转储。我发现第二个命令执行时,PWD不是一个集合,它是默认的$ HOME目录。所以我改变了我的脚本,将两个exec语句合并为一个

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

这适合我。