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

本文介绍了一个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()

转载于:https://www.cnblogs.com/xiangerfer/p/10985240.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值