Check for database corruption when making backup

This commit is contained in:
JonnyWong16 2019-11-20 19:03:44 -08:00
parent 3b24bbee5f
commit 089a981f6e

View file

@ -69,10 +69,17 @@ def db_filename(filename=FILENAME):
def make_backup(cleanup=False, scheduler=False): def make_backup(cleanup=False, scheduler=False):
""" Makes a backup of db, removes all but the last 5 backups """ """ 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: 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: 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_folder = plexpy.CONFIG.BACKUP_DIR
backup_file_fp = os.path.join(backup_folder, backup_file) 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) shutil.copyfile(db_filename(), backup_file_fp)
db.connection.rollback() db.connection.rollback()
if cleanup: # Only cleanup if the database integrity is okay
if cleanup and integrity:
now = time.time() now = time.time()
# Delete all scheduled backup older than BACKUP_DAYS. # Delete all scheduled backup older than BACKUP_DAYS.
for root, dirs, files in os.walk(backup_folder): for root, dirs, files in os.walk(backup_folder):