0

我正在尝试使用 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

正如您从日志中看到的那样,标准输出显示连接到 127.0.0.1 但没有恢复。

请有任何建议

4

1 回答 1

0

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

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

这对我有用。

于 2015-06-25T12:08:32.293 回答