Skip to content

Commit

Permalink
Merge pull request #118 from cupcakearmy/1.4.0
Browse files Browse the repository at this point in the history
1.4.0
  • Loading branch information
cupcakearmy authored Oct 30, 2021
2 parents ddc3acc + 439076d commit de663f2
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 83 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.4.0] - 2021-10-30

### Added

- Allow specify to specify a backend for location backup
- Global restic flags
- Generic ENV support for backends

### Changed

- Install now only requires `wget`
- Env variable for the `KEY` has been renamed from `AUTORESTIC_[BACKEND NAME]_KEY` -> `AUTORESTIC_[BACKEND NAME]_RESTIC_PASSWORD`

### Fixed

- Error handling during upgrade & uninstall

## [1.3.0] - 2021-10-26

### Added
Expand Down
10 changes: 8 additions & 2 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"strings"

"github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/colors"
Expand All @@ -22,8 +23,13 @@ var backupCmd = &cobra.Command{
CheckErr(err)
errors := 0
for _, name := range selected {
location, _ := internal.GetLocation(name)
errs := location.Backup(false)
var splitted = strings.Split(name, "@")
var specificBackend = ""
if len(splitted) > 1 {
specificBackend = splitted[1]
}
location, _ := internal.GetLocation(splitted[0])
errs := location.Backup(false, specificBackend)
for err := range errs {
colors.Error.Println(err)
errors++
Expand Down
6 changes: 3 additions & 3 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ var uninstallCmd = &cobra.Command{
Use: "uninstall",
Short: "Uninstall restic and autorestic",
Run: func(cmd *cobra.Command, args []string) {
noRestic, _ := cmd.Flags().GetBool("no-restic")
bins.Uninstall(!noRestic)
restic, _ := cmd.Flags().GetBool("restic")
bins.Uninstall(restic)
},
}

func init() {
rootCmd.AddCommand(uninstallCmd)
uninstallCmd.Flags().Bool("no-restic", false, "do not uninstall restic.")
uninstallCmd.Flags().Bool("restic", false, "also uninstall restic.")
}
6 changes: 3 additions & 3 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "Upgrade autorestic and restic",
Run: func(cmd *cobra.Command, args []string) {
noRestic, _ := cmd.Flags().GetBool("no-restic")
err := bins.Upgrade(!noRestic)
restic, _ := cmd.Flags().GetBool("restic")
err := bins.Upgrade(restic)
CheckErr(err)
},
}

func init() {
rootCmd.AddCommand(upgradeCmd)
upgradeCmd.Flags().Bool("no-restic", false, "also update restic")
upgradeCmd.Flags().Bool("restic", true, "also update restic")
}
2 changes: 2 additions & 0 deletions docs/markdown/backend/available.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ In theory [all the restic backends](https://restic.readthedocs.io/en/stable/030_

Those tested are the following:

> ℹ️ You can also [specify the `env` variables in a config file](/backend/env) to separate them from the config file.
## Local

```yaml
Expand Down
55 changes: 43 additions & 12 deletions docs/markdown/backend/env.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,67 @@
# Environment

> ⚠ Available since version `v1.3.0`
> ⚠ Available since version `v1.4.0`
Sometimes it's favorable not having the encryption keys in the config files.
For that `autorestic` allows passing the backend keys as `ENV` variables, or through an env file.
For that `autorestic` allows passing the env variables to backend password as `ENV` variables, or through an env file.
You can also pass whatever `env` variable to restic by prefixing it with `AUTORESTIC_[BACKEND NAME]_`.

The syntax for the `ENV` variables is as follows: `AUTORESTIC_[BACKEND NAME]_KEY`.
> ℹ️ Env variables and file overwrite the config file in the following order:
>
> Env Variables > Env File (`.autorestic.env`) > Config file (`.autorestic.yaml`)
## Env file

Alternatively `autorestic` can load an env file, located next to `.autorestic.yml` called `.autorestic.env`.

```
AUTORESTIC_FOO_RESTIC_PASSWORD=secret123
```

### Example with repository password

The syntax for the `ENV` variables is as follows: `AUTORESTIC_[BACKEND NAME]_RESTIC_PASSWORD`.

```yaml | autorestic.yaml
backend:
foo:
type: ...
path: ...
key: secret123 # => AUTORESTIC_FOO_KEY=secret123
key: secret123 # => AUTORESTIC_FOO_RESTIC_PASSWORD=secret123
```
## Example
This means we could remove `key: secret123` from `.autorestic.yaml` and execute as follows:

```bash
AUTORESTIC_FOO_KEY=secret123 autorestic backup ...
AUTORESTIC_FOO_RESTIC_PASSWORD=secret123 autorestic backup ...
```

## Env file
### Example with Backblaze B2

```yaml | autorestic.yaml
backends:
bb:
type: b2
path: myBucket
key: myPassword
env:
B2_ACCOUNT_ID: 123
B2_ACCOUNT_KEY: 456
```

Alternatively `autorestic` can load an env file, located next to `autorestic.yml` called `.autorestic.env`.
You could create an `.autorestic.env` or pass the following `ENV` variables to autorestic:

```| .autorestic.env
AUTORESTIC_FOO_KEY=secret123
```
AUTORESTIC_BB_RESTIC_PASSWORD=myPassword
AUTORESTIC_BB_B2_ACCOUNT_ID=123
AUTORESTIC_BB_B2_ACCOUNT_KEY=456
```

after that you can simply use `autorestic` as your are used to.
```yaml | autorestic.yaml
backends:
bb:
type: b2
path: myBucket
```

> :ToCPrevNext
8 changes: 1 addition & 7 deletions docs/markdown/backend/options.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Options

For the `backup` and `forget` commands you can pass any native flags to `restic`.

> It is also possible to set options for an [a specific location](/location/options).
> ℹ️ For more detail see the [location docs](/location/options) for options, as they are the same.
```yaml
backend:
Expand All @@ -18,8 +16,4 @@ backend:
In this example, whenever `autorestic` runs `restic backup` it will append a `--tag abc --tag` to the native command.

For more detail see the [location docs](/location/options) for options, as they are the same.

> For flags without arguments you can set them to `true`. They will be handled accordingly.

> :ToCPrevNext
8 changes: 8 additions & 0 deletions docs/markdown/cli/backup.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ autorestic backup -a
autorestic backup -l foo -l bar
```

## Specific location

`autorestic` also allows selecting specific backends for a location with the `location@backend` syntax.

```bash
autorestic backup -l location@backend
```

> :ToCPrevNext
4 changes: 2 additions & 2 deletions docs/markdown/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

Linux & macOS. Windows is not supported. If you have problems installing please open an issue :)

Autorestic requires `curl`, `wget` and `bzip2` to be installed. For most systems these should be already installed.
Autorestic requires `bash`, `wget` and `bzip2` to be installed. For most systems these should be already installed.

```bash
curl -s https://raw.githubusercontent.com/CupCakeArmy/autorestic/master/install.sh | bash
wget -qO - https://raw.githubusercontent.com/CupCakeArmy/autorestic/master/install.sh | bash
```

## Alternatives
Expand Down
46 changes: 42 additions & 4 deletions docs/markdown/location/options.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
# Options

For the `backup` and `forget` commands you can pass any native flags to `restic`.
For the `backup` and `forget` commands you can pass any native flags to `restic`. In addition you can specify flags for every command with `all`.

> It is also possible to set options for an [entire backend](/backend/options).
If flags don't start with `-` they will get prefixed with `--`.

Flags without arguments can be set to `true`. They will be handled accordingly.

> ℹ️ It is also possible to set options for an [entire backend](/backend/options) or globally (see below).
```yaml
locations:
foo:
# ...
options:
all:
some-flag: 123
# Equivalent to
--some-flag: 123
backup:
boolean-flag: true
tag:
- foo
- bar
```
## Example
In this example, whenever `autorestic` runs `restic backup` it will append a `--tag abc --tag` to the native command.

```yaml
locations:
Expand All @@ -16,8 +40,22 @@ locations:
- bar
```

In this example, whenever `autorestic` runs `restic backup` it will append a `--tag abc --tag` to the native command.
## Global Options

> For flags without arguments you can set them to `true`. They will be handled accordingly.
It is possible to specify global flags that will be run every time restic is invoked. To do so specify them under `global` in your config file.

```yaml
global:
all:
cache-dir: ~/restic
backup:
tag:
- foo
backends:
# ...
locations:
# ...
```

> :ToCPrevNext
2 changes: 1 addition & 1 deletion docs/markdown/quick.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Installation

```bash
curl -s https://raw.githubusercontent.com/CupCakeArmy/autorestic/master/install.sh | bash
wget -qO - https://raw.githubusercontent.com/CupCakeArmy/autorestic/master/install.sh | bash
```

See [installation](/installation) for alternative options.
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ else
fi
echo $ARCH

curl -s https://api.github.com/repos/cupcakearmy/autorestic/releases/latest \
wget -qO - https://api.github.com/repos/cupcakearmy/autorestic/releases/latest \
| grep "browser_download_url.*_${OS}_${ARCH}" \
| cut -d : -f 2,3 \
| tr -d \" \
Expand Down
35 changes: 16 additions & 19 deletions internal/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,27 @@ func (b Backend) generateRepo() (string, error) {

func (b Backend) getEnv() (map[string]string, error) {
env := make(map[string]string)
// Key
if b.Key != "" {
env["RESTIC_PASSWORD"] = b.Key
} else {
key, err := b.getKey()
if err != nil {
return nil, err
}
env["RESTIC_PASSWORD"] = key
}

// From config file
repo, err := b.generateRepo()
env["RESTIC_REPOSITORY"] = repo
for key, value := range b.Env {
env[strings.ToUpper(key)] = value
}

// From Envfile and passed as env
var prefix = "AUTORESTIC_" + strings.ToUpper(b.name) + "_"
for _, variable := range os.Environ() {
var splitted = strings.SplitN(variable, "=", 2)
if strings.HasPrefix(splitted[0], prefix) {
env[strings.TrimPrefix(splitted[0], prefix)] = splitted[1]
}
}

return env, err
}

Expand All @@ -85,17 +92,6 @@ func generateRandomKey() string {
return key
}

func (b Backend) getKey() (string, error) {
if b.Key != "" {
return b.Key, nil
}
keyName := "AUTORESTIC_" + strings.ToUpper(b.name) + "_KEY"
if key, found := os.LookupEnv(keyName); found {
return key, nil
}
return "", fmt.Errorf("no key found for backend \"%s\"", b.name)
}

func (b Backend) validate() error {
if b.Type == "" {
return fmt.Errorf(`Backend "%s" has no "type"`, b.name)
Expand All @@ -105,8 +101,9 @@ func (b Backend) validate() error {
}
if b.Key == "" {
// Check if key is set in environment
if _, err := b.getKey(); err != nil {
// If not generate a new one
env, _ := b.getEnv()
if _, found := env["RESTIC_PASSWORD"]; !found {
// No key set in config file or env => generate random key and save file
key := generateRandomKey()
b.Key = key
c := GetConfig()
Expand Down
Loading

1 comment on commit de663f2

@vercel
Copy link

@vercel vercel bot commented on de663f2 Oct 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.