mongodump逻辑备份策略

Posted Demonson

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodump逻辑备份策略相关的知识,希望对你有一定的参考价值。

vim auto_mongodump.py

#!/usr/bin env python3
import os
import requests
import random
import time
import datetime
import socket
#定义服务器,用户名、密码、备份的路径
DB_HOST = '127.0.0.1:27017'
DB_USER = 'root'
DB_USER_PASSWD = 'root'
BACKUP_PATH = '/home/bak/'
RESULT = 1
BAK_NUM = 2
robootlist =["https://oapi.dingtalk.com/robot/send?access_token=0aaf7bb5512ea7767d68531513b8361548f109500630515d48f99f3393269e3d","https://oapi.dingtalk.com/robot/send?access_token=9f0fa2372d902ab2d081da72b191634c9aa73cfa64f25f541ace2fc697fb7aa8","https://oapi.dingtalk.com/robot/send?access_token=3643f861ba5911b0a8c0c77bfdf221a0f2744646f046ae312555779a9803bd52"]



DATETIME = time.strftime('%Y%m%d-%H%M%S')
TODAYBACKUPPATH = BACKUP_PATH + DATETIME

print("createing backup folder!")
#创建备份文件夹
if not os.path.exists(TODAYBACKUPPATH):
    os.makedirs(TODAYBACKUPPATH)

print("checking for databases names file")


#定义执行备份脚本,读取文件中的数据库名称,注意按行读写,不校验是否存在该库
def run_backup():
    dumpcmd = "/usr/local/mongodb/bin/mongodump  -u=" +DB_USER+ " -p=" +DB_USER_PASSWD+ " -h=" +DB_HOST+ " --authenticationDatabase admin -o " +TODAYBACKUPPATH
    print(dumpcmd)
    global RESULT
    RESULT = os.system(dumpcmd)
#执行压缩的函数
def run_tar():
    compress_file = TODAYBACKUPPATH + ".tar.gz"
    compress_cmd = "tar -czvf " +compress_file+" "+DATETIME
    os.chdir(BACKUP_PATH)
    os.system("pwd")
    os.system(compress_cmd)
    print("compress complete!")
    #删除备份文件夹
    remove_cmd = "rm -rf "+TODAYBACKUPPATH
    os.system(remove_cmd)

#执行删除历史备份的函数
def rm_tar():
    os.chdir(BACKUP_PATH)
    os.system("pwd")
    dirList = os.listdir("./")
    #print(dirList)
    new_list = []
    for i in dirList:
        if '20' in i:
            new_list.append(i)
    #print(new_list)
    new_list.sort(reverse=True)
    #删除备份文件夹
    n = 1
    for a in new_list:
        if n > BAK_NUM:
            os.remove(a)
            #print(a)
        n = n + 1
		

#执行报警的函数
def to_alert():
    if RESULT != 0:
        hostname=socket.gethostname()
        ip=socket.gethostbyname(hostname)
        msg = ip + ': mongodump is failed!!!'
        print(msg)
        ramt = random.randint(0,2)
        try:
            sd = requests.post(robootlist[ramt],json="msgtype": "text","text": "content": msg,)
            print(sd.text)
        except Exception as e:
            print(e)
                
		

#备份数据库文件存在就执行备份和压缩,否则退出


if os.system("ps -ef|grep mysql|grep -v grep") ==256:
    print("database is not runing..")
    to_alert()
    exit()
else:
    run_backup()
    run_tar()
    rm_tar()
    to_alert()
    print("backup success!")

crontab 定时备份

0 1 * * * /usr/bin/python3 /home/bak/auto_mongodump.py > /home/bak/auto_mongodump.log 2>&1

开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系

以上是关于mongodump逻辑备份策略的主要内容,如果未能解决你的问题,请参考以下文章

mongodump与mongorestore的使用示例

mongodump与mongorestore的使用示例

话说MongoDB备份与恢复

MongoDB的备份和恢复

MongoDB 备份(mongodump)与恢复(mongorestore)

MongoDB 备份(mongodump)与恢复(mongorestore)