mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2025-09-06 18:40:40 +00:00
Compare commits
32 Commits
v1.7.10
...
44efd64683
Author | SHA1 | Date | |
---|---|---|---|
|
44efd64683 | ||
|
5ed1af31e0 | ||
|
8108c52f50 | ||
|
eb4013b51d | ||
|
6424c64304 | ||
|
d39dafaef1 | ||
|
5a0f7e94f4 | ||
|
6a60d02759 | ||
1f7240c6a0 | |||
592d1093ac | |||
83481072bb | |||
|
776638e6fe | ||
|
bc74d3f13e | ||
|
dd01e97cf3 | ||
|
aabb0989c6 | ||
62a81d1420 | |||
e4b33cad1f | |||
|
a82273ec13 | ||
|
2418da5636 | ||
|
7508df7d66 | ||
6e34196220 | |||
|
dc56911a45 | ||
edb3ba35d8 | |||
|
12f6143bb4 | ||
|
a6bf1d1408 | ||
|
13aa560fda | ||
|
bbb1c85cad | ||
|
4cc44315ab | ||
|
b3440cd87c | ||
|
4848702929 | ||
b5604b8b9f | |||
|
24220f6b62 |
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@@ -3,7 +3,7 @@ name: Main
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
- "v*.*.*"
|
- 'v*.*.*'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
docker:
|
docker:
|
||||||
@@ -40,7 +40,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-go@v3
|
- uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: "^1.20"
|
go-version: '^1.21'
|
||||||
- name: Build
|
- name: Build
|
||||||
run: go run build/build.go
|
run: go run build/build.go
|
||||||
- name: Release
|
- name: Release
|
||||||
|
38
.github/workflows/ci.yml
vendored
Normal file
38
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
name: CI
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
push:
|
||||||
|
branches: [master]
|
||||||
|
|
||||||
|
env:
|
||||||
|
RESTIC_VERSION: "0.17.1"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Install restic@${{ env.RESTIC_VERSION }}
|
||||||
|
run: |
|
||||||
|
mkdir -p tools/restic
|
||||||
|
curl --fail --location --silent --show-error --output tools/restic/restic.bz2 \
|
||||||
|
"https://github.com/restic/restic/releases/download/v${RESTIC_VERSION}/restic_${RESTIC_VERSION}_linux_amd64.bz2"
|
||||||
|
bzip2 -d tools/restic/restic.bz2
|
||||||
|
chmod +x tools/restic/restic
|
||||||
|
echo "$GITHUB_WORKSPACE/tools/restic" >> "$GITHUB_PATH"
|
||||||
|
- run: restic version
|
||||||
|
|
||||||
|
- uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: '^1.21'
|
||||||
|
- run: go test -v ./...
|
||||||
|
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: '^1.21'
|
||||||
|
- run: go build -v .
|
@@ -1,4 +1,4 @@
|
|||||||
FROM golang:1.20-alpine as builder
|
FROM golang:1.23-alpine as builder
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY go.* .
|
COPY go.* .
|
||||||
@@ -6,8 +6,8 @@ RUN go mod download
|
|||||||
COPY . .
|
COPY . .
|
||||||
RUN go build
|
RUN go build
|
||||||
|
|
||||||
FROM restic/restic:0.16.0
|
FROM restic/restic:0.17.1
|
||||||
RUN apk add --no-cache rclone bash curl
|
RUN apk add --no-cache rclone bash curl docker-cli
|
||||||
COPY --from=builder /app/autorestic /usr/bin/autorestic
|
COPY --from=builder /app/autorestic /usr/bin/autorestic
|
||||||
ENTRYPOINT []
|
ENTRYPOINT []
|
||||||
CMD [ "autorestic" ]
|
CMD [ "autorestic" ]
|
||||||
|
@@ -34,7 +34,7 @@ Autorestic is a wrapper around the amazing [restic](https://restic.net/). While
|
|||||||
- Backup locations to multiple backends
|
- Backup locations to multiple backends
|
||||||
- Snapshot policies and pruning
|
- Snapshot policies and pruning
|
||||||
- Fully encrypted
|
- Fully encrypted
|
||||||
- Pre/After hooks
|
- Before/after backup hooks
|
||||||
- Exclude pattern/files
|
- Exclude pattern/files
|
||||||
- Cron jobs for automatic backup
|
- Cron jobs for automatic backup
|
||||||
- Backup & Restore docker volume
|
- Backup & Restore docker volume
|
||||||
|
73
cmd/backup_test.go
Normal file
73
cmd/backup_test.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func runCmd(t *testing.T, args ...string) error {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
viper.Reset()
|
||||||
|
rootCmd.SetArgs(args)
|
||||||
|
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBackupCmd(t *testing.T) {
|
||||||
|
workDir := t.TempDir()
|
||||||
|
|
||||||
|
// Prepare content to be backed up
|
||||||
|
locationDir := path.Join(workDir, "my-location")
|
||||||
|
err := os.Mkdir(locationDir, 0750)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
err = os.WriteFile(path.Join(locationDir, "back-me-up.txt"), []byte("hello world"), 0640)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
// Write config file
|
||||||
|
config, err := yaml.Marshal(map[string]interface{}{
|
||||||
|
"version": 2,
|
||||||
|
"locations": map[string]map[string]interface{}{
|
||||||
|
"my-location": {
|
||||||
|
"type": "local",
|
||||||
|
"from": []string{locationDir},
|
||||||
|
"to": []string{"test"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"backends": map[string]map[string]interface{}{
|
||||||
|
"test": {
|
||||||
|
"type": "local",
|
||||||
|
"path": path.Join(workDir, "test-backend"),
|
||||||
|
"key": "supersecret",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
assert.Nil(t, err)
|
||||||
|
configPath := path.Join(workDir, ".autorestic.yml")
|
||||||
|
err = os.WriteFile(configPath, config, 0640)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
// Init repo (not initialized by default)
|
||||||
|
err = runCmd(t, "exec", "--ci", "-a", "-c", configPath, "init")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
// Do the backup
|
||||||
|
err = runCmd(t, "backup", "--ci", "-a", "-c", configPath)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
// Restore in a separate dir
|
||||||
|
restoreDir := path.Join(workDir, "restore")
|
||||||
|
err = runCmd(t, "restore", "--ci", "-c", configPath, "-l", "my-location", "--to", restoreDir)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
// Check restored file
|
||||||
|
restoredContent, err := os.ReadFile(path.Join(restoreDir, locationDir, "back-me-up.txt"))
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, "hello world", string(restoredContent))
|
||||||
|
}
|
81
cmd/unlock.go
Normal file
81
cmd/unlock.go
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/cupcakearmy/autorestic/internal"
|
||||||
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
||||||
|
"github.com/cupcakearmy/autorestic/internal/lock"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var unlockCmd = &cobra.Command{
|
||||||
|
Use: "unlock",
|
||||||
|
Short: "Unlock autorestic only if you are sure that no other instance is running",
|
||||||
|
Long: `Unlock autorestic only if you are sure that no other instance is running.
|
||||||
|
To check you can run "ps aux | grep autorestic".`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
internal.GetConfig()
|
||||||
|
|
||||||
|
force, _ := cmd.Flags().GetBool("force")
|
||||||
|
|
||||||
|
if !force && isAutoresticRunning() {
|
||||||
|
colors.Error.Print("Another autorestic instance is running. Are you sure you want to unlock? (yes/no): ")
|
||||||
|
var response string
|
||||||
|
fmt.Scanln(&response)
|
||||||
|
if strings.ToLower(response) != "yes" {
|
||||||
|
colors.Primary.Println("Unlocking aborted.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err := lock.Unlock()
|
||||||
|
if err != nil {
|
||||||
|
colors.Error.Println("Could not unlock:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
colors.Success.Println("Unlock successful")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(unlockCmd)
|
||||||
|
unlockCmd.Flags().Bool("force", false, "force unlock")
|
||||||
|
}
|
||||||
|
|
||||||
|
// isAutoresticRunning checks if autorestic is running
|
||||||
|
// and returns true if it is.
|
||||||
|
// It also prints the processes to stdout.
|
||||||
|
func isAutoresticRunning() bool {
|
||||||
|
cmd := exec.Command("sh", "-c", "ps aux | grep autorestic")
|
||||||
|
var out bytes.Buffer
|
||||||
|
cmd.Stdout = &out
|
||||||
|
err := cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := strings.Split(out.String(), "\n")
|
||||||
|
autoresticProcesses := []string{}
|
||||||
|
currentPid := fmt.Sprint(os.Getpid())
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
if strings.Contains(line, "autorestic") && !strings.Contains(line, "grep autorestic") && !strings.Contains(line, currentPid) {
|
||||||
|
autoresticProcesses = append(autoresticProcesses, line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(autoresticProcesses) > 0 {
|
||||||
|
colors.Faint.Println("Found autorestic processes:")
|
||||||
|
for _, proc := range autoresticProcesses {
|
||||||
|
colors.Faint.Println(proc)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
@@ -1 +1 @@
|
|||||||
v20.8.0
|
v22.7.0
|
||||||
|
@@ -1,14 +1,15 @@
|
|||||||
{
|
{
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "NEXT_TELEMETRY_DISABLED=1 next build",
|
"build": "NEXT_TELEMETRY_DISABLED=1 next build",
|
||||||
"dev": "NEXT_TELEMETRY_DISABLED=1 next"
|
"dev": "NEXT_TELEMETRY_DISABLED=1 next",
|
||||||
|
"start": "NEXT_TELEMETRY_DISABLED=1 next start"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"next": "^13.5.3",
|
"next": "^14.2.7",
|
||||||
"nextra": "^2.13.1",
|
"nextra": "^2.13.4",
|
||||||
"nextra-theme-docs": "^2.13.1",
|
"nextra-theme-docs": "^2.13.4",
|
||||||
"react": "^18.2.0",
|
"react": "^18.3.1",
|
||||||
"react-dom": "^18.2.0"
|
"react-dom": "^18.3.1"
|
||||||
},
|
},
|
||||||
"packageManager": "pnpm@8.8.0"
|
"packageManager": "pnpm@9.9.0"
|
||||||
}
|
}
|
||||||
|
@@ -16,3 +16,41 @@ backends:
|
|||||||
## Types
|
## Types
|
||||||
|
|
||||||
We restic supports multiple types of backends. See the [full list](/backend/available) for details.
|
We restic supports multiple types of backends. See the [full list](/backend/available) for details.
|
||||||
|
|
||||||
|
## Avoid Generating Keys
|
||||||
|
|
||||||
|
By default, `autorestic` will generate a key for every backend if none is defined. This is done by updating your config file with the key.
|
||||||
|
|
||||||
|
In cases where you want to provide the key yourself, you can ensure that `autorestic` doesn't accidentally generate one for you by setting `requireKey: true`.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```yaml | .autorestic.yml
|
||||||
|
version: 2
|
||||||
|
|
||||||
|
backends:
|
||||||
|
foo:
|
||||||
|
type: local
|
||||||
|
path: /data/my/backups
|
||||||
|
# Alternatively, you can set the key through the `AUTORESTIC_FOO_RESTIC_PASSWORD` environment variable.
|
||||||
|
key: ... your key here ...
|
||||||
|
requireKey: true
|
||||||
|
```
|
||||||
|
|
||||||
|
With this setting, if a key is missing, `autorestic` will crash instead of generating a new key and updating your config file.
|
||||||
|
|
||||||
|
## Automatic Backend Initialization
|
||||||
|
|
||||||
|
`autorestic` is able to automatically initialize backends for you. This is done by setting `init: true` in the config for a given backend. For example:
|
||||||
|
|
||||||
|
```yaml | .autorestic.yml
|
||||||
|
backend:
|
||||||
|
foo:
|
||||||
|
type: ...
|
||||||
|
path: ...
|
||||||
|
init: true
|
||||||
|
```
|
||||||
|
|
||||||
|
When you set `init: true` on a backend config, `autorestic` will automatically initialize the underlying `restic` repository that powers the backend if it's not already initialized. In practice, this means that the backend will be initialized the first time it is being backed up to.
|
||||||
|
|
||||||
|
This option is helpful in cases where you want to automate the configuration of `autorestic`. This means that instead of running `autorestic exec init -b ...` manually when you create a new backend, you can let `autorestic` initialize it for you.
|
||||||
|
@@ -4,7 +4,7 @@
|
|||||||
autorestic forget [-l, --location] [-a, --all] [--dry-run] [--prune]
|
autorestic forget [-l, --location] [-a, --all] [--dry-run] [--prune]
|
||||||
```
|
```
|
||||||
|
|
||||||
This will prune and remove old data form the backends according to the [keep policy you have specified for the location](/location/forget).
|
This will prune and remove old data form the backends according to the [keep policy you have specified for the location](/location/options/forget).
|
||||||
|
|
||||||
The `--dry-run` flag will do a dry run showing what would have been deleted, but won't touch the actual data.
|
The `--dry-run` flag will do a dry run showing what would have been deleted, but won't touch the actual data.
|
||||||
|
|
||||||
|
32
docs/pages/cli/unlock.md
Normal file
32
docs/pages/cli/unlock.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Unlock
|
||||||
|
|
||||||
|
In case autorestic throws the error message `an instance is already running. exiting`, but there is no instance running you can unlock the lock.
|
||||||
|
|
||||||
|
To verify that there is no instance running you can use `ps aux | grep autorestic`.
|
||||||
|
|
||||||
|
Example with no instance running:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
> ps aux | grep autorestic
|
||||||
|
root 39260 0.0 0.0 6976 2696 pts/11 S+ 19:41 0:00 grep autorestic
|
||||||
|
```
|
||||||
|
|
||||||
|
Example with an instance running:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
> ps aux | grep autorestic
|
||||||
|
root 29465 0.0 0.0 1162068 7380 pts/7 Sl+ 19:28 0:00 autorestic --ci backup -a
|
||||||
|
root 39260 0.0 0.0 6976 2696 pts/11 S+ 19:41 0:00 grep autorestic
|
||||||
|
```
|
||||||
|
|
||||||
|
**If an instance is running you should not unlock as it could lead to data loss!**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
autorestic unlock
|
||||||
|
```
|
||||||
|
|
||||||
|
Use the `--force` to prevent the confirmation prompt if an instance is running.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
autorestic unlock --force
|
||||||
|
```
|
@@ -1,5 +1,7 @@
|
|||||||
# 🏘 Community
|
# 🏘 Community
|
||||||
|
|
||||||
|
## Software
|
||||||
|
|
||||||
A list of community driven projects. (No official affiliation)
|
A list of community driven projects. (No official affiliation)
|
||||||
|
|
||||||
- SystemD Units: <https://gitlab.com/py_crash/autorestic-systemd-units>
|
- SystemD Units: <https://gitlab.com/py_crash/autorestic-systemd-units>
|
||||||
@@ -9,3 +11,7 @@ A list of community driven projects. (No official affiliation)
|
|||||||
- Ansible Role: <https://github.com/FuzzyMistborn/ansible-role-autorestic>
|
- Ansible Role: <https://github.com/FuzzyMistborn/ansible-role-autorestic>
|
||||||
- Ansible Role: <https://0xacab.org/varac-projects/ansible-role-autorestic>
|
- Ansible Role: <https://0xacab.org/varac-projects/ansible-role-autorestic>
|
||||||
- Ansible Role: <https://github.com/dbrennand/ansible-role-autorestic>
|
- Ansible Role: <https://github.com/dbrennand/ansible-role-autorestic>
|
||||||
|
|
||||||
|
## Writing
|
||||||
|
|
||||||
|
- [restic: excellent resource for local and cloud backup](https://notes.nicfab.eu/en/posts/restic/)
|
||||||
|
@@ -56,6 +56,8 @@ version: 2
|
|||||||
|
|
||||||
extras:
|
extras:
|
||||||
hooks: &foo
|
hooks: &foo
|
||||||
|
prevalidate:
|
||||||
|
- echo "Wake up!"
|
||||||
before:
|
before:
|
||||||
- echo "Hello"
|
- echo "Hello"
|
||||||
after:
|
after:
|
||||||
|
@@ -13,7 +13,7 @@ Autorestic is a wrapper around the amazing [restic](https://restic.net/). While
|
|||||||
- Backup locations to multiple backends
|
- Backup locations to multiple backends
|
||||||
- Snapshot policies and pruning
|
- Snapshot policies and pruning
|
||||||
- Fully encrypted
|
- Fully encrypted
|
||||||
- Pre/After hooks
|
- Before/after backup hooks
|
||||||
- Exclude pattern/files
|
- Exclude pattern/files
|
||||||
- Cron jobs for automatic backup
|
- Cron jobs for automatic backup
|
||||||
- Backup & Restore docker volumes
|
- Backup & Restore docker volumes
|
||||||
|
@@ -18,7 +18,7 @@ services:
|
|||||||
|
|
||||||
```yaml | .autorestic.yml
|
```yaml | .autorestic.yml
|
||||||
locations:
|
locations:
|
||||||
foo:
|
hello:
|
||||||
from: my-data
|
from: my-data
|
||||||
type: volume
|
type: volume
|
||||||
# ...
|
# ...
|
||||||
|
@@ -6,23 +6,28 @@ They consist of a list of commands that will be executed in the same directory a
|
|||||||
|
|
||||||
The following hooks groups are supported, none are required:
|
The following hooks groups are supported, none are required:
|
||||||
|
|
||||||
|
- `prevalidate`
|
||||||
- `before`
|
- `before`
|
||||||
- `after`
|
- `after`
|
||||||
- `failure`
|
- `failure`
|
||||||
- `success`
|
- `success`
|
||||||
|
|
||||||
|
The difference between `prevalidate` and `before` hooks are that `prevalidate` is run before checking the backup location is valid, including checking that the `from` directories exist. This can be useful, for example, to mount the source filesystem that contains the directories listed in `from`.
|
||||||
|
|
||||||
```yml | .autorestic.yml
|
```yml | .autorestic.yml
|
||||||
locations:
|
locations:
|
||||||
my-location:
|
my-location:
|
||||||
from: /data
|
from: /data
|
||||||
to: my-backend
|
to: my-backend
|
||||||
hooks:
|
hooks:
|
||||||
|
prevalidate:
|
||||||
|
- echo "Checks"
|
||||||
before:
|
before:
|
||||||
- echo "One"
|
- echo "One"
|
||||||
- echo "Two"
|
- echo "Two"
|
||||||
- echo "Three"
|
- echo "Three"
|
||||||
after:
|
after:
|
||||||
- echo "Byte"
|
- echo "Bye"
|
||||||
failure:
|
failure:
|
||||||
- echo "Something went wrong"
|
- echo "Something went wrong"
|
||||||
success:
|
success:
|
||||||
@@ -31,13 +36,15 @@ locations:
|
|||||||
|
|
||||||
## Flowchart
|
## Flowchart
|
||||||
|
|
||||||
1. `before` hook
|
1. `prevalidate` hook
|
||||||
2. Run backup
|
2. Check backup location
|
||||||
3. `after` hook
|
3. `before` hook
|
||||||
4. - `success` hook if no errors were found
|
4. Run backup
|
||||||
|
5. `after` hook
|
||||||
|
6. - `success` hook if no errors were found
|
||||||
- `failure` hook if at least one error was encountered
|
- `failure` hook if at least one error was encountered
|
||||||
|
|
||||||
If the `before` hook encounters errors the backup and `after` hooks will be skipped and only the `failed` hooks will run.
|
If either the `prevalidate` or `before` hook encounters errors then the backup and `after` hooks will be skipped and only the `failed` hooks will run.
|
||||||
|
|
||||||
## Environment variables
|
## Environment variables
|
||||||
|
|
||||||
|
3928
docs/pnpm-lock.yaml
generated
3928
docs/pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
7
go.mod
7
go.mod
@@ -1,6 +1,6 @@
|
|||||||
module github.com/cupcakearmy/autorestic
|
module github.com/cupcakearmy/autorestic
|
||||||
|
|
||||||
go 1.20
|
go 1.21
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/blang/semver/v4 v4.0.0
|
github.com/blang/semver/v4 v4.0.0
|
||||||
@@ -11,9 +11,12 @@ require (
|
|||||||
github.com/robfig/cron v1.2.0
|
github.com/robfig/cron v1.2.0
|
||||||
github.com/spf13/cobra v1.4.0
|
github.com/spf13/cobra v1.4.0
|
||||||
github.com/spf13/viper v1.11.0
|
github.com/spf13/viper v1.11.0
|
||||||
|
github.com/stretchr/testify v1.9.0
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/fsnotify/fsnotify v1.5.1 // indirect
|
github.com/fsnotify/fsnotify v1.5.1 // indirect
|
||||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
||||||
@@ -23,6 +26,7 @@ require (
|
|||||||
github.com/mitchellh/mapstructure v1.4.3 // indirect
|
github.com/mitchellh/mapstructure v1.4.3 // indirect
|
||||||
github.com/pelletier/go-toml v1.9.4 // indirect
|
github.com/pelletier/go-toml v1.9.4 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.0.0-beta.8 // indirect
|
github.com/pelletier/go-toml/v2 v2.0.0-beta.8 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/spf13/afero v1.8.2 // indirect
|
github.com/spf13/afero v1.8.2 // indirect
|
||||||
github.com/spf13/cast v1.4.1 // indirect
|
github.com/spf13/cast v1.4.1 // indirect
|
||||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||||
@@ -32,5 +36,4 @@ require (
|
|||||||
golang.org/x/text v0.3.8 // indirect
|
golang.org/x/text v0.3.8 // indirect
|
||||||
gopkg.in/ini.v1 v1.66.4 // indirect
|
gopkg.in/ini.v1 v1.66.4 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
|
|
||||||
)
|
)
|
||||||
|
7
go.sum
7
go.sum
@@ -182,8 +182,9 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
|
|||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
|
||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
|
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
|
||||||
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
|
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
|
||||||
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
@@ -487,8 +488,8 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
@@ -10,6 +10,8 @@ if [[ $NATIVE_OS == *"linux"* ]]; then
|
|||||||
OS=linux
|
OS=linux
|
||||||
elif [[ $NATIVE_OS == *"darwin"* ]]; then
|
elif [[ $NATIVE_OS == *"darwin"* ]]; then
|
||||||
OS=darwin
|
OS=darwin
|
||||||
|
elif [[ $NATIVE_OS == *"freebsd"* ]]; then
|
||||||
|
OS=freebsd
|
||||||
else
|
else
|
||||||
echo "Could not determine OS automatically, please check the release page manually: https://github.com/cupcakearmy/autorestic/releases"
|
echo "Could not determine OS automatically, please check the release page manually: https://github.com/cupcakearmy/autorestic/releases"
|
||||||
exit 1
|
exit 1
|
||||||
@@ -17,7 +19,7 @@ fi
|
|||||||
echo $OS
|
echo $OS
|
||||||
|
|
||||||
NATIVE_ARCH=$(uname -m | tr '[:upper:]' '[:lower:]')
|
NATIVE_ARCH=$(uname -m | tr '[:upper:]' '[:lower:]')
|
||||||
if [[ $NATIVE_ARCH == *"x86_64"* ]]; then
|
if [[ $NATIVE_ARCH == *"x86_64"* || $NATIVE_ARCH == *"amd64"* ]]; then
|
||||||
ARCH=amd64
|
ARCH=amd64
|
||||||
elif [[ $NATIVE_ARCH == *"arm64"* || $NATIVE_ARCH == *"aarch64"* ]]; then
|
elif [[ $NATIVE_ARCH == *"arm64"* || $NATIVE_ARCH == *"aarch64"* ]]; then
|
||||||
ARCH=arm64
|
ARCH=arm64
|
||||||
|
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/cupcakearmy/autorestic/internal/colors"
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
||||||
@@ -18,13 +19,15 @@ type BackendRest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Backend struct {
|
type Backend struct {
|
||||||
name string
|
name string
|
||||||
Type string `mapstructure:"type,omitempty"`
|
Type string `mapstructure:"type,omitempty"`
|
||||||
Path string `mapstructure:"path,omitempty"`
|
Path string `mapstructure:"path,omitempty"`
|
||||||
Key string `mapstructure:"key,omitempty"`
|
Key string `mapstructure:"key,omitempty"`
|
||||||
Env map[string]string `mapstructure:"env,omitempty"`
|
RequireKey bool `mapstructure:"requireKey,omitempty"`
|
||||||
Rest BackendRest `mapstructure:"rest,omitempty"`
|
Init bool `mapstructure:"init,omitempty"`
|
||||||
Options Options `mapstructure:"options,omitempty"`
|
Env map[string]string `mapstructure:"env,omitempty"`
|
||||||
|
Rest BackendRest `mapstructure:"rest,omitempty"`
|
||||||
|
Options Options `mapstructure:"options,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetBackend(name string) (Backend, bool) {
|
func GetBackend(name string) (Backend, bool) {
|
||||||
@@ -57,6 +60,8 @@ func (b Backend) generateRepo() (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var nonAlphaRegex = regexp.MustCompile("[^A-Za-z0-9]")
|
||||||
|
|
||||||
func (b Backend) getEnv() (map[string]string, error) {
|
func (b Backend) getEnv() (map[string]string, error) {
|
||||||
env := make(map[string]string)
|
env := make(map[string]string)
|
||||||
// Key
|
// Key
|
||||||
@@ -72,7 +77,9 @@ func (b Backend) getEnv() (map[string]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// From Envfile and passed as env
|
// From Envfile and passed as env
|
||||||
var prefix = "AUTORESTIC_" + strings.ToUpper(b.name) + "_"
|
nameForEnv := strings.ToUpper(b.name)
|
||||||
|
nameForEnv = nonAlphaRegex.ReplaceAllString(nameForEnv, "_")
|
||||||
|
var prefix = "AUTORESTIC_" + nameForEnv + "_"
|
||||||
for _, variable := range os.Environ() {
|
for _, variable := range os.Environ() {
|
||||||
var splitted = strings.SplitN(variable, "=", 2)
|
var splitted = strings.SplitN(variable, "=", 2)
|
||||||
if strings.HasPrefix(splitted[0], prefix) {
|
if strings.HasPrefix(splitted[0], prefix) {
|
||||||
@@ -104,6 +111,9 @@ func (b Backend) validate() error {
|
|||||||
// Check if key is set in environment
|
// Check if key is set in environment
|
||||||
env, _ := b.getEnv()
|
env, _ := b.getEnv()
|
||||||
if _, found := env["RESTIC_PASSWORD"]; !found {
|
if _, found := env["RESTIC_PASSWORD"]; !found {
|
||||||
|
if b.RequireKey {
|
||||||
|
return fmt.Errorf("backend %s requires a key but none was provided", b.name)
|
||||||
|
}
|
||||||
// No key set in config file or env => generate random key and save file
|
// No key set in config file or env => generate random key and save file
|
||||||
key := generateRandomKey()
|
key := generateRandomKey()
|
||||||
b.Key = key
|
b.Key = key
|
||||||
@@ -121,20 +131,44 @@ func (b Backend) validate() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
options := ExecuteOptions{Envs: env, Silent: true}
|
options := ExecuteOptions{Envs: env, Silent: true}
|
||||||
// Check if already initialized
|
|
||||||
|
err = b.EnsureInit()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
cmd := []string{"check"}
|
cmd := []string{"check"}
|
||||||
cmd = append(cmd, combineBackendOptions("check", b)...)
|
cmd = append(cmd, combineBackendOptions("check", b)...)
|
||||||
_, _, err = ExecuteResticCommand(options, cmd...)
|
_, _, err = ExecuteResticCommand(options, cmd...)
|
||||||
if err == nil {
|
return err
|
||||||
return nil
|
}
|
||||||
} else {
|
|
||||||
// If not initialize
|
// EnsureInit initializes the backend if it is not already initialized
|
||||||
colors.Body.Printf("Initializing backend \"%s\"...\n", b.name)
|
func (b Backend) EnsureInit() error {
|
||||||
cmd := []string{"init"}
|
env, err := b.getEnv()
|
||||||
cmd = append(cmd, combineBackendOptions("init", b)...)
|
if err != nil {
|
||||||
_, _, err := ExecuteResticCommand(options, cmd...)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
options := ExecuteOptions{Envs: env, Silent: true}
|
||||||
|
|
||||||
|
checkInitCmd := []string{"cat", "config"}
|
||||||
|
checkInitCmd = append(checkInitCmd, combineBackendOptions("cat", b)...)
|
||||||
|
_, _, err = ExecuteResticCommand(options, checkInitCmd...)
|
||||||
|
|
||||||
|
// Note that `restic` has a special exit code (10) to indicate that the
|
||||||
|
// repository does not exist. This exit code was introduced in `restic@0.17.0`
|
||||||
|
// on 2024-07-26. We're not using it here because this is a too recent and
|
||||||
|
// people on older versions of `restic` won't have this feature work correctly.
|
||||||
|
// See: https://restic.readthedocs.io/en/latest/075_scripting.html#exit-codes
|
||||||
|
if err != nil {
|
||||||
|
colors.Body.Printf("Initializing backend \"%s\"...\n", b.name)
|
||||||
|
initCmd := []string{"init"}
|
||||||
|
initCmd = append(initCmd, combineBackendOptions("init", b)...)
|
||||||
|
_, _, err := ExecuteResticCommand(options, initCmd...)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b Backend) Exec(args []string) error {
|
func (b Backend) Exec(args []string) error {
|
||||||
@@ -143,6 +177,7 @@ func (b Backend) Exec(args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
options := ExecuteOptions{Envs: env}
|
options := ExecuteOptions{Envs: env}
|
||||||
|
args = append(args, combineBackendOptions("exec", b)...)
|
||||||
_, out, err := ExecuteResticCommand(options, args...)
|
_, out, err := ExecuteResticCommand(options, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
colors.Error.Println(out)
|
colors.Error.Println(out)
|
||||||
@@ -182,6 +217,7 @@ func (b Backend) ExecDocker(l Location, args []string) (int, string, error) {
|
|||||||
case "s3":
|
case "s3":
|
||||||
case "azure":
|
case "azure":
|
||||||
case "gs":
|
case "gs":
|
||||||
|
case "rest":
|
||||||
// No additional setup needed
|
// No additional setup needed
|
||||||
case "rclone":
|
case "rclone":
|
||||||
// Read host rclone config and mount it into the container
|
// Read host rclone config and mount it into the container
|
||||||
|
@@ -3,9 +3,12 @@ package internal
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/cupcakearmy/autorestic/internal/flags"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGenerateRepo(t *testing.T) {
|
func TestGenerateRepo(t *testing.T) {
|
||||||
@@ -194,6 +197,33 @@ func TestGetEnv(t *testing.T) {
|
|||||||
assertEqual(t, result["B2_ACCOUNT_ID"], "foo123")
|
assertEqual(t, result["B2_ACCOUNT_ID"], "foo123")
|
||||||
assertEqual(t, result["B2_ACCOUNT_KEY"], "foo456")
|
assertEqual(t, result["B2_ACCOUNT_KEY"], "foo456")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
for _, char := range "@-_:/" {
|
||||||
|
t.Run(fmt.Sprintf("env var with special char (%c)", char), func(t *testing.T) {
|
||||||
|
// generate env variables
|
||||||
|
// TODO better way to teardown
|
||||||
|
defer os.Unsetenv("AUTORESTIC_FOO_BAR_RESTIC_PASSWORD")
|
||||||
|
defer os.Unsetenv("AUTORESTIC_FOO_BAR_B2_ACCOUNT_ID")
|
||||||
|
defer os.Unsetenv("AUTORESTIC_FOO_BAR_B2_ACCOUNT_KEY")
|
||||||
|
os.Setenv("AUTORESTIC_FOO_BAR_RESTIC_PASSWORD", "secret123")
|
||||||
|
os.Setenv("AUTORESTIC_FOO_BAR_B2_ACCOUNT_ID", "foo123")
|
||||||
|
os.Setenv("AUTORESTIC_FOO_BAR_B2_ACCOUNT_KEY", "foo456")
|
||||||
|
|
||||||
|
b := Backend{
|
||||||
|
name: fmt.Sprintf("foo%cbar", char),
|
||||||
|
Type: "local",
|
||||||
|
Path: "/foo/bar",
|
||||||
|
}
|
||||||
|
result, err := b.getEnv()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error %v", err)
|
||||||
|
}
|
||||||
|
assertEqual(t, result["RESTIC_REPOSITORY"], "/foo/bar")
|
||||||
|
assertEqual(t, result["RESTIC_PASSWORD"], "secret123")
|
||||||
|
assertEqual(t, result["B2_ACCOUNT_ID"], "foo123")
|
||||||
|
assertEqual(t, result["B2_ACCOUNT_KEY"], "foo456")
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidate(t *testing.T) {
|
func TestValidate(t *testing.T) {
|
||||||
@@ -222,4 +252,82 @@ func TestValidate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
assertEqual(t, err.Error(), "Backend \"foo\" has no \"path\"")
|
assertEqual(t, err.Error(), "Backend \"foo\" has no \"path\"")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("require key with no key", func(t *testing.T) {
|
||||||
|
b := Backend{
|
||||||
|
name: "foo",
|
||||||
|
Type: "local",
|
||||||
|
Path: "~/foo/bar",
|
||||||
|
RequireKey: true,
|
||||||
|
}
|
||||||
|
err := b.validate()
|
||||||
|
fmt.Printf("error: %v\n", err)
|
||||||
|
assert.EqualError(t, err, "backend foo requires a key but none was provided")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateInitsRepo(t *testing.T) {
|
||||||
|
// This is normally initialized by the cobra commands but they don't run in
|
||||||
|
// this test so we do it ourselves.
|
||||||
|
flags.RESTIC_BIN = "restic"
|
||||||
|
|
||||||
|
workDir := t.TempDir()
|
||||||
|
|
||||||
|
b := Backend{
|
||||||
|
name: "test",
|
||||||
|
Type: "local",
|
||||||
|
Path: path.Join(workDir, "backend"),
|
||||||
|
Key: "supersecret",
|
||||||
|
}
|
||||||
|
|
||||||
|
config = &Config{Backends: map[string]Backend{"test": b}}
|
||||||
|
defer func() { config = nil }()
|
||||||
|
|
||||||
|
// Check should fail because the repo doesn't exist
|
||||||
|
err := b.Exec([]string{"check"})
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
err = b.validate()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Check should pass now
|
||||||
|
err = b.Exec([]string{"check"})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnsureInit(t *testing.T) {
|
||||||
|
// This is normally initialized by the cobra commands but they don't run in
|
||||||
|
// this test so we do it ourselves.
|
||||||
|
flags.RESTIC_BIN = "restic"
|
||||||
|
|
||||||
|
workDir := t.TempDir()
|
||||||
|
|
||||||
|
b := Backend{
|
||||||
|
name: "test",
|
||||||
|
Type: "local",
|
||||||
|
Path: path.Join(workDir, "backend"),
|
||||||
|
Key: "supersecret",
|
||||||
|
}
|
||||||
|
|
||||||
|
config = &Config{Backends: map[string]Backend{"test": b}}
|
||||||
|
defer func() { config = nil }()
|
||||||
|
|
||||||
|
// Check should fail because the repo doesn't exist
|
||||||
|
err := b.Exec([]string{"check"})
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
err = b.EnsureInit()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Check should pass now
|
||||||
|
err = b.Exec([]string{"check"})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Run again to make sure it's idempotent
|
||||||
|
err = b.EnsureInit()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Check should still pass
|
||||||
|
err = b.Exec([]string{"check"})
|
||||||
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@@ -37,7 +36,7 @@ func dlJSON(url string) (GithubRelease, error) {
|
|||||||
return parsed, err
|
return parsed, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return parsed, err
|
return parsed, err
|
||||||
|
|
||||||
@@ -73,9 +72,10 @@ func downloadAndInstallAsset(body GithubRelease, name string) error {
|
|||||||
// Uncompress
|
// Uncompress
|
||||||
bz := bzip2.NewReader(resp.Body)
|
bz := bzip2.NewReader(resp.Body)
|
||||||
|
|
||||||
// Save to tmp
|
// Save to tmp file in the same directory as the install directory
|
||||||
// Linux does not support overwriting the file that is currently being overwritten, but it can be deleted and a new one moved in its place.
|
// Linux does not support overwriting the file that is currently being running
|
||||||
tmp, err := ioutil.TempFile(os.TempDir(), "autorestic-")
|
// But it can be delete the old one and a new one moved in its place.
|
||||||
|
tmp, err := os.CreateTemp(INSTALL_PATH, "autorestic-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -89,24 +89,26 @@ func downloadAndInstallAsset(body GithubRelease, name string) error {
|
|||||||
|
|
||||||
to := path.Join(INSTALL_PATH, name)
|
to := path.Join(INSTALL_PATH, name)
|
||||||
defer os.Remove(tmp.Name()) // Cleanup temporary file after thread exits
|
defer os.Remove(tmp.Name()) // Cleanup temporary file after thread exits
|
||||||
if err := os.Rename(tmp.Name(), to); err != nil {
|
|
||||||
colors.Error.Printf("os.Rename() failed (%v), retrying with io.Copy()\n", err.Error())
|
mode := os.FileMode(0755)
|
||||||
var src *os.File
|
if originalBin, err := os.Lstat(to); err == nil {
|
||||||
var dst *os.File
|
mode = originalBin.Mode()
|
||||||
if src, err = os.Open(tmp.Name()); err != nil {
|
err := os.Remove(to)
|
||||||
return err
|
if err != nil {
|
||||||
}
|
|
||||||
if dst, err = os.Create(to); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := io.Copy(dst, src); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := os.Chmod(to, 0755); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = os.Rename(tmp.Name(), to)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.Chmod(to, mode)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
colors.Success.Printf("Successfully installed '%s' under %s\n", name, INSTALL_PATH)
|
colors.Success.Printf("Successfully installed '%s' under %s\n", name, INSTALL_PATH)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -17,7 +17,7 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
const VERSION = "1.7.10"
|
const VERSION = "1.8.3"
|
||||||
|
|
||||||
type OptionMap map[string][]interface{}
|
type OptionMap map[string][]interface{}
|
||||||
type Options map[string]OptionMap
|
type Options map[string]OptionMap
|
||||||
@@ -132,10 +132,11 @@ func (c *Config) Describe() {
|
|||||||
|
|
||||||
tmp = ""
|
tmp = ""
|
||||||
hooks := map[string][]string{
|
hooks := map[string][]string{
|
||||||
"Before": l.Hooks.Before,
|
"PreValidate": l.Hooks.PreValidate,
|
||||||
"After": l.Hooks.After,
|
"Before": l.Hooks.Before,
|
||||||
"Failure": l.Hooks.Failure,
|
"After": l.Hooks.After,
|
||||||
"Success": l.Hooks.Success,
|
"Failure": l.Hooks.Failure,
|
||||||
|
"Success": l.Hooks.Success,
|
||||||
}
|
}
|
||||||
for hook, commands := range hooks {
|
for hook, commands := range hooks {
|
||||||
if len(commands) > 0 {
|
if len(commands) > 0 {
|
||||||
|
@@ -33,11 +33,12 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Hooks struct {
|
type Hooks struct {
|
||||||
Dir string `mapstructure:"dir"`
|
Dir string `mapstructure:"dir"`
|
||||||
Before HookArray `mapstructure:"before,omitempty"`
|
PreValidate HookArray `mapstructure:"prevalidate,omitempty"`
|
||||||
After HookArray `mapstructure:"after,omitempty"`
|
Before HookArray `mapstructure:"before,omitempty"`
|
||||||
Success HookArray `mapstructure:"success,omitempty"`
|
After HookArray `mapstructure:"after,omitempty"`
|
||||||
Failure HookArray `mapstructure:"failure,omitempty"`
|
Success HookArray `mapstructure:"success,omitempty"`
|
||||||
|
Failure HookArray `mapstructure:"failure,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LocationCopy = map[string][]string
|
type LocationCopy = map[string][]string
|
||||||
@@ -184,12 +185,18 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hooks before location validation
|
||||||
|
if err := l.ExecuteHooks(l.Hooks.PreValidate, options); err != nil {
|
||||||
|
errors = append(errors, err)
|
||||||
|
goto after
|
||||||
|
}
|
||||||
|
|
||||||
if err := l.validate(); err != nil {
|
if err := l.validate(); err != nil {
|
||||||
errors = append(errors, err)
|
errors = append(errors, err)
|
||||||
goto after
|
goto after
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hooks
|
// Hooks after location validation
|
||||||
if err := l.ExecuteHooks(l.Hooks.Before, options); err != nil {
|
if err := l.ExecuteHooks(l.Hooks.Before, options); err != nil {
|
||||||
errors = append(errors, err)
|
errors = append(errors, err)
|
||||||
goto after
|
goto after
|
||||||
@@ -215,6 +222,14 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if backend.Init {
|
||||||
|
err = backend.EnsureInit()
|
||||||
|
if err != nil {
|
||||||
|
errors = append(errors, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cmd := []string{"backup"}
|
cmd := []string{"backup"}
|
||||||
cmd = append(cmd, combineAllOptions("backup", l, backend)...)
|
cmd = append(cmd, combineAllOptions("backup", l, backend)...)
|
||||||
if cron {
|
if cron {
|
||||||
@@ -289,12 +304,13 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// After hooks
|
// After backup hooks
|
||||||
if err := l.ExecuteHooks(l.Hooks.After, options); err != nil {
|
if err := l.ExecuteHooks(l.Hooks.After, options); err != nil {
|
||||||
errors = append(errors, err)
|
errors = append(errors, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
after:
|
after:
|
||||||
|
// Success/failure hooks
|
||||||
var commands []string
|
var commands []string
|
||||||
var isSuccess = len(errors) == 0
|
var isSuccess = len(errors) == 0
|
||||||
if isSuccess {
|
if isSuccess {
|
||||||
|
2
main.go
2
main.go
@@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
|
|||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
You may obtain a copy of the License at
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
Unless required by applicable law or agreed to in writing, software
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
Reference in New Issue
Block a user