Preserve last creation log per profile for scheduler. By @real-yfprojects (#1296)

This commit is contained in:
yfprojects 2022-05-16 12:00:31 +00:00 committed by GitHub
parent f76aa5a5f4
commit b650ed3eb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 7 deletions

View File

@ -1,6 +1,7 @@
import os
from datetime import datetime, timedelta
from peewee import Tuple, fn
from playhouse import signals
from vorta.autostart import open_app_at_startup
@ -34,9 +35,27 @@ def init_db(con=None):
DB.create_tables([RepoModel, RepoPassword, BackupProfileModel, SourceFileModel, SettingsModel,
ArchiveModel, WifiSettingModel, EventLogModel, SchemaVersion])
# Delete old log entries after 3 months.
three_months_ago = datetime.now() - timedelta(days=180)
EventLogModel.delete().where(EventLogModel.start_time < three_months_ago)
# Delete old log entries after 6 months.
# The last `create` command of each profile must not be deleted
# since the scheduler uses it to determine the last backup time.
last_backups_per_profile = (
EventLogModel.select(EventLogModel.profile,
fn.MAX(EventLogModel.start_time))
.where(EventLogModel.subcommand == 'create')
.group_by(EventLogModel.profile))
last_scheduled_backups_per_profile = (
EventLogModel.select(EventLogModel.profile,
fn.MAX(EventLogModel.start_time))
.where(EventLogModel.subcommand == 'create',
EventLogModel.category == 'scheduled')
.group_by(EventLogModel.profile))
three_months_ago = datetime.now() - timedelta(days=6 * 30)
entry = Tuple(EventLogModel.profile, EventLogModel.start_time)
EventLogModel.delete().where(
EventLogModel.start_time < three_months_ago,
entry.not_in(last_backups_per_profile),
entry.not_in(last_scheduled_backups_per_profile)).execute()
# Migrations
current_schema, created = SchemaVersion.get_or_create(id=1, defaults={'version': SCHEMA_VERSION})
@ -57,7 +76,3 @@ def init_db(con=None):
s.group = setting['group']
s.save()
# Delete old log entries after 3 months.
three_months_ago = datetime.now() - timedelta(days=3)
EventLogModel.delete().where(EventLogModel.start_time < three_months_ago).execute()