Skip to content

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

fix: resolves broken global restic and rclone settings.

towfiqi42d84c0fe837
diff --git a/backend/src/managers/BaseSystemManager.ts b/backend/src/managers/BaseSystemManager.ts
index 5a012a8..42377c4 100644
--- a/backend/src/managers/BaseSystemManager.ts
+++ b/backend/src/managers/BaseSystemManager.ts
@@ -1,6 +1,6 @@
import os from 'os';
import { execSync } from 'child_process';
-import { chmod, readdir, readFile, stat, writeFile, mkdtemp, rm } from 'fs/promises';
+import { chmod, readdir, stat, mkdtemp, rm } from 'fs/promises';
import { createWriteStream } from 'fs';
import path from 'path';
import si from 'systeminformation';
@@ -13,6 +13,7 @@ import { DeviceSettings, DiscoveredFilesystem, LsblkDevice, LsblkOutput } from '
import { BaseStorageManager } from './BaseStorageManager';
import { runCommand } from '../utils/runCommand';
import { appPaths } from '../utils/AppPaths';
+import { saveRcloneGlobalSettings, saveResticGlobalSettings } from '../utils/globalSettings';
const execAsync = promisify(exec);
@@ -632,89 +633,24 @@ export class BaseSystemManager {
deviceSettings: DeviceSettings
): Promise<{ success: boolean; result: string }> {
try {
- const platform = os.platform();
- const envVars: [string, string | number | undefined][] = [];
-
- // Collect Restic env vars
+ // Persist restic global settings to data/config/restic_global.json
if (deviceSettings.restic) {
- const { maxProcessor, cacheDir, readConcurrency, packSize } = deviceSettings.restic;
- envVars.push(
- ['GOMAXPROCS', maxProcessor],
- ['RESTIC_CACHE_DIR', cacheDir],
- ['RESTIC_READ_CONCURRENCY', readConcurrency],
- ['RESTIC_PACK_SIZE', packSize]
- );
+ saveResticGlobalSettings(deviceSettings.restic);
}
- // Collect Rclone env vars
+ // Persist rclone global settings to data/config/rclone_global.json
if (deviceSettings.rclone) {
- const {
- tempDir,
- cacheDir,
- bwlimit,
- timeout,
- retries,
- lowLevelRetries,
- transfers,
- checkers,
- bufferSize,
- multiThreadStream,
- configPass,
- } = deviceSettings.rclone;
-
- envVars.push(
- ['RCLONE_TEMP_DIR', tempDir],
- ['RCLONE_CACHE_DIR', cacheDir],
- ['RCLONE_BWLIMIT', bwlimit],
- ['RCLONE_TIMEOUT', timeout],
- ['RCLONE_RETRIES', retries],
- ['RCLONE_LOW_LEVEL_RETRIES', lowLevelRetries],
- ['RCLONE_TRANSFERS', transfers],
- ['RCLONE_CHECKERS', checkers],
- ['RCLONE_BUFFER_SIZE', bufferSize],
- ['RCLONE_MULTI_THREAD_STREAMS', multiThreadStream],
- ['RCLONE_CONFIG_PASS', configPass]
- );
- }
-
- if (platform === 'win32') {
- for (const [key, value] of envVars) {
- if (value === null || value === undefined) {
- // Check if key exists before attempting deletion
- try {
- await execAsync(`REG QUERY "HKCU\\Environment" /v ${key}`);
- await execAsync(`REG DELETE "HKCU\\Environment" /F /V ${key}`);
- } catch {
- // Key doesn't exist, skip deletion
- continue;
- }
- } else {
- await execAsync(`SETX ${key} "${value}"`);
- }
- }
- } else {
- const envFile = '/etc/environment';
- let content = await readFile(envFile, 'utf8');
-
- for (const [key, value] of envVars) {
- content = content.replace(new RegExp(`^${key}=.*`, 'm'), '');
- if (value !== null && value !== undefined) {
- content += `\n${key}=${value}`;
- }
- }
-
- content = content.replace(/\n+/g, '\n').trim();
- await writeFile(envFile, content);
+ saveRcloneGlobalSettings(deviceSettings.rclone);
}
return {
success: true,
- result: 'System environment variables updated successfully!',
+ result: 'Global rclone/restic settings updated successfully!',
};
} catch (error: any) {
return {
success: false,
- result: error?.message || 'Failed to update system environment variables',
+ result: error?.message || 'Failed to update global settings',
};
}
}
diff --git a/backend/src/services/DeviceService.ts b/backend/src/services/DeviceService.ts
index 1711787..aa0d8bb 100644
--- a/backend/src/services/DeviceService.ts
+++ b/backend/src/services/DeviceService.ts
@@ -9,7 +9,7 @@ import { LocalStrategy, RemoteStrategy, SystemStrategy } from '../strategies/sys
import { StorageStore } from '../stores/StorageStore';
import { providers } from '../utils/providers';
import { configService } from './ConfigService';
-import { AppError } from 'src/utils/AppError';
+import { AppError } from '../utils/AppError';
export class DeviceService {
protected connectedDeviceIds: Set<string> | undefined;
diff --git a/backend/src/utils/globalSettings.ts b/backend/src/utils/globalSettings.ts
new file mode 100644
index 0000000..c7281bb
--- /dev/null
+++ b/backend/src/utils/globalSettings.ts
@@ -0,0 +1,163 @@
+/**
+ * Manages global rclone and restic settings stored as JSON files in data/config/.
+ * These files are written when the user updates device settings and read by
+ * runRcloneCommand / runResticCommand to inject the settings as CLI flags or env vars.
+ *
+ * This avoids setting system-level environment variables (security concern)
+ * and avoids requiring database access from utility functions / managers.
+ */
+
+import fs from 'fs';
+import path from 'path';
+import { appPaths } from './AppPaths';
+import { DeviceSettings } from '../types/devices';
+
+// ---------- File paths ----------
+
+const getRcloneSettingsPath = () => path.join(appPaths.getConfigDir(), 'rclone_global.json');
+const getResticSettingsPath = () => path.join(appPaths.getConfigDir(), 'restic_global.json');
+
+// ---------- In-memory caches ----------
+
+let rcloneSettingsCache: DeviceSettings['rclone'] | null = null;
+let resticSettingsCache: DeviceSettings['restic'] | null = null;
+
+// ---------- Write helpers (called from updateSettings) ----------
+
+/**
+ * Persist rclone global settings to `data/config/rclone_global.json`.
+ * Only creates the file when the user explicitly changes settings.
+ */
+export function saveRcloneGlobalSettings(settings: DeviceSettings['rclone']): void {
+ const filePath = getRcloneSettingsPath();
+ fs.writeFileSync(filePath, JSON.stringify(settings, null, 2), { mode: 0o600 });
+ // Update in-memory cache
+ rcloneSettingsCache = settings ?? null;
+}
+
+/**
+ * Persist restic global settings to `data/config/restic_global.json`.
+ * Only creates the file when the user explicitly changes settings.
+ */
+export function saveResticGlobalSettings(settings: DeviceSettings['restic']): void {
+ const filePath = getResticSettingsPath();
+ fs.writeFileSync(filePath, JSON.stringify(settings, null, 2), { mode: 0o600 });
+ // Update in-memory cache
+ resticSettingsCache = settings ?? null;
+}
+
+// ---------- Read helpers (called from runRcloneCommand / runResticCommand) ----------
+
+/**
+ * Load rclone global settings. Returns `undefined` if no settings file exists.
+ * The result is cached in memory so the file is read at most once per process lifetime
+ * (cache is invalidated on write via `saveRcloneGlobalSettings`).
+ */
+export function loadRcloneGlobalSettings(): DeviceSettings['rclone'] | undefined {
+ if (rcloneSettingsCache !== null) {
+ return rcloneSettingsCache;
+ }
+
+ const filePath = getRcloneSettingsPath();
+ try {
+ if (fs.existsSync(filePath)) {
+ const raw = fs.readFileSync(filePath, 'utf-8');
+ const parsed = JSON.parse(raw) as DeviceSettings['rclone'];
+ rcloneSettingsCache = parsed ?? null;
+ return parsed;
+ }
+ } catch (error) {
+ console.error('[globalSettings] Failed to read rclone_global.json:', error);
+ }
+ return undefined;
+}
+
+/**
+ * Load restic global settings. Returns `undefined` if no settings file exists.
+ * The result is cached in memory so the file is read at most once per process lifetime
+ * (cache is invalidated on write via `saveResticGlobalSettings`).
+ */
+export function loadResticGlobalSettings(): DeviceSettings['restic'] | undefined {
+ if (resticSettingsCache !== null) {
+ return resticSettingsCache;
+ }
+
+ const filePath = getResticSettingsPath();
+ try {
+ if (fs.existsSync(filePath)) {
+ const raw = fs.readFileSync(filePath, 'utf-8');
+ const parsed = JSON.parse(raw) as DeviceSettings['restic'];
+ resticSettingsCache = parsed ?? null;
+ return parsed;
+ }
+ } catch (error) {
+ console.error('[globalSettings] Failed to read restic_global.json:', error);
+ }
+ return undefined;
+}
+
+// ---------- Env / args builders for command runners ----------
+
+/**
+ * Build extra environment variables for rclone based on global settings.
+ * These are merged into the env passed to `spawn()`.
+ */
+export function buildRcloneEnvFromSettings(): Record<string, string> {
+ const settings = loadRcloneGlobalSettings();
+ if (!settings) return {};
+
+ const env: Record<string, string> = {};
+
+ if (settings.tempDir) env.RCLONE_TEMP_DIR = settings.tempDir;
+ if (settings.cacheDir) env.RCLONE_CACHE_DIR = settings.cacheDir;
+ if (settings.bwlimit) env.RCLONE_BWLIMIT = settings.bwlimit;
+ if (settings.timeout) env.RCLONE_TIMEOUT = settings.timeout;
+ if (settings.retries) env.RCLONE_RETRIES = String(settings.retries);
+ if (settings.lowLevelRetries) env.RCLONE_LOW_LEVEL_RETRIES = String(settings.lowLevelRetries);
+ if (settings.transfers) env.RCLONE_TRANSFERS = String(settings.transfers);
+ if (settings.checkers) env.RCLONE_CHECKERS = String(settings.checkers);
+ if (settings.bufferSize) env.RCLONE_BUFFER_SIZE = settings.bufferSize;
+ if (settings.multiThreadStream)
+ env.RCLONE_MULTI_THREAD_STREAMS = String(settings.multiThreadStream);
+ if (settings.configPass) env.RCLONE_CONFIG_PASS = settings.configPass;
+
+ return env;
+}
+
+/**
+ * Build extra environment variables for restic based on global settings.
+ * These are merged into the env passed to `spawn()`.
+ */
+export function buildResticEnvFromSettings(): Record<string, string> {
+ const settings = loadResticGlobalSettings();
+ if (!settings) return {};
+
+ const env: Record<string, string> = {};
+
+ if (settings.maxProcessor) env.GOMAXPROCS = String(settings.maxProcessor);
+ if (settings.cacheDir) env.RESTIC_CACHE_DIR = settings.cacheDir;
+ if (settings.readConcurrency) env.RESTIC_READ_CONCURRENCY = String(settings.readConcurrency);
+ if (settings.packSize) env.RESTIC_PACK_SIZE = settings.packSize;
+
+ return env;
+}
+
+/**
+ * Build extra CLI arguments for restic based on global settings.
+ * These are appended to the args array passed to `spawn()`.
+ */
+export function buildResticArgsFromSettings(): string[] {
+ const settings = loadResticGlobalSettings();
+ if (!settings) return [];
+
+ const extraArgs: string[] = [];
+
+ if (settings.packSize) {
+ extraArgs.push('--pack-size', settings.packSize);
+ }
+ if (settings.readConcurrency) {
+ extraArgs.push('--read-concurrency', String(settings.readConcurrency));
+ }
+
+ return extraArgs;
+}
diff --git a/backend/src/utils/rclone/rclone.ts b/backend/src/utils/rclone/rclone.ts
index 4099844..422123e 100644
--- a/backend/src/utils/rclone/rclone.ts
+++ b/backend/src/utils/rclone/rclone.ts
@@ -3,18 +3,23 @@ import os from 'os';
import { getBinaryPath } from '../binaryPathResolver';
import { getRcloneConfigPath } from './helpers';
import { configService } from '../../services/ConfigService';
+import { buildRcloneEnvFromSettings } from '../globalSettings';
export function runRcloneCommand(args: string[], env?: Record<string, string>): Promise<string> {
return new Promise((resolve, reject) => {
const rcloneBinary = getBinaryPath('rclone');
const rcloneConfigPath = getRcloneConfigPath();
const localAppData = process.env.LOCALAPPDATA || os.homedir();
+ // Inject global rclone settings from data/config/rclone_global.json
+ const globalRcloneEnv = buildRcloneEnvFromSettings();
+
const envVars = {
PATH: process.env.PATH, // Needed for docker instances
RCLONE_CONFIG: rcloneConfigPath,
LOCALAPPDATA: localAppData,
RCLONE_CONFIG_PASS: configService.config.ENCRYPTION_KEY,
- ...env,
+ ...globalRcloneEnv,
+ ...env, // Per-call overrides still take precedence
};
// console.log('rcloneBinary :', rcloneBinary);
const rcProcess = spawn(rcloneBinary, args, { env: envVars });
diff --git a/backend/src/utils/restic/restic.ts b/backend/src/utils/restic/restic.ts
index c00ddb5..ed0fa01 100644
--- a/backend/src/utils/restic/restic.ts
+++ b/backend/src/utils/restic/restic.ts
@@ -7,6 +7,7 @@ import { getBinaryPath } from '../binaryPathResolver';
import { appPaths } from '../AppPaths';
import { generateResticRepoPath } from './helpers';
import { configService } from '../../services/ConfigService';
+import { buildResticEnvFromSettings, buildRcloneEnvFromSettings } from '../globalSettings';
export function runResticCommand(
args: string[],
@@ -32,12 +33,16 @@ export function runResticCommand(
const currentPath = env?.PATH || process.env.PATH || process.env.Path || '';
const newPath = `${rcloneDir}${path.delimiter}${currentPath}`;
+ // Inject global restic & rclone settings from data/config/*.json
+ const globalResticEnv = buildResticEnvFromSettings();
+ const globalRcloneEnv = buildRcloneEnvFromSettings();
+
const envVars = {
- // ...process.env,
- ...env,
+ ...globalResticEnv,
+ ...globalRcloneEnv,
+ ...env, // Per-call overrides still take precedence
PATH: newPath,
RCLONE_CONFIG: rcloneConfigPath,
- // RCLONE_PROGRESS: 'true',
LOCALAPPDATA: localAppData,
RCLONE_CONFIG_PASS: configService.config.ENCRYPTION_KEY,
};