diff --git a/src/backup.ts b/src/backup.ts index bfdd837..d71e304 100644 --- a/src/backup.ts +++ b/src/backup.ts @@ -3,21 +3,16 @@ import { Writer } from 'clitastic' import { config, VERBOSE } from './autorestic' import { getEnvFromBackend } from './backend' import { Locations, Location } from './types' -import { exec, ConfigError } from './utils' -import { CONFIG_FILE } from './config' -import { resolve, dirname, isAbsolute } from 'path' +import { exec, ConfigError, pathRelativeToConfigFile } from './utils' export const backupSingle = (name: string, from: string, to: string) => { if (!config) throw ConfigError const writer = new Writer(name + to.blue + ' : ' + 'Backing up... ⏳') const backend = config.backends[to] - // Check if is an absolute path, otherwise get the path relative to the config file - const pathRelativeToConfigFile = isAbsolute(from) - ? from - : resolve(dirname(CONFIG_FILE), from) + const path = pathRelativeToConfigFile(to) - const cmd = exec('restic', ['backup', pathRelativeToConfigFile], { + const cmd = exec('restic', ['backup', path], { env: getEnvFromBackend(backend), }) diff --git a/src/utils.ts b/src/utils.ts index 77c3b9c..d39a316 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,6 +2,8 @@ import axios from 'axios' import { spawnSync, SpawnSyncOptions } from 'child_process' import { randomBytes } from 'crypto' import { createWriteStream } from 'fs' +import { isAbsolute, resolve, dirname } from 'path' +import { CONFIG_FILE } from './config' export const exec = ( command: string, @@ -26,7 +28,7 @@ export const checkIfResticIsAvailable = () => checkIfCommandIsAvailable( 'restic', 'Restic is not installed'.red + - ' https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases' + ' https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases' ) export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => { @@ -74,4 +76,9 @@ export const downloadFile = async (url: string, to: string) => }) }) +// Check if is an absolute path, otherwise get the path relative to the config file +export const pathRelativeToConfigFile = (path: string): string => isAbsolute(path) + ? path + : resolve(dirname(CONFIG_FILE), path) + export const ConfigError = new Error('Config file not found')