parent
d38e1613c3
commit
1709fd5974
147
clean-backups.py
147
clean-backups.py
|
@ -9,6 +9,79 @@ default_daily = 7
|
|||
default_weekly = 4
|
||||
default_monthly = 3
|
||||
|
||||
class backupFile():
|
||||
def __init__(self, daily, weekly, monthly, path = None, dateformat = "%Y%m%d") -> None:
|
||||
self.path = path
|
||||
self.daily = daily
|
||||
self.weekly = weekly
|
||||
self.monthly = monthly
|
||||
self.dateformat = dateformat
|
||||
curr_date = date.today() # Maybe this will be used to specify date as starting point...
|
||||
if self.path == None:
|
||||
self.filename = None
|
||||
else:
|
||||
self.filename = os.path.basename(path)
|
||||
dates = []
|
||||
|
||||
# Daily
|
||||
for i in range(0, daily):
|
||||
day = curr_date - timedelta(days = i)
|
||||
dates.append(day)
|
||||
# Weekly
|
||||
monday = curr_date - timedelta(days=date.weekday(curr_date))
|
||||
for i in range(0,weekly):
|
||||
day = monday - timedelta( days = (i * 7))
|
||||
if day not in dates:
|
||||
dates.append(day)
|
||||
# Monthly
|
||||
day = curr_date.replace(day=1)
|
||||
for i in range(0,monthly):
|
||||
if day not in dates:
|
||||
dates.append(day)
|
||||
day = (day - timedelta(days=1)).replace(day=1)
|
||||
self.dates = dates
|
||||
|
||||
def newFile(self, path, daily = None, weekly = None, monthly = None, dateformat = None):
|
||||
if daily == None: daily = self.daily
|
||||
if weekly == None: weekly = self.weekly
|
||||
if monthly == None: monthly = self.monthly
|
||||
if dateformat == None: dateformat = self.dateformat
|
||||
newfile = backupFile(daily, weekly, monthly, path)
|
||||
return(newfile)
|
||||
|
||||
def __str__(self):
|
||||
val = "<{path}>".format(
|
||||
path = self.path,
|
||||
)
|
||||
return(val)
|
||||
|
||||
def needRemove(self):
|
||||
if self.filename == None:
|
||||
need_remove = False
|
||||
else:
|
||||
need_remove = True
|
||||
for date in self.dates:
|
||||
if date.strftime(self.dateformat) in self.filename:
|
||||
need_remove = False
|
||||
break
|
||||
return(need_remove)
|
||||
|
||||
def remove(self, force = False):
|
||||
print("Removing {file}...".format(file = self))
|
||||
# Check force option
|
||||
if force:
|
||||
os.unlink(self.path)
|
||||
else:
|
||||
# Remove interactively
|
||||
print("Are you sure? (y/n) ", end="")
|
||||
answer = input()
|
||||
if answer == "y":
|
||||
os.unlink(self.path)
|
||||
|
||||
def removeIfNeeded(self, force = False):
|
||||
if self.needRemove():
|
||||
self.remove(force = force)
|
||||
|
||||
# Argument parser
|
||||
parser = argparse.ArgumentParser(description="Cleanup old backups")
|
||||
# path argument
|
||||
|
@ -63,80 +136,6 @@ monthly = args.monthly
|
|||
force = args.force
|
||||
directory = args.path[0]
|
||||
|
||||
class backupFile():
|
||||
def __init__(self, daily, weekly, monthly, path = None, dateformat = "%Y%m%d", dates = None,) -> None:
|
||||
self.path = path
|
||||
self.daily = daily
|
||||
self.weekly = weekly
|
||||
self.monthly = monthly
|
||||
self.dateformat = dateformat
|
||||
curr_date = date.today() # Maybe this will be used to specify date as starting point...
|
||||
if self.path == None:
|
||||
self.filename = None
|
||||
else:
|
||||
self.filename = os.path.basename(path)
|
||||
if dates == None:
|
||||
dates = []
|
||||
|
||||
# Daily
|
||||
for i in range(0, daily):
|
||||
day = curr_date - timedelta(days = i)
|
||||
dates.append(day)
|
||||
# Weekly
|
||||
monday = curr_date - timedelta(days=date.weekday(curr_date))
|
||||
for i in range(0,weekly):
|
||||
day = monday - timedelta( days = (i * 7))
|
||||
if day not in dates:
|
||||
dates.append(day)
|
||||
# Monthly
|
||||
day = curr_date.replace(day=1)
|
||||
for i in range(0,monthly):
|
||||
if day not in dates:
|
||||
dates.append(day)
|
||||
day = (day - timedelta(days=1)).replace(day=1)
|
||||
self.dates = dates
|
||||
|
||||
def newFile(self, path, daily = None, weekly = None, monthly = None, dateformat = None):
|
||||
if daily == None: daily = self.daily
|
||||
if weekly == None: weekly = self.weekly
|
||||
if monthly == None: monthly = self.monthly
|
||||
if dateformat == None: dateformat = self.dateformat
|
||||
newfile = backupFile(daily, weekly, monthly, path, dates = self.dates)
|
||||
return(newfile)
|
||||
|
||||
def __str__(self):
|
||||
val = "<{path}>".format(
|
||||
path = self.path,
|
||||
)
|
||||
return(val)
|
||||
|
||||
def needRemove(self):
|
||||
if self.filename == None:
|
||||
need_remove = False
|
||||
else:
|
||||
need_remove = True
|
||||
for date in self.dates:
|
||||
if date.strftime(self.dateformat) in self.filename:
|
||||
need_remove = False
|
||||
break
|
||||
return(need_remove)
|
||||
|
||||
def remove(self, force = False):
|
||||
print("Removing {file}...".format(file = self))
|
||||
# Check force option
|
||||
if force:
|
||||
os.unlink(self.path)
|
||||
else:
|
||||
# Remove interactively
|
||||
print("Are you sure? (y/n) ", end="")
|
||||
answer = input()
|
||||
if answer == "y":
|
||||
os.unlink(self.path)
|
||||
|
||||
def removeIfNeeded(self, force = False):
|
||||
if self.needRemove():
|
||||
self.remove(force = force)
|
||||
|
||||
# File processing
|
||||
files = backupFile(daily = daily, weekly = weekly, monthly = monthly)
|
||||
# Generate file list with full paths
|
||||
|
|
Loading…
Reference in New Issue