Skip to content

Commit

Permalink
chore: simply make it possible to do DB backups (#50)
Browse files Browse the repository at this point in the history
* chore: simply make it possible to do DB backups

* add some frontend messaging
  • Loading branch information
fuziontech authored Sep 15, 2023
1 parent 3288464 commit 7fbe8de
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion frontend/src/pages/Backups/ScheduledBackups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ export default function ScheduledBackups() {
return (
<div>
<h1 style={{ textAlign: 'left' }}>Scheduled Backups</h1>
<p style={{ textAlign: 'left' }}>
It's a bit of a hack, but if you want to backup a database just omit the table when you create the
scheduled backup.
</p>
<Button onClick={() => showModal()}>Create Backup</Button>
<Modal
title={editingRow ? 'Edit Backup' : 'Create Backup'}
Expand Down Expand Up @@ -273,7 +277,7 @@ export default function ScheduledBackups() {
<Form.Item<FieldType>
label="Table"
name="table"
rules={[{ required: true, message: 'Please select a table to back up' }]}
rules={[{ required: false, message: 'Please select a table to back up' }]}
>
<Input />
</Form.Item>
Expand Down
10 changes: 9 additions & 1 deletion housewatch/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ def run_backup(backup_id: str, incremental: bool = False):

@app.task(track_started=True, ignore_result=False, max_retries=0)
def schedule_backups():
# Every interval (default 60 seconds), check if any backups need to be run
# We do this so that if we disable or change the schedule of a backup, it will be picked up within 60 seconds
# instead of waiting for the next deployment or restart of HouseWatch service
from housewatch.models.backup import ScheduledBackup

logger.info("Running scheduled backups")
logger.info("Checking if scheduled backups need to be run")
backups = ScheduledBackup.objects.filter(enabled=True)
now = timezone.now()
for backup in backups:
Expand All @@ -65,11 +68,16 @@ def schedule_backups():
nir = timezone.make_aware(nir)

logger.info("Checking backup", backup_id=backup.id, next_run=nr, next_incremental_run=nir, now=now)
# The idea here is to first check if the base backup needs to be run, and if not, check if the incremental
# backup needs to be run. This is because incremental backups are only useful if the base backup has been run
# at least once.
if nr < now:
# check if base backup needs to be run and run it
run_backup.delay(backup.id)
backup.last_run_time = now
backup.save()
elif backup.incremental_schedule is not None and nir < now:
# check if incremental backup needs to be run and run it
run_backup.delay(backup.id, incremental=True)
backup.last_incremental_run_time = now
backup.save()
Expand Down

0 comments on commit 7fbe8de

Please sign in to comment.