Skip to content

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

chore: adds the ability to run rclone commands with progress tracking and cancel

towfiqid32da5ad380e
diff --git a/backend/src/utils/rclone/rclone.ts b/backend/src/utils/rclone/rclone.ts
index b206548..1867c2c 100644
--- a/backend/src/utils/rclone/rclone.ts
+++ b/backend/src/utils/rclone/rclone.ts
@@ -1,4 +1,4 @@
-import { spawn } from 'child_process';
+import { spawn, ChildProcess } from 'child_process';
import os from 'os';
import { getBinaryPath } from '../binaryPathResolver';
import { getRcloneConfigPath } from './helpers';
@@ -53,3 +53,62 @@ export function runRcloneCommand(args: string[], env?: Record<string, string>):
});
});
}
+
+/**
+ * Spawns an rclone process and returns both the ChildProcess (for tracking/cancellation)
+ * and a promise that resolves with the full output on completion.
+ * The optional `onStderr` callback is called for each chunk of stderr data,
+ * enabling real-time progress parsing.
+ */
+export function runRcloneCommandWithProgress(
+ args: string[],
+ options?: { onStderr?: (chunk: string) => void; env?: Record<string, string> }
+): { process: ChildProcess; promise: Promise<string> } {
+ const rcloneBinary = getBinaryPath('rclone');
+ const rcloneConfigPath = getRcloneConfigPath();
+ const localAppData = process.env.LOCALAPPDATA || os.homedir();
+ const globalRcloneEnv = buildRcloneEnvFromSettings();
+
+ const envVars = {
+ PATH: process.env.PATH,
+ RCLONE_CONFIG: rcloneConfigPath,
+ LOCALAPPDATA: localAppData,
+ RCLONE_CONFIG_PASS: configService.config.ENCRYPTION_KEY,
+ ...globalRcloneEnv,
+ ...options?.env,
+ };
+
+ const rcProcess = spawn(rcloneBinary, args, { env: envVars });
+ let output = '';
+ let errorOutput = '';
+
+ const promise = new Promise<string>((resolve, reject) => {
+ rcProcess.stdout?.on('data', (data: Buffer) => {
+ output += data.toString();
+ });
+
+ rcProcess.stderr?.on('data', (data: Buffer) => {
+ const chunk = data.toString();
+ errorOutput += chunk;
+ options?.onStderr?.(chunk);
+ });
+
+ rcProcess.on('close', (code: number) => {
+ if (code === 0) {
+ const outputRes = output.trim();
+ const errorOutputRes = errorOutput.trim();
+ if (outputRes) {
+ resolve(outputRes);
+ } else if (errorOutputRes) {
+ resolve(errorOutputRes);
+ } else {
+ resolve('');
+ }
+ } else {
+ reject(new Error(errorOutput || `Rclone command failed`));
+ }
+ });
+ });
+
+ return { process: rcProcess, promise };
+}