python保留7天备份文件

脚本功能

服务的备份脚本,功能是保留7天的备份数据,并且将最新的备份数据拷贝到备份盘

#!/usr/bin/env python
# coding: utf-8

import os
import datetime
import shutil

# confluence数据备份,因为confluence不能保留7天备份,并且每天全备份,占用空间很大,所以写脚本,保留7天备份文件。
data_directory = "/data/var/atlassian/application-data/confluence/backups/"
backup_directory = "/backup/"

# backup file name: backup-2019_02_24.zip
today_backup_file = "backup-" + str(datetime.date.today() + datetime.timedelta(days = -1)).replace("-", "_") + ".zip"
day_list = []

for i in range(1, 8):
    day = str(datetime.date.today() + datetime.timedelta(days = -i)).replace("-", "_")
    filename = "backup-" + day + ".zip"
    day_list.append(filename)

if os.path.exists(data_directory) and os.path.exists(backup_directory):
    # 循环数据目录,将7天的数据文件保留,其他的删除,并将最新的数据备份包,拷贝到其他的盘上
    
    for file in os.listdir(data_directory):
        filepath = os.path.join(data_directory, file)
        if file == today_backup_file:
            shutil.copy(filepath, backup_directory)
        elif file not in day_list:
            file = os.remove(filepath)
    原文作者:老张爱喝牛栏山
    原文地址: https://segmentfault.com/a/1190000018270400
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