Compare commits

..

9 Commits
0.8 ... 0.9

Author SHA1 Message Date
fd2fd91635 Update README.md 2019-12-05 00:31:41 +01:00
9c09ce1d79 Update README.md 2019-12-05 00:31:05 +01:00
c2f6f51789 Update README.md 2019-12-05 00:27:01 +01:00
cupcakearmy
f09cf90653 hooks for backups 2019-12-05 00:24:20 +01:00
cupcakearmy
d352aced37 version bump 2019-12-05 00:24:11 +01:00
cupcakearmy
563d4ffb96 remove duplicate code 2019-12-05 00:23:49 +01:00
cupcakearmy
1c6a061dd1 cleanup types 2019-12-05 00:23:15 +01:00
cupcakearmy
504ad639ab function to convert a variable to an array if its not already 2019-12-05 00:23:06 +01:00
f7a15c6d86 Update README.md 2019-12-05 00:22:01 +01:00
8 changed files with 126 additions and 78 deletions

114
README.md
View File

@@ -14,7 +14,17 @@ Autorestic is a wrapper around the amazing [restic](https://restic.net/). While
- Simple interface - Simple interface
- Fully encrypted - Fully encrypted
## Installation ###### 📒 Docs
- [Locations](#-locations)
- [Pruning & Deleting old files](#pruning-and-snapshot-policies)
- [Excluding files](#excluding-filesfolders)
- [Hooks](#before--after-hooks)
- [Backends](#-backends)
## 🛳 Installation
Linux & macOS. Windows is not supported.
``` ```
curl -s https://raw.githubusercontent.com/CupCakeArmy/autorestic/master/install.sh | bash curl -s https://raw.githubusercontent.com/CupCakeArmy/autorestic/master/install.sh | bash
@@ -60,13 +70,13 @@ If we would check only one location we could run the following: `autorestic chec
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! 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!
### Backup ### 📦 Backup
``` ```
autorestic backup -a autorestic backup -a
``` ```
### Restore ### 📼 Restore
``` ```
autorestic restore -a --to /path/where/to/restore autorestic restore -a --to /path/where/to/restore
@@ -81,6 +91,9 @@ 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` This will restore the location `home` to the `/path/where/to/restore` folder and taking the data from the backend `hdd`
### 📲 Updates
Autorestic can update itself! Super handy right? Simply run `autorestic update` and we will check for you if there are updates for restic and autorestic and install them if necessary.
## 🗂 Locations ## 🗂 Locations
@@ -95,43 +108,7 @@ locations:
- also-backup-to-this-backend - also-backup-to-this-backend
``` ```
## 💽 Backends #### Pruning and snapshot policies
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. Autorestic supports declaring snapshot policies for location to avoid keeping old snapshot around if you don't need them.
@@ -176,7 +153,7 @@ f8f8f976 2019-12-02 12:11:08 computer within 2w /etc
3 snapshots 3 snapshots
``` ```
### Excluding files/folders #### Excluding files/folders
If you want to exclude certain files or folders it done easily by specifiyng the right flags in the location you desire to filter. The flags are taken straight from the [restic cli exclude rules](https://restic.readthedocs.io/en/latest/040_backup.html#excluding-files). If you want to exclude certain files or folders it done easily by specifiyng the right flags in the location you desire to filter. The flags are taken straight from the [restic cli exclude rules](https://restic.readthedocs.io/en/latest/040_backup.html#excluding-files).
@@ -201,6 +178,61 @@ backends:
... ...
``` ```
#### Before / After hooks
Sometimes you might want to stop an app/db before backing up data and start the service again after the backup has completed. This is what the hooks are made for. Simply add them to your location config. You can have as many commands as you wish.
```yaml
locations:
my-location:
from: /data
to:
- local
- remote
hooks:
before:
- echo "Hello"
- echo "Human"
after:
- echo "kthxbye"
```
## 💽 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
```
## Contributors ## Contributors
This amazing people helped the project! This amazing people helped the project!

View File

@@ -3,7 +3,6 @@ import minimist from 'minimist'
import { init } from './config' import { init } from './config'
import handlers, { error, help } from './handlers' import handlers, { error, help } from './handlers'
import { Config } from './types'
@@ -26,7 +25,7 @@ export const { _: commands, ...flags } = minimist(process.argv.slice(2), {
string: ['l', 'b'], string: ['l', 'b'],
}) })
export const VERSION = '0.8' export const VERSION = '0.9'
export const INSTALL_DIR = '/usr/local/bin' export const INSTALL_DIR = '/usr/local/bin'
export const VERBOSE = flags.verbose export const VERBOSE = flags.verbose

View File

@@ -3,7 +3,7 @@ import { Writer } from 'clitastic'
import { config, VERBOSE } from './autorestic' import { config, VERBOSE } from './autorestic'
import { getEnvFromBackend } from './backend' import { getEnvFromBackend } from './backend'
import { Locations, Location } from './types' import { Locations, Location } from './types'
import { exec, ConfigError, pathRelativeToConfigFile, getFlagsFromLocation } from './utils' import { exec, ConfigError, pathRelativeToConfigFile, getFlagsFromLocation, makeArrayIfIsNot, execPlain } from './utils'
@@ -29,10 +29,22 @@ export const backupLocation = (name: string, location: Location) => {
const filler = new Array(name.length + 3).fill(' ').join('') const filler = new Array(name.length + 3).fill(' ').join('')
let first = true let first = true
for (const t of Array.isArray(location.to) ? location.to : [location.to]) { if (location.hooks && location.hooks.before)
for (const command of makeArrayIfIsNot(location.hooks.before)) {
const cmd = execPlain(command)
if (cmd) console.log(cmd.out, cmd.err)
}
for (const t of makeArrayIfIsNot(location.to)) {
backupSingle(first ? display : filler, t, location) backupSingle(first ? display : filler, t, location)
if (first) first = false if (first) first = false
} }
if (location.hooks && location.hooks.after)
for (const command of makeArrayIfIsNot(location.hooks.after)) {
const cmd = execPlain(command)
if (cmd) console.log(cmd.out, cmd.err)
}
} }
export const backupAll = (locations?: Locations) => { export const backupAll = (locations?: Locations) => {

View File

@@ -3,7 +3,7 @@ import { resolve } from 'path'
import yaml from 'js-yaml' import yaml from 'js-yaml'
import { flags } from './autorestic' import { flags } from './autorestic'
import { Backend, Config } from './types' import { Backend, Config } from './types'
import { makeObjectKeysLowercase, rand } from './utils' import { makeArrayIfIsNot, makeObjectKeysLowercase, rand } from './utils'
import { homedir } from 'os' import { homedir } from 'os'
@@ -48,8 +48,8 @@ export const normalizeAndCheckBackups = (config: Config) => {
`The backup "${name}" is missing some required attributes`, `The backup "${name}" is missing some required attributes`,
) )
if (Array.isArray(to)) for (const t of to) checkDestination(t, name) for (const t of makeArrayIfIsNot(to))
else checkDestination(to, name) checkDestination(t, name)
} }
} }

View File

@@ -3,7 +3,7 @@ import { Writer } from 'clitastic'
import { config, VERBOSE } from './autorestic' import { config, VERBOSE } from './autorestic'
import { getEnvFromBackend } from './backend' import { getEnvFromBackend } from './backend'
import { Locations, Location, Flags } from './types' import { Locations, Location, Flags } from './types'
import { exec, ConfigError, pathRelativeToConfigFile, getFlagsFromLocation } from './utils' import { exec, ConfigError, pathRelativeToConfigFile, getFlagsFromLocation, makeArrayIfIsNot } from './utils'
@@ -38,7 +38,7 @@ export const forgetLocation = (name: string, backup: Location, dryRun: boolean)
const filler = new Array(name.length + 3).fill(' ').join('') const filler = new Array(name.length + 3).fill(' ').join('')
let first = true let first = true
for (const t of Array.isArray(backup.to) ? backup.to : [backup.to]) { for (const t of makeArrayIfIsNot(backup.to)) {
const nameOrBlankSpaces: string = first ? display : filler const nameOrBlankSpaces: string = first ? display : filler
forgetSingle(nameOrBlankSpaces, t, backup, dryRun) forgetSingle(nameOrBlankSpaces, t, backup, dryRun)
if (first) first = false if (first) first = false

View File

@@ -8,15 +8,14 @@ import { config, INSTALL_DIR, VERSION } from './autorestic'
import { checkAndConfigureBackends, getBackendsFromLocations, getEnvFromBackend } from './backend' import { checkAndConfigureBackends, getBackendsFromLocations, getEnvFromBackend } from './backend'
import { backupAll } from './backup' import { backupAll } from './backup'
import { forgetAll } from './forget' import { forgetAll } from './forget'
import { Backend, Backends, Flags, Locations } from './types' import { Backends, Flags, Locations } from './types'
import { import {
checkIfCommandIsAvailable, checkIfCommandIsAvailable,
checkIfResticIsAvailable, checkIfResticIsAvailable,
downloadFile, downloadFile,
exec, exec,
filterObjectByKey, filterObjectByKey,
singleToArray, ConfigError, makeArrayIfIsNot,
ConfigError,
} from './utils' } from './utils'
@@ -35,7 +34,7 @@ const parseBackend = (flags: Flags): Backends => {
) )
if (flags.all) return config.backends if (flags.all) return config.backends
else { else {
const backends = singleToArray<string>(flags.backend) const backends = makeArrayIfIsNot<string>(flags.backend)
for (const backend of backends) for (const backend of backends)
if (!config.backends[backend]) if (!config.backends[backend])
throw new Error('Invalid backend: '.red + backend) throw new Error('Invalid backend: '.red + backend)
@@ -55,7 +54,7 @@ const parseLocations = (flags: Flags): Locations => {
if (flags.all) { if (flags.all) {
return config.locations return config.locations
} else { } else {
const locations = singleToArray<string>(flags.location) const locations = makeArrayIfIsNot<string>(flags.location)
for (const location of locations) for (const location of locations)
if (!config.locations[location]) if (!config.locations[location])
throw new Error('Invalid location: '.red + location) throw new Error('Invalid location: '.red + location)

View File

@@ -1,3 +1,7 @@
export type StringOrArray = string | string[]
// BACKENDS
type BackendLocal = { type BackendLocal = {
type: 'local' type: 'local'
key: string key: string
@@ -62,30 +66,26 @@ export type Backend =
export type Backends = { [name: string]: Backend } export type Backends = { [name: string]: Backend }
export type ForgetPolicy = { // LOCATIONS
last?: number,
hourly?: number,
daily?: number,
weekly?: number,
monthly?: number,
yearly?: number,
within?: string,
tags?: string[],
}
export type Location = { export type Location = {
from: string from: string
to: string | string[] to: StringOrArray
keep?: ForgetPolicy hooks?: {
before?: StringOrArray
after?: StringOrArray
}
options?: { options?: {
[key: string]: { [key: string]: {
[key: string]: string | string[] [key: string]: StringOrArray
} }
} }
} }
export type Locations = { [name: string]: Location } export type Locations = { [name: string]: Location }
// OTHER
export type Config = { export type Config = {
locations: Locations locations: Locations
backends: Backends backends: Backends

View File

@@ -2,7 +2,7 @@ import axios from 'axios'
import { spawnSync, SpawnSyncOptions } from 'child_process' import { spawnSync, SpawnSyncOptions } from 'child_process'
import { randomBytes } from 'crypto' import { randomBytes } from 'crypto'
import { createWriteStream } from 'fs' import { createWriteStream } from 'fs'
import { isAbsolute, resolve, dirname } from 'path' import { dirname, isAbsolute, resolve } from 'path'
import { CONFIG_FILE } from './config' import { CONFIG_FILE } from './config'
import { Location } from './types' import { Location } from './types'
@@ -27,6 +27,16 @@ export const exec = (
return { out, err } return { out, err }
} }
export const execPlain = (command: string, opt: SpawnSyncOptions = {}) => {
const split = command.split(' ')
if (split.length < 1) {
console.log(`The command ${command} is not valid`.red)
return
}
return exec(split[0], split.slice(1), opt)
}
export const checkIfResticIsAvailable = () => export const checkIfResticIsAvailable = () =>
checkIfCommandIsAvailable( checkIfCommandIsAvailable(
'restic', 'restic',
@@ -50,9 +60,6 @@ export function rand(length = 32): string {
} }
export const singleToArray = <T>(singleOrArray: T | T[]): T[] =>
Array.isArray(singleOrArray) ? singleOrArray : [singleOrArray]
export const filterObject = <T>( export const filterObject = <T>(
obj: { [key: string]: T }, obj: { [key: string]: T },
filter: (item: [string, T]) => boolean, filter: (item: [string, T]) => boolean,
@@ -98,12 +105,11 @@ export const getFlagsFromLocation = (location: Location, command?: string): stri
let flags: string[] = [] let flags: string[] = []
// Map the flags to an array for the exec function. // Map the flags to an array for the exec function.
for (let [flag, values] of Object.entries(all)) { for (let [flag, values] of Object.entries(all))
if (!Array.isArray(values)) for (const value of makeArrayIfIsNot(values))
values = [values]
for (const value of values)
flags = [...flags, `--${String(flag)}`, String(value)] flags = [...flags, `--${String(flag)}`, String(value)]
}
return flags return flags
} }
export const makeArrayIfIsNot = <T>(maybeArray: T | T[]): T[] => Array.isArray(maybeArray) ? maybeArray : [maybeArray]