Compare commits

...

8 Commits
0.4 ... 0.5

Author SHA1 Message Date
cupcakearmy
de27034b94 config optional if not required for current operation 2019-10-26 20:52:17 +02:00
cupcakearmy
9dafe9d36a wrong version bump 2019-10-26 20:09:19 +02:00
cupcakearmy
d47e7d0912 directories are now relative to its config file location 2019-10-26 20:07:52 +02:00
cupcakearmy
e47d6be854 small bugs 2019-10-26 20:07:41 +02:00
cupcakearmy
993fe072e2 also check for default file in the current directory 2019-10-26 20:07:36 +02:00
cupcakearmy
3d1e28e574 typos 2019-10-26 20:07:19 +02:00
cupcakearmy
3c0ebdfb4a prettier and ignore yarn 2019-10-26 20:06:48 +02:00
cupcakearmy
2653633c91 target only macos and linux 2019-06-21 13:32:30 +02:00
10 changed files with 469 additions and 381 deletions

1
.gitignore vendored
View File

@@ -1,6 +1,7 @@
node_modules/
package-lock.json
.idea
yarn.lock
config.yml
bin

3
.prettierrc Normal file
View File

@@ -0,0 +1,3 @@
semi: false
singleQuote: true
trailingComma: 'es5'

View File

