実験用ブログ

・勉強したことをメモしておく

バックアップ世代管理

2019-12-10 01:57:57 | 勉強
import os
import shutil
import datetime as dt
import glob

import const



def backup():
# 現在日を取得
today = dt.date.today()
yyyymmdd = today.strftime('%Y%m%d')
target = const.root_dir + "/model_" + yyyymmdd

# 既に現在日のバックアップが存在する場合は末尾に連番を付ける
if os.path.exists(target):
save_flg = False
for i in range(1, 1000):
target_000 = target + "_" + str(i).zfill(3)
if not os.path.exists(target_000):
target = target_000
save_flg = True
break
if not save_flg:
raise Exception("これ以上バックアップできません")

# バックアップを作成
shutil.copytree(const.root_dir + "/model", target)



def remove_old():
# 現在日を取得
today = dt.datetime.today()

# バックアップの一覧を取得し、現在日より30日古ければ削除する
model_path = const.root_dir + "/model_*"
model_list = glob.glob(model_path)
for model in model_list:
yyyymmdd = os.path.basename(model)[6:14]
target_date = dt.datetime.strptime(yyyymmdd, '%Y%m%d')
if today > target_date + dt.timedelta(days=30):
print("古くなったバックアップを削除します file=" + model)
shutil.rmtree(model)


最新の画像もっと見る