使用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()

posted @ 2019-06-06 15:31  xiangerfer  阅读(215)  评论(0编辑  收藏  举报