Skip to content

Commit 6f48c26

Browse files
committed
fix(linstor): Docstrings, bugfix, simplifications.
Don't except/raise LinstorDatabaseBackupError in _check_database_backup(). Simpler glob search for backup files, as the date is properly validated by datetime. Fix bug in database_backup_age(). Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
1 parent 5c09c9c commit 6f48c26

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

drivers/LinstorSR.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,16 +1590,15 @@ def database_backup(self, name=""):
15901590
"""
15911591
Generate a new database backup file.
15921592
This operation should not prevent the underlying action to be successful.
1593-
Hence all Exceptions are caught and re-raised only if asked to.
1594-
delay: skip backup if the last one was generated less than delay seconds ago.
1593+
Hence all Exceptions are caught and logged.
15951594
"""
15961595
if not self._linstor:
15971596
self._reconnect()
15981597
try:
15991598
self._linstor.database_backup(name)
16001599
except Exception as e:
16011600
util.SMlog(
1602-
"[database_backup] Error during creation: {}".format(e),
1601+
f"[database_backup] Error during creation: {e}",
16031602
priority=util.LOG_ERR,
16041603
)
16051604

drivers/linstorvolumemanager.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,7 +1801,7 @@ def database_backup_age(cls):
18011801
If not called on the Controller, since backups are not available,
18021802
returns a huge value (a timestamp of now).
18031803
"""
1804-
return (datetime.now() - cls._get_latest_database_backup()).total_seconds()
1804+
return (datetime.now() - cls._get_latest_database_backup()[1]).total_seconds()
18051805

18061806
def database_backup(self, name=""):
18071807
# Create new backup
@@ -2714,27 +2714,49 @@ def _get_volume_properties(self, volume_uuid):
27142714

27152715
@classmethod
27162716
def _list_database_backup(cls, database_backup_dir):
2717+
"""
2718+
List all visible backup files in database_backup_dir.
2719+
DATABASE_BACKUP_DIR_MAIN is only available on the Linstor Controller.
2720+
DATABASE_BACKUP_DIR_SPARE will list backups previously made when the Host was the Linstor Controller.
2721+
This may not be useful information if it is not the Controller anymore.
2722+
"""
27172723
for path in database_backup_dir.glob(DATABASE_BACKUP_NAME_FORMAT.format(
2718-
"20[0-9][0-9][01][0-9][0-3][0-9]_[0-2][0-9][0-5][0-9][0-5][0-9]", "*") + ".zip"):
2724+
"[0-9]" * 8 + "_" + "[0-9]" * 6, "*") + ".zip"):
27192725
try:
27202726
yield path, datetime.strptime(path.name.split("-")[1], DATABASE_BACKUP_DATE_FORMAT)
27212727
except (ValueError, IndexError):
27222728
continue
27232729

27242730
@classmethod
27252731
def _get_sorted_database_backup(cls, database_backup_dir):
2732+
"""
2733+
Return list of backups in database_backup_dir, alongside their creation date.
2734+
Sorted by date from the more recent to the older one.
2735+
"""
27262736
return sorted(cls._list_database_backup(database_backup_dir),
27272737
reverse=True,
27282738
key=lambda p: p[1])
27292739

27302740
@classmethod
27312741
def _get_latest_database_backup(cls):
2742+
"""
2743+
Return the latest backup in DATABASE_BACKUP_DIR_MAIN, and its creation date.
2744+
Returns (None, timestamp(0)) when none are found.
2745+
None will be found if it is not called on the Linstor Controller.
2746+
(cf _list_database_backup)
2747+
"""
27322748
return max(cls._list_database_backup(DATABASE_BACKUP_DIR_MAIN),
27332749
default=(None, datetime.fromtimestamp(0)),
27342750
key=lambda p: p[1])
27352751

27362752
@classmethod
27372753
def _check_database_backup(cls, database_backup_file):
2754+
"""
2755+
Make some validation of a database backup zip-file.
2756+
Check its a valid zipfile, and CRC-test its content.
2757+
Check it contains a non-empty linstordb.mv.db file.
2758+
Always raises a LinstorDatabaseBackupError if checks failed.
2759+
"""
27382760
try:
27392761
with zipfile.ZipFile(database_backup_file, mode="r") as archive:
27402762
if archive.testzip() is not None:
@@ -2745,8 +2767,6 @@ def _check_database_backup(cls, database_backup_file):
27452767
linstordb = linstordb[0]
27462768
if linstordb.file_size == 0:
27472769
raise LinstorDatabaseBackupError("linstordb.mv.db is empty")
2748-
except LinstorDatabaseBackupError:
2749-
raise
27502770
except (FileNotFoundError, zipfile.BadZipFile, zipfile.LargeZipFile) as e:
27512771
raise LinstorDatabaseBackupError(e) from e
27522772

0 commit comments

Comments
 (0)