zoukankan      html  css  js  c++  java
  • 使用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()
    
    
  • 相关阅读:
    C#简单的工厂模式
    Microsoft.XMLHttp的属性和方法的简介及使用
    Google Earth 2007中文修订版
    偷懒秘笈之一键生成 Ajax Control Toolkit 标记 (转)
    C#获取硬盘序列号
    Google和百度、雅虎的站内搜索代码
    Visual Studio 2005 Team Suite 180 天试用版
    几种 Dotnet ORM 库的比较
    Windows Server 2003 SP2 0918 Personal 精简版
    通用水晶报表绑定类[原]
  • 原文地址:https://www.cnblogs.com/xiangerfer/p/10985240.html
Copyright © 2011-2022 走看看