在某些业务场景,我们或许只迁移部分表的数据,大部分都是迁移表结构和索引,这种如何实现,如下:
#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文件,一个循环执行语句,我们就可以还原数据库了