Last active
August 29, 2015 14:07
-
-
Save akerouanton/8024a209921dcdb721b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from os import listdir,path,remove | |
from collections import namedtuple | |
from time import strptime | |
import datetime | |
import string | |
import shutil | |
BackupRange = namedtuple('BackupRange', ['daily', 'weekly', 'monthly']) | |
class BackupFile: | |
PREFIX = 'AUTOBACKUP' | |
SEP = '-' | |
EXT = '.tar.gz' | |
PATH = '/srv/backup/' | |
def __init__(self, volume, date, time, ext): | |
self.volume = volume | |
self.date = date | |
self.time = time | |
self.ext = ext | |
def __repr__(self): | |
return string.join([ | |
BackupFile.PREFIX, | |
self.volume, | |
self.date.isoformat().replace('-', '.'), | |
self.time.strftime("%H.%M.%S") | |
], BackupFile.SEP) + self.ext | |
def retrieve_file_list(): | |
files = [] | |
for f in (f for f in listdir(BackupFile.PATH) if path.isfile(path.join(BackupFile.PATH, f)) and f.endswith(BackupFile.EXT)): | |
f = f.strip(BackupFile.EXT) | |
if f.startswith(BackupFile.PREFIX) and f.count(BackupFile.SEP) == 3: | |
prefix, fvolume, fdate, ftime = f.split(BackupFile.SEP) | |
ts = strptime(fdate + " " + ftime, "%Y.%m.%d %H.%M.%S") | |
fdate = datetime.date(ts.tm_year, ts.tm_mon, ts.tm_mday) | |
ftime = datetime.time(ts.tm_hour, ts.tm_min, ts.tm_sec) | |
files.append(BackupFile(fvolume, fdate, ftime, BackupFile.EXT)) | |
return files | |
def copy_files(to, files): | |
for idx,f in enumerate(files): | |
print str(idx+1) + ') Copying ' + str(f) + ' ...' | |
shutil.copy(path.join(BackupFile.PATH, str(f)), path.join(BackupFile.PATH, to, str(f))) | |
def delete_files(files): | |
for idx,f in enumerate(files): | |
print str(idx+1) + ') Deleting ' + str(f) + ' ...' | |
remove(path.join(BackupFile.PATH, str(f))) | |
def get_limits(): | |
today = datetime.date.today() | |
last_monday = today - datetime.timedelta(today.weekday()) | |
daily = last_monday - datetime.timedelta(7) | |
weekly = daily - datetime.timedelta(0, 0, 0, 0, 0, 0, 4) | |
monthly = weekly - datetime.timedelta(0, 0, 0, 0, 0, 0, 53) | |
return BackupRange(daily=daily, weekly=weekly, monthly=monthly) | |
def filter_files_by_limits(files, limits): | |
daily = [ f for f in files if f.date >= limits.daily ] | |
weekly = [ f for f in files if f.date >= limits.weekly and f.date < limits.daily and f.date.weekday() == 0 ] | |
monthly = [ f for f in files if f.date >= limits.monthly and f.date < limits.weekly and f.date.day == 1 ] | |
return BackupRange(daily=daily, weekly=weekly, monthly=monthly) | |
files = retrieve_file_list() | |
limits = get_limits() | |
need_copy = filter_files_by_limits(files, limits) | |
copy_union = need_copy.daily + need_copy.weekly + need_copy.monthly | |
need_del = [ f for f in files if f not in copy_union ] | |
print str(len(copy_union)) + " file(s) need to be copied." | |
copy_files("daily/", need_copy.daily) | |
copy_files("weekly/", need_copy.weekly) | |
copy_files("monthly/", need_copy.monthly) | |
print str(len(files)) + " file(s) need to be deleted." | |
delete_files(files) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment