Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

FEATURE (intervals): When the time is selected on UI, adjust it to UTC before saving in API

Rostislav Dugin62449657c2de
diff --git a/backend/internal/features/intervals/model_test.go b/backend/internal/features/intervals/model_test.go
index 67a5093e..00f0fb9f 100644
--- a/backend/internal/features/intervals/model_test.go
+++ b/backend/internal/features/intervals/model_test.go
@@ -94,6 +94,16 @@ func TestInterval_ShouldTriggerBackup_Daily(t *testing.T) {
assert.True(t, should)
},
)
+
+ t.Run(
+ "Backup yesterday at 15:00: Trigger backup today at 09:00",
+ func(t *testing.T) {
+ now := time.Date(2024, 1, 15, 9, 0, 0, 0, time.UTC)
+ lastBackup := time.Date(2024, 1, 14, 15, 0, 0, 0, time.UTC) // Yesterday at 15:00
+ should := interval.ShouldTriggerBackup(now, &lastBackup)
+ assert.True(t, should)
+ },
+ )
}
func TestInterval_ShouldTriggerBackup_Weekly(t *testing.T) {
diff --git a/frontend/src/entity/databases/model/BackupNotificationType.ts b/frontend/src/entity/databases/model/BackupNotificationType.ts
index bc46d471..3accac7f 100644
--- a/frontend/src/entity/databases/model/BackupNotificationType.ts
+++ b/frontend/src/entity/databases/model/BackupNotificationType.ts
@@ -1,4 +1,4 @@
export enum BackupNotificationType {
- BACKUP_FAILED = "BACKUP_FAILED",
- BACKUP_SUCCESS = "BACKUP_SUCCESS",
+ BACKUP_FAILED = 'BACKUP_FAILED',
+ BACKUP_SUCCESS = 'BACKUP_SUCCESS',
}
diff --git a/frontend/src/entity/databases/model/Period.ts b/frontend/src/entity/databases/model/Period.ts
index c785ba32..ae2bd1ae 100644
--- a/frontend/src/entity/databases/model/Period.ts
+++ b/frontend/src/entity/databases/model/Period.ts
@@ -1,13 +1,13 @@
export enum Period {
- DAY = "DAY",
- WEEK = "WEEK",
- MONTH = "MONTH",
- THREE_MONTH = "3_MONTH",
- SIX_MONTH = "6_MONTH",
- YEAR = "YEAR",
- TWO_YEARS = "2_YEARS",
- THREE_YEARS = "3_YEARS",
- FOUR_YEARS = "4_YEARS",
- FIVE_YEARS = "5_YEARS",
- FOREVER = "FOREVER",
-}
\ No newline at end of file
+ DAY = 'DAY',
+ WEEK = 'WEEK',
+ MONTH = 'MONTH',
+ THREE_MONTH = '3_MONTH',
+ SIX_MONTH = '6_MONTH',
+ YEAR = 'YEAR',
+ TWO_YEARS = '2_YEARS',
+ THREE_YEARS = '3_YEARS',
+ FOUR_YEARS = '4_YEARS',
+ FIVE_YEARS = '5_YEARS',
+ FOREVER = 'FOREVER',
+}
diff --git a/frontend/src/entity/databases/model/postgresql/PostgresqlVersion.ts b/frontend/src/entity/databases/model/postgresql/PostgresqlVersion.ts
index bcaecb32..a2daa402 100644
--- a/frontend/src/entity/databases/model/postgresql/PostgresqlVersion.ts
+++ b/frontend/src/entity/databases/model/postgresql/PostgresqlVersion.ts
@@ -1,7 +1,7 @@
export enum PostgresqlVersion {
- PostgresqlVersion13 = "13",
- PostgresqlVersion14 = "14",
- PostgresqlVersion15 = "15",
- PostgresqlVersion16 = "16",
- PostgresqlVersion17 = "17",
+ PostgresqlVersion13 = '13',
+ PostgresqlVersion14 = '14',
+ PostgresqlVersion15 = '15',
+ PostgresqlVersion16 = '16',
+ PostgresqlVersion17 = '17',
}
diff --git a/frontend/src/entity/restores/model/Restore.ts b/frontend/src/entity/restores/model/Restore.ts
index 36a397b7..978a3881 100644
--- a/frontend/src/entity/restores/model/Restore.ts
+++ b/frontend/src/entity/restores/model/Restore.ts
@@ -4,11 +4,11 @@ import { RestoreStatus } from './RestoreStatus';
export interface Restore {
id: string;
status: RestoreStatus;
-
+
postgresql?: PostgresqlDatabase;
-
+
failMessage?: string;
-
+
restoreDurationMs: number;
createdAt: string;
}
diff --git a/frontend/src/entity/storages/models/LocalStorage.ts b/frontend/src/entity/storages/models/LocalStorage.ts
index 9d250d4f..7636cfab 100644
--- a/frontend/src/entity/storages/models/LocalStorage.ts
+++ b/frontend/src/entity/storages/models/LocalStorage.ts
@@ -1 +1 @@
-export type LocalStorage = object
+export type LocalStorage = object;
diff --git a/frontend/src/features/databases/ui/edit/EditDatabaseBaseInfoComponent.tsx b/frontend/src/features/databases/ui/edit/EditDatabaseBaseInfoComponent.tsx
index 23092c4e..efd23a2d 100644
--- a/frontend/src/features/databases/ui/edit/EditDatabaseBaseInfoComponent.tsx
+++ b/frontend/src/features/databases/ui/edit/EditDatabaseBaseInfoComponent.tsx
@@ -6,19 +6,13 @@ import { useEffect, useMemo, useState } from 'react';
import { type Database, databaseApi } from '../../../../entity/databases';
import { Period } from '../../../../entity/databases/model/Period';
import { type Interval, IntervalType } from '../../../../entity/intervals';
-
-interface Props {
- database: Database;
-
- isShowName?: boolean;
-
- isShowCancelButton?: boolean;
- onCancel: () => void;
-
- saveButtonText?: string;
- isSaveToApi: boolean;
- onSaved: (database: Database) => void;
-}
+import {
+ getLocalDayOfMonth,
+ getLocalWeekday,
+ getUserTimeFormat,
+ getUtcDayOfMonth,
+ getUtcWeekday,
+} from '../../../../shared/time/utils';
const weekdayOptions = [
{ value: 1, label: 'Mon' },
@@ -30,22 +24,23 @@ const weekdayOptions = [
{ value: 7, label: 'Sun' },
];
-// Function to detect if user prefers 12-hour format based on their locale
-const getUserTimeFormat = () => {
- const locale = navigator.language || 'en-US';
- const testDate = new Date(2023, 0, 1, 13, 0, 0); // 1 PM
- const timeString = testDate.toLocaleTimeString(locale, { hour: 'numeric' });
- return timeString.includes('PM') || timeString.includes('AM');
-};
+interface Props {
+ database: Database;
+
+ isShowName?: boolean;
+ isShowCancelButton?: boolean;
+ onCancel: () => void;
+
+ saveButtonText?: string;
+ isSaveToApi: boolean;
+ onSaved: (db: Database) => void;
+}
export const EditDatabaseBaseInfoComponent = ({
database,
-
isShowName,
-
isShowCancelButton,
onCancel,
-
saveButtonText,
isSaveToApi,
onSaved,
@@ -54,73 +49,83 @@ export const EditDatabaseBaseInfoComponent = ({
const [isUnsaved, setIsUnsaved] = useState(false);
const [isSaving, setIsSaving] = useState(false);
- // Detect user's preferred time format (12-hour vs 24-hour)
const timeFormat = useMemo(() => {
- const is12Hour = getUserTimeFormat();
- return {
- use12Hours: is12Hour,
- format: is12Hour ? 'h:mm A' : 'HH:mm',
- };
+ const is12 = getUserTimeFormat();
+ return { use12Hours: is12, format: is12 ? 'h:mm A' : 'HH:mm' };
}, []);
const updateDatabase = (patch: Partial<Database>) => {
- if (!editingDatabase) return;
- setEditingDatabase({ ...editingDatabase, ...patch });
+ setEditingDatabase((prev) => (prev ? { ...prev, ...patch } : prev));
+ setIsUnsaved(true);
+ };
+
+ const saveInterval = (patch: Partial<Interval>) => {
+ setEditingDatabase((prev) => {
+ if (!prev) return prev;
+
+ const updatedBackupInterval = { ...(prev.backupInterval ?? {}), ...patch };
+
+ if (!updatedBackupInterval.id && prev.backupInterval?.id) {
+ updatedBackupInterval.id = prev.backupInterval.id;
+ }
+
+ return { ...prev, backupInterval: updatedBackupInterval as Interval };
+ });
+
setIsUnsaved(true);
};
const saveDatabase = async () => {
if (!editingDatabase) return;
-
if (isSaveToApi) {
setIsSaving(true);
-
try {
await databaseApi.updateDatabase(editingDatabase);
setIsUnsaved(false);
} catch (e) {
alert((e as Error).message);
}
-
setIsSaving(false);
}
-
onSaved(editingDatabase);
};
- const saveInterval = (patch: Partial<Interval>) => {
- if (!editingDatabase) return;
- const current = editingDatabase.backupInterval ?? ({} as Interval);
- updateDatabase({ backupInterval: { ...current, ...patch } });
- };
-
useEffect(() => {
setIsSaving(false);
setIsUnsaved(false);
-
setEditingDatabase({ ...database });
}, [database]);
if (!editingDatabase) return null;
-
const { backupInterval } = editingDatabase;
+ // UTC → local conversions for display
const localTime: Dayjs | undefined = backupInterval?.timeOfDay
- ? dayjs.utc(backupInterval.timeOfDay, 'HH:mm').local() /* cast to user tz */
+ ? dayjs.utc(backupInterval.timeOfDay, 'HH:mm').local()
: undefined;
- let isAllFieldsFilled = true;
-
- if (!editingDatabase.name) isAllFieldsFilled = false;
- if (!editingDatabase.storePeriod) isAllFieldsFilled = false;
-
- if (!editingDatabase.backupInterval?.interval) isAllFieldsFilled = false;
- if (editingDatabase.backupInterval?.interval === IntervalType.WEEKLY) {
- if (!editingDatabase.backupInterval?.weekday) isAllFieldsFilled = false;
- }
- if (editingDatabase.backupInterval?.interval === IntervalType.MONTHLY) {
- if (!editingDatabase.backupInterval.dayOfMonth) isAllFieldsFilled = false;
- }
+ const displayedWeekday: number | undefined =
+ backupInterval?.interval === IntervalType.WEEKLY &&
+ backupInterval.weekday &&
+ backupInterval.timeOfDay
+ ? getLocalWeekday(backupInterval.weekday, backupInterval.timeOfDay)
+ : backupInterval?.weekday;
+
+ const displayedDayOfMonth: number | undefined =
+ backupInterval?.interval === IntervalType.MONTHLY &&
+ backupInterval.dayOfMonth &&
+ backupInterval.timeOfDay
+ ? getLocalDayOfMonth(backupInterval.dayOfMonth, backupInterval.timeOfDay)
+ : backupInterval?.dayOfMonth;
+
+ // mandatory-field check
+ const isAllFieldsFilled =
+ Boolean(editingDatabase.name) &&
+ Boolean(editingDatabase.storePeriod) &&
+ Boolean(backupInterval?.interval) &&
+ (!backupInterval ||
+ ((backupInterval.interval !== IntervalType.WEEKLY || displayedWeekday) &&
+ (backupInterval.interval !== IntervalType.MONTHLY || displayedDayOfMonth)));
return (
<div>
@@ -129,9 +134,7 @@ export const EditDatabaseBaseInfoComponent = ({
<div className="min-w-[150px]">Name</div>
<Input
value={editingDatabase.name || ''}
- onChange={(e) => {
- updateDatabase({ name: e.target.value });
- }}
+ onChange={(e) => updateDatabase({ name: e.target.value })}
size="small"
placeholder="My favourite DB"
className="max-w-[200px] grow"
@@ -143,9 +146,7 @@ export const EditDatabaseBaseInfoComponent = ({
<div className="min-w-[150px]">Backup interval</div>
<Select
value={backupInterval?.interval}
- onChange={(v) => {
- saveInterval({ interval: v });
- }}
+ onChange={(v) => saveInterval({ interval: v })}
size="small"
className="max-w-[200px] grow"
options={[
@@ -154,7 +155,6 @@ export const EditDatabaseBaseInfoComponent = ({
{ label: 'Weekly', value: IntervalType.WEEKLY },
{ label: 'Monthly', value: IntervalType.MONTHLY },
]}
- placeholder="Select backup interval"
/>
</div>
@@ -162,14 +162,15 @@ export const EditDatabaseBaseInfoComponent = ({
<div className="mb-1 flex w-full items-center">
<div className="min-w-[150px]">Backup weekday</div>
<Select
- value={backupInterval.weekday}
- onChange={(v) => {
- saveInterval({ weekday: v });
+ value={displayedWeekday}
+ onChange={(localWeekday) => {
+ if (!localWeekday) return;
+ const ref = localTime ?? dayjs();
+ saveInterval({ weekday: getUtcWeekday(localWeekday, ref) });
}}
size="small"
className="max-w-[200px] grow"
options={weekdayOptions}
- placeholder="Select backup weekday"
/>
</div>
)}
@@ -180,13 +181,14 @@ export const EditDatabaseBaseInfoComponent = ({
<InputNumber
min={1}
max={31}
- value={backupInterval.dayOfMonth}
- onChange={(v) => {
- saveInterval({ dayOfMonth: v ?? 1 });
+ value={displayedDayOfMonth}
+ onChange={(localDom) => {
+ if (!localDom) return;
+ const ref = localTime ?? dayjs();
+ saveInterval({ dayOfMonth: getUtcDayOfMonth(localDom, ref) });
}}
size="small"
className="max-w-[200px] grow"
- placeholder="Select backup day of month"
/>
</div>
)}
@@ -198,15 +200,22 @@ export const EditDatabaseBaseInfoComponent = ({
value={localTime}
format={timeFormat.format}
use12Hours={timeFormat.use12Hours}
- onChange={(t) => {
- if (!t) return;
- // convert local picker value → UTC "HH:mm"
- const utcString = t.utc().format('HH:mm');
- saveInterval({ timeOfDay: utcString });
- }}
allowClear={false}
size="small"
className="max-w-[200px] grow"
+ onChange={(t) => {
+ if (!t) return;
+ const patch: Partial<Interval> = { timeOfDay: t.utc().format('HH:mm') };
+
+ if (backupInterval?.interval === IntervalType.WEEKLY && displayedWeekday) {
+ patch.weekday = getUtcWeekday(displayedWeekday, t);
+ }
+ if (backupInterval?.interval === IntervalType.MONTHLY && displayedDayOfMonth) {
+ patch.dayOfMonth = getUtcDayOfMonth(displayedDayOfMonth, t);
+ }
+
+ saveInterval(patch);
+ }}
/>
</div>
)}
@@ -215,9 +224,7 @@ export const EditDatabaseBaseInfoComponent = ({
<div className="min-w-[150px]">Store period</div>
<Select
value={editingDatabase.storePeriod}
- onChange={(v) => {
- updateDatabase({ storePeriod: v });
- }}
+ onChange={(v) => updateDatabase({ storePeriod: v })}
size="small"
className="max-w-[200px] grow"
options={[
@@ -236,7 +243,7 @@ export const EditDatabaseBaseInfoComponent = ({
/>
<Tooltip
className="cursor-pointer"
- title="How long to keep the backups? Make sure that you have enough space on the storage you are using (local, S3, Goole Drive, etc.)."
+ title="How long to keep the backups? Make sure you have enough storage space."
>
<InfoCircleOutlined className="ml-2" style={{ color: 'gray' }} />
</Tooltip>
@@ -244,15 +251,14 @@ export const EditDatabaseBaseInfoComponent = ({
<div className="mt-5 flex">
{isShowCancelButton && (
- <Button className="mr-1" danger ghost onClick={() => onCancel()}>
+ <Button danger ghost className="mr-1" onClick={onCancel}>
Cancel
</Button>
)}
-
<Button
- className={`${isShowCancelButton ? 'ml-1' : 'ml-auto'} mr-5`}
type="primary"
- onClick={() => saveDatabase()}
+ className={`${isShowCancelButton ? 'ml-1' : 'ml-auto'} mr-5`}
+ onClick={saveDatabase}
loading={isSaving}
disabled={!isUnsaved || !isAllFieldsFilled}
>
diff --git a/frontend/src/features/databases/ui/show/ShowDatabaseBaseInfoComponent.tsx b/frontend/src/features/databases/ui/show/ShowDatabaseBaseInfoComponent.tsx
index 4f17e9b1..2b666ecc 100644
--- a/frontend/src/features/databases/ui/show/ShowDatabaseBaseInfoComponent.tsx
+++ b/frontend/src/features/databases/ui/show/ShowDatabaseBaseInfoComponent.tsx
@@ -4,6 +4,11 @@ import { useMemo } from 'react';
import { type Database } from '../../../../entity/databases';
import { Period } from '../../../../entity/databases/model/Period';
import { IntervalType } from '../../../../entity/intervals';
+import {
+ getLocalDayOfMonth,
+ getLocalWeekday,
+ getUserTimeFormat,
+} from '../../../../shared/time/utils';
interface Props {
database: Database;
@@ -41,14 +46,6 @@ const periodLabels = {
[Period.FOREVER]: 'Forever',
};
-// Function to detect if user prefers 12-hour format based on their locale
-const getUserTimeFormat = () => {
- const locale = navigator.language || 'en-US';
- const testDate = new Date(2023, 0, 1, 13, 0, 0); // 1 PM
- const timeString = testDate.toLocaleTimeString(locale, { hour: 'numeric' });
- return timeString.includes('PM') || timeString.includes('AM');
-};
-
export const ShowDatabaseBaseInfoComponent = ({ database, isShowName }: Props) => {
// Detect user's preferred time format (12-hour vs 24-hour)
const timeFormat = useMemo(() => {
@@ -67,6 +64,21 @@ export const ShowDatabaseBaseInfoComponent = ({ database, isShowName }: Props) =
const formattedTime = localTime ? localTime.format(timeFormat.format) : '';
+ // Convert UTC weekday/day-of-month to local equivalents for display
+ const displayedWeekday: number | undefined =
+ backupInterval?.interval === IntervalType.WEEKLY &&
+ backupInterval.weekday &&
+ backupInterval.timeOfDay
+ ? getLocalWeekday(backupInterval.weekday, backupInterval.timeOfDay)
+ : backupInterval?.weekday;
+
+ const displayedDayOfMonth: number | undefined =
+ backupInterval?.interval === IntervalType.MONTHLY &&
+ backupInterval.dayOfMonth &&
+ backupInterval.timeOfDay
+ ? getLocalDayOfMonth(backupInterval.dayOfMonth, backupInterval.timeOfDay)
+ : backupInterval?.dayOfMonth;
+
return (
<div>
{isShowName && (
@@ -85,9 +97,7 @@ export const ShowDatabaseBaseInfoComponent = ({ database, isShowName }: Props) =
<div className="mb-1 flex w-full items-center">
<div className="min-w-[150px]">Backup weekday</div>
<div>
- {backupInterval.weekday
- ? weekdayLabels[backupInterval.weekday as keyof typeof weekdayLabels]
- : ''}
+ {displayedWeekday ? weekdayLabels[displayedWeekday as keyof typeof weekdayLabels] : ''}
</div>
</div>
)}
@@ -95,7 +105,7 @@ export const ShowDatabaseBaseInfoComponent = ({ database, isShowName }: Props) =
{backupInterval?.interval === IntervalType.MONTHLY && (
<div className="mb-1 flex w-full items-center">
<div className="min-w-[150px]">Backup day of month</div>
- <div>{backupInterval.dayOfMonth || ''}</div>
+ <div>{displayedDayOfMonth || ''}</div>
</div>
)}
diff --git a/frontend/src/features/notifiers/ui/NotifierCardComponent.tsx b/frontend/src/features/notifiers/ui/NotifierCardComponent.tsx
index baacb230..77f10322 100644
--- a/frontend/src/features/notifiers/ui/NotifierCardComponent.tsx
+++ b/frontend/src/features/notifiers/ui/NotifierCardComponent.tsx
@@ -20,7 +20,7 @@ export const NotifierCardComponent = ({
className={`mb-3 cursor-pointer rounded p-3 shadow ${selectedNotifierId === notifier.id ? 'bg-blue-100' : 'bg-white'}`}
onClick={() => setSelectedNotifierId(notifier.id)}
>
- <div className="font-bold mb-1">{notifier.name}</div>
+ <div className="mb-1 font-bold">{notifier.name}</div>
<div className="flex items-center">
<div className="text-sm text-gray-500">
diff --git a/frontend/src/shared/time/getUserTimeFormat.ts b/frontend/src/shared/time/getUserTimeFormat.ts
index c0a0befd..42d16a2d 100644
--- a/frontend/src/shared/time/getUserTimeFormat.ts
+++ b/frontend/src/shared/time/getUserTimeFormat.ts
@@ -3,9 +3,9 @@ export const getUserTimeFormat = () => {
const testDate = new Date(2023, 0, 1, 13, 0, 0); // 1 PM
const timeString = testDate.toLocaleTimeString(locale, { hour: 'numeric' });
const is12Hour = timeString.includes('PM') || timeString.includes('AM');
-
+
return {
use12Hours: is12Hour,
format: is12Hour ? 'DD.MM.YYYY h:mm:ss A' : 'DD.MM.YYYY HH:mm:ss',
};
-};
\ No newline at end of file
+};
diff --git a/frontend/src/shared/time/utils.ts b/frontend/src/shared/time/utils.ts
new file mode 100644
index 00000000..57da851f
--- /dev/null
+++ b/frontend/src/shared/time/utils.ts
@@ -0,0 +1,42 @@
+import dayjs, { Dayjs } from 'dayjs';
+
+// Detect whether the user's locale prefers 12-hour time
+export const getUserTimeFormat = () => {
+ const locale = navigator.language || 'en-US';
+ return /[AP]M/.test(new Date(2023, 0, 1, 13).toLocaleTimeString(locale, { hour: 'numeric' }));
+};
+
+// UTC weekday (1–7) + HH:mm → local weekday (1–7)
+export const getLocalWeekday = (utcWeekday: number, utcTime: string): number => {
+ const [h, m] = utcTime.split(':').map(Number);
+ const local = dayjs
+ .utc()
+ .day(utcWeekday % 7)
+ .hour(h)
+ .minute(m)
+ .local();
+ return ((local.day() + 6) % 7) + 1; // 1 = Mon … 7 = Sun
+};
+
+// Local weekday (1–7) + local time → UTC weekday (1–7)
+export const getUtcWeekday = (localWeekday: number, localTime: Dayjs): number => {
+ const utc = dayjs()
+ .day(localWeekday % 7)
+ .hour(localTime.hour())
+ .minute(localTime.minute())
+ .utc();
+ return ((utc.day() + 6) % 7) + 1;
+};
+
+// UTC day-of-month (1–31) + HH:mm → local day-of-month (1–31)
+export const getLocalDayOfMonth = (utcDom: number, utcTime: string): number => {
+ const [h, m] = utcTime.split(':').map(Number);
+ const local = dayjs.utc().date(utcDom).hour(h).minute(m).local();
+ return local.date();
+};
+
+// Local day-of-month + local time → UTC day-of-month (1–31)
+export const getUtcDayOfMonth = (localDom: number, localTime: Dayjs): number => {
+ const utc = localTime.date(localDom).utc();
+ return utc.date();
+};