From 089a981f6e5d1744b3d061036f1b7456e6cac114 Mon Sep 17 00:00:00 2001 From: JonnyWong16 Date: Wed, 20 Nov 2019 19:03:44 -0800 Subject: [PATCH] Check for database corruption when making backup --- plexpy/database.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/plexpy/database.py b/plexpy/database.py index ed9d77cc..a6a34bfe 100644 --- a/plexpy/database.py +++ b/plexpy/database.py @@ -69,10 +69,17 @@ def db_filename(filename=FILENAME): def make_backup(cleanup=False, scheduler=False): """ Makes a backup of db, removes all but the last 5 backups """ + # Check the integrity of the database first + integrity = (integrity_check() == 'ok') + + corrupt = '' + if not integrity: + corrupt = '.corrupt' + if scheduler: - backup_file = 'tautulli.backup-%s.sched.db' % arrow.now().format('YYYYMMDDHHmmss') + backup_file = 'tautulli.backup-{}{}.sched.db'.format(arrow.now().format('YYYYMMDDHHmmss'), corrupt) else: - backup_file = 'tautulli.backup-%s.db' % arrow.now().format('YYYYMMDDHHmmss') + backup_file = 'tautulli.backup-{}{}.db'.format(arrow.now().format('YYYYMMDDHHmmss'), corrupt) backup_folder = plexpy.CONFIG.BACKUP_DIR backup_file_fp = os.path.join(backup_folder, backup_file) @@ -85,7 +92,8 @@ def make_backup(cleanup=False, scheduler=False): shutil.copyfile(db_filename(), backup_file_fp) db.connection.rollback() - if cleanup: + # Only cleanup if the database integrity is okay + if cleanup and integrity: now = time.time() # Delete all scheduled backup older than BACKUP_DAYS. for root, dirs, files in os.walk(backup_folder):