GithubHelp home page GithubHelp logo

transfer-via-ssh-tunnel's Introduction

transfer-via-ssh-tunnel

transfer mongoDB data via SSH tunnel

from sshtunnel import SSHTunnelForwarder
import pymongo
import gridfs
import pymysql
from bson.objectid import ObjectId

if __name__ == '__main__':
    local_mysql_db = pymysql.connect(
        "192.168.1.127", 
        "username", 
        "password", 
        "table_name", 
        charset='utf8', 
        cursorclass=pymysql.cursors.SSDictCursor)
    local_mysql_cursor = local_mysql_db.cursor()
    local_mongo_conn = pymongo.MongoClient('192.168.1.127', 27017) # local mongodb address
    local_mongo_db = local_mongo_conn["mongo_database_name"]
    local_gfs = gridfs.GridFS(local_mongo_db, collection="fs")
    local_mysql_cursor.execute("SELECT DISTINCT(logo_url) FROM organization WHERE logo_url IS NOT NULL") # query all record which needs transfer
    
    # define ssh tunnel
    server = SSHTunnelForwarder(
        ('45.77.xxx.xxx', 22), # remote ssh server address
        ssh_username="nobrowning", # ssh username
        ssh_password="123456", # ssh password
        remote_bind_address=('192.168.0.84', 27017)# remote mongodb address
    )
    # start ssh tunnel
    server.start()
    remote_mongo_conn = pymongo.MongoClient('127.0.0.1', server.local_bind_port) # must be 127.0.0.1
    remote_mongo_db = remote_mongo_conn["mongo_database_name"]
    remote_gfs = gridfs.GridFS(remote_mongo_db, collection="fs")

    # transfer
    for file_name in local_mysql_cursor.fetchall():
        name = str(file_name['logo_url'])
        name = name[0: name.find('.')]
        file = local_gfs.find_one({"_id": ObjectId(name)})
        if not remote_gfs.exists(document_or_id=ObjectId(name)):
            remote_gfs.put(file, _id=ObjectId(name), filename=file.filename, contentType=file.content_type, metadata=file.metadata)

    # close ssh tunnel
    remote_mongo_conn.close()
    local_mongo_conn.close()
    local_mysql_db.close()
    server.stop()

transfer-via-ssh-tunnel's People

Contributors

nobrowning avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.