Compare commits

...

48 Commits
0.2 ... 0.7

Author SHA1 Message Date
cupcakearmy
a8f4c23254 version bump 2019-12-04 20:53:06 +01:00
cupcakearmy
1c9f6d7d91 Merge remote-tracking branch 'origin/master' 2019-12-04 20:50:41 +01:00
cupcakearmy
18c3f4a06f use a simpler restore flag 2019-12-04 20:50:32 +01:00
632062a23f Update README.md 2019-12-04 20:50:12 +01:00
3d1d7ba256 Update README.md 2019-12-04 20:45:58 +01:00
cupcakearmy
417c54db4d cleanup 2019-12-04 20:38:59 +01:00
cupcakearmy
a9696bbc0c parse the flags in the config file to an array for the exec command 2019-12-04 20:38:48 +01:00
cupcakearmy
45f7506478 added options to the location type 2019-12-04 20:38:27 +01:00
cupcakearmy
d7cdeafe60 moved around params 2019-12-04 20:38:14 +01:00
cupcakearmy
cf09cdbb30 cleanup and support for exclusion 2019-12-04 20:38:04 +01:00
cupcakearmy
88059fe405 method to get all the backends from a list of locations 2019-12-04 20:37:50 +01:00
cupcakearmy
cdf18430b6 remove old todo 2019-12-03 23:38:49 +01:00
cupcakearmy
352754dad9 formatting & trailing commas 2019-12-03 23:37:55 +01:00
cupcakearmy
b68dc75053 removed unused import 2019-12-03 23:31:20 +01:00
cupcakearmy
6a055d3114 moved path resolver into utils 2019-12-03 23:31:13 +01:00
cupcakearmy
b5daff07eb replace indead of adding 2019-12-03 23:30:53 +01:00
b2d01d77d9 Update README.md 2019-12-03 09:52:11 +01:00
f41c042fce Merge pull request #6 from EliotBerriot/2-forget
Fix #2: support pruning and forget via snapshot policies
2019-12-03 09:43:38 +01:00
a81498ac42 Merge branch 'master' into 2-forget 2019-12-03 09:43:28 +01:00
1731ee30b3 Update README.md 2019-12-03 09:39:51 +01:00
1f4f1a1855 Merge pull request #5 from EliotBerriot/1-s3
Fix #1: fixed broken initialization check under S3
2019-12-03 09:39:25 +01:00
13cb764067 Update README.md 2019-12-03 09:19:26 +01:00
8058f37368 Merge pull request #3 from ChanceM/patch-1
Update README.md spelling correction.
2019-12-03 09:16:02 +01:00
Eliot Berriot
57ffa1e3fa Fix #2: support pruning and forget via snapshot policies 2019-12-02 14:57:10 +01:00
Eliot Berriot
671542cd30 Fix #1: fixed broken initialization check under S3 2019-12-02 11:14:02 +01:00
Gregory Moore
322df9f0bd Update README.md spelling correction.
Habe to have.
2019-11-30 09:53:41 -08:00
cupcakearmy
652158d1ed use bash 2019-11-27 19:30:01 +01:00
cupcakearmy
06ce8180fb support for absolute paths 2019-10-26 21:50:48 +02:00
81f513d77b Update README.md 2019-10-26 21:49:25 +02:00
e32521e6ea Update README.md 2019-10-26 21:49:06 +02:00
f5c5b39b30 Update README.md 2019-10-26 21:48:32 +02:00
e016c8defc Update README.md 2019-10-26 21:40:15 +02:00
a2e0a0c9cc Update README.md 2019-10-26 21:34:02 +02:00
cupcakearmy
f9b04ea342 remove sample 2019-10-26 21:31:33 +02:00
770c9dd7d4 Update README.md 2019-10-26 21:30:47 +02:00
cupcakearmy
851bbe5776 sketch 2019-10-26 21:21:56 +02:00
cupcakearmy
8fb6bdb3c6 version bump 2019-10-26 21:03:22 +02:00
cupcakearmy
47f5d91e89 version as normal command 2019-10-26 21:03:08 +02:00
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
cupcakearmy
6a17444c4c show version number 2019-06-21 12:34:29 +02:00
cupcakearmy
9538881f9f test autoupdate function 2019-06-21 12:30:35 +02:00
15 changed files with 597 additions and 185 deletions

