Improve incremental backups rotation in mariabackup script

Currently, incremental backups rotation has 2 disadvantages:
1. If full backup is removed manually(accidentally by an engineer as an example),
its incrementals will never be deleted.
2. Script assumes that it will always remove incrementals only for a single
oldest full backup. But there may be corner cases where multiple full backups
will be deleted at once, then script will delete increments only for one of them,
leaving the others forever.

This commit fixes above situations by removing all incremental backups older
than the oldest full backup.
I also changed few variable names to make it easier to understand.

Change-Id: If5b11490d4a61f3200a3bda32b6ace25e12f2216
This commit is contained in:
Damian Dabrowski
2022-02-07 17:28:40 +01:00
parent baf0fa7f97
commit 6424f8f1ae

View File

@@ -167,18 +167,19 @@ def rotate_backups(dest, copies, full_backup_filename, increment_backup_filename
get_lock_file()
full_list = [os.path.normpath(dest+'/'+f) for f in os.listdir(dest) if f.startswith(full_backup_filename)]
increment_list = [ os.path.normpath(dest+'/'+f) for f in os.listdir(dest) if f.startswith(increment_backup_filename)]
# Rotate full backups
if len(full_list) > copies:
full_list.sort()
left = parsedate(full_list[0].split(full_backup_filename)[1])
right = parsedate(full_list[1].split(full_backup_filename)[1])
for files in increment_list:
stamp = parsedate(files.split(increment_backup_filename)[1])
if stamp > left and stamp < right:
rmtree(files)
while len(full_list) > copies:
folder = min(full_list, key=os.path.getmtime)
full_list.remove(folder)
rmtree(folder)
oldest_full_backup = min(full_list, key=os.path.getmtime)
full_list.remove(oldest_full_backup)
rmtree(oldest_full_backup)
# Remove all incremental backups older than the oldest full backup
oldest_full_backup_timestamp = parsedate(oldest_full_backup.split(full_backup_filename)[1])
for increment in increment_list:
increment_timestamp = parsedate(increment.split(increment_backup_filename)[1])
if increment_timestamp < oldest_full_backup_timestamp:
rmtree(increment)
os.unlink("/var/run/mariabackup-galera/db_backup.pid")