Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 61 additions & 4 deletions cmd/standard-backups-restic-backend/restic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@ package main

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"regexp"

"github.com/hashicorp/go-version"
)

func resticBin() string {
restic := os.Getenv("RESTIC")
if restic == "" {
restic = "restic"
}
return restic
}

func resticCmd(repo string, env map[string]string, args ...string) *exec.Cmd {
finalArgs := []string{"--repo", repo}
finalArgs = append(finalArgs, args...)
cmd := exec.Command("restic", finalArgs...)
cmd := exec.Command(resticBin(), finalArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

Expand Down Expand Up @@ -42,16 +54,61 @@ func resticOutput(repo string, env map[string]string, args ...string) ([]byte, e
return output.Bytes(), nil
}

func resticRawVersion() (string, error) {
cmd := exec.Command(resticBin(), "version", "--json")
stdout := bytes.NewBuffer(nil)
cmd.Stdout = stdout
err := cmd.Run()
if err != nil {
return "", err
}
output := stdout.Bytes()
var versionMessage struct {
Version string `json:"version"`
}
err = json.Unmarshal(output, &versionMessage)
if err == nil {
return versionMessage.Version, nil
}

// Older versions don't honor `--json` on `restic version`. Parsing out manually.
versionRegexp := regexp.MustCompile(`^restic (\d+\.\d+\.\d+) compiled`)
matches := versionRegexp.FindSubmatch(output)
if matches == nil {
return "", fmt.Errorf("could not parse restic version from '%s'", output)
}
return string(matches[1]), nil
}

func resticVersion() (*version.Version, error) {
raw, err := resticRawVersion()
if err != nil {
return nil, fmt.Errorf("failed to determine restic version: %w", err)
}
res, err := version.NewVersion(raw)
if err != nil {
return nil, fmt.Errorf("failed to determine restic version (raw=%s): %w", raw, err)
}
return res, nil
}

func checkRepoExists(repo string, env map[string]string) (bool, error) {
cmd := resticCmd(repo, env, "cat", "config")
cmd.Stderr = nil
cmd.Stdout = nil

err := cmd.Run()
var exitError *exec.ExitError
if errors.As(err, &exitError) &&
exitError.ExitCode() == 10 /*repo does not exist*/ {
return false, nil
if errors.As(err, &exitError) {
v, err := resticVersion()
if err != nil {
return false, fmt.Errorf("failed to check if repo %s exists: %w", repo, err)
}
if v.LessThan(version.Must(version.NewVersion("0.17.0"))) /*no dedicated exit code*/ ||
exitError.ExitCode() == 10 /*repo does not exist*/ {
return false, nil
}
return false, fmt.Errorf("failed to check if repo %s exists: %w", repo, err)
} else if err != nil {
return false, fmt.Errorf("failed to check if repo %s exists: %w", repo, err)
}
Expand Down
39 changes: 39 additions & 0 deletions cmd/standard-backups-restic-backend/restic_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package main

import (
"fmt"
"os"
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestOptionsToArgs(t *testing.T) {
Expand Down Expand Up @@ -57,3 +61,38 @@ func TestOptionsToArgs(t *testing.T) {
})
}
}

func TestCheckRepoExists(t *testing.T) {
tests := map[string]string{
"system": "restic",
}
restic016, ok := os.LookupEnv("RESTIC_0_16")
if ok {
tests["0.16"] = fmt.Sprintf("%s/bin/restic", restic016)
}
for name, restic := range tests {
t.Run(name, func(t *testing.T) {
t.Setenv("RESTIC", restic)
t.Run("exists", func(t *testing.T) {
repo := t.TempDir()
cmd := exec.Command(restic, "init", "--repo", repo)
cmd.Env = append(cmd.Env, "RESTIC_PASSWORD=supersecret")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
require.NoError(t, err)

res, err := checkRepoExists(repo, map[string]string{
"RESTIC_PASSWORD": "supersecret",
})
assert.NoError(t, err)
assert.True(t, res)
})
t.Run("not-exists", func(t *testing.T) {
res, err := checkRepoExists(t.TempDir(), map[string]string{})
assert.NoError(t, err)
assert.False(t, res)
})
})
}
}
13 changes: 13 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
'';

devShells.default = pkgs.mkShell {
# This old version of restic doesn't return the right status code when a
# repository is not found. We want to support it too because it's still
# in use in ubuntu 24.04.
RESTIC_0_16 = pkgs.restic.overrideAttrs (final: previous: rec {
version = "0.16.5";
src = pkgs.fetchFromGitHub {
owner = "restic";
repo = "restic";
rev = "v${version}";
hash = "sha256-WwySXQU8eoyQRcI+zF+pIIKLEFheTnqkPTw0IZeUrhA=";
};
vendorHash = "sha256-VZTX0LPZkqN4+OaaIkwepbGwPtud8Cu7Uq7t1bAUC8M=";
});
hardeningDisable = [
"fortify" # required to use delve debugger
];
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/go-viper/mapstructure/v2 v2.5.0
github.com/goccy/go-yaml v1.19.2
github.com/gookit/color v1.6.0
github.com/hashicorp/go-version v1.8.0
github.com/k0kubun/pp/v3 v3.5.1
github.com/lithammer/dedent v1.1.0
github.com/olekukonko/tablewriter v1.1.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ github.com/gookit/assert v0.1.1 h1:lh3GcawXe/p+cU7ESTZ5Ui3Sm/x8JWpIis4/1aF0mY0=
github.com/gookit/assert v0.1.1/go.mod h1:jS5bmIVQZTIwk42uXl4lyj4iaaxx32tqH16CFj0VX2E=
github.com/gookit/color v1.6.0 h1:JjJXBTk1ETNyqyilJhkTXJYYigHG24TM9Xa2M1xAhRA=
github.com/gookit/color v1.6.0/go.mod h1:9ACFc7/1IpHGBW8RwuDm/0YEnhg3dwwXpoMsmtyHfjs=
github.com/hashicorp/go-version v1.8.0 h1:KAkNb1HAiZd1ukkxDFGmokVZe1Xy9HG6NUp+bPle2i4=
github.com/hashicorp/go-version v1.8.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/k0kubun/pp/v3 v3.5.1 h1:fS8Xt0MWVVSiKwfXeIdE0WJlktdA87/gt0Hs0+j2R2s=
Expand Down
Loading