4
.gitignore vendored
View File

@@ -1,8 +1,10 @@
node_modules/
package-lock.json
.idea
yarn.lock
config.yml
bin
lib
data
.autorestic.yml

3
.prettierrc Normal file
View File

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

188
README.md
View File

@@ -1,9 +1,193 @@
# autorestic
High level CLI utility for restic
High backup level CLI utility for [restic](https://restic.net/).
Autorestic is a wrapper around the amazing [restic](https://restic.net/). While being amazing the restic cli can be a bit overwhelming and difficoult to manage if you have many different location that you want to backup to multiple locations. This utility is aimed at making this easier 🙂
![Sketch](./docs/Sketch.png)
## 🌈 Features
- Config files, no CLI
- Predictable
- Backup locations to multiple backends
- Snapshot policies and pruning
- Simple interface
- Fully encrypted
## Installation
```
curl -s https://raw.githubusercontent.com/CupCakeArmy/autorestic/master/install.sh | sh
curl -s https://raw.githubusercontent.com/CupCakeArmy/autorestic/master/install.sh | bash
```
## 🚀 Quickstart
### Setup
First we need to configure our locations and backends. Simply create a `.autorestic.yml` either in your home directory of in the folder from which you will execute `autorestic`.
Optionally you can specify the location of your config file by passing it as argument: `autorestic -c ../path/config.yml`
```yaml
locations:
home:
from: /home/me
to: remote
important:
from: /path/to/important/stuff
to:
- remote
- hdd
backends:
remote:
type: b2
path: 'myBucket:backup/home'
B2_ACCOUNT_ID: account_id
B2_ACCOUNT_KEY: account_key
hdd:
type: local
path: /mnt/my_external_storage
```
Then we check if everything is correct by running the `check` command. We will pass the `-a` (or `--all`) to tell autorestic to check all the locations.
Lets see a more realistic example (from the config above)
```
autorestic check -l important
```
If we would check only one location we could run the following: `autorestic -l home check`.
### Backup
```
autorestic backup -a
```
### Restore
```
autorestic restore -a --to /path/where/to/restore
```
This will restore all the locations to the selected target. If for one location there are more than one backends specified autorestic will take the first one.
```
autorestic restore -l home --from hdd --to /path/where/to/restore
```
This will restore the location `home` to the `/path/where/to/restore` folder and taking the data from the backend `hdd`
## 🗂 Locations
A location simply a folder on your machine that restic will backup. The paths can be relative from the config file. A location can have multiple backends, so that the data is secured across multiple servers.
```yaml
locations:
my-location-name:
from: path/to/backup
to:
- name-of-backend
- also-backup-to-this-backend
```
## 💽 Backends
Backends are the place where you data will be saved. Backups are incremental and encrypted.
### Fields
##### `type`
Type of the backend see a list [here](https://restic.readthedocs.io/en/stable/030_preparing_a_new_repo.html)
Supported are:
- [Local](https://restic.readthedocs.io/en/stable/030_preparing_a_new_repo.html#local)
- [Backblaze B2](https://restic.readthedocs.io/en/stable/030_preparing_a_new_repo.html#backblaze-b2)
- [Amazon S3](https://restic.readthedocs.io/en/stable/030_preparing_a_new_repo.html#amazon-s3)
- [Minio](https://restic.readthedocs.io/en/stable/030_preparing_a_new_repo.html#minio-server)
- [Google Cloud Storage](https://restic.readthedocs.io/en/stable/030_preparing_a_new_repo.html#google-cloud-storage)
- [Microsoft Azure Storage](https://restic.readthedocs.io/en/stable/030_preparing_a_new_repo.html#microsoft-azure-blob-storage)
For each backend you need to specify the right variables as shown in the example below.
##### `path`
The path on the remote server.
For object storages as
##### Example
```yaml
backends:
name-of-backend:
type: b2
path: 'myAccount:myBucket/my/path'
B2_ACCOUNT_ID: backblaze_account_id
B2_ACCOUNT_KEY: backblaze_account_key
```
## Pruning and snapshot policies
Autorestic supports declaring snapshot policies for location to avoid keeping old snapshot around if you don't need them.
This is based on [Restic's snapshots policies](https://restic.readthedocs.io/en/latest/060_forget.html#removing-snapshots-according-to-a-policy), and can be enabled for each location as shown below:
```yaml
locations:
etc:
from: /etc
to: local
keep:
# options matches the --keep-* options used in the restic forget CLI
# cf https://restic.readthedocs.io/en/latest/060_forget.html#removing-snapshots-according-to-a-policy
last: 5 # always keep at least 5 snapshots
hourly: 3 # keep 3 last hourly shapshots
daily: 4 # keep 4 last daily shapshots
weekly: 1 # keep 1 last weekly shapshots
monthly: 12 # keep 12 last monthly shapshots
yearly: 7 # keep 7 last yearly shapshots
within: "2w" # keep snapshots from the last 2 weeks
```
Pruning can be triggered using `autorestic forget -a`, for all locations, or selectively with `autorestic forget -l <location>`. **please note that contrary to the restic CLI, `restic forget` will call `restic prune` internally.**
Run with the `--dry-run` flag to only print information about the process without actually pruning the snapshots. This is especially useful for debugging or testing policies:
```
$ autorestic forget -a --dry-run --verbose
Configuring Backends
local : Done ✓
Removing old shapshots according to policy
etc ▶ local : Removing old spnapshots… ⏳
etc ▶ local : Running in dry-run mode, not touching data
etc ▶ local : Forgeting old snapshots… ⏳Applying Policy: all snapshots within 2d of the newest
keep 3 snapshots:
ID Time Host Tags Reasons Paths
-----------------------------------------------------------------------------
531b692a 2019-12-02 12:07:28 computer within 2w /etc
51659674 2019-12-02 12:08:46 computer within 2w /etc
f8f8f976 2019-12-02 12:11:08 computer within 2w /etc
-----------------------------------------------------------------------------
3 snapshots
```
##### Note
Note that the data is automatically encrypted on the server. The key will be generated and added to your config file. Every backend will have a separate key. You should keep a copy of the keys somewhere in case your server dies. Otherwise DATA IS LOST!
## Contributors
This amazing people helped the project!
- @ChanceM [Docs]
- @EliotBerriot [Docs, Pruning, S3]

View File

@@ -1,18 +0,0 @@
locations:
home:
from: /home/myUser
to: remote
important:
from: /path/to/important/stuff
to:
- remote
- hdd
backends:
remote:
type: b2
path: 'myBucket:backup/home'
B2_ACCOUNT_ID: account_id
B2_ACCOUNT_KEY: account_key
hdd:
type: local
path: /mnt/my_external_storage

BIN
docs/Sketch.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 MiB

View File

@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
OUT_FILE=/usr/local/bin/autorestic

View File

@@ -4,12 +4,12 @@
"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,12 @@
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,29 +14,32 @@ 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',
d: 'dry-run',
},
boolean: ['a'],
boolean: ['a', 'd'],
string: ['l', 'b'],
})
export const VERSION = '0.1'
export const DEFAULT_CONFIG = '/.autorestic.yml'
export const VERSION = '0.7'
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()
export const config = init()
function main() {
if (commands.length < 1) return help()
if (commands.length < 1)
help()
else {
const command: string = commands[0]
const args: string[] = commands.slice(1)
;(handlers[command] || error)(args, flags)
}
main()

View File

@@ -1,12 +1,13 @@
import { Writer } from 'clitastic'
import { config, VERBOSE } from './autorestic'
import { Backend, Backends } from './types'
import { exec } from './utils'
import { Backend, Backends, Locations } from './types'
import { exec, ConfigError } from './utils'
const ALREADY_EXISTS = /(?=.*exists)(?=.*already)(?=.*config).*/
const ALREADY_EXISTS = /(?=.*already)(?=.*config).*/
export const getPathFromBackend = (backend: Backend): string => {
switch (backend.type) {
case 'local':
@@ -24,7 +25,6 @@ export const getPathFromBackend = (backend: Backend): string => {
}
}
export const getEnvFromBackend = (backend: Backend) => {
const { type, path, key, ...rest } = backend
return {
@@ -34,6 +34,12 @@ export const getEnvFromBackend = (backend: Backend) => {
}
}
export const getBackendsFromLocations = (locations: Locations): string[] => {
const backends = new Set<string>()
for (const to of Object.values(locations).map(location => location.to))
Array.isArray(to) ? to.forEach(t => backends.add(t)) : backends.add(to)
return Array.from(backends)
}
export const checkAndConfigureBackend = (name: string, backend: Backend) => {
const writer = new Writer(name.blue + ' : ' + 'Configuring... ⏳')
@@ -49,8 +55,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,35 +3,46 @@ 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, pathRelativeToConfigFile, getFlagsFromLocation } from './utils'
export const backupSingle = (name: string, from: string, to: string) => {
export const backupSingle = (name: string, to: string, location: Location) => {
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 path = pathRelativeToConfigFile(location.from)
const cmd = exec(
'restic',
['backup', path, ...getFlagsFromLocation(location, 'backup')],
{ 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) => {
export const backupLocation = (name: string, location: Location) => {
const display = name.yellow + ' ▶ '
if (Array.isArray(backup.to)) {
const filler = new Array(name.length + 3).fill(' ').join('')
if (Array.isArray(location.to)) {
let first = true
for (const t of backup.to) {
const nameOrBlankSpaces: string = first ? display : new Array(name.length + 3).fill(' ').join('')
backupSingle(nameOrBlankSpaces, backup.from, t)
for (const t of location.to) {
backupSingle(first ? display : filler, t, location)
if (first) first = false
}
} else
backupSingle(display, backup.from, backup.to)
} else backupSingle(display, location.from, location)
}
export const backupAll = (locations?: Locations) => {
if (!locations) {
if (!config) throw ConfigError
locations = 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)
for (const [name, location] of Object.entries(locations))
backupLocation(name, location)
}

View File

@@ -1,16 +1,23 @@
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 +31,6 @@ export const normalizeAndCheckBackends = (config: Config) => {
}
}
export const normalizeAndCheckBackups = (config: Config) => {
config.locations = makeObjectKeysLowercase(config.locations)
const backends = Object.keys(config.backends)
@@ -34,20 +40,45 @@ 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 | undefined => {
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) {
}
}
}
export const init = (): Config => {
const raw: Config = makeObjectKeysLowercase(yaml.safeLoad(readFileSync(CONFIG_FILE).toString()))
export let CONFIG_FILE: string = ''
export const init = (): Config | undefined => {
const file = findConfigFile()
if (file) CONFIG_FILE = file
else return
const raw: Config = makeObjectKeysLowercase(
yaml.safeLoad(readFileSync(CONFIG_FILE).toString()),
)
normalizeAndCheckBackends(raw)
normalizeAndCheckBackups(raw)

62
src/forget.ts Normal file
View File

@@ -0,0 +1,62 @@
import { Writer } from 'clitastic'
import { config, VERBOSE } from './autorestic'
import { getEnvFromBackend } from './backend'
import { Locations, Location, ForgetPolicy, Flags } from './types'
import { exec, ConfigError } from './utils'
export const forgetSingle = (dryRun: boolean, name: string, from: string, to: string, policy: ForgetPolicy) => {
if (!config) throw ConfigError
const writer = new Writer(name + to.blue + ' : ' + 'Removing old spnapshots… ⏳')
const backend = config.backends[to]
const flags = [] as any[]
for (const [name, value] of Object.entries(policy)) {
flags.push(`--keep-${name}`)
flags.push(value)
}
if (dryRun) {
flags.push('--dry-run')
}
const env = getEnvFromBackend(backend)
writer.replaceLn(name + to.blue + ' : ' + 'Forgeting old snapshots… ⏳')
const cmd = exec('restic', ['forget', '--path', from, '--prune', ...flags], { env })
if (VERBOSE) console.log(cmd.out, cmd.err)
writer.done(name + to.blue + ' : ' + 'Done ✓'.green)
}
export const forgetLocation = (dryRun: boolean, name: string, backup: Location, policy?: ForgetPolicy) => {
const display = name.yellow + ' ▶ '
if (!policy) {
console.log(display + 'skipping, no policy declared')
} else {
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('')
forgetSingle(dryRun, nameOrBlankSpaces, backup.from, t, policy)
if (first) first = false
}
} else forgetSingle(dryRun, display, backup.from, backup.to, policy)
}
}
export const forgetAll = (backups?: Locations, flags?: Flags) => {
if (!config) throw ConfigError
if (!backups) {
backups = config.locations
}
console.log('\nRemoving old shapshots according to policy'.underline.grey)
const dryRun = flags ? flags['dry-run'] : false
if (dryRun) console.log('Running in dry-run mode, not touching data\n'.yellow)
for (const [name, backup] of Object.entries(backups)) {
const policy = config.locations[name].keep
forgetLocation(dryRun, name, backup, policy)
}
}

View File

@@ -4,10 +4,11 @@ import { unlinkSync } from 'fs'
import { tmpdir } from 'os'
import { join, resolve } from 'path'
import { config, CONFIG_FILE, INSTALL_DIR, VERSION } from './autorestic'
import { checkAndConfigureBackends, getEnvFromBackend } from './backend'
import { config, INSTALL_DIR, VERSION } from './autorestic'
import { checkAndConfigureBackends, getBackendsFromLocations, getEnvFromBackend } from './backend'
import { backupAll } from './backup'
import { Backends, Flags, Locations } from './types'
import { forgetAll } from './forget'
import { Backend, Backends, Flags, Locations } from './types'
import {
checkIfCommandIsAvailable,
checkIfResticIsAvailable,
@@ -15,18 +16,24 @@ 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 +44,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,33 +70,64 @@ const handlers: Handlers = {
checkAndConfigureBackends(backends)
},
backup(args, flags) {
if (!config) throw ConfigError
checkIfResticIsAvailable()
const locations: Locations = parseLocations(flags)
const backends = new Set<string>()
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, getBackendsFromLocations(locations)),
)
backupAll(locations)
console.log('\nFinished!'.underline + ' 🎉')
},
restore(args, flags) {
if (!config) throw ConfigError
checkIfResticIsAvailable()
if (!flags.to) {
console.log(`You need to specify the restore path with --to`.red)
return
}
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 baseText = name.green + '\t\t'
const w = new Writer(baseText + `Starting...`)
let backend: string = Array.isArray(location.to) ? location.to[0] : location.to
if (flags.from) {
if (!location.to.includes(flags.from)) {
w.done(baseText + `Backend ${flags.from} is not a valid location for ${name}`.red)
continue
}
backend = flags.from
w.replaceLn(baseText + `Restoring from ${backend.blue}...`)
} else if (Array.isArray(location.to) && location.to.length > 1) {
w.replaceLn(baseText + `Restoring from ${backend.blue}...\tTo select a specific backend pass the ${'--from'.blue} flag`)
}
const env = getEnvFromBackend(config.backends[backend])
exec(
'restic',
['restore', 'latest', '--path', resolve(location.from), ...args],
['restore', 'latest', '--path', resolve(location.from), '--target', flags.to],
{ env },
)
w.done(name.green + '\t\tDone 🎉')
}
},
forget(args, flags) {
if (!config) throw ConfigError
checkIfResticIsAvailable()
const locations: Locations = parseLocations(flags)
checkAndConfigureBackends(
filterObjectByKey(config.backends, getBackendsFromLocations(locations)),
)
forgetAll(locations, flags)
console.log('\nFinished!'.underline + ' 🎉')
},
exec(args, flags) {
checkIfResticIsAvailable()
const backends = parseBackend(flags)
@@ -116,25 +156,26 @@ 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(
'Cannot get the right binary.'.red,
'Please see https://bit.ly/2Y1Rzai',
)
if (!dl)
return console.log(
'Cannot get the right binary.'.red,
'Please see https://bit.ly/2Y1Rzai',
)
const tmp = join(tmpdir(), name)
const extracted = tmp.slice(0, -4) //without the .bz2
await downloadFile(dl.browser_download_url, tmp)
// TODO: Native bz2
// Decompress
w.replaceLn('Decompressing binary... 📦')
exec('bzip2', ['-dk', tmp])
unlinkSync(tmp)
@@ -143,7 +184,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 +202,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'
@@ -184,33 +228,44 @@ const handlers: Handlers = {
w.done('All up to date! 🚀')
},
version() {
console.log('version'.grey, VERSION)
},
}
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 forget [-l, --location] [-a, --all] [--dry-run] Forget old snapshots according to declared policies' +
'\n restore [-l, --location] [--from backend] [--to <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,63 +1,87 @@
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 ForgetPolicy = {
last?: number,
hourly?: number,
daily?: number,
weekly?: number,
monthly?: number,
yearly?: number,
within?: string,
tags?: string[],
}
export type Location = {
from: string,
from: string
to: string | string[]
keep?: ForgetPolicy
options?: {
[key: string]: {
[key: string]: string | string[]
}
}
}
export type Locations = { [name: string]: Location }

View File

@@ -2,9 +2,17 @@ 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'
import { Location } from './types'
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 +27,12 @@ export const exec = (command: string, args: string[], { env, ...rest }: SpawnSyn
return { out, err }
}
export const checkIfResticIsAvailable = () => checkIfCommandIsAvailable(
'restic',
'Restic is not installed'.red + ' https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases',
)
export const checkIfResticIsAvailable = () =>
checkIfCommandIsAvailable(
'restic',
'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,8 +41,7 @@ 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]),
)
@@ -40,24 +49,61 @@ 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 filterObject = <T>(obj: { [key: string]: T }, filter: (item: [string, T]) => boolean): { [key: string]: T } => Object.fromEntries(Object.entries(obj).filter(filter))
export const singleToArray = <T>(singleOrArray: T | T[]): T[] =>
Array.isArray(singleOrArray) ? singleOrArray : [singleOrArray]
export const filterObjectByKey = <T>(obj: { [key: string]: T }, keys: string[]) => filterObject(obj, ([key]) => keys.includes(key))
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 downloadFile = async (url: string, to: string) => new Promise<void>(async res => {
const { data: file } = await axios({
method: 'get',
url: url,
responseType: 'stream',
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 => {
const { data: file } = await axios({
method: 'get',
url: url,
responseType: 'stream',
})
const stream = createWriteStream(to)
const writer = file.pipe(stream)
writer.on('close', () => {
stream.close()
res()
})
})
const stream = createWriteStream(to)
// 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)
const writer = file.pipe(stream)
writer.on('close', () => {
stream.close()
res()
})
})
export const ConfigError = new Error('Config file not found')
export const getFlagsFromLocation = (location: Location, command?: string): string[] => {
if (!location.options) return []
const all = {
...location.options.global,
...(location.options[command || ''] || {}),
}
let flags: string[] = []
// Map the flags to an array for the exec function.
for (let [flag, values] of Object.entries(all)) {
if (!Array.isArray(values))
values = [values]
for (const value of values)
flags = [...flags, `--${flag}`, value]
}
return flags
}