|
|
@ -9,113 +9,97 @@ import { flags } from './autorestic'
|
|
|
|
import { Backend, Config } from './types'
|
|
|
|
import { Backend, Config } from './types'
|
|
|
|
import { makeArrayIfIsNot, makeObjectKeysLowercase, rand } from './utils'
|
|
|
|
import { makeArrayIfIsNot, makeObjectKeysLowercase, rand } from './utils'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export enum LocationFromPrefixes {
|
|
|
|
export enum LocationFromPrefixes {
|
|
|
|
Filesystem,
|
|
|
|
Filesystem,
|
|
|
|
DockerVolume
|
|
|
|
DockerVolume,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const normalizeAndCheckBackends = (config: Config) => {
|
|
|
|
export const normalizeAndCheckBackends = (config: Config) => {
|
|
|
|
config.backends = makeObjectKeysLowercase(config.backends)
|
|
|
|
config.backends = makeObjectKeysLowercase(config.backends)
|
|
|
|
|
|
|
|
|
|
|
|
for (const [name, { type, path, key, ...rest }] of Object.entries(
|
|
|
|
for (const [name, { type, path, key, ...rest }] of Object.entries(config.backends)) {
|
|
|
|
config.backends,
|
|
|
|
if (!type || !path) throw new Error(`The backend "${name}" is missing some required attributes`)
|
|
|
|
)) {
|
|
|
|
|
|
|
|
if (!type || !path)
|
|
|
|
const tmp: any = {
|
|
|
|
throw new Error(
|
|
|
|
type,
|
|
|
|
`The backend "${name}" is missing some required attributes`,
|
|
|
|
path,
|
|
|
|
)
|
|
|
|
key: key || rand(128),
|
|
|
|
|
|
|
|
}
|
|
|
|
const tmp: any = {
|
|
|
|
for (const [key, value] of Object.entries(rest)) tmp[key.toUpperCase()] = value
|
|
|
|
type,
|
|
|
|
|
|
|
|
path,
|
|
|
|
config.backends[name] = tmp as Backend
|
|
|
|
key: key || rand(128),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(rest))
|
|
|
|
|
|
|
|
tmp[key.toUpperCase()] = value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config.backends[name] = tmp as Backend
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const normalizeAndCheckLocations = (config: Config) => {
|
|
|
|
export const normalizeAndCheckLocations = (config: Config) => {
|
|
|
|
config.locations = makeObjectKeysLowercase(config.locations)
|
|
|
|
config.locations = makeObjectKeysLowercase(config.locations)
|
|
|
|
const backends = Object.keys(config.backends)
|
|
|
|
const backends = Object.keys(config.backends)
|
|
|
|
|
|
|
|
|
|
|
|
const checkDestination = (backend: string, location: string) => {
|
|
|
|
const checkDestination = (backend: string, location: string) => {
|
|
|
|
if (!backends.includes(backend))
|
|
|
|
if (!backends.includes(backend)) throw new Error(`Cannot find the backend "${backend}" for "${location}"`)
|
|
|
|
throw new Error(`Cannot find the backend "${backend}" for "${location}"`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (const [name, { from, to, cron, ...rest }] of Object.entries(config.locations)) {
|
|
|
|
for (const [name, { from, to, cron, ...rest }] of Object.entries(config.locations)) {
|
|
|
|
if (!from) throw new Error(`The location "${name.blue}" is missing the "${'from'.underline.red}" source folder. See https://git.io/Jf0xw`)
|
|
|
|
if (!from)
|
|
|
|
if (!to || (Array.isArray(to) && !to.length))
|
|
|
|
throw new Error(`The location "${name.blue}" is missing the "${'from'.underline.red}" source folder. See https://git.io/Jf0xw`)
|
|
|
|
throw new Error(`The location "${name.blue}" has no backend "${'to'.underline.red}" to save the backups. See https://git.io/Jf0xw`)
|
|
|
|
if (!to || (Array.isArray(to) && !to.length))
|
|
|
|
|
|
|
|
throw new Error(`The location "${name.blue}" has no backend "${'to'.underline.red}" to save the backups. See https://git.io/Jf0xw`)
|
|
|
|
for (const t of makeArrayIfIsNot(to)) checkDestination(t, name)
|
|
|
|
|
|
|
|
|
|
|
|
for (const t of makeArrayIfIsNot(to))
|
|
|
|
if (cron) {
|
|
|
|
checkDestination(t, name)
|
|
|
|
try {
|
|
|
|
|
|
|
|
CronParser.parseExpression(cron)
|
|
|
|
if (cron) {
|
|
|
|
} catch {
|
|
|
|
try {
|
|
|
|
throw new Error(`The location "${name.blue}" has an invalid ${'cron'.underline.red} entry. See https://git.io/Jf0xP`)
|
|
|
|
CronParser.parseExpression(cron)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
}
|
|
|
|
throw new Error(`The location "${name.blue}" has an invalid ${'cron'.underline.red} entry. See https://git.io/Jf0xP`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const findConfigFile = (): string => {
|
|
|
|
const findConfigFile = (): string => {
|
|
|
|
const config = '.autorestic.yml'
|
|
|
|
const config = '.autorestic.yml'
|
|
|
|
const paths = [
|
|
|
|
const paths = [resolve(flags.config || ''), resolve('./' + config), homedir() + '/' + config]
|
|
|
|
resolve(flags.config || ''),
|
|
|
|
for (const path of paths) {
|
|
|
|
resolve('./' + config),
|
|
|
|
try {
|
|
|
|
homedir() + '/' + config,
|
|
|
|
const file = statSync(path)
|
|
|
|
]
|
|
|
|
if (file.isFile()) return path
|
|
|
|
for (const path of paths) {
|
|
|
|
} catch (e) {}
|
|
|
|
try {
|
|
|
|
}
|
|
|
|
const file = statSync(path)
|
|
|
|
throw new Error('Config file not found')
|
|
|
|
if (file.isFile()) return path
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Config file not found')
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export let CONFIG_FILE: string = ''
|
|
|
|
export let CONFIG_FILE: string = ''
|
|
|
|
|
|
|
|
|
|
|
|
export const init = (): Config => {
|
|
|
|
export const init = (): Config => {
|
|
|
|
const file = findConfigFile()
|
|
|
|
const file = findConfigFile()
|
|
|
|
CONFIG_FILE = file
|
|
|
|
CONFIG_FILE = file
|
|
|
|
|
|
|
|
|
|
|
|
const raw: Config = makeObjectKeysLowercase(
|
|
|
|
const parsed = yaml.safeLoad(readFileSync(CONFIG_FILE).toString())
|
|
|
|
yaml.safeLoad(readFileSync(CONFIG_FILE).toString()),
|
|
|
|
if (!parsed || typeof parsed === 'string') throw new Error('Could not parse the config file')
|
|
|
|
)
|
|
|
|
const raw: Config = makeObjectKeysLowercase(parsed)
|
|
|
|
|
|
|
|
|
|
|
|
const current = JSON.stringify(raw)
|
|
|
|
const current = JSON.stringify(raw)
|
|
|
|
|
|
|
|
|
|
|
|
normalizeAndCheckBackends(raw)
|
|
|
|
normalizeAndCheckBackends(raw)
|
|
|
|
normalizeAndCheckLocations(raw)
|
|
|
|
normalizeAndCheckLocations(raw)
|
|
|
|
|
|
|
|
|
|
|
|
const changed = JSON.stringify(raw) !== current
|
|
|
|
const changed = JSON.stringify(raw) !== current
|
|
|
|
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
if (changed) {
|
|
|
|
const OLD_CONFIG_FILE = CONFIG_FILE + '.old'
|
|
|
|
const OLD_CONFIG_FILE = CONFIG_FILE + '.old'
|
|
|
|
copyFileSync(CONFIG_FILE, OLD_CONFIG_FILE)
|
|
|
|
copyFileSync(CONFIG_FILE, OLD_CONFIG_FILE)
|
|
|
|
writeFileSync(CONFIG_FILE, yaml.safeDump(raw))
|
|
|
|
writeFileSync(CONFIG_FILE, yaml.safeDump(raw))
|
|
|
|
console.log(
|
|
|
|
console.log(
|
|
|
|
'\n' +
|
|
|
|
'\n' +
|
|
|
|
'⚠️ MOVED OLD CONFIG FILE TO: ⚠️'.red.underline.bold +
|
|
|
|
'⚠️ MOVED OLD CONFIG FILE TO: ⚠️'.red.underline.bold +
|
|
|
|
'\n' +
|
|
|
|
'\n' +
|
|
|
|
OLD_CONFIG_FILE +
|
|
|
|
OLD_CONFIG_FILE +
|
|
|
|
'\n' +
|
|
|
|
'\n' +
|
|
|
|
'What? Why? '.grey + 'https://git.io/Jf0xK'.underline.grey +
|
|
|
|
'What? Why? '.grey +
|
|
|
|
'\n'
|
|
|
|
'https://git.io/Jf0xK'.underline.grey +
|
|
|
|
)
|
|
|
|
'\n'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
return raw
|
|
|
|
|
|
|
|
|
|
|
|
return raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|