Skip to content

A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.

fix: resolves broken storage update when there was no plan associated

towfiqi0b9417d331c0
diff --git a/backend/__tests__/managers/BaseStorageManager.test.ts b/backend/__tests__/managers/BaseStorageManager.test.ts
index d38f133..4d43618 100644
--- a/backend/__tests__/managers/BaseStorageManager.test.ts
+++ b/backend/__tests__/managers/BaseStorageManager.test.ts
@@ -234,6 +234,7 @@ describe('BaseStorageManager', () => {
'us-west-2',
'timeout',
'30',
+ '--obscure',
'--non-interactive',
]);
expect(mockRunRcloneCommand).toHaveBeenNthCalledWith(2, ['lsd', 'test-remote:']);
@@ -262,6 +263,7 @@ describe('BaseStorageManager', () => {
'test-remote',
'region',
'us-west-2',
+ '--obscure',
'--non-interactive',
]);
expect(mockRunRcloneCommand).toHaveBeenNthCalledWith(2, ['lsd', 'test-remote:']);
@@ -269,6 +271,7 @@ describe('BaseStorageManager', () => {
'config',
'update',
'test-remote',
+ '--obscure',
'--non-interactive',
'region',
'us-east-1',
diff --git a/backend/__tests__/services/StorageService.test.ts b/backend/__tests__/services/StorageService.test.ts
index e234a22..c63dbb4 100644
--- a/backend/__tests__/services/StorageService.test.ts
+++ b/backend/__tests__/services/StorageService.test.ts
@@ -231,6 +231,126 @@ describe('StorageService', () => {
});
});
+ // ----------------------------------------
+ // updateStorage
+ // ----------------------------------------
+ describe('updateStorage', () => {
+ const storageId = 'storage-to-update';
+ const existingStorage = {
+ id: storageId,
+ name: 'My SFTP',
+ type: 'sftp',
+ settings: { idle_timeout: '1m0s' },
+ credentials: { host: 'encrypted(example.com)', port: 'encrypted(22)' },
+ } as unknown as Storage;
+
+ const payload: Partial<Storage> = {
+ type: 'sftp',
+ authType: 'password',
+ settings: { idle_timeout: '1m0s' },
+ credentials: { host: 'example.com', port: '2222' },
+ tags: [],
+ };
+
+ beforeEach(() => {
+ mockStorageStore.getById.mockResolvedValue(existingStorage);
+ mockStorageStore.update.mockImplementation((id, data) =>
+ Promise.resolve({ id, ...data } as Storage)
+ );
+ mockPlanStore.getStoragePlans.mockResolvedValue([]);
+ mockStorageStore.getReplicationPlanSources = jest.fn().mockResolvedValue([]);
+ mockLocalStrategy.updateRemoteStorage = jest
+ .fn()
+ .mockResolvedValue({ success: true, result: 'OK' });
+ mockRemoteStrategy.updateRemoteStorage = jest
+ .fn()
+ .mockResolvedValue({ success: true, result: 'OK' });
+ });
+
+ it('pushes the config to main even when no plan uses the storage', async () => {
+ await storageService.updateStorage(storageId, payload);
+
+ expect(LocalStrategy).toHaveBeenCalled();
+ expect(mockLocalStrategy.updateRemoteStorage).toHaveBeenCalledWith(
+ 'My SFTP',
+ expect.objectContaining({
+ new: expect.objectContaining({ port: '2222' }),
+ })
+ );
+ });
+
+ it('pushes the config to the device that owns a plan', async () => {
+ mockPlanStore.getStoragePlans.mockResolvedValue([{ sourceId: 'device-2' } as any]);
+
+ await storageService.updateStorage(storageId, payload);
+
+ expect(RemoteStrategy).toHaveBeenCalledWith('device-2');
+ expect(mockRemoteStrategy.updateRemoteStorage).toHaveBeenCalledTimes(1);
+ expect(mockLocalStrategy.updateRemoteStorage).toHaveBeenCalledTimes(1);
+ });
+
+ it('pushes the config to the device that replicates to the storage', async () => {
+ mockStorageStore.getReplicationPlanSources = jest.fn().mockResolvedValue(['device-3']);
+
+ await storageService.updateStorage(storageId, payload);
+
+ expect(mockStorageStore.getReplicationPlanSources).toHaveBeenCalledWith(storageId);
+ expect(RemoteStrategy).toHaveBeenCalledWith('device-3');
+ expect(mockRemoteStrategy.updateRemoteStorage).toHaveBeenCalledTimes(1);
+ });
+
+ it('lists a device once when it owns a plan and replicates to the storage', async () => {
+ mockPlanStore.getStoragePlans.mockResolvedValue([{ sourceId: 'device-2' } as any]);
+ mockStorageStore.getReplicationPlanSources = jest.fn().mockResolvedValue(['device-2']);
+
+ await storageService.updateStorage(storageId, payload);
+
+ expect(mockRemoteStrategy.updateRemoteStorage).toHaveBeenCalledTimes(1);
+ });
+
+ it('sends the old values so a failed update can be reverted', async () => {
+ await storageService.updateStorage(storageId, payload);
+
+ expect(mockLocalStrategy.updateRemoteStorage).toHaveBeenCalledWith(
+ 'My SFTP',
+ expect.objectContaining({
+ old: expect.objectContaining({ host: 'example.com', port: '22' }),
+ })
+ );
+ });
+
+ it('does not resend the credentials when they did not change', async () => {
+ const samePayload = {
+ ...payload,
+ credentials: { host: 'example.com', port: '22' },
+ };
+
+ await storageService.updateStorage(storageId, samePayload);
+
+ const sent = (mockLocalStrategy.updateRemoteStorage as jest.Mock).mock.calls[0][1];
+ expect(sent.new).toEqual({ idle_timeout: '1m0s' });
+ });
+
+ it('writes the encrypted credentials to the database', async () => {
+ await storageService.updateStorage(storageId, payload);
+
+ expect(mockStorageStore.update).toHaveBeenCalledWith(
+ storageId,
+ expect.objectContaining({
+ credentials: { host: 'encrypted(example.com)', port: 'encrypted(2222)' },
+ })
+ );
+ });
+
+ it('throws NotFoundError if the storage does not exist', async () => {
+ mockStorageStore.getById.mockResolvedValue(null);
+
+ await expect(storageService.updateStorage(storageId, payload)).rejects.toThrow(
+ 'Storage not found'
+ );
+ });
+ });
+
// ----------------------------------------
// deleteStorage
// ----------------------------------------
diff --git a/backend/__tests__/stores/StorageStore.test.ts b/backend/__tests__/stores/StorageStore.test.ts
index 329a476..e22a6c2 100644
--- a/backend/__tests__/stores/StorageStore.test.ts
+++ b/backend/__tests__/stores/StorageStore.test.ts
@@ -256,4 +256,83 @@ describe('StorageStore', () => {
expect(result).toBe(false);
});
});
+
+ describe('getReplicationPlanSources', () => {
+ const replicationPlan = (
+ id: string,
+ sourceId: string,
+ targetId: string,
+ storageId = 'primary'
+ ) => ({
+ id,
+ title: `Plan ${id}`,
+ createdAt: new Date(),
+ isActive: true,
+ stats: null,
+ method: 'restic',
+ storageId,
+ sourceId,
+ settings: { replication: { enabled: true, storages: [{ storageId: targetId }] } },
+ });
+
+ it('should return the source of every plan replicating to the storage', async () => {
+ mockDb.query.plans.findMany.mockResolvedValue([
+ replicationPlan('plan-1', 'main', 'storage-1'),
+ replicationPlan('plan-2', 'device-2', 'storage-1'),
+ ]);
+
+ const result = await storageStore.getReplicationPlanSources('storage-1');
+
+ expect(result).toEqual(['main', 'device-2']);
+ });
+
+ it('should list a source once when it owns two replication plans', async () => {
+ mockDb.query.plans.findMany.mockResolvedValue([
+ replicationPlan('plan-1', 'device-2', 'storage-1'),
+ replicationPlan('plan-2', 'device-2', 'storage-1'),
+ ]);
+
+ const result = await storageStore.getReplicationPlanSources('storage-1');
+
+ expect(result).toEqual(['device-2']);
+ });
+
+ it('should ignore a plan that replicates to a different storage', async () => {
+ mockDb.query.plans.findMany.mockResolvedValue([
+ replicationPlan('plan-1', 'device-2', 'storage-9'),
+ ]);
+
+ const result = await storageStore.getReplicationPlanSources('storage-1');
+
+ expect(result).toEqual([]);
+ });
+
+ it('should ignore a plan whose primary storage is the same storage', async () => {
+ mockDb.query.plans.findMany.mockResolvedValue([
+ replicationPlan('plan-1', 'device-2', 'storage-1', 'storage-1'),
+ ]);
+
+ const result = await storageStore.getReplicationPlanSources('storage-1');
+
+ expect(result).toEqual([]);
+ });
+
+ it('should ignore a plan with replication disabled', async () => {
+ const plan = replicationPlan('plan-1', 'device-2', 'storage-1');
+ plan.settings.replication.enabled = false;
+ mockDb.query.plans.findMany.mockResolvedValue([plan]);
+
+ const result = await storageStore.getReplicationPlanSources('storage-1');
+
+ expect(result).toEqual([]);
+ });
+
+ it('should return an empty list when no plan exists', async () => {
+ mockDb.query.plans.findMany.mockResolvedValue([]);
+
+ const result = await storageStore.getReplicationPlanSources('storage-1');
+
+ expect(result).toEqual([]);
+ });
+ });
});
diff --git a/backend/__tests__/utils/providers.test.ts b/backend/__tests__/utils/providers.test.ts
new file mode 100644
index 0000000..63c8317
--- /dev/null
+++ b/backend/__tests__/utils/providers.test.ts
@@ -0,0 +1,69 @@
+import { providers } from '../../src/utils/providers';
+
+describe('providers setup()', () => {
+ const base = { host: 'example.com', user: 'bob', pass: 'secret' };
+ const baseArgs = ['host', 'example.com', 'user', 'bob', 'pass', 'secret'];
+
+ describe('sftp', () => {
+ it('sends the port when one is given', () => {
+ expect(providers.sftp.setup({ ...base, port: '2222' })).toEqual([
+ ...baseArgs,
+ 'port',
+ '2222',
+ ]);
+ });
+
+ it('omits the port when it is blank', () => {
+ expect(providers.sftp.setup({ ...base, port: '' })).toEqual(baseArgs);
+ });
+
+ it('omits the port when it is absent', () => {
+ expect(providers.sftp.setup(base)).toEqual(baseArgs);
+ });
+ });
+
+ describe('ftp', () => {
+ it('sends the port and both TLS switches', () => {
+ expect(
+ providers.ftp.setup({ ...base, port: '2121', tls: 'true', explicit_tls: 'false' })
+ ).toEqual([...baseArgs, 'port', '2121', 'tls', 'true', 'explicit_tls', 'false']);
+ });
+
+ it('omits the switches that are absent', () => {
+ expect(providers.ftp.setup({ ...base, explicit_tls: 'true' })).toEqual([
+ ...baseArgs,
+ 'explicit_tls',
+ 'true',
+ ]);
+ });
+
+ it('converts a boolean switch to a string, because spawn rejects other types', () => {
+ const creds = { ...base, port: 2121, tls: true } as unknown as Record<string, string>;
+ const args = providers.ftp.setup(creds) as string[];
+
+ expect(args).toEqual([...baseArgs, 'port', '2121', 'tls', 'true']);
+ args.forEach(arg => expect(typeof arg).toBe('string'));
+ });
+
+ it('keeps a switch that is explicitly false', () => {
+ const creds = { ...base, tls: false } as unknown as Record<string, string>;
+ expect(providers.ftp.setup(creds)).toEqual([...baseArgs, 'tls', 'false']);
+ });
+ });
+
+ describe('smb', () => {
+ it('sends the port and the domain', () => {
+ expect(providers.smb.setup({ ...base, port: '1445', domain: 'ACME' })).toEqual([
+ ...baseArgs,
+ 'port',
+ '1445',
+ 'domain',
+ 'ACME',
+ ]);
+ });
+
+ it('omits both when they are absent', () => {
+ expect(providers.smb.setup(base)).toEqual(baseArgs);
+ });
+ });
+});
diff --git a/backend/src/services/StorageService.ts b/backend/src/services/StorageService.ts
index 2325239..16d4caf 100644
--- a/backend/src/services/StorageService.ts
+++ b/backend/src/services/StorageService.ts
@@ -239,18 +239,18 @@ export class StorageService {
* need it re-pushed when the storage is edited.
*/
protected async resolveStorageSources(storageId: string): Promise<string[]> {
+ const sourceIds = new Set<string>(['main']);
const plansWithStorage = await this.planStore.getStoragePlans(storageId);
- if (!plansWithStorage || plansWithStorage.length === 0) {
- return [];
- }
+ const replicationSources = await this.storageStore.getReplicationPlanSources(storageId);
- const sourceIds = new Set<string>();
- for (const plan of plansWithStorage) {
- if (plan.sourceId) {
- sourceIds.add(plan.sourceId);
- }
+ for (const plan of plansWithStorage || []) {
+ if (plan.sourceId) sourceIds.add(plan.sourceId);
}
+ // A device that replicates to this storage also holds a config entry for it.
+ for (const sourceId of replicationSources) {
+ sourceIds.add(sourceId);
+ }
return [...sourceIds];
}
diff --git a/backend/src/stores/StorageStore.ts b/backend/src/stores/StorageStore.ts
index a093144..0a62138 100644
--- a/backend/src/stores/StorageStore.ts
+++ b/backend/src/stores/StorageStore.ts
@@ -3,7 +3,7 @@ import { sql } from 'drizzle-orm/sql';
import { DatabaseType } from '../db';
import { Storage, NewStorage, storages, StorageFull } from '../db/schema/storages';
import { providers } from '../utils/providers';
-import { PlanChildItem, PlanBackupSettings } from '../types/plans';
+import { PlanChildItem, PlanBackupSettings, ReplicationPlanItem } from '../types/plans';
/**
* StorageStore is a class for managing storage records in the database.
@@ -142,14 +142,24 @@ export class StorageStore {
return map.get(storageId) || [];
}
+ /**
+ * Returns the devices that own a plan replicating to the given storage.
+ * These devices hold an rclone config entry for it and need it re-pushed on edit.
+ */
+ async getReplicationPlanSources(storageId: string): Promise<string[]> {
+ const map = await this.getReplicationPlansForStorages([storageId]);
+ const plans = map.get(storageId) || [];
+ return [...new Set(plans.map(plan => plan.sourceId).filter(Boolean))];
+ }
+
/**
* Returns plans that use a given storage for replication (inside settings.replication.storages).
* Used to accurately count all plans associated with a storage.
*/
private async getReplicationPlansForStorages(
storageIds: string[]
- ): Promise<Map<string, PlanChildItem[]>> {
- const result = new Map<string, PlanChildItem[]>();
+ ): Promise<Map<string, ReplicationPlanItem[]>> {
+ const result = new Map<string, ReplicationPlanItem[]>();
if (storageIds.length === 0) return result;
// Query all plans that have replication settings
@@ -163,6 +173,7 @@ export class StorageStore {
method: true,
settings: true,
storageId: true,
+ sourceId: true,
},
});
@@ -184,6 +195,7 @@ export class StorageStore {
isActive: plan.isActive,
stats: plan.stats,
method: plan.method,
+ sourceId: plan.sourceId,
});
result.set(repStorage.storageId, existing);
}
diff --git a/backend/src/types/plans.ts b/backend/src/types/plans.ts
index 4a940d1..4092c7e 100644
--- a/backend/src/types/plans.ts
+++ b/backend/src/types/plans.ts
@@ -168,6 +168,8 @@ export interface PlanReplicationSettings {
storages: PlanReplicationStorage[];
}
+export type ReplicationPlanItem = PlanChildItem & { sourceId: string };
+
export interface PlanBackupSettings {
interval: PlanInterval;
prune: PlanPrune;