@@ -4,12 +4,13 @@
"build": "tsc",
"build:watch": "tsc -w",
"dev": "tsnd --no-notify --respawn ./src/autorestic.ts",
"bin": "npm run build && pkg lib/autorestic.js --out-path bin"
"bin": "npm run build && pkg lib/autorestic.js --targets latest-macos-x64,latest-linux-x64 --out-path bin"
},
"devDependencies": {
"@types/decompress": "^4.2.3",
"@types/js-yaml": "^3.12.1",
"@types/minimist": "^1.2.0",
"@types/node": "^12.11.7",
"pkg": "^4.4.0",
"ts-node-dev": "^1.0.0-pre.40",
"typescript": "^3.5.1"

View File

@@ -1,13 +1,10 @@
import 'colors'
import minimist from 'minimist'
import { homedir } from 'os'
import { resolve } from 'path'
import { init } from './config'
import handlers, { error, help } from './handlers'
import { Config } from './types'
process.on('uncaughtException', err => {
console.log(err.message)
process.exit(1)
@@ -15,32 +12,30 @@ process.on('uncaughtException', err => {
export const { _: commands, ...flags } = minimist(process.argv.slice(2), {
alias: {
'c': 'config',
'v': 'verbose',
'h': 'help',
'a': 'all',
'l': 'location',
'b': 'backend',
c: 'config',
v: 'version',
h: 'help',
a: 'all',
l: 'location',
b: 'backend',
},
boolean: ['a'],
string: ['l', 'b'],
})
export const VERSION = '0.4'
export const DEFAULT_CONFIG = '/.autorestic.yml'
export const VERSION = '0.5'
export const INSTALL_DIR = '/usr/local/bin'
export const CONFIG_FILE: string = resolve(flags.config || homedir() + DEFAULT_CONFIG)
export const VERBOSE = flags.verbose
export const config: Config = init()
if (flags.version) {
console.log('version'.grey, VERSION)
process.exit(0)
}
export const config = init()
function main() {
if (flags.version)
return console.log('version'.grey, VERSION)
if (commands.length < 1)
return help()
if (commands.length < 1) return help()
const command: string = commands[0]
const args: string[] = commands.slice(1)

View File

@@ -2,11 +2,10 @@ import { Writer } from 'clitastic'
import { config, VERBOSE } from './autorestic'
import { Backend, Backends } from './types'
import { exec } from './utils'
import { exec, ConfigError } from './utils'
const ALREADY_EXISTS = /(?=.*exists)(?=.*already)(?=.*config).*/
export const getPathFromBackend = (backend: Backend): string => {
switch (backend.type) {
case 'local':
@@ -24,7 +23,6 @@ export const getPathFromBackend = (backend: Backend): string => {
}
}
export const getEnvFromBackend = (backend: Backend) => {
const { type, path, key, ...rest } = backend
return {
@@ -34,7 +32,6 @@ export const getEnvFromBackend = (backend: Backend) => {
}
}
export const checkAndConfigureBackend = (name: string, backend: Backend) => {
const writer = new Writer(name.blue + ' : ' + 'Configuring... ⏳')
const env = getEnvFromBackend(backend)
@@ -49,8 +46,12 @@ export const checkAndConfigureBackend = (name: string, backend: Backend) => {
writer.done(name.blue + ' : ' + 'Done ✓'.green)
}
export const checkAndConfigureBackends = (backends?: Backends) => {
if (!backends) {
if (!config) throw ConfigError
backends = config.backends
}
export const checkAndConfigureBackends = (backends: Backends = config.backends) => {
console.log('\nConfiguring Backends'.grey.underline)
for (const [name, backend] of Object.entries(backends))
checkAndConfigureBackend(name, backend)

View File

@@ -3,34 +3,44 @@ import { Writer } from 'clitastic'
import { config, VERBOSE } from './autorestic'
import { getEnvFromBackend } from './backend'
import { Locations, Location } from './types'
import { exec } from './utils'
import { exec, ConfigError } from './utils'
import { CONFIG_FILE } from './config'
import { resolve, dirname } from 'path'
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]
const cmd = exec('restic', ['backup', from], { env: getEnvFromBackend(backend) })
const pathRelativeToConfigFile = resolve(dirname(CONFIG_FILE), from)
const cmd = exec('restic', ['backup', pathRelativeToConfigFile], {
env: getEnvFromBackend(backend),
})
if (VERBOSE) console.log(cmd.out, cmd.err)
writer.done(name + to.blue + ' : ' + 'Done ✓'.green)
}
export const backupLocation = (name: string, backup: Location) => {
const display = name.yellow + ' ▶ '
if (Array.isArray(backup.to)) {
let first = true
for (const t of backup.to) {
const nameOrBlankSpaces: string = first ? display : new Array(name.length + 3).fill(' ').join('')
const nameOrBlankSpaces: string = first
? display
: new Array(name.length + 3).fill(' ').join('')
backupSingle(nameOrBlankSpaces, backup.from, t)
if (first) first = false
}
} else
backupSingle(display, backup.from, backup.to)
} else backupSingle(display, backup.from, backup.to)
}
export const backupAll = (backups?: Locations) => {
if (!backups) {
if (!config) throw ConfigError
backups = config.locations
}
export const backupAll = (backups: Locations = config.locations) => {
console.log('\nBacking Up'.underline.grey)
for (const [name, backup] of Object.entries(backups))
backupLocation(name, backup)

View File

@@ -1,16 +1,21 @@
import { readFileSync, writeFileSync } from 'fs'
import { readFileSync, writeFileSync, statSync } from 'fs'
import { resolve } from 'path'
import yaml from 'js-yaml'
import { CONFIG_FILE } from './autorestic'
import { flags } from './autorestic'
import { Backend, Config } from './types'
import { makeObjectKeysLowercase, rand } from './utils'
import { homedir } from 'os'
export const normalizeAndCheckBackends = (config: Config) => {
config.backends = makeObjectKeysLowercase(config.backends)
for (const [name, { type, path, key, ...rest }] of Object.entries(config.backends)) {
if (!type || !path) throw new Error(`The backend "${name}" is missing some required attributes`)
for (const [name, { type, path, key, ...rest }] of Object.entries(
config.backends
)) {
if (!type || !path)
throw new Error(
`The backend "${name}" is missing some required attributes`
)
const tmp: any = {
type,
@@ -24,7 +29,6 @@ export const normalizeAndCheckBackends = (config: Config) => {
}
}
export const normalizeAndCheckBackups = (config: Config) => {
config.locations = makeObjectKeysLowercase(config.locations)
const backends = Object.keys(config.backends)
@@ -34,20 +38,47 @@ export const normalizeAndCheckBackups = (config: Config) => {
throw new Error(`Cannot find the backend "${backend}" for "${backup}"`)
}
for (const [name, { from, to, ...rest }] of Object.entries(config.locations)) {
if (!from || !to) throw new Error(`The backup "${name}" is missing some required attributes`)
for (const [name, { from, to, ...rest }] of Object.entries(
config.locations
)) {
if (!from || !to)
throw new Error(
`The backup "${name}" is missing some required attributes`
)
if (Array.isArray(to))
for (const t of to)
checkDestination(t, name)
else
checkDestination(to, name)
if (Array.isArray(to)) for (const t of to) checkDestination(t, name)
else checkDestination(to, name)
}
}
const findConfigFile = (): string => {
const config = '.autorestic.yml'
const paths = [
resolve(flags.config || ''),
resolve('./' + config),
homedir() + '/' + config,
]
for (const path of paths) {
try {
const file = statSync(path)
if (file.isFile()) return path
} catch (e) {}
}
throw new Error('No Config file found')
}
export const init = (): Config => {
const raw: Config = makeObjectKeysLowercase(yaml.safeLoad(readFileSync(CONFIG_FILE).toString()))
export let CONFIG_FILE: string = ''
export const init = (): Config | undefined => {
try {
CONFIG_FILE = findConfigFile()
} catch (e) {
return
}
const raw: Config = makeObjectKeysLowercase(
yaml.safeLoad(readFileSync(CONFIG_FILE).toString())
)
normalizeAndCheckBackends(raw)
normalizeAndCheckBackups(raw)

View File

@@ -4,7 +4,7 @@ import { unlinkSync } from 'fs'
import { tmpdir } from 'os'
import { join, resolve } from 'path'
import { config, CONFIG_FILE, INSTALL_DIR, VERSION } from './autorestic'
import { config, INSTALL_DIR, VERSION } from './autorestic'
import { checkAndConfigureBackends, getEnvFromBackend } from './backend'
import { backupAll } from './backup'
import { Backends, Flags, Locations } from './types'
@@ -15,18 +15,22 @@ import {
exec,
filterObjectByKey,
singleToArray,
ConfigError,
} from './utils'
export type Handlers = { [command: string]: (args: string[], flags: Flags) => void }
export type Handlers = {
[command: string]: (args: string[], flags: Flags) => void
}
const parseBackend = (flags: Flags): Backends => {
if (!config) throw ConfigError
if (!flags.all && !flags.backend)
throw new Error('No backends specified.'.red
+ '\n--all [-a]\t\t\t\tCheck all.'
+ '\n--backend [-b] myBackend\t\tSpecify one or more backend',
throw new Error(
'No backends specified.'.red +
'\n--all [-a]\t\t\t\tCheck all.' +
'\n--backend [-b] myBackend\t\tSpecify one or more backend'
)
if (flags.all)
return config.backends
if (flags.all) return config.backends
else {
const backends = singleToArray<string>(flags.backend)
for (const backend of backends)
@@ -37,10 +41,12 @@ const parseBackend = (flags: Flags): Backends => {
}
const parseLocations = (flags: Flags): Locations => {
if (!config) throw ConfigError
if (!flags.all && !flags.location)
throw new Error('No locations specified.'.red
+ '\n--all [-a]\t\t\t\tBackup all.'
+ '\n--location [-l] site1\t\t\tSpecify one or more locations',
throw new Error(
'No locations specified.'.red +
'\n--all [-a]\t\t\t\tBackup all.' +
'\n--location [-l] site1\t\t\tSpecify one or more locations'
)
if (flags.all) {
@@ -61,6 +67,7 @@ const handlers: Handlers = {
checkAndConfigureBackends(backends)
},
backup(args, flags) {
if (!config) throw ConfigError
checkIfResticIsAvailable()
const locations: Locations = parseLocations(flags)
@@ -68,22 +75,29 @@ const handlers: Handlers = {
for (const to of Object.values(locations).map(location => location.to))
Array.isArray(to) ? to.forEach(t => backends.add(t)) : backends.add(to)
checkAndConfigureBackends(filterObjectByKey(config.backends, Array.from(backends)))
checkAndConfigureBackends(
filterObjectByKey(config.backends, Array.from(backends))
)
backupAll(locations)
console.log('\nFinished!'.underline + ' 🎉')
},
restore(args, flags) {
if (!config) throw ConfigError
checkIfResticIsAvailable()
const locations = parseLocations(flags)
for (const [name, location] of Object.entries(locations)) {
const w = new Writer(name.green + `\t\tRestoring... ⏳`)
const env = getEnvFromBackend(config.backends[Array.isArray(location.to) ? location.to[0] : location.to])
const env = getEnvFromBackend(
config.backends[
Array.isArray(location.to) ? location.to[0] : location.to
]
)
exec(
'restic',
['restore', 'latest', '--path', resolve(location.from), ...args],
{ env },
{ env }
)
w.done(name.green + '\t\tDone 🎉')
}
@@ -104,8 +118,7 @@ const handlers: Handlers = {
checkIfResticIsAvailable()
console.log('Restic is already installed')
return
} catch (e) {
}
} catch (e) {}
const w = new Writer('Checking latest version... ⏳')
checkIfCommandIsAvailable('bzip2')
@@ -116,16 +129,19 @@ const handlers: Handlers = {
})
const archMap: { [a: string]: string } = {
'x32': '386',
'x64': 'amd64',
x32: '386',
x64: 'amd64',
}
w.replaceLn('Downloading binary... 🌎')
const name = `${json.name.replace(' ', '_')}_${process.platform}_${archMap[process.arch]}.bz2`
const name = `${json.name.replace(' ', '_')}_${process.platform}_${
archMap[process.arch]
}.bz2`
const dl = json.assets.find((asset: any) => asset.name === name)
if (!dl) return console.log(
if (!dl)
return console.log(
'Cannot get the right binary.'.red,
'Please see https://bit.ly/2Y1Rzai',
'Please see https://bit.ly/2Y1Rzai'
)
const tmp = join(tmpdir(), name)
@@ -143,7 +159,9 @@ const handlers: Handlers = {
exec('chmod', ['+x', extracted])
exec('mv', [extracted, INSTALL_DIR + '/restic'])
w.done(`\nFinished! restic is installed under: ${INSTALL_DIR}`.underline + ' 🎉')
w.done(
`\nFinished! restic is installed under: ${INSTALL_DIR}`.underline + ' 🎉'
)
},
uninstall() {
for (const bin of ['restic', 'autorestic'])
@@ -159,20 +177,21 @@ const handlers: Handlers = {
const w = new Writer('Checking for latest restic version... ⏳')
exec('restic', ['self-update'])
w.replaceLn('Checking for latest autorestic version... ⏳')
const { data: json } = await axios({
method: 'get',
url: 'https://api.github.com/repos/cupcakearmy/autorestic/releases/latest',
url:
'https://api.github.com/repos/cupcakearmy/autorestic/releases/latest',
responseType: 'json',
})
if (json.tag_name != VERSION) {
const platformMap: { [key: string]: string } = {
'darwin': 'macos',
darwin: 'macos',
}
const name = `autorestic_${platformMap[process.platform] || process.platform}_${process.arch}`
const name = `autorestic_${platformMap[process.platform] ||
process.platform}_${process.arch}`
const dl = json.assets.find((asset: any) => asset.name === name)
const to = INSTALL_DIR + '/autorestic'
@@ -187,30 +206,36 @@ const handlers: Handlers = {
}
export const help = () => {
console.log('\nAutorestic'.blue + ` - ${VERSION} - Easy Restic CLI Utility`
+ '\n'
+ '\nOptions:'.yellow
+ `\n -c, --config Specify config file. Default: ${CONFIG_FILE}`
+ '\n'
+ '\nCommands:'.yellow
+ '\n check [-b, --backend] [-a, --all] Check backends'
+ '\n backup [-l, --location] [-a, --all] Backup all or specified locations'
+ '\n restore [-l, --location] [-- --target <out dir>] Check backends'
+ '\n'
+ '\n exec [-b, --backend] [-a, --all] <command> -- [native options] Execute native restic command'
+ '\n'
+ '\n install install restic'
+ '\n uninstall uninstall restic'
+ '\n update update restic'
+ '\n help Show help'
+ '\n'
+ '\nExamples: '.yellow + 'https://git.io/fjVbg'
+ '\n',
console.log(
'\nAutorestic'.blue +
` - ${VERSION} - Easy Restic CLI Utility` +
'\n' +
'\nOptions:'.yellow +
`\n -c, --config Specify config file. Default: .autorestic.yml` +
'\n' +
'\nCommands:'.yellow +
'\n check [-b, --backend] [-a, --all] Check backends' +
'\n backup [-l, --location] [-a, --all] Backup all or specified locations' +
'\n restore [-l, --location] [-- --target <out dir>] Restore all or specified locations' +
'\n' +
'\n exec [-b, --backend] [-a, --all] <command> -- [native options] Execute native restic command' +
'\n' +
'\n install install restic' +
'\n uninstall uninstall restic' +
'\n update update restic' +
'\n help Show help' +
'\n' +
'\nExamples: '.yellow +
'https://git.io/fjVbg' +
'\n'
)
}
export const error = () => {
help()
console.log(`Invalid Command:`.red.underline, `${process.argv.slice(2).join(' ')}`)
console.log(
`Invalid Command:`.red.underline,
`${process.argv.slice(2).join(' ')}`
)
}
export default handlers

View File

@@ -1,62 +1,69 @@
type BackendLocal = {
type: 'local',
key: string,
type: 'local'
key: string
path: string
}
type BackendSFTP = {
type: 'sftp',
key: string,
path: string,
password?: string,
type: 'sftp'
key: string
path: string
password?: string
}
type BackendREST = {
type: 'rest',
key: string,
path: string,
user?: string,
type: 'rest'
key: string
path: string
user?: string
password?: string
}
type BackendS3 = {
type: 's3',
key: string,
path: string,
aws_access_key_id: string,
aws_secret_access_key: string,
type: 's3'
key: string
path: string
aws_access_key_id: string
aws_secret_access_key: string
}
type BackendB2 = {
type: 'b2',
key: string,
path: string,
b2_account_id: string,
type: 'b2'
key: string
path: string
b2_account_id: string
b2_account_key: string
}
type BackendAzure = {
type: 'azure',
key: string,
path: string,
azure_account_name: string,
type: 'azure'
key: string
path: string
azure_account_name: string
azure_account_key: string
}
type BackendGS = {
type: 'gs',
key: string,
path: string,
google_project_id: string,
type: 'gs'
key: string
path: string
google_project_id: string
google_application_credentials: string
}
export type Backend = BackendAzure | BackendB2 | BackendGS | BackendLocal | BackendREST | BackendS3 | BackendSFTP
export type Backend =
| BackendAzure
| BackendB2
| BackendGS
| BackendLocal
| BackendREST
| BackendS3
| BackendSFTP
export type Backends = { [name: string]: Backend }
export type Location = {
from: string,
from: string
to: string | string[]
}

View File

@@ -3,8 +3,11 @@ import { spawnSync, SpawnSyncOptions } from 'child_process'
import { randomBytes } from 'crypto'
import { createWriteStream } from 'fs'
export const exec = (command: string, args: string[], { env, ...rest }: SpawnSyncOptions = {}) => {
export const exec = (
command: string,
args: string[],
{ env, ...rest }: SpawnSyncOptions = {}
) => {
const cmd = spawnSync(command, args, {
...rest,
env: {
@@ -19,10 +22,12 @@ export const exec = (command: string, args: string[], { env, ...rest }: SpawnSyn
return { out, err }
}
export const checkIfResticIsAvailable = () => checkIfCommandIsAvailable(
export const checkIfResticIsAvailable = () =>
checkIfCommandIsAvailable(
'restic',
'Restic is not installed'.red + ' https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases',
)
'Restic is not installed'.red +
' https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases'
)
export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => {
if (require('child_process').spawnSync(cmd).error)
@@ -31,22 +36,29 @@ export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => {
export const makeObjectKeysLowercase = (object: Object): any =>
Object.fromEntries(
Object.entries(object)
.map(([key, value]) => [key.toLowerCase(), value]),
Object.entries(object).map(([key, value]) => [key.toLowerCase(), value])
)
export function rand(length = 32): string {
return randomBytes(length / 2).toString('hex')
}
export const singleToArray = <T>(singleOrArray: T | T[]): T[] => Array.isArray(singleOrArray) ? singleOrArray : [singleOrArray]
export const singleToArray = <T>(singleOrArray: T | T[]): T[] =>
Array.isArray(singleOrArray) ? singleOrArray : [singleOrArray]
export const filterObject = <T>(obj: { [key: string]: T }, filter: (item: [string, T]) => boolean): { [key: string]: T } => Object.fromEntries(Object.entries(obj).filter(filter))
export const filterObject = <T>(
obj: { [key: string]: T },
filter: (item: [string, T]) => boolean
): { [key: string]: T } =>
Object.fromEntries(Object.entries(obj).filter(filter))
export const filterObjectByKey = <T>(obj: { [key: string]: T }, keys: string[]) => filterObject(obj, ([key]) => keys.includes(key))
export const filterObjectByKey = <T>(
obj: { [key: string]: T },
keys: string[]
) => filterObject(obj, ([key]) => keys.includes(key))
export const downloadFile = async (url: string, to: string) => new Promise<void>(async res => {
export const downloadFile = async (url: string, to: string) =>
new Promise<void>(async res => {
const { data: file } = await axios({
method: 'get',
url: url,
@@ -60,4 +72,6 @@ export const downloadFile = async (url: string, to: string) => new Promise<void>
stream.close()
res()
})
})
})
export const ConfigError = new Error('Config file not found')