Skip to content

Latest commit

 

History

History
151 lines (106 loc) · 3.04 KB

脚本.md

File metadata and controls

151 lines (106 loc) · 3.04 KB

脚本

mysql备份及恢复

##备份

mysqldump -p'lott-jh-abcd1234' --data community > community_`date +%m%d%H%M`.sql

##恢复

#!/bin/sh

bdir=$1

db=community_test

mysqladmin -u root -pabc123 drop  $db
mysqladmin -u root -pabc123 create  $db
mysql -u root -pabc123 $db < $bdir/community.sql

mongodb备份及恢复

##备份

#!/bin/sh

mongodump  -d community -o mongo_community_`date +%m%d%H%M`				

恢复

mongorestore  -d community ./community

当mongorestore工具不可用时

导出数据

# -- coding=utf-8 --
from __future__ import unicode_literals
from pymongo import MongoClient
import sys
import json
from datetime import date
from decimal import Decimal
from bson import ObjectId
from json import JSONEncoder
reload(sys)
sys.setdefaultencoding( "utf-8" )

client = MongoClient('mongodb://127.0.0.1:27017/')

class CustomJSONEncoder(json.JSONEncoder):

    def default(self, obj):
        try:
            if isinstance(obj, date):
                return obj.isoformat().replace('T', ' ')
            if isinstance(obj, Decimal):
                return '{}'.format(obj.quantize(Decimal('.01')))
            if isinstance(obj, ObjectId):
                return str(obj)
            iterable = iter(obj)
        except TypeError:
            pass
        else:
            return list(iterable)
        return JSONEncoder.default(self, obj)



if __name__ == '__main__':
    if len(sys.argv) <= 2:
        print "{} <export file path> [<db name>] ".format(sys.argv[0])
        sys.exit(1)

    db = client.community
    if len(sys.argv) == 3:
        db = client[sys.argv[2]]

    colls = [n for n in db.collection_names()] 
    for n in colls:
        col = db[n]
        data = list(col.find({}))
        if len(data) == 0:
            print "{} is empty".format(n)
            continue

        with open('{}/{}'.format(sys.argv[1], n),"w") as f:
            json.dump(data, f, ensure_ascii=False, encoding='utf-8', sort_keys=True,indent=2, cls=CustomJSONEncoder)

导入数据

# -- coding=utf-8 --
from pymongo import MongoClient
from pprint import pprint
import sys
import os
import json5
from bson import ObjectId


client = MongoClient('mongodb://127.0.0.1:27017/')
db = client.community_test


def insert_mongo(col, data):
    col.drop()
    result = col.insert_many(data)
    pprint(result.inserted_ids)
    print "插入 {} 文档数: {} 个 ".format(col,len(result.inserted_ids))
    print "****************************************************************************"


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print "{}  <file path> ".format(sys.argv[0])
        sys.exit(1)
    for n in os.listdir(sys.argv[1]):
        print "import", n
        with open('{}/{}'.format(sys.argv[1], n),"r") as f:
            col = db[n]
            data = json5.loads(f.read())
            for d in data:
                d['_id'] = ObjectId(d['_id']) 
            insert_mongo(col, data)

统计代码行数

find . "(" -name "*.html" ")" -print | xargs wc -l