Skip to content

Instantly share code, notes, and snippets.

@nrtkbb
Created January 28, 2018 10:07
Show Gist options
  • Save nrtkbb/965bb054dc79b754ecf6383c2a764309 to your computer and use it in GitHub Desktop.
Save nrtkbb/965bb054dc79b754ecf6383c2a764309 to your computer and use it in GitHub Desktop.
バックアップファイルを徐々に間引くもの(3日以内は毎日残し、1週間を超えたら月曜日と1日だけ残し、31日を超えたら1日だけ残す)
#/usr/bin/python
# -*- coding: utf-8 -*-
import os
import datetime
BASE_DIR = './backup'
MOST_DAYS = 3
def before_datetime(days):
# type: (int) -> datetime
today = datetime.date.today()
most_days = datetime.timedelta(days=days)
judge_day = today - most_days
t = datetime.time(0, 0, 0)
judge_datetime = datetime.datetime.combine(judge_day, t)
return judge_datetime
def isMostRecent(img_datetime):
# type: (str) -> bool
# True with in 3 days
return img_datetime > before_datetime(MOST_DAYS)
def isWeeklyOr1st(img_datetime):
# type: (str) -> bool
# True with in 1 months
if img_datetime < before_datetime(31):
return False
# monday is True
if 0 == img_datetime.weekday():
return True
# or 1st day is True
return 1 == img_datetime.day
def is1st(img_datetime):
# type: (str) -> bool
return 1 == img_datetime.day
def main():
imgs = os.listdir(BASE_DIR)
for img_path in imgs:
path = os.path.join(BASE_DIR, img_path)
mtime = os.stat(path).st_mtime
img_datetime = datetime.datetime.fromtimestamp(mtime)
if isMostRecent(img_datetime):
print('most recent {}'.format(path))
continue
if isWeeklyOr1st(img_datetime):
print('weekly or 1st {}'.format(path))
continue
if is1st(img_datetime):
print('month 1st {}'.format(path))
continue
print('delete {}'.format(path))
# os.remove(path)
if __name__ == '__main__':
main()
#/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import subprocess
def touch(y, m, d):
cmd = ['touch',
'-d',
'{}/{}/{}'.format(y, m, d),
'{}{:02}{:02}.img'.format(y, m, d),
]
p = subprocess.Popen(cmd)
p.wait()
if __name__ == '__main__':
today = datetime.date.today()
for i in xrange(1000):
d = today - datetime.timedelta(days=i)
touch(d.year, d.month, d.day)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment