Code style changes
This commit is contained in:
parent
cbc2db2a93
commit
47d2d20043
100
clean-backups.py
100
clean-backups.py
|
@ -10,14 +10,15 @@ DEFAULT_WEEKLY = 4
|
||||||
DEFAULT_MONTHLY = 3
|
DEFAULT_MONTHLY = 3
|
||||||
DEFAULT_TIMESTAMP_FORMAT = "%Y%m%d"
|
DEFAULT_TIMESTAMP_FORMAT = "%Y%m%d"
|
||||||
|
|
||||||
class BackupFile():
|
|
||||||
|
class BackupFile:
|
||||||
"""
|
"""
|
||||||
Manipulations with backup files
|
Manipulations with backup files
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
* retention_daily - daily renention period
|
* retention_daily - daily retention period
|
||||||
* retention_weekly - weekly retention period
|
* retention_weekly - weekly retention period
|
||||||
* retention_monthly - monthly retention perod
|
* retention_monthly - monthly retention period
|
||||||
* file_path (optional) - file path
|
* file_path (optional) - file path
|
||||||
* dateformat (optional) - format of timestamps (default is '%Y%m%d')
|
* dateformat (optional) - format of timestamps (default is '%Y%m%d')
|
||||||
"""
|
"""
|
||||||
|
@ -26,15 +27,15 @@ class BackupFile():
|
||||||
retention_daily,
|
retention_daily,
|
||||||
retention_weekly,
|
retention_weekly,
|
||||||
retention_monthly,
|
retention_monthly,
|
||||||
file_path = None,
|
file_path=None,
|
||||||
dateformat = "%Y%m%d") -> None:
|
dateformat="%Y%m%d"
|
||||||
|
) -> None:
|
||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
self.daily = retention_daily
|
self.daily = retention_daily
|
||||||
self.weekly = retention_weekly
|
self.weekly = retention_weekly
|
||||||
self.monthly = retention_monthly
|
self.monthly = retention_monthly
|
||||||
self.dateformat = dateformat
|
self.dateformat = dateformat
|
||||||
curr_date = date.today() # Maybe this will be used to specify date as starting point...
|
curr_date = date.today() # Maybe this will be used to specify date as starting point...
|
||||||
if self.file_path is None:
|
if self.file_path is None:
|
||||||
self.file_name = None
|
self.file_name = None
|
||||||
else:
|
else:
|
||||||
|
@ -42,17 +43,17 @@ class BackupFile():
|
||||||
dates = []
|
dates = []
|
||||||
# Daily
|
# Daily
|
||||||
for i in range(0, self.daily):
|
for i in range(0, self.daily):
|
||||||
day = curr_date - timedelta(days = i)
|
day = curr_date - timedelta(days=i)
|
||||||
dates.append(day)
|
dates.append(day)
|
||||||
# Weekly
|
# Weekly
|
||||||
monday = curr_date - timedelta(days=date.weekday(curr_date))
|
monday = curr_date - timedelta(days=date.weekday(curr_date))
|
||||||
for i in range(0,self.weekly):
|
for i in range(0, self.weekly):
|
||||||
day = monday - timedelta( days = (i * 7))
|
day = monday - timedelta(days=(i*7))
|
||||||
if day not in dates:
|
if day not in dates:
|
||||||
dates.append(day)
|
dates.append(day)
|
||||||
# Monthly
|
# Monthly
|
||||||
day = curr_date.replace(day=1)
|
day = curr_date.replace(day=1)
|
||||||
for i in range(0,self.monthly):
|
for i in range(0, self.monthly):
|
||||||
if day not in dates:
|
if day not in dates:
|
||||||
dates.append(day)
|
dates.append(day)
|
||||||
day = (day - timedelta(days=1)).replace(day=1)
|
day = (day - timedelta(days=1)).replace(day=1)
|
||||||
|
@ -61,14 +62,14 @@ class BackupFile():
|
||||||
def new_file(
|
def new_file(
|
||||||
self,
|
self,
|
||||||
file_path,
|
file_path,
|
||||||
retention_daily = None,
|
retention_daily=None,
|
||||||
retention_weekly = None,
|
retention_weekly=None,
|
||||||
retention_monthly = None,
|
retention_monthly=None,
|
||||||
dateformat = None):
|
dateformat=None
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
Create new instance of BackupFile, can be used for retention settings inheritance.
|
Create new instance of BackupFile, can be used for retention settings inheritance.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if retention_daily is None:
|
if retention_daily is None:
|
||||||
retention_daily = self.daily
|
retention_daily = self.daily
|
||||||
if retention_weekly is None:
|
if retention_weekly is None:
|
||||||
|
@ -104,7 +105,7 @@ class BackupFile():
|
||||||
break
|
break
|
||||||
return need_remove
|
return need_remove
|
||||||
|
|
||||||
def remove(self, force_remove = False):
|
def remove(self, force_remove=False):
|
||||||
"""
|
"""
|
||||||
Remove file
|
Remove file
|
||||||
|
|
||||||
|
@ -122,66 +123,67 @@ class BackupFile():
|
||||||
if answer == "y":
|
if answer == "y":
|
||||||
os.unlink(self.file_path)
|
os.unlink(self.file_path)
|
||||||
|
|
||||||
def remove_if_needed(self, force_remove = False):
|
def remove_if_needed(self, force_remove=False):
|
||||||
"""
|
"""
|
||||||
Remove file if it's too old.
|
Remove file if it's too old.
|
||||||
"""
|
"""
|
||||||
if self.need_remove():
|
if self.need_remove():
|
||||||
self.remove(force_remove = force_remove)
|
self.remove(force_remove=force_remove)
|
||||||
|
|
||||||
|
|
||||||
# Argument parser
|
# Argument parser
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description = "Cleanup old backups",
|
description="Cleanup old backups",
|
||||||
epilog = "For a complete timestamp format description, see the python strftime() " +
|
epilog="For a complete timestamp format description, see the python strftime() " +
|
||||||
"documentation: https://docs.python.org/3/library/datetime.html" +
|
"documentation: https://docs.python.org/3/library/datetime.html" +
|
||||||
"#strftime-strptime-behavior"
|
"#strftime-strptime-behavior"
|
||||||
)
|
)
|
||||||
# path argument
|
# path argument
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"path",
|
"path",
|
||||||
metavar = "PATH",
|
metavar="PATH",
|
||||||
type = str,
|
type=str,
|
||||||
nargs = 1,
|
nargs=1,
|
||||||
help = "directory path"
|
help="directory path"
|
||||||
)
|
)
|
||||||
# daily argument
|
# daily argument
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-d", "--daily",
|
"-d", "--daily",
|
||||||
type = int,
|
type=int,
|
||||||
default = DEFAULT_DAILY,
|
default=DEFAULT_DAILY,
|
||||||
metavar = "N",
|
metavar="N",
|
||||||
help = f"keep N daily backups, default: {DEFAULT_DAILY}"
|
help=f"keep N daily backups, default: {DEFAULT_DAILY}"
|
||||||
)
|
)
|
||||||
# weekly argument
|
# weekly argument
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-w", "--weekly",
|
"-w", "--weekly",
|
||||||
type = int,
|
type=int,
|
||||||
default = DEFAULT_WEEKLY,
|
default=DEFAULT_WEEKLY,
|
||||||
metavar = "N",
|
metavar="N",
|
||||||
help = f"keep N weekly backups, default: {DEFAULT_WEEKLY}"
|
help=f"keep N weekly backups, default: {DEFAULT_WEEKLY}"
|
||||||
)
|
)
|
||||||
# monthly argument
|
# monthly argument
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-m", "--monthly",
|
"-m", "--monthly",
|
||||||
type = int,
|
type=int,
|
||||||
default = DEFAULT_MONTHLY,
|
default=DEFAULT_MONTHLY,
|
||||||
metavar = "N",
|
metavar="N",
|
||||||
help = f"keep N monthly backups, default: {DEFAULT_MONTHLY}"
|
help=f"keep N monthly backups, default: {DEFAULT_MONTHLY}"
|
||||||
)
|
)
|
||||||
# force removal
|
# force removal
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-f", "--force",
|
"-f", "--force",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help = "suppress remove confirmation"
|
help="suppress remove confirmation"
|
||||||
)
|
)
|
||||||
# timestamp format
|
# timestamp format
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-t",
|
"-t",
|
||||||
"--timestamp-format",
|
"--timestamp-format",
|
||||||
type = str,
|
type=str,
|
||||||
default = DEFAULT_TIMESTAMP_FORMAT,
|
default=DEFAULT_TIMESTAMP_FORMAT,
|
||||||
metavar = "FORMAT",
|
metavar="FORMAT",
|
||||||
help = f"format of timestamp, default: {DEFAULT_TIMESTAMP_FORMAT}".replace(r"%",r"%%")
|
help=f"format of timestamp, default: {DEFAULT_TIMESTAMP_FORMAT}".replace(r"%", r"%%")
|
||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
@ -194,10 +196,10 @@ timestamp_format = args.timestamp_format
|
||||||
|
|
||||||
# File processing
|
# File processing
|
||||||
files = BackupFile(
|
files = BackupFile(
|
||||||
retention_daily = daily,
|
retention_daily=daily,
|
||||||
retention_weekly = weekly,
|
retention_weekly=weekly,
|
||||||
retention_monthly = monthly,
|
retention_monthly=monthly,
|
||||||
dateformat = timestamp_format
|
dateformat=timestamp_format
|
||||||
)
|
)
|
||||||
# Generate file list with full paths
|
# Generate file list with full paths
|
||||||
paths = [
|
paths = [
|
||||||
|
@ -206,4 +208,4 @@ paths = [
|
||||||
]
|
]
|
||||||
for path in paths:
|
for path in paths:
|
||||||
f = files.new_file(path)
|
f = files.new_file(path)
|
||||||
f.remove_if_needed(force_remove = force)
|
f.remove_if_needed(force_remove=force)
|
||||||
|
|
Loading…
Reference in New Issue