Setup backup server configuration for backup replication.
This commit is contained in:
1 parent
768da9cf45
commit
a59f1dfe8b
8 files changed
+212
-11
No files matched your search
@@ -0,0 +1,117 @@
|
||||
from datetime import datetime
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from argparse import ArgumentParser, FileType
|
||||
|
||||
def filter_backups_timeslot(offset, backups, timeslot='days', keep=7, mode='max', printy=True):
|
||||
keep_backups = []
|
||||
for timeslot_off in range(1, keep):
|
||||
indices = [
|
||||
idx
|
||||
for idx,value in enumerate(backups)
|
||||
if (
|
||||
value[1] >= (offset - relativedelta(**{timeslot:timeslot_off}))
|
||||
)
|
||||
]
|
||||
if not indices:
|
||||
continue
|
||||
if mode == "min":
|
||||
#removed not considered backups for this slot!
|
||||
for idx in reversed(indices[1:]):
|
||||
backups.pop(idx)
|
||||
keep_backups.append(backups.pop(indices[0]))
|
||||
elif mode == "max":
|
||||
keep_backups.append(backups.pop(indices[-1]))
|
||||
#removed not considered backups for this slot!
|
||||
for idx in reversed(indices[:-1]):
|
||||
backups.pop(idx)
|
||||
else:
|
||||
for idx in sorted(indices, reverse=True):
|
||||
keep_backups.append(backups.pop(idx))
|
||||
return keep_backups, backups
|
||||
|
||||
def filter_backups(backuplist, show_keep=True, keep_hours=24, keep_days=7, keep_weeks=4, keep_months=12, keep_years=10):
|
||||
backups = sorted(
|
||||
[
|
||||
(backup, datetime.fromtimestamp(int(backup.split('-')[1])))
|
||||
for backup in backuplist
|
||||
], key=lambda v: v[1], reverse=True
|
||||
)
|
||||
offset = backups[0][1]
|
||||
keep_backups = [backups.pop(0)]
|
||||
|
||||
part_keep_backups, backups = filter_backups_timeslot(offset, backups, timeslot='hours',keep=keep_hours,mode='all')
|
||||
keep_backups.extend(part_keep_backups)
|
||||
part_keep_backups, backups = filter_backups_timeslot(offset, backups, timeslot='days',keep=keep_days,mode='max')
|
||||
keep_backups.extend(part_keep_backups)
|
||||
part_keep_backups, backups = filter_backups_timeslot(offset, backups, timeslot='weeks',keep=keep_weeks,mode='max')
|
||||
keep_backups.extend(part_keep_backups)
|
||||
part_keep_backups, backups = filter_backups_timeslot(offset, backups, timeslot='months',keep=keep_months,mode='max')
|
||||
keep_backups.extend(part_keep_backups)
|
||||
part_keep_backups, backups = filter_backups_timeslot(offset, backups, timeslot='years',keep=keep_years,mode='max')
|
||||
keep_backups.extend(part_keep_backups)
|
||||
|
||||
keeps = [backup[0] for backup in keep_backups]
|
||||
if not show_keep:
|
||||
return [backup for backup in backuplist if backup not in keeps]
|
||||
return keeps
|
||||
|
||||
def main():
|
||||
parser = ArgumentParser(description="Program to print backups to keep or delete based on time stamps.")
|
||||
parser.add_argument(
|
||||
"-f", "--file", metavar="FILE", type=FileType('r'), required=True,
|
||||
help="Path to file with list of backups to filter. Expects per line one backup name in format <appname>-<timestamp>")
|
||||
parser.add_argument(
|
||||
"-d", "--print-delete", action="store_true", required=False,
|
||||
help="Instead of objects to kept, print out objects to be deleted."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-H", "--hours-keep", metavar="KEEP", type=int, required=False,
|
||||
default=24, help="Number of objects to keep on hourly basis."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-D", "--days-keep", metavar="KEEP", type=int, required=False,
|
||||
default=7, help="Number of objects to keep on daily basis."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-W", "--weeks-keep", metavar="KEEP", type=int, required=False,
|
||||
default=4, help="Number of objects to keep on weekly basis."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-M", "--months-keep", metavar="KEEP", type=int, required=False,
|
||||
default=12, help="Number of objects to keep on monthly basis."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-Y", "--years-keep", metavar="KEEP", type=int, required=False,
|
||||
default=10, help="Number of objects to keep on yearly basis."
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
show_keep = not args.print_delete
|
||||
keep_hours = args.hours_keep
|
||||
keep_days = args.days_keep
|
||||
keep_weeks = args.weeks_keep
|
||||
keep_months = args.months_keep
|
||||
keep_years = args.years_keep
|
||||
|
||||
backuplist = [
|
||||
line.strip()
|
||||
for line in args.file
|
||||
if len(line.strip()) > 0
|
||||
]
|
||||
fail = False
|
||||
for idx,backup in enumerate(backuplist):
|
||||
if len(backup) < 10 or not backup[-10:].isnumeric():
|
||||
print(f"WARNING! Line {idx+1} has no timestamp suffix")
|
||||
fail = True
|
||||
if fail:
|
||||
exit(1)
|
||||
|
||||
considered_backups = filter_backups(
|
||||
backuplist, show_keep,
|
||||
keep_hours, keep_days, keep_weeks, keep_months, keep_years
|
||||
)
|
||||
for backup in considered_backups:
|
||||
print(backup)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user