import logging from datetime import datetime from datetime import date, timedelta import pymongo """ 备份数据库 """ master_host_ip = "192.168.0.147" slave_host_ip = "127.0.0.1" class replication_mongodb(object): def __init__(self): self.master_client = pymongo.MongoClient(host=master_host_ip, port=27017) def get_master_collection(self): databases_list = self.master_client.list_database_names() databases_list.remove(‘config‘) databases_list.remove(‘admin‘) databases_list.remove(‘local‘) db_collection_list = [] for db in databases_list: collection_names = self.master_client[db].list_collection_names() for collection in collection_names: db_collection_list.append((db, collection)) return db_collection_list def master_salve(self, master_db_name, master_collection_name, slave_host, salve_db_name="", salve_collection_name=""): if salve_db_name == "": salve_db_name = master_db_name if salve_collection_name == "": salve_collection_name = master_collection_name with self.master_client[master_db_name][master_collection_name].find({}, {"_id": 0}, no_cursor_timeout=True) as master_cursor: salve_client = pymongo.MongoClient(host=slave_host, port=27017) salve_cursor = salve_client[salve_db_name][salve_collection_name] salve_cursor.drop() salve_cursor.insert_many(list(master_cursor)) def del_expired_db(self, host_ip, db_name): del_client = pymongo.MongoClient(host=host_ip, port=27017) for collection in del_client[db_name].list_collection_names(): del_client[db_name][collection].drop() def mongo_main(): replication = replication_mongodb() LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p" logging.basicConfig(filename=‘replication_local2.0.log‘, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT) now_date = datetime.now().strftime("%Y-%m-%d") for tup in replication.get_master_collection(): print(str(tup), "正在备份。。。。") salve_db_name = "FWQ_" + now_date + "_0" salve_collection_name = tup[0] + "/" + tup[1] replication.master_salve(tup[0], tup[1], slave_host_ip, salve_db_name=salve_db_name, salve_collection_name=salve_collection_name) logging.info(str(tup) + " 备份成功") print(tup, "----->备份成功") expired_time = 7 # 需要删除N天之前的备份 before_n_days = (date.today() + timedelta(days=-expired_time)).strftime("%Y-%m-%d") expired_db_name = "FWQ_" + before_n_days + "_0" replication.del_expired_db(slave_host_ip, expired_db_name) logging.info(str(expired_db_name) + " 过期数据库删除成功") print(expired_db_name, "----->过期数据库删除成功") if __name__ == ‘__main__‘: mongo_main()
7天过期删除
原文:https://www.cnblogs.com/lqn404/p/13800916.html