Skip to content

Commit 6c0d0a3

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 02c0c1a commit 6c0d0a3

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
@@ -1592,16 +1592,15 @@ def database_backup(self, name=""):
15921592
"""
15931593
Generate a new database backup file.
15941594
This operation should not prevent the underlying action to be successful.
1595-
Hence all Exceptions are caught and re-raised only if asked to.
1596-
delay: skip backup if the last one was generated less than delay seconds ago.
1595+
Hence all Exceptions are caught and logged.
15971596
"""
15981597
if not self._linstor:
15991598
self._reconnect()
16001599
try:
16011600
self._linstor.database_backup(name)
16021601
except Exception as e:
16031602
util.SMlog(
1604-
"[database_backup] Error during creation: {}".format(e),
1603+
f"[database_backup] Error during creation: {e}",
16051604
priority=util.LOG_ERR,
16061605
)
16071606

drivers/linstorvolumemanager.py

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

17891789
def database_backup(self, name=""):
17901790
# Create new backup
@@ -2676,27 +2676,49 @@ def _get_volume_properties(self, volume_uuid):
26762676

26772677
@classmethod
26782678
def _list_database_backup(cls, database_backup_dir):
2679+
"""
2680+
List all visible backup files in database_backup_dir.
2681+
DATABASE_BACKUP_DIR_MAIN is only available on the Linstor Controller.
2682+
DATABASE_BACKUP_DIR_SPARE will list backups previously made when the Host was the Linstor Controller.
2683+
This may not be useful information if it is not the Controller anymore.
2684+
"""
26792685
for path in database_backup_dir.glob(DATABASE_BACKUP_NAME_FORMAT.format(
2680-
"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"):
2686+
"[0-9]" * 8 + "_" + "[0-9]" * 6, "*") + ".zip"):
26812687
try:
26822688
yield path, datetime.strptime(path.name.split("-")[1], DATABASE_BACKUP_DATE_FORMAT)
26832689
except (ValueError, IndexError):
26842690
continue
26852691

26862692
@classmethod
26872693
def _get_sorted_database_backup(cls, database_backup_dir):
2694+
"""
2695+
Return list of backups in database_backup_dir, alongside their creation date.
2696+
Sorted by date from the more recent to the older one.
2697+
"""
26882698
return sorted(cls._list_database_backup(database_backup_dir),
26892699
reverse=True,
26902700
key=lambda p: p[1])
26912701

26922702
@classmethod
26932703
def _get_latest_database_backup(cls):
2704+
"""
2705+
Return the latest backup in DATABASE_BACKUP_DIR_MAIN, and its creation date.
2706+
Returns (None, timestamp(0)) when none are found.
2707+
None will be found if it is not called on the Linstor Controller.
2708+
(cf _list_database_backup)
2709+
"""
26942710
return max(cls._list_database_backup(DATABASE_BACKUP_DIR_MAIN),
26952711
default=(None, datetime.fromtimestamp(0)),
26962712
key=lambda p: p[1])
26972713

26982714
@classmethod
26992715
def _check_database_backup(cls, database_backup_file):
2716+
"""
2717+
Make some validation of a database backup zip-file.
2718+
Check its a valid zipfile, and CRC-test its content.
2719+
Check it contains a non-empty linstordb.mv.db file.
2720+
Always raises a LinstorDatabaseBackupError if checks failed.
2721+
"""
27002722
try:
27012723
with zipfile.ZipFile(database_backup_file, mode="r") as archive:
27022724
if archive.testzip() is not None:
@@ -2707,8 +2729,6 @@ def _check_database_backup(cls, database_backup_file):
27072729
linstordb = linstordb[0]
27082730
if linstordb.file_size == 0:
27092731
raise LinstorDatabaseBackupError("linstordb.mv.db is empty")
2710-
except LinstorDatabaseBackupError:
2711-
raise
27122732
except (FileNotFoundError, zipfile.BadZipFile, zipfile.LargeZipFile) as e:
27132733
raise LinstorDatabaseBackupError(e) from e
27142734

0 commit comments

Comments
 (0)