mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2025-09-06 10:30:39 +00:00
Compare commits
89 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
bcabd467c9 | ||
|
005072b90f | ||
|
d13d4f7cf1 | ||
330e3254f7 | |||
38763ed919 | |||
|
886b6362cd | ||
|
9ece1d867d | ||
|
485ada6599 | ||
|
e80db74af4 | ||
|
2fd9e2dd22 | ||
0c654eacf1 | |||
|
8fdf5188ff | ||
|
22d93f0b9c | ||
|
f940f23338 | ||
|
678aa96c06 | ||
|
e51eacf13c | ||
12d2e010bb | |||
e25e65e052 | |||
4491cfd536 | |||
d0e82b47e1 | |||
|
90f9a998e8 | ||
|
b40adcae1f | ||
|
ad5afab355 | ||
|
5b0011330c | ||
fd2fd91635 | |||
9c09ce1d79 | |||
c2f6f51789 | |||
|
f09cf90653 | ||
|
d352aced37 | ||
|
563d4ffb96 | ||
|
1c6a061dd1 | ||
|
504ad639ab | ||
f7a15c6d86 | |||
|
2f0092befe | ||
|
1026e68b68 | ||
2389c59aa9 | |||
087aeaf578 | |||
3b7062f733 | |||
|
96b63c744b | ||
|
9669b70e20 | ||
|
bcb081234c | ||
|
336f44e9dc | ||
|
d0cda7f1d5 | ||
|
a8f4c23254 | ||
|
1c9f6d7d91 | ||
|
18c3f4a06f | ||
632062a23f | |||
3d1d7ba256 | |||
|
417c54db4d | ||
|
a9696bbc0c | ||
|
45f7506478 | ||
|
d7cdeafe60 | ||
|
cf09cdbb30 | ||
|
88059fe405 | ||
|
cdf18430b6 | ||
|
352754dad9 | ||
|
b68dc75053 | ||
|
6a055d3114 | ||
|
b5daff07eb | ||
b2d01d77d9 | |||
f41c042fce | |||
a81498ac42 | |||
1731ee30b3 | |||
1f4f1a1855 | |||
13cb764067 | |||
8058f37368 | |||
|
57ffa1e3fa | ||
|
671542cd30 | ||
|
322df9f0bd | ||
|
652158d1ed | ||
|
06ce8180fb | ||
81f513d77b | |||
e32521e6ea | |||
f5c5b39b30 | |||
e016c8defc | |||
a2e0a0c9cc | |||
|
f9b04ea342 | ||
770c9dd7d4 | |||
|
851bbe5776 | ||
|
8fb6bdb3c6 | ||
|
47f5d91e89 | ||
|
de27034b94 | ||
|
9dafe9d36a | ||
|
d47e7d0912 | ||
|
e47d6be854 | ||
|
993fe072e2 | ||
|
3d1e28e574 | ||
|
3c0ebdfb4a | ||
|
2653633c91 |
17
.gitignore
vendored
17
.gitignore
vendored
@@ -1,8 +1,19 @@
|
|||||||
node_modules/
|
# Editors
|
||||||
package-lock.json
|
|
||||||
.idea
|
.idea
|
||||||
|
|
||||||
config.yml
|
# Node
|
||||||
|
node_modules/
|
||||||
|
package-lock.json
|
||||||
|
yarn.lock
|
||||||
|
|
||||||
|
# Build & Runtime
|
||||||
bin
|
bin
|
||||||
lib
|
lib
|
||||||
data
|
data
|
||||||
|
restore
|
||||||
|
docker
|
||||||
|
Dockerfile
|
||||||
|
|
||||||
|
# Config
|
||||||
|
.autorestic.yml
|
||||||
|
.docker.yml
|
3
.prettierrc
Normal file
3
.prettierrc
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
semi: false
|
||||||
|
singleQuote: true
|
||||||
|
trailingComma: 'es5'
|
53
CHANGELOG.md
Normal file
53
CHANGELOG.md
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
## 0.13
|
||||||
|
|
||||||
|
- Restored files are now without the prefix path.
|
||||||
|
- Support for making backups of docker volumes and restoring them (not incremental).
|
||||||
|
- Show error to user during backup
|
||||||
|
|
||||||
|
## 0.12
|
||||||
|
|
||||||
|
- fix self update on linux (Fix #15)
|
||||||
|
|
||||||
|
## 0.11
|
||||||
|
|
||||||
|
- tilde in arguments (Fix #14)
|
||||||
|
|
||||||
|
## 0.10
|
||||||
|
|
||||||
|
- Show elapsed time (Fix #12)
|
||||||
|
- Remove some code duplication
|
||||||
|
- New info command to quickly show an overview of your config (Fix #11)
|
||||||
|
|
||||||
|
## 0.9
|
||||||
|
|
||||||
|
- Hooks
|
||||||
|
- Cleanup
|
||||||
|
|
||||||
|
## 0.8
|
||||||
|
|
||||||
|
- Support for native flags in the backup and forget commands.
|
||||||
|
- Forget cleanup
|
||||||
|
|
||||||
|
## 0.7
|
||||||
|
|
||||||
|
- Cleanup
|
||||||
|
- Support for excluding files
|
||||||
|
- Ability to prune keeping the last x snapshots according to restic policy rules
|
||||||
|
|
||||||
|
## 0.6
|
||||||
|
|
||||||
|
- support for absolute paths
|
||||||
|
|
||||||
|
## 0.5
|
||||||
|
|
||||||
|
- config optional if not required for current operation
|
||||||
|
|
||||||
|
## 0.4
|
||||||
|
|
||||||
|
- show version number
|
||||||
|
|
||||||
|
## 0.3
|
||||||
|
|
||||||
|
- test autoupdate function
|
339
README.md
339
README.md
@@ -1,9 +1,342 @@
|
|||||||
# autorestic
|
# 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 🙂
|
||||||
|
|
||||||
## Installation
|

|
||||||
|
|
||||||
|
## 🌈 Features
|
||||||
|
|
||||||
|
- Config files, no CLI
|
||||||
|
- Predictable
|
||||||
|
- Backup locations to multiple backends
|
||||||
|
- Snapshot policies and pruning
|
||||||
|
- Simple interface
|
||||||
|
- Fully encrypted
|
||||||
|
|
||||||
|
### 📒 Docs
|
||||||
|
|
||||||
|
* [Locations](#-locations)
|
||||||
|
* [Pruning & Deleting old files](#pruning-and-snapshot-policies)
|
||||||
|
* [Excluding files](#excluding-filesfolders)
|
||||||
|
* [Hooks](#before--after-hooks)
|
||||||
|
* [Backends](#-backends)
|
||||||
|
* [Commands](#-commands)
|
||||||
|
* [Examples](#-examples)
|
||||||
|
|
||||||
|
## 🛳 Installation
|
||||||
|
|
||||||
|
Linux & macOS. Windows is not supported.
|
||||||
|
|
||||||
```
|
```
|
||||||
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.
|
||||||
|
|
||||||
|
If we would check only one location we could run the following: `autorestic check -l home`. Otherwise simpply check all locations with `autorestic check -a`
|
||||||
|
|
||||||
|
##### 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!
|
||||||
|
|
||||||
|
### 📦 Backup
|
||||||
|
|
||||||
|
```
|
||||||
|
autorestic backup -a
|
||||||
|
```
|
||||||
|
|
||||||
|
### 📼 Restore
|
||||||
|
|
||||||
|
```
|
||||||
|
autorestic restore -l home --from hdd --to /path/where/to/restore
|
||||||
|
```
|
||||||
|
|
||||||
|
### 📲 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
|
||||||
|
|
||||||
|
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
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 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
|
||||||
|
options:
|
||||||
|
forget:
|
||||||
|
keep-last: 5 # always keep at least 5 snapshots
|
||||||
|
keep-hourly: 3 # keep 3 last hourly shapshots
|
||||||
|
keep-daily: 4 # keep 4 last daily shapshots
|
||||||
|
keep-weekly: 1 # keep 1 last weekly shapshots
|
||||||
|
keep-monthly: 12 # keep 12 last monthly shapshots
|
||||||
|
keep-yearly: 7 # keep 7 last yearly shapshots
|
||||||
|
keep-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
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 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).
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
locations:
|
||||||
|
my-location:
|
||||||
|
from: /data
|
||||||
|
to:
|
||||||
|
- local
|
||||||
|
- remote
|
||||||
|
options:
|
||||||
|
backup:
|
||||||
|
exclude:
|
||||||
|
- '*.nope'
|
||||||
|
- '*.abc'
|
||||||
|
exclude-file: .gitignore
|
||||||
|
|
||||||
|
backends:
|
||||||
|
local:
|
||||||
|
...
|
||||||
|
remote:
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 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
|
||||||
|
```
|
||||||
|
|
||||||
|
## 👉 Commands
|
||||||
|
|
||||||
|
* [info](#info)
|
||||||
|
* [check](#check)
|
||||||
|
* [backup](#backup)
|
||||||
|
* [forget](#forget)
|
||||||
|
* [restore](#restore)
|
||||||
|
* [exec](#exec)
|
||||||
|
* [intall](#install)
|
||||||
|
* [uninstall](#uninstall)
|
||||||
|
* [upgrade](#upgrade)
|
||||||
|
|
||||||
|
|
||||||
|
### Info
|
||||||
|
|
||||||
|
```
|
||||||
|
autorestic info
|
||||||
|
```
|
||||||
|
|
||||||
|
Shows all the information in the config file. Usefull for a quick overview of what location backups where.
|
||||||
|
|
||||||
|
Pro tip: if it gets a bit long you can read it more easily with `autorestic info | less` 😉
|
||||||
|
|
||||||
|
### Check
|
||||||
|
|
||||||
|
```
|
||||||
|
autorestic check [-b, --backend] [-a, --all]
|
||||||
|
```
|
||||||
|
|
||||||
|
Checks the backends and configures them if needed. Can be applied to all with the `-a` flag or by specifying one or more backends with the `-b` or `--backend` flag.
|
||||||
|
|
||||||
|
|
||||||
|
### Backup
|
||||||
|
|
||||||
|
```
|
||||||
|
autorestic backup [-l, --location] [-a, --all]
|
||||||
|
```
|
||||||
|
|
||||||
|
Performes a backup of all locations if the `-a` flag is passed. To only backup some locations pass one or more `-l` or `--location` flags.
|
||||||
|
|
||||||
|
|
||||||
|
### Restore
|
||||||
|
|
||||||
|
```
|
||||||
|
autorestic restore [-l, --location] [--from backend] [--to <out dir>]
|
||||||
|
```
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
Lets see a more realistic example (from the config above)
|
||||||
|
```
|
||||||
|
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`
|
||||||
|
|
||||||
|
### Forget
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
autorestic forget [-l, --location] [-a, --all] [--dry-run]
|
||||||
|
```
|
||||||
|
|
||||||
|
This will prune and remove old data form the backends according to the [keep policy you have specified for the location](#pruning-and-snapshot-policies)
|
||||||
|
|
||||||
|
The `--dry-run` flag will do a dry run showing what would have been deleted, but won't touch the actual data.
|
||||||
|
|
||||||
|
|
||||||
|
### Exec
|
||||||
|
|
||||||
|
```
|
||||||
|
autorestic exec [-b, --backend] [-a, --all] <command> -- [native options]
|
||||||
|
```
|
||||||
|
|
||||||
|
This is avery handy command which enables you to run any native restic command on desired backends. An example would be listing all the snapshots of all your backends:
|
||||||
|
|
||||||
|
```
|
||||||
|
autorestic exec -a -- snapshots
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Install
|
||||||
|
|
||||||
|
Installs both restic and autorestic
|
||||||
|
|
||||||
|
#### Uninstall
|
||||||
|
|
||||||
|
Uninstall both restic and autorestic
|
||||||
|
|
||||||
|
#### Upgrade
|
||||||
|
|
||||||
|
Upgrades both restic and autorestic automagically
|
||||||
|
|
||||||
|
## 🐣 Examples
|
||||||
|
|
||||||
|
### List all the snapshots for all the backends
|
||||||
|
|
||||||
|
```
|
||||||
|
autorestic -a exec snapshots
|
||||||
|
```
|
||||||
|
|
||||||
|
### Unlock a locked repository
|
||||||
|
|
||||||
|
⚠️ Only do this if you know what you are doing. E.g. if you accidentally cancelled a running operation
|
||||||
|
|
||||||
|
```
|
||||||
|
autorestic -b my-backend exec unlock
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contributors
|
||||||
|
|
||||||
|
This amazing people helped the project!
|
||||||
|
|
||||||
|
- @ChanceM [Docs]
|
||||||
|
- @EliotBerriot [Docs, Pruning, S3]
|
||||||
|
@@ -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
BIN
docs/Sketch.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.9 MiB |
@@ -1,4 +1,4 @@
|
|||||||
#!/bin/sh
|
#!/bin/bash
|
||||||
|
|
||||||
OUT_FILE=/usr/local/bin/autorestic
|
OUT_FILE=/usr/local/bin/autorestic
|
||||||
|
|
||||||
|
@@ -4,21 +4,22 @@
|
|||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"build:watch": "tsc -w",
|
"build:watch": "tsc -w",
|
||||||
"dev": "tsnd --no-notify --respawn ./src/autorestic.ts",
|
"dev": "tsnd --no-notify --respawn ./src/autorestic.ts",
|
||||||
"bin": "npm run build && pkg lib/autorestic.js --out-path bin"
|
"bin": "yarn run build && pkg lib/autorestic.js --targets latest-macos-x64,latest-linux-x64 --out-path bin"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/decompress": "^4.2.3",
|
|
||||||
"@types/js-yaml": "^3.12.1",
|
"@types/js-yaml": "^3.12.1",
|
||||||
"@types/minimist": "^1.2.0",
|
"@types/minimist": "^1.2.0",
|
||||||
|
"@types/node": "^12.11.7",
|
||||||
"pkg": "^4.4.0",
|
"pkg": "^4.4.0",
|
||||||
"ts-node-dev": "^1.0.0-pre.40",
|
"ts-node-dev": "^1.0.0-pre.40",
|
||||||
"typescript": "^3.5.1"
|
"typescript": "^3.7"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^0.19.0",
|
"axios": "^0.19.0",
|
||||||
"clitastic": "0.0.1",
|
"clitastic": "0.0.1",
|
||||||
"colors": "^1.3.3",
|
"colors": "^1.3.3",
|
||||||
"js-yaml": "^3.13.1",
|
"js-yaml": "^3.13.1",
|
||||||
"minimist": "^1.2.0"
|
"minimist": "^1.2.0",
|
||||||
|
"uhrwerk": "^1.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,11 +1,9 @@
|
|||||||
import 'colors'
|
import 'colors'
|
||||||
import minimist from 'minimist'
|
import minimist from 'minimist'
|
||||||
import { homedir } from 'os'
|
|
||||||
import { resolve } from 'path'
|
|
||||||
|
|
||||||
import { init } from './config'
|
import { init } from './config'
|
||||||
import handlers, { error, help } from './handlers'
|
import handlers, { error, help } from './handlers'
|
||||||
import { Config } from './types'
|
|
||||||
|
|
||||||
|
|
||||||
process.on('uncaughtException', err => {
|
process.on('uncaughtException', err => {
|
||||||
@@ -15,36 +13,34 @@ process.on('uncaughtException', err => {
|
|||||||
|
|
||||||
export const { _: commands, ...flags } = minimist(process.argv.slice(2), {
|
export const { _: commands, ...flags } = minimist(process.argv.slice(2), {
|
||||||
alias: {
|
alias: {
|
||||||
'c': 'config',
|
c: 'config',
|
||||||
'v': 'verbose',
|
v: 'version',
|
||||||
'h': 'help',
|
h: 'help',
|
||||||
'a': 'all',
|
a: 'all',
|
||||||
'l': 'location',
|
l: 'location',
|
||||||
'b': 'backend',
|
b: 'backend',
|
||||||
|
d: 'dry-run',
|
||||||
},
|
},
|
||||||
boolean: ['a'],
|
boolean: ['a', 'd'],
|
||||||
string: ['l', 'b'],
|
string: ['l', 'b'],
|
||||||
})
|
})
|
||||||
|
|
||||||
export const VERSION = '0.4'
|
export const VERSION = '0.13'
|
||||||
export const DEFAULT_CONFIG = '/.autorestic.yml'
|
|
||||||
export const INSTALL_DIR = '/usr/local/bin'
|
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 VERBOSE = flags.verbose
|
||||||
|
|
||||||
export const config: Config = init()
|
export const config = init()
|
||||||
|
|
||||||
function main() {
|
|
||||||
if (flags.version)
|
|
||||||
return console.log('version'.grey, VERSION)
|
|
||||||
|
|
||||||
if (commands.length < 1)
|
|
||||||
return help()
|
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
if (commands.length < 1) return help()
|
||||||
|
|
||||||
const command: string = commands[0]
|
const command: string = commands[0]
|
||||||
const args: string[] = commands.slice(1)
|
const args: string[] = commands.slice(1)
|
||||||
;(handlers[command] || error)(args, flags)
|
|
||||||
|
const fn = handlers[command] || error
|
||||||
|
await fn(args, flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
|
||||||
|
main().catch((e: Error) => console.error(e.message))
|
||||||
|
@@ -1,16 +1,17 @@
|
|||||||
import { Writer } from 'clitastic'
|
import { Writer } from 'clitastic'
|
||||||
|
|
||||||
import { config, VERBOSE } from './autorestic'
|
import { config, VERBOSE } from './autorestic'
|
||||||
import { Backend, Backends } from './types'
|
import { Backend, Backends, Locations } from './types'
|
||||||
import { exec } from './utils'
|
import { exec, ConfigError, pathRelativeToConfigFile } from './utils'
|
||||||
|
|
||||||
const ALREADY_EXISTS = /(?=.*exists)(?=.*already)(?=.*config).*/
|
|
||||||
|
|
||||||
|
|
||||||
|
const ALREADY_EXISTS = /(?=.*already)(?=.*config).*/
|
||||||
|
|
||||||
export const getPathFromBackend = (backend: Backend): string => {
|
export const getPathFromBackend = (backend: Backend): string => {
|
||||||
switch (backend.type) {
|
switch (backend.type) {
|
||||||
case 'local':
|
case 'local':
|
||||||
return backend.path
|
return pathRelativeToConfigFile(backend.path)
|
||||||
case 'b2':
|
case 'b2':
|
||||||
case 'azure':
|
case 'azure':
|
||||||
case 'gs':
|
case 'gs':
|
||||||
@@ -24,7 +25,6 @@ export const getPathFromBackend = (backend: Backend): string => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export const getEnvFromBackend = (backend: Backend) => {
|
export const getEnvFromBackend = (backend: Backend) => {
|
||||||
const { type, path, key, ...rest } = backend
|
const { type, path, key, ...rest } = backend
|
||||||
return {
|
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) => {
|
export const checkAndConfigureBackend = (name: string, backend: Backend) => {
|
||||||
const writer = new Writer(name.blue + ' : ' + 'Configuring... ⏳')
|
const writer = new Writer(name.blue + ' : ' + 'Configuring... ⏳')
|
||||||
@@ -49,8 +55,12 @@ export const checkAndConfigureBackend = (name: string, backend: Backend) => {
|
|||||||
writer.done(name.blue + ' : ' + 'Done ✓'.green)
|
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)
|
console.log('\nConfiguring Backends'.grey.underline)
|
||||||
for (const [name, backend] of Object.entries(backends))
|
for (const [name, backend] of Object.entries(backends))
|
||||||
checkAndConfigureBackend(name, backend)
|
checkAndConfigureBackend(name, backend)
|
||||||
|
114
src/backup.ts
114
src/backup.ts
@@ -1,37 +1,115 @@
|
|||||||
import { Writer } from 'clitastic'
|
import { Writer } from 'clitastic'
|
||||||
|
import { mkdirSync } from 'fs'
|
||||||
|
|
||||||
import { config, VERBOSE } from './autorestic'
|
import { config, VERBOSE } from './autorestic'
|
||||||
import { getEnvFromBackend } from './backend'
|
import { getEnvFromBackend } from './backend'
|
||||||
import { Locations, Location } from './types'
|
import { LocationFromPrefixes } from './config'
|
||||||
import { exec } from './utils'
|
import { Locations, Location, Backend } from './types'
|
||||||
|
import {
|
||||||
|
exec,
|
||||||
|
ConfigError,
|
||||||
|
pathRelativeToConfigFile,
|
||||||
|
getFlagsFromLocation,
|
||||||
|
makeArrayIfIsNot,
|
||||||
|
execPlain,
|
||||||
|
MeasureDuration,
|
||||||
|
fill,
|
||||||
|
decodeLocationFromPrefix,
|
||||||
|
checkIfDockerVolumeExistsOrFail,
|
||||||
|
getPathFromVolume,
|
||||||
|
} from './utils'
|
||||||
|
|
||||||
|
|
||||||
export const backupSingle = (name: string, from: string, to: string) => {
|
|
||||||
const writer = new Writer(name + to.blue + ' : ' + 'Backing up... ⏳')
|
|
||||||
const backend = config.backends[to]
|
|
||||||
const cmd = exec('restic', ['backup', from], { env: getEnvFromBackend(backend) })
|
|
||||||
|
|
||||||
if (VERBOSE) console.log(cmd.out, cmd.err)
|
export const backupFromFilesystem = (from: string, location: Location, backend: Backend, tags?: string[]) => {
|
||||||
writer.done(name + to.blue + ' : ' + 'Done ✓'.green)
|
const path = pathRelativeToConfigFile(from)
|
||||||
|
|
||||||
|
const { out, err, status } = exec(
|
||||||
|
'restic',
|
||||||
|
['backup', '.', ...getFlagsFromLocation(location, 'backup')],
|
||||||
|
{ env: getEnvFromBackend(backend), cwd: path },
|
||||||
|
)
|
||||||
|
|
||||||
|
if (VERBOSE) console.log(out, err)
|
||||||
|
if (status != 0 || err.length > 0)
|
||||||
|
throw new Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const backupFromVolume = (volume: string, location: Location, backend: Backend) => {
|
||||||
|
const tmp = getPathFromVolume(volume)
|
||||||
|
try {
|
||||||
|
mkdirSync(tmp)
|
||||||
|
checkIfDockerVolumeExistsOrFail(volume)
|
||||||
|
|
||||||
export const backupLocation = (name: string, backup: Location) => {
|
// For incremental backups. Unfortunately due to how the docker mounts work the permissions get lost.
|
||||||
|
// execPlain(`docker run --rm -v ${volume}:/data -v ${tmp}:/backup alpine cp -aT /data /backup`)
|
||||||
|
execPlain(`docker run --rm -v ${volume}:/data -v ${tmp}:/backup alpine tar cf /backup/archive.tar -C /data .`)
|
||||||
|
|
||||||
|
backupFromFilesystem(tmp, location, backend)
|
||||||
|
} catch (e) {
|
||||||
|
throw e
|
||||||
|
} finally {
|
||||||
|
execPlain(`rm -rf ${tmp}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const backupSingle = (name: string, to: string, location: Location) => {
|
||||||
|
if (!config) throw ConfigError
|
||||||
|
const delta = new MeasureDuration()
|
||||||
|
const writer = new Writer(name + to.blue + ' : ' + 'Backing up... ⏳')
|
||||||
|
|
||||||
|
try {
|
||||||
|
const backend = config.backends[to]
|
||||||
|
const [type, value] = decodeLocationFromPrefix(location.from)
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
|
||||||
|
case LocationFromPrefixes.Filesystem:
|
||||||
|
backupFromFilesystem(value, location, backend)
|
||||||
|
break
|
||||||
|
|
||||||
|
case LocationFromPrefixes.DockerVolume:
|
||||||
|
backupFromVolume(value, location, backend)
|
||||||
|
break
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.done(`${name}${to.blue} : ${'Done ✓'.green} (${delta.finished(true)})`)
|
||||||
|
} catch (e) {
|
||||||
|
writer.done(`${name}${to.blue} : ${'Failed!'.red} (${delta.finished(true)}) ${e.message}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const backupLocation = (name: string, location: Location) => {
|
||||||
const display = name.yellow + ' ▶ '
|
const display = name.yellow + ' ▶ '
|
||||||
if (Array.isArray(backup.to)) {
|
const filler = fill(name.length + 3)
|
||||||
let first = true
|
let first = true
|
||||||
for (const t of backup.to) {
|
|
||||||
const nameOrBlankSpaces: string = first ? display : new Array(name.length + 3).fill(' ').join('')
|
if (location.hooks && location.hooks.before)
|
||||||
backupSingle(nameOrBlankSpaces, backup.from, t)
|
for (const command of makeArrayIfIsNot(location.hooks.before)) {
|
||||||
|
const cmd = execPlain(command, {})
|
||||||
|
console.log(cmd.out, cmd.err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const t of makeArrayIfIsNot(location.to)) {
|
||||||
|
backupSingle(first ? display : filler, t, location)
|
||||||
if (first) first = false
|
if (first) first = false
|
||||||
}
|
}
|
||||||
} else
|
|
||||||
backupSingle(display, backup.from, backup.to)
|
if (location.hooks && location.hooks.after)
|
||||||
|
for (const command of makeArrayIfIsNot(location.hooks.after)) {
|
||||||
|
const cmd = execPlain(command)
|
||||||
|
console.log(cmd.out, cmd.err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
console.log('\nBacking Up'.underline.grey)
|
||||||
for (const [name, backup] of Object.entries(backups))
|
for (const [name, location] of Object.entries(locations))
|
||||||
backupLocation(name, backup)
|
backupLocation(name, location)
|
||||||
}
|
}
|
@@ -1,16 +1,31 @@
|
|||||||
import { readFileSync, writeFileSync } from 'fs'
|
import { readFileSync, writeFileSync, statSync } from 'fs'
|
||||||
|
import { resolve } from 'path'
|
||||||
|
import { homedir } from 'os'
|
||||||
|
|
||||||
import yaml from 'js-yaml'
|
import yaml from 'js-yaml'
|
||||||
import { CONFIG_FILE } 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'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export enum LocationFromPrefixes {
|
||||||
|
Filesystem,
|
||||||
|
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(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`)
|
)) {
|
||||||
|
if (!type || !path)
|
||||||
|
throw new Error(
|
||||||
|
`The backend "${name}" is missing some required attributes`,
|
||||||
|
)
|
||||||
|
|
||||||
const tmp: any = {
|
const tmp: any = {
|
||||||
type,
|
type,
|
||||||
@@ -24,7 +39,6 @@ export const normalizeAndCheckBackends = (config: Config) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export const normalizeAndCheckBackups = (config: Config) => {
|
export const normalizeAndCheckBackups = (config: Config) => {
|
||||||
config.locations = makeObjectKeysLowercase(config.locations)
|
config.locations = makeObjectKeysLowercase(config.locations)
|
||||||
const backends = Object.keys(config.backends)
|
const backends = Object.keys(config.backends)
|
||||||
@@ -34,20 +48,45 @@ export const normalizeAndCheckBackups = (config: Config) => {
|
|||||||
throw new Error(`Cannot find the backend "${backend}" for "${backup}"`)
|
throw new Error(`Cannot find the backend "${backend}" for "${backup}"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const [name, { from, to, ...rest }] of Object.entries(config.locations)) {
|
for (const [name, { from, to, ...rest }] of Object.entries(
|
||||||
if (!from || !to) throw new Error(`The backup "${name}" is missing some required attributes`)
|
config.locations,
|
||||||
|
)) {
|
||||||
|
if (!from || !to)
|
||||||
|
throw new Error(
|
||||||
|
`The backup "${name}" is missing some required attributes`,
|
||||||
|
)
|
||||||
|
|
||||||
if (Array.isArray(to))
|
for (const t of makeArrayIfIsNot(to))
|
||||||
for (const t of to)
|
|
||||||
checkDestination(t, name)
|
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 => {
|
export let CONFIG_FILE: string = ''
|
||||||
const raw: Config = makeObjectKeysLowercase(yaml.safeLoad(readFileSync(CONFIG_FILE).toString()))
|
|
||||||
|
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)
|
normalizeAndCheckBackends(raw)
|
||||||
normalizeAndCheckBackups(raw)
|
normalizeAndCheckBackups(raw)
|
||||||
|
78
src/forget.ts
Normal file
78
src/forget.ts
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
import { Writer } from 'clitastic'
|
||||||
|
|
||||||
|
import { config, VERBOSE } from './autorestic'
|
||||||
|
import { getEnvFromBackend } from './backend'
|
||||||
|
import { LocationFromPrefixes } from './config'
|
||||||
|
import { Locations, Location, Flags } from './types'
|
||||||
|
import {
|
||||||
|
exec,
|
||||||
|
ConfigError,
|
||||||
|
pathRelativeToConfigFile,
|
||||||
|
getFlagsFromLocation,
|
||||||
|
makeArrayIfIsNot,
|
||||||
|
fill, decodeLocationFromPrefix, getPathFromVolume,
|
||||||
|
} from './utils'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export const forgetSingle = (name: string, to: string, location: Location, dryRun: boolean) => {
|
||||||
|
if (!config) throw ConfigError
|
||||||
|
const base = name + to.blue + ' : '
|
||||||
|
const writer = new Writer(base + 'Removing old snapshots… ⏳')
|
||||||
|
|
||||||
|
const backend = config.backends[to]
|
||||||
|
const flags = getFlagsFromLocation(location, 'forget')
|
||||||
|
|
||||||
|
const [type, value] = decodeLocationFromPrefix(location.from)
|
||||||
|
let path: string
|
||||||
|
switch (type) {
|
||||||
|
case LocationFromPrefixes.Filesystem:
|
||||||
|
path = pathRelativeToConfigFile(value)
|
||||||
|
break
|
||||||
|
case LocationFromPrefixes.DockerVolume:
|
||||||
|
path = getPathFromVolume(value)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags.length == 0) {
|
||||||
|
writer.done(base + 'Skipping, no policy declared')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (dryRun) flags.push('--dry-run')
|
||||||
|
|
||||||
|
writer.replaceLn(base + 'Forgetting old snapshots… ⏳')
|
||||||
|
const cmd = exec(
|
||||||
|
'restic',
|
||||||
|
['forget', '--path', path, '--prune', ...flags],
|
||||||
|
{ env: getEnvFromBackend(backend) },
|
||||||
|
)
|
||||||
|
|
||||||
|
if (VERBOSE) console.log(cmd.out, cmd.err)
|
||||||
|
writer.done(base + 'Done ✓'.green)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const forgetLocation = (name: string, backup: Location, dryRun: boolean) => {
|
||||||
|
const display = name.yellow + ' ▶ '
|
||||||
|
const filler = fill(name.length + 3)
|
||||||
|
let first = true
|
||||||
|
|
||||||
|
for (const t of makeArrayIfIsNot(backup.to)) {
|
||||||
|
const nameOrBlankSpaces: string = first ? display : filler
|
||||||
|
forgetSingle(nameOrBlankSpaces, t, backup, dryRun)
|
||||||
|
if (first) first = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const forgetAll = (backups?: Locations, flags?: Flags) => {
|
||||||
|
if (!config) throw ConfigError
|
||||||
|
if (!backups) {
|
||||||
|
backups = config.locations
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('\nRemoving old snapshots 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))
|
||||||
|
forgetLocation(name, backup, dryRun)
|
||||||
|
}
|
164
src/handlers.ts
164
src/handlers.ts
@@ -1,12 +1,16 @@
|
|||||||
import axios from 'axios'
|
import { chmodSync, renameSync, unlinkSync } from 'fs'
|
||||||
import { Writer } from 'clitastic'
|
|
||||||
import { unlinkSync } from 'fs'
|
|
||||||
import { tmpdir } from 'os'
|
import { tmpdir } from 'os'
|
||||||
import { join, resolve } from 'path'
|
import { join, resolve } from 'path'
|
||||||
|
|
||||||
import { config, CONFIG_FILE, INSTALL_DIR, VERSION } from './autorestic'
|
import axios from 'axios'
|
||||||
import { checkAndConfigureBackends, getEnvFromBackend } from './backend'
|
import { Writer } from 'clitastic'
|
||||||
|
|
||||||
|
import { config, INSTALL_DIR, VERSION } from './autorestic'
|
||||||
|
import { checkAndConfigureBackends, getBackendsFromLocations, getEnvFromBackend } from './backend'
|
||||||
import { backupAll } from './backup'
|
import { backupAll } from './backup'
|
||||||
|
import { forgetAll } from './forget'
|
||||||
|
import showAll from './info'
|
||||||
|
import { restoreSingle } from './restore'
|
||||||
import { Backends, Flags, Locations } from './types'
|
import { Backends, Flags, Locations } from './types'
|
||||||
import {
|
import {
|
||||||
checkIfCommandIsAvailable,
|
checkIfCommandIsAvailable,
|
||||||
@@ -14,21 +18,26 @@ import {
|
|||||||
downloadFile,
|
downloadFile,
|
||||||
exec,
|
exec,
|
||||||
filterObjectByKey,
|
filterObjectByKey,
|
||||||
singleToArray,
|
ConfigError, makeArrayIfIsNot,
|
||||||
} from './utils'
|
} 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 => {
|
const parseBackend = (flags: Flags): Backends => {
|
||||||
|
if (!config) throw ConfigError
|
||||||
if (!flags.all && !flags.backend)
|
if (!flags.all && !flags.backend)
|
||||||
throw new Error('No backends specified.'.red
|
throw new Error(
|
||||||
+ '\n--all [-a]\t\t\t\tCheck all.'
|
'No backends specified.'.red +
|
||||||
+ '\n--backend [-b] myBackend\t\tSpecify one or more backend',
|
'\n--all [-a]\t\t\t\tCheck all.' +
|
||||||
|
'\n--backend [-b] myBackend\t\tSpecify one or more backend',
|
||||||
)
|
)
|
||||||
if (flags.all)
|
if (flags.all) return config.backends
|
||||||
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)
|
||||||
@@ -37,16 +46,18 @@ const parseBackend = (flags: Flags): Backends => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const parseLocations = (flags: Flags): Locations => {
|
const parseLocations = (flags: Flags): Locations => {
|
||||||
|
if (!config) throw ConfigError
|
||||||
if (!flags.all && !flags.location)
|
if (!flags.all && !flags.location)
|
||||||
throw new Error('No locations specified.'.red
|
throw new Error(
|
||||||
+ '\n--all [-a]\t\t\t\tBackup all.'
|
'No locations specified.'.red +
|
||||||
+ '\n--location [-l] site1\t\t\tSpecify one or more locations',
|
'\n--all [-a]\t\t\t\tBackup all.' +
|
||||||
|
'\n--location [-l] site1\t\t\tSpecify one or more 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)
|
||||||
@@ -61,32 +72,39 @@ const handlers: Handlers = {
|
|||||||
checkAndConfigureBackends(backends)
|
checkAndConfigureBackends(backends)
|
||||||
},
|
},
|
||||||
backup(args, flags) {
|
backup(args, flags) {
|
||||||
|
if (!config) throw ConfigError
|
||||||
checkIfResticIsAvailable()
|
checkIfResticIsAvailable()
|
||||||
const locations: Locations = parseLocations(flags)
|
const locations: Locations = parseLocations(flags)
|
||||||
|
|
||||||
const backends = new Set<string>()
|
checkAndConfigureBackends(
|
||||||
for (const to of Object.values(locations).map(location => location.to))
|
filterObjectByKey(config.backends, getBackendsFromLocations(locations)),
|
||||||
Array.isArray(to) ? to.forEach(t => backends.add(t)) : backends.add(to)
|
)
|
||||||
|
|
||||||
checkAndConfigureBackends(filterObjectByKey(config.backends, Array.from(backends)))
|
|
||||||
backupAll(locations)
|
backupAll(locations)
|
||||||
|
|
||||||
console.log('\nFinished!'.underline + ' 🎉')
|
console.log('\nFinished!'.underline + ' 🎉')
|
||||||
},
|
},
|
||||||
restore(args, flags) {
|
restore(args, flags) {
|
||||||
|
if (!config) throw ConfigError
|
||||||
checkIfResticIsAvailable()
|
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])
|
|
||||||
|
|
||||||
exec(
|
const locations = parseLocations(flags)
|
||||||
'restic',
|
const keys = Object.keys(locations)
|
||||||
['restore', 'latest', '--path', resolve(location.from), ...args],
|
if (keys.length < 1) throw new Error(`You need to specify the location to restore with --location`.red)
|
||||||
{ env },
|
if (keys.length > 2) throw new Error(`Only one location is supported at a time when restoring`.red)
|
||||||
|
|
||||||
|
restoreSingle(keys[0], flags.from, flags.to)
|
||||||
|
},
|
||||||
|
forget(args, flags) {
|
||||||
|
if (!config) throw ConfigError
|
||||||
|
checkIfResticIsAvailable()
|
||||||
|
const locations: Locations = parseLocations(flags)
|
||||||
|
|
||||||
|
checkAndConfigureBackends(
|
||||||
|
filterObjectByKey(config.backends, getBackendsFromLocations(locations)),
|
||||||
)
|
)
|
||||||
w.done(name.green + '\t\tDone 🎉')
|
forgetAll(locations, flags)
|
||||||
}
|
|
||||||
|
console.log('\nFinished!'.underline + ' 🎉')
|
||||||
},
|
},
|
||||||
exec(args, flags) {
|
exec(args, flags) {
|
||||||
checkIfResticIsAvailable()
|
checkIfResticIsAvailable()
|
||||||
@@ -99,12 +117,15 @@ const handlers: Handlers = {
|
|||||||
console.log(out, err)
|
console.log(out, err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
info() {
|
||||||
|
showAll()
|
||||||
|
},
|
||||||
async install() {
|
async install() {
|
||||||
try {
|
try {
|
||||||
checkIfResticIsAvailable()
|
checkIfResticIsAvailable()
|
||||||
console.log('Restic is already installed')
|
console.log('Restic is already installed')
|
||||||
return
|
return
|
||||||
} catch (e) {
|
} catch {
|
||||||
}
|
}
|
||||||
|
|
||||||
const w = new Writer('Checking latest version... ⏳')
|
const w = new Writer('Checking latest version... ⏳')
|
||||||
@@ -116,14 +137,15 @@ const handlers: Handlers = {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const archMap: { [a: string]: string } = {
|
const archMap: { [a: string]: string } = {
|
||||||
'x32': '386',
|
x32: '386',
|
||||||
'x64': 'amd64',
|
x64: 'amd64',
|
||||||
}
|
}
|
||||||
|
|
||||||
w.replaceLn('Downloading binary... 🌎')
|
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)
|
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,
|
'Cannot get the right binary.'.red,
|
||||||
'Please see https://bit.ly/2Y1Rzai',
|
'Please see https://bit.ly/2Y1Rzai',
|
||||||
)
|
)
|
||||||
@@ -133,17 +155,17 @@ const handlers: Handlers = {
|
|||||||
|
|
||||||
await downloadFile(dl.browser_download_url, tmp)
|
await downloadFile(dl.browser_download_url, tmp)
|
||||||
|
|
||||||
// TODO: Native bz2
|
|
||||||
// Decompress
|
|
||||||
w.replaceLn('Decompressing binary... 📦')
|
w.replaceLn('Decompressing binary... 📦')
|
||||||
exec('bzip2', ['-dk', tmp])
|
exec('bzip2', ['-dk', tmp])
|
||||||
unlinkSync(tmp)
|
unlinkSync(tmp)
|
||||||
|
|
||||||
w.replaceLn(`Moving to ${INSTALL_DIR} 🚙`)
|
w.replaceLn(`Moving to ${INSTALL_DIR} 🚙`)
|
||||||
exec('chmod', ['+x', extracted])
|
chmodSync(extracted, 0o755)
|
||||||
exec('mv', [extracted, INSTALL_DIR + '/restic'])
|
renameSync(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() {
|
uninstall() {
|
||||||
for (const bin of ['restic', 'autorestic'])
|
for (const bin of ['restic', 'autorestic'])
|
||||||
@@ -159,17 +181,17 @@ const handlers: Handlers = {
|
|||||||
const w = new Writer('Checking for latest restic version... ⏳')
|
const w = new Writer('Checking for latest restic version... ⏳')
|
||||||
exec('restic', ['self-update'])
|
exec('restic', ['self-update'])
|
||||||
|
|
||||||
|
|
||||||
w.replaceLn('Checking for latest autorestic version... ⏳')
|
w.replaceLn('Checking for latest autorestic version... ⏳')
|
||||||
const { data: json } = await axios({
|
const { data: json } = await axios({
|
||||||
method: 'get',
|
method: 'get',
|
||||||
url: 'https://api.github.com/repos/cupcakearmy/autorestic/releases/latest',
|
url:
|
||||||
|
'https://api.github.com/repos/cupcakearmy/autorestic/releases/latest',
|
||||||
responseType: 'json',
|
responseType: 'json',
|
||||||
})
|
})
|
||||||
|
|
||||||
if (json.tag_name != VERSION) {
|
if (json.tag_name != VERSION) {
|
||||||
const platformMap: { [key: string]: string } = {
|
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}`
|
||||||
@@ -179,38 +201,50 @@ const handlers: Handlers = {
|
|||||||
w.replaceLn('Downloading binary... 🌎')
|
w.replaceLn('Downloading binary... 🌎')
|
||||||
await downloadFile(dl.browser_download_url, to)
|
await downloadFile(dl.browser_download_url, to)
|
||||||
|
|
||||||
exec('chmod', ['+x', to])
|
chmodSync(to, 0o755)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.done('All up to date! 🚀')
|
w.done('All up to date! 🚀')
|
||||||
},
|
},
|
||||||
|
version() {
|
||||||
|
console.log('version'.grey, VERSION)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
export const help = () => {
|
export const help = () => {
|
||||||
console.log('\nAutorestic'.blue + ` - ${VERSION} - Easy Restic CLI Utility`
|
console.log(
|
||||||
+ '\n'
|
'\nAutorestic'.blue +
|
||||||
+ '\nOptions:'.yellow
|
` - ${VERSION} - Easy Restic CLI Utility` +
|
||||||
+ `\n -c, --config Specify config file. Default: ${CONFIG_FILE}`
|
'\n' +
|
||||||
+ '\n'
|
'\nOptions:'.yellow +
|
||||||
+ '\nCommands:'.yellow
|
`\n -c, --config Specify config file. Default: .autorestic.yml` +
|
||||||
+ '\n check [-b, --backend] [-a, --all] Check backends'
|
'\n' +
|
||||||
+ '\n backup [-l, --location] [-a, --all] Backup all or specified locations'
|
'\nCommands:'.yellow +
|
||||||
+ '\n restore [-l, --location] [-- --target <out dir>] Check backends'
|
'\n info Show all locations and backends' +
|
||||||
+ '\n'
|
'\n check [-b, --backend] [-a, --all] Check backends' +
|
||||||
+ '\n exec [-b, --backend] [-a, --all] <command> -- [native options] Execute native restic command'
|
'\n backup [-l, --location] [-a, --all] Backup all or specified locations' +
|
||||||
+ '\n'
|
'\n forget [-l, --location] [-a, --all] [--dry-run] Forget old snapshots according to declared policies' +
|
||||||
+ '\n install install restic'
|
'\n restore [-l, --location] [--from backend] [--to <out dir>] Restore all or specified locations' +
|
||||||
+ '\n uninstall uninstall restic'
|
'\n' +
|
||||||
+ '\n update update restic'
|
'\n exec [-b, --backend] [-a, --all] <command> -- [native options] Execute native restic command' +
|
||||||
+ '\n help Show help'
|
'\n' +
|
||||||
+ '\n'
|
'\n install install restic' +
|
||||||
+ '\nExamples: '.yellow + 'https://git.io/fjVbg'
|
'\n uninstall uninstall restic' +
|
||||||
+ '\n',
|
'\n update update restic' +
|
||||||
|
'\n help Show help' +
|
||||||
|
'\n' +
|
||||||
|
'\nExamples: '.yellow +
|
||||||
|
'https://git.io/fjVbg' +
|
||||||
|
'\n',
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const error = () => {
|
export const error = () => {
|
||||||
help()
|
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
|
export default handlers
|
28
src/info.ts
Normal file
28
src/info.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { config } from './autorestic'
|
||||||
|
import { ConfigError, fill, treeToString } from './utils'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const showAll = () => {
|
||||||
|
if (!config) throw ConfigError
|
||||||
|
|
||||||
|
console.log('\n\n' + fill(32, '_') + 'LOCATIONS:'.underline)
|
||||||
|
for (const [key, data] of Object.entries(config.locations)) {
|
||||||
|
console.log(`\n${key.blue.underline}:`)
|
||||||
|
console.log(treeToString(
|
||||||
|
data,
|
||||||
|
['to:', 'from:', 'hooks:', 'options:'],
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('\n\n' + fill(32, '_') + 'BACKENDS:'.underline)
|
||||||
|
for (const [key, data] of Object.entries(config.backends)) {
|
||||||
|
console.log(`\n${key.blue.underline}:`)
|
||||||
|
console.log(treeToString(
|
||||||
|
data,
|
||||||
|
['type:', 'path:', 'key:'],
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default showAll
|
81
src/restore.ts
Normal file
81
src/restore.ts
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import { Writer } from 'clitastic'
|
||||||
|
import { resolve } from 'path'
|
||||||
|
|
||||||
|
import { config } from './autorestic'
|
||||||
|
import { getEnvFromBackend } from './backend'
|
||||||
|
import { LocationFromPrefixes } from './config'
|
||||||
|
import { Backend } from './types'
|
||||||
|
import {
|
||||||
|
checkIfDockerVolumeExistsOrFail,
|
||||||
|
ConfigError,
|
||||||
|
decodeLocationFromPrefix,
|
||||||
|
exec,
|
||||||
|
execPlain,
|
||||||
|
getPathFromVolume,
|
||||||
|
} from './utils'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export const restoreToFilesystem = (from: string, to: string, backend: Backend) => {
|
||||||
|
exec(
|
||||||
|
'restic',
|
||||||
|
['restore', 'latest', '--path', resolve(from), '--target', to],
|
||||||
|
{ env: getEnvFromBackend(backend) },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const restoreToVolume = (volume: string, backend: Backend) => {
|
||||||
|
const tmp = getPathFromVolume(volume)
|
||||||
|
try {
|
||||||
|
restoreToFilesystem(tmp, tmp, backend)
|
||||||
|
try {
|
||||||
|
checkIfDockerVolumeExistsOrFail(volume)
|
||||||
|
} catch {
|
||||||
|
execPlain(`docker volume create ${volume}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// For incremental backups. Unfortunately due to how the docker mounts work the permissions get lost.
|
||||||
|
// execPlain(`docker run --rm -v ${volume}:/data -v ${tmp}:/backup alpine cp -aT /backup /data`)
|
||||||
|
execPlain(`docker run --rm -v ${volume}:/data -v ${tmp}:/backup alpine tar xf /backup/archive.tar -C /data`)
|
||||||
|
} finally {
|
||||||
|
execPlain(`rm -rf ${tmp}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const restoreSingle = (locationName: string, from: string, to?: string) => {
|
||||||
|
if (!config) throw ConfigError
|
||||||
|
|
||||||
|
const location = config.locations[locationName]
|
||||||
|
|
||||||
|
const baseText = locationName.green + '\t\t'
|
||||||
|
const w = new Writer(baseText + `Restoring...`)
|
||||||
|
|
||||||
|
let backendName: string = Array.isArray(location.to) ? location.to[0] : location.to
|
||||||
|
if (from) {
|
||||||
|
if (!location.to.includes(from)) {
|
||||||
|
w.done(baseText + `Backend ${from} is not a valid location for ${locationName}`.red)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
backendName = from
|
||||||
|
w.replaceLn(baseText + `Restoring from ${backendName.blue}...`)
|
||||||
|
} else if (Array.isArray(location.to) && location.to.length > 1) {
|
||||||
|
w.replaceLn(baseText + `Restoring from ${backendName.blue}...\tTo select a specific backend pass the ${'--from'.blue} flag`)
|
||||||
|
}
|
||||||
|
const backend = config.backends[backendName]
|
||||||
|
|
||||||
|
const [type, value] = decodeLocationFromPrefix(location.from)
|
||||||
|
switch (type) {
|
||||||
|
|
||||||
|
case LocationFromPrefixes.Filesystem:
|
||||||
|
if (!to) throw new Error(`You need to specify the restore path with --to`.red)
|
||||||
|
restoreToFilesystem(value, to, backend)
|
||||||
|
break
|
||||||
|
|
||||||
|
case LocationFromPrefixes.DockerVolume:
|
||||||
|
restoreToVolume(value, backend)
|
||||||
|
break
|
||||||
|
|
||||||
|
}
|
||||||
|
w.done(locationName.green + '\t\tDone 🎉')
|
||||||
|
}
|
||||||
|
|
84
src/types.ts
84
src/types.ts
@@ -1,67 +1,91 @@
|
|||||||
|
export type StringOrArray = string | string[]
|
||||||
|
|
||||||
|
// BACKENDS
|
||||||
|
|
||||||
type BackendLocal = {
|
type BackendLocal = {
|
||||||
type: 'local',
|
type: 'local'
|
||||||
key: string,
|
key: string
|
||||||
path: string
|
path: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type BackendSFTP = {
|
type BackendSFTP = {
|
||||||
type: 'sftp',
|
type: 'sftp'
|
||||||
key: string,
|
key: string
|
||||||
path: string,
|
path: string
|
||||||
password?: string,
|
password?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type BackendREST = {
|
type BackendREST = {
|
||||||
type: 'rest',
|
type: 'rest'
|
||||||
key: string,
|
key: string
|
||||||
path: string,
|
path: string
|
||||||
user?: string,
|
user?: string
|
||||||
password?: string
|
password?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type BackendS3 = {
|
type BackendS3 = {
|
||||||
type: 's3',
|
type: 's3'
|
||||||
key: string,
|
key: string
|
||||||
path: string,
|
path: string
|
||||||
aws_access_key_id: string,
|
aws_access_key_id: string
|
||||||
aws_secret_access_key: string,
|
aws_secret_access_key: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type BackendB2 = {
|
type BackendB2 = {
|
||||||
type: 'b2',
|
type: 'b2'
|
||||||
key: string,
|
key: string
|
||||||
path: string,
|
path: string
|
||||||
b2_account_id: string,
|
b2_account_id: string
|
||||||
b2_account_key: string
|
b2_account_key: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type BackendAzure = {
|
type BackendAzure = {
|
||||||
type: 'azure',
|
type: 'azure'
|
||||||
key: string,
|
key: string
|
||||||
path: string,
|
path: string
|
||||||
azure_account_name: string,
|
azure_account_name: string
|
||||||
azure_account_key: string
|
azure_account_key: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type BackendGS = {
|
type BackendGS = {
|
||||||
type: 'gs',
|
type: 'gs'
|
||||||
key: string,
|
key: string
|
||||||
path: string,
|
path: string
|
||||||
google_project_id: string,
|
google_project_id: string
|
||||||
google_application_credentials: 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 Backends = { [name: string]: Backend }
|
||||||
|
|
||||||
|
// LOCATIONS
|
||||||
|
|
||||||
export type Location = {
|
export type Location = {
|
||||||
from: string,
|
from: string
|
||||||
to: string | string[]
|
to: StringOrArray
|
||||||
|
hooks?: {
|
||||||
|
before?: StringOrArray
|
||||||
|
after?: StringOrArray
|
||||||
|
}
|
||||||
|
options?: {
|
||||||
|
[key: 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
|
||||||
|
173
src/utils.ts
173
src/utils.ts
@@ -1,11 +1,18 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { spawnSync, SpawnSyncOptions } from 'child_process'
|
import { spawnSync, SpawnSyncOptions } from 'child_process'
|
||||||
import { randomBytes } from 'crypto'
|
import { createHash, randomBytes } from 'crypto'
|
||||||
import { createWriteStream } from 'fs'
|
import { createWriteStream, renameSync, unlinkSync } from 'fs'
|
||||||
|
import { homedir, tmpdir } from 'os'
|
||||||
|
import { dirname, isAbsolute, join, resolve } from 'path'
|
||||||
|
import { Duration, Humanizer } from 'uhrwerk'
|
||||||
|
|
||||||
|
import { CONFIG_FILE, LocationFromPrefixes } 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 { stdout, stderr, status } = spawnSync(command, args, {
|
||||||
const cmd = spawnSync(command, args, {
|
|
||||||
...rest,
|
...rest,
|
||||||
env: {
|
env: {
|
||||||
...process.env,
|
...process.env,
|
||||||
@@ -13,26 +20,35 @@ export const exec = (command: string, args: string[], { env, ...rest }: SpawnSyn
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const out = cmd.stdout && cmd.stdout.toString().trim()
|
const out = stdout && stdout.toString().trim()
|
||||||
const err = cmd.stderr && cmd.stderr.toString().trim()
|
const err = stderr && stderr.toString().trim()
|
||||||
|
|
||||||
return { out, err }
|
return { out, err, status }
|
||||||
}
|
}
|
||||||
|
|
||||||
export const checkIfResticIsAvailable = () => checkIfCommandIsAvailable(
|
export const execPlain = (command: string, opt: SpawnSyncOptions = {}) => {
|
||||||
|
const split = command.split(' ')
|
||||||
|
if (split.length < 1) throw new Error(`The command ${command} is not valid`.red)
|
||||||
|
|
||||||
|
return exec(split[0], split.slice(1), opt)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const checkIfResticIsAvailable = () =>
|
||||||
|
checkIfCommandIsAvailable(
|
||||||
'restic',
|
'restic',
|
||||||
'Restic is not installed'.red + ' https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases',
|
'restic is not installed'.red +
|
||||||
|
'\nEither run ' + 'autorestic install'.green +
|
||||||
|
'\nOr go to https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases',
|
||||||
)
|
)
|
||||||
|
|
||||||
export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => {
|
export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => {
|
||||||
if (require('child_process').spawnSync(cmd).error)
|
if (spawnSync(cmd).error)
|
||||||
throw new Error(errorMsg ? errorMsg : `"${errorMsg}" is not installed`.red)
|
throw new Error(errorMsg ? errorMsg : `"${cmd}" is not installed`.red)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const makeObjectKeysLowercase = (object: Object): any =>
|
export const makeObjectKeysLowercase = (object: Object): any =>
|
||||||
Object.fromEntries(
|
Object.fromEntries(
|
||||||
Object.entries(object)
|
Object.entries(object).map(([key, value]) => [key.toLowerCase(), value]),
|
||||||
.map(([key, value]) => [key.toLowerCase(), value]),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -40,24 +56,145 @@ export function rand(length = 32): string {
|
|||||||
return randomBytes(length / 2).toString('hex')
|
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 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({
|
const { data: file } = await axios({
|
||||||
method: 'get',
|
method: 'get',
|
||||||
url: url,
|
url: url,
|
||||||
responseType: 'stream',
|
responseType: 'stream',
|
||||||
})
|
})
|
||||||
|
|
||||||
const stream = createWriteStream(to)
|
const tmp = join(tmpdir(), rand(64))
|
||||||
|
const stream = createWriteStream(tmp)
|
||||||
|
|
||||||
const writer = file.pipe(stream)
|
const writer = file.pipe(stream)
|
||||||
writer.on('close', () => {
|
writer.on('close', () => {
|
||||||
stream.close()
|
stream.close()
|
||||||
|
try {
|
||||||
|
// Delete file if already exists. Needed if the binary wants to replace itself.
|
||||||
|
// Unix does not allow to overwrite a file that is being executed, but you can remove it and save other one at its place
|
||||||
|
unlinkSync(to)
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
renameSync(tmp, to)
|
||||||
res()
|
res()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 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 resolveTildePath = (path: string): string | null =>
|
||||||
|
(path.length === 0 || path[0] !== '~')
|
||||||
|
? null
|
||||||
|
: join(homedir(), path.slice(1))
|
||||||
|
|
||||||
|
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))
|
||||||
|
for (const value of makeArrayIfIsNot(values)) {
|
||||||
|
const stringValue = String(value)
|
||||||
|
const resolvedTilde = resolveTildePath(stringValue)
|
||||||
|
flags = [...flags, `--${String(flag)}`, resolvedTilde === null ? stringValue : resolvedTilde]
|
||||||
|
}
|
||||||
|
|
||||||
|
return flags
|
||||||
|
}
|
||||||
|
|
||||||
|
export const makeArrayIfIsNot = <T>(maybeArray: T | T[]): T[] => Array.isArray(maybeArray) ? maybeArray : [maybeArray]
|
||||||
|
|
||||||
|
export const fill = (length: number, filler = ' '): string => new Array(length).fill(filler).join('')
|
||||||
|
|
||||||
|
export const capitalize = (string: string): string => string.charAt(0).toUpperCase() + string.slice(1)
|
||||||
|
|
||||||
|
export const treeToString = (obj: Object, highlight = [] as string[]): string => {
|
||||||
|
let cleaned = JSON.stringify(obj, null, 2)
|
||||||
|
.replace(/[{}"\[\],]/g, '')
|
||||||
|
.replace(/^ {2}/mg, '')
|
||||||
|
.replace(/\n\s*\n/g, '\n')
|
||||||
|
.trim()
|
||||||
|
|
||||||
|
for (const word of highlight)
|
||||||
|
cleaned = cleaned.replace(word, capitalize(word).green)
|
||||||
|
|
||||||
|
return cleaned
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export class MeasureDuration {
|
||||||
|
private static Humanizer: Humanizer = [
|
||||||
|
[d => d.hours() > 0, d => `${d.hours()}h ${d.minutes()}min`],
|
||||||
|
[d => d.minutes() > 0, d => `${d.minutes()}min ${d.seconds()}s`],
|
||||||
|
[d => d.seconds() > 0, d => `${d.seconds()}s`],
|
||||||
|
[() => true, d => `${d.milliseconds()}ms`],
|
||||||
|
]
|
||||||
|
|
||||||
|
private start = Date.now()
|
||||||
|
|
||||||
|
|
||||||
|
finished(human?: false): number
|
||||||
|
finished(human?: true): string
|
||||||
|
finished(human?: boolean): number | string {
|
||||||
|
const delta = Date.now() - this.start
|
||||||
|
|
||||||
|
return human
|
||||||
|
? new Duration(delta, 'ms').humanize(MeasureDuration.Humanizer)
|
||||||
|
: delta
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const decodeLocationFromPrefix = (from: string): [LocationFromPrefixes, string] => {
|
||||||
|
const firstDelimiter = from.indexOf(':')
|
||||||
|
if (firstDelimiter === -1) return [LocationFromPrefixes.Filesystem, from]
|
||||||
|
|
||||||
|
const type = from.substr(0, firstDelimiter)
|
||||||
|
const value = from.substr(firstDelimiter + 1)
|
||||||
|
|
||||||
|
switch (type.toLowerCase()) {
|
||||||
|
case 'volume':
|
||||||
|
return [LocationFromPrefixes.DockerVolume, value]
|
||||||
|
case 'path':
|
||||||
|
return [LocationFromPrefixes.Filesystem, value]
|
||||||
|
default:
|
||||||
|
throw new Error(`Could not decode the location from: ${from}`.red)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const hash = (plain: string): string => createHash('sha1').update(plain).digest().toString('hex')
|
||||||
|
|
||||||
|
export const getPathFromVolume = (volume: string) => pathRelativeToConfigFile(hash(volume))
|
||||||
|
|
||||||
|
export const checkIfDockerVolumeExistsOrFail = (volume: string) => {
|
||||||
|
const cmd = exec('docker', [
|
||||||
|
'volume', 'inspect', volume,
|
||||||
|
])
|
||||||
|
if (cmd.err.length > 0)
|
||||||
|
throw new Error('Volume not found')
|
||||||
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user