backupcleaner/clean-backups.py

189 lines
5.2 KiB
Python
Raw Normal View History

2022-05-31 00:54:41 +03:00
#!/usr/bin/env python3
2022-05-31 12:30:19 +03:00
"""Backup rotation script"""
2022-05-31 00:54:41 +03:00
from datetime import date, timedelta
import os
import argparse
# Default retention parameters
2022-05-31 12:30:19 +03:00
DEFAULT_DAILY = 7
DEFAULT_WEEKLY = 4
DEFAULT_MONTHLY = 3
2022-05-31 00:54:41 +03:00
2022-05-31 12:30:19 +03:00
class BackupFile():
"""
Manipulations with backup files
Arguments:
* retention_daily - daily renention period
* retention_weekly - weekly retention period
* retention_monthly - monthly retention perod
* file_path (optional) - file path
* dateformat (optional) - format of timestamps (default is '%Y%m%d')
"""
def __init__(
self,
retention_daily,
retention_weekly,
retention_monthly,
file_path = None,
dateformat = "%Y%m%d") -> None:
self.file_path = file_path
self.daily = retention_daily
self.weekly = retention_weekly
self.monthly = retention_monthly
2022-05-31 00:54:41 +03:00
self.dateformat = dateformat
curr_date = date.today() # Maybe this will be used to specify date as starting point...
2022-05-31 12:30:19 +03:00
if self.file_path is None:
self.file_name = None
2022-05-31 00:54:41 +03:00
else:
2022-05-31 12:30:19 +03:00
self.file_name = os.path.basename(self.file_path)
dates = []
# Daily
2022-05-31 12:30:19 +03:00
for i in range(0, self.daily):
day = curr_date - timedelta(days = i)
dates.append(day)
# Weekly
monday = curr_date - timedelta(days=date.weekday(curr_date))
2022-05-31 12:30:19 +03:00
for i in range(0,self.weekly):
day = monday - timedelta( days = (i * 7))
if day not in dates:
dates.append(day)
# Monthly
day = curr_date.replace(day=1)
2022-05-31 12:30:19 +03:00
for i in range(0,self.monthly):
if day not in dates:
2022-05-31 00:54:41 +03:00
dates.append(day)
day = (day - timedelta(days=1)).replace(day=1)
2022-05-31 00:54:41 +03:00
self.dates = dates
2022-05-31 12:30:19 +03:00
def new_file(
self,
file_path,
retention_daily = None,
retention_weekly = None,
retention_monthly = None,
dateformat = None):
"""
Create new instance of BackupFile, can be used for retention settings inheritance.
"""
2022-05-31 00:54:41 +03:00
2022-05-31 12:30:19 +03:00
if retention_daily is None:
retention_daily = self.daily
if retention_weekly is None:
retention_weekly = self.weekly
if retention_monthly is None:
retention_monthly = self.monthly
if dateformat is None:
dateformat = self.dateformat
new_file = BackupFile(
retention_daily,
retention_weekly,
retention_monthly,
file_path,
dateformat
2022-05-31 00:54:41 +03:00
)
2022-05-31 12:30:19 +03:00
return new_file
2022-05-31 00:54:41 +03:00
2022-05-31 12:30:19 +03:00
def __str__(self):
val = f"<{self.file_path}>"
return val
def need_remove(self):
"""
Check if file is too old and needs to remove.
"""
if self.file_name is None:
2022-05-31 00:54:41 +03:00
need_remove = False
else:
need_remove = True
2022-05-31 12:30:19 +03:00
for single_date in self.dates:
if single_date.strftime(self.dateformat) in self.file_name:
2022-05-31 00:54:41 +03:00
need_remove = False
break
2022-05-31 12:30:19 +03:00
return need_remove
def remove(self, force_remove = False):
"""
Remove file
Arguments:
* force_remove - suppress remove confirmation
"""
print(f"Removing {self}...")
2022-05-31 00:54:41 +03:00
# Check force option
2022-05-31 12:30:19 +03:00
if force_remove:
os.unlink(self.file_path)
2022-05-31 00:54:41 +03:00
else:
# Remove interactively
print("Are you sure? (y/n) ", end="")
answer = input()
if answer == "y":
2022-05-31 12:30:19 +03:00
os.unlink(self.file_path)
def remove_if_needed(self, force_remove = False):
"""
Remove file if it's too old.
"""
if self.need_remove():
self.remove(force_remove = force_remove)
2022-05-31 00:54:41 +03:00
# Argument parser
parser = argparse.ArgumentParser(description="Cleanup old backups")
# path argument
parser.add_argument(
"path",
metavar = "PATH",
type = str,
nargs = 1,
help = "directory path"
)
# daily argument
parser.add_argument(
"-d", "--daily",
type = int,
2022-05-31 12:30:19 +03:00
default = DEFAULT_DAILY,
metavar = "N",
2022-05-31 12:30:19 +03:00
help = f"keep N daily backups, default: {DEFAULT_DAILY}"
)
# weekly argument
parser.add_argument(
"-w", "--weekly",
type = int,
2022-05-31 12:30:19 +03:00
default = DEFAULT_WEEKLY,
metavar = "N",
2022-05-31 12:30:19 +03:00
help = f"keep N weekly backups, default: {DEFAULT_WEEKLY}"
)
# monthly argument
parser.add_argument(
"-m", "--monthly",
type = int,
2022-05-31 12:30:19 +03:00
default = DEFAULT_MONTHLY,
metavar = "N",
2022-05-31 12:30:19 +03:00
help = f"keep N monthly backups, default: {DEFAULT_MONTHLY}"
)
# force removal
parser.add_argument(
"-f", "--force",
action="store_true",
help = "suppress remove confirmation"
)
args = parser.parse_args()
daily = args.daily
weekly = args.weekly
monthly = args.monthly
force = args.force
directory = args.path[0]
2022-05-31 00:54:41 +03:00
# File processing
2022-05-31 12:30:19 +03:00
files = BackupFile(retention_daily = daily, retention_weekly = weekly, retention_monthly = monthly)
2022-05-31 00:54:41 +03:00
# Generate file list with full paths
2022-05-31 12:30:19 +03:00
paths = [
os.path.join(directory, f) for f in os.listdir(directory)
if os.path.isfile(os.path.join(directory, f))
]
2022-05-31 00:54:41 +03:00
for path in paths:
2022-05-31 12:30:19 +03:00
f = files.new_file(path)
f.remove_if_needed(force_remove = force)