• 使用Python脚本伪造指定时间区间的数据库备份


    为监管需求,需要保留时间非常长的数据库备份。存储代价太大。所以存在了,临时抱佛脚,伪造备份。。

    以下脚本功能,在于根据一个备份,复制出一段时间的备份。并且更改备份的文件时间戳。可以用shell轻松写出。Python也方便。在此记录一下,方便有人需要。

    由于此次为IO密集型操作。所以并发执行也并无明显加速效果,也就单进程执行。代码实在累赘,别介意。

    # -*- coding: utf-8 -*-
    # project:  NewPCFirst
    # date: 2019/6/6
    # phone: 475982055
    # author: dba_yix
    # function: 制作备份


    from datetime import datetime, timedelta

    import os


    class Backuper(object):

    def __init__(self, datelist, filedir, copysourcefile):
    self.datelist = datelist
    self.filedir = filedir
    self.copysource = os.path.join(filedir, copysourcefile)

    def create(self):
    # 拿到需要备份的目录。
    for backdir in self.datelist:
    # 判断文件夹是否存在
    backfile = os.path.join(self.filedir, str(backdir))
    backfiledir = backfile.split(" ")[0]
    #
    if not os.path.exists(backfiledir):
    osCommand = "cp -r %s %s" % (self.copysource, backfiledir)
    osCommandChangeDirDateTime = "touch -d %s %s" % (backdir, backfiledir)
    osCommandChangeFileDateTime = "touch -d %s %s/*" % (backdir, backfiledir)
    os.system(osCommand)
    os.system(osCommandChangeDirDateTime)
    os.system(osCommandChangeFileDateTime)


    class DateBetweenTwoDate(object):

    @staticmethod
    def returnDateList(start_date, end_date):
    start_date = datetime.strptime(start_date, '%Y-%m-%d')
    end_date = datetime.strptime(end_date, '%Y-%m-%d')

    intervaldays = (end_date - start_date).days

    __dateList = []
    i = 0
    while i < intervaldays:
    import random
    randomSecond = random.randint(0, 120)
    generalDate = start_date + timedelta(days=i) + timedelta(seconds=randomSecond)

    __dateList.append(generalDate)
    i += 1

    return __dateList

    def main():
    # 得到要制作备份的日期。填入两个事件区间。
    datelist = DateBetweenTwoDate.returnDateList('2018-08-01', '2019-06-10')

    backuper = Backuper(datelist, '/backup/databack/WALLET_APP', '2019-06-06')
    backuper.create()


    if __name__ == '__main__':
    main()
    
    
  • 相关阅读:
    怎样使用两行代码实现博客园打赏功能
    使用vue开发微信公众号下SPA站点的填坑之旅
    贝叶斯公式与最大后验估计(MAP)
    多元高斯分布(The Multivariate normal distribution)
    Jacobian矩阵、Hessian矩阵和Newton's method
    导数、方向导数与梯度
    解决只有单引号的Json格式转换成bean问题
    浅析Java中的final关键字
    观察者模式/ java实现附代码 /
    Java内存区域与内存溢出异常
  • 原文地址:https://www.cnblogs.com/xiangerfer/p/10985240.html
Copyright © 2020-2023  润新知