python實現mongo只遷移表結構和索引

在某些業務場景,我們或許只遷移部分表的數據,大部分都是遷移表結構和索引,這種如何實現,如下:

#coding=utf-8
import subprocess
import pymongo

path_indexs = '/home/indexs/'
path_table_structure = '/home/table_structure/'
mongo_client = pymongo.MongoClient(host="127.0.0.1", port=27017, connect=True)
mongo_dbs = mongo_client.database_names()

#遷移索引
fun_index = """
db.getCollectionNames().forEach(function (col) {
    if (!db[col]) return;
    var indexes = db[col].getIndexes();
    indexes.forEach(function (c) {
        var fields = '', result = '', options1 = {};
        for (var i in c) {
            if (i == 'key') {
                fields = c[i];
            } else if (i == 'name' && c[i] == '_id_') {
                return;
            } else if (i != 'name' && i != 'v' && i != 'ns') {
                options1[i] = c[i];
            }
        }
        var fields = JSON.stringify(fields);
        var options = JSON.stringify(options1);
        if (options == '{}') {
            result = \\"db.\\" + col + \\".createIndex(\\" + fields + \\"); \\";
        } else {
            result = \\"db.\\" + col + \\".createIndex(\\" + fields + \\", \\" + options + \\"); \\";
        }
        result = result
            .replace(/{\\"floatApprox\\":-1,\\"top\\":-1,\\"bottom\\":-1}/ig, '-1')
            .replace(/{\\"floatApprox\\":(-?\d+)}/ig, '$1')
            .replace(/\{\\"\$numberLong\\":\\"(-?\d+)\\"\}/ig, '$1');
        print(result);
    });
});"""


#遷移表結構
fun_table_structure = """
db.getCollectionNames().forEach(function (col) {
    var runCommandStr = \\"\\";
    runCommandStr = \\"db.createCollection('\\" + col + \\"');\\"
    print(runCommandStr);
});
"""


def dump_mongo(path, dump):
    for db in mongo_dbs:
        print "正在生成{}庫文件,請稍等".format(db)
        cmd = 'mongo {} --eval "{}"'.format(db, dump)
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        res = p.stdout.read()
        with open('{}{}.txt'.format(path, db), 'wb') as f:
            f.write(res)


if __name__ == '__main__':
    dump_mongo(path_table_structure, fun_table_structure)
    dump_mongo(path_indexs, fun_index)
    subprocess.Popen('tar -czvf mongo_migrate.tar.gz {} {}'.format(path_indexs, path_table_structure), shell=True)

這裏,我們得到了創建mongo表結構和索引的語句,通過讀取該txt文件,一個循環執行語句,我們就可以還原數據庫了

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章