Compare commits

..

2 Commits

Author SHA1 Message Date
rdelaage
9697e080e6 Merge 8e24286ca3 into 3b57602fe8 2023-08-03 12:21:06 +02:00
Romain de Laage
8e24286ca3 chore: split internal package into multiple subpackages 2023-07-26 09:06:30 +02:00
79 changed files with 8181 additions and 3434 deletions

View File

@@ -3,7 +3,7 @@ name: Main
on:
push:
tags:
- 'v*.*.*'
- "v*.*.*"
jobs:
docker:
@@ -40,7 +40,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '^1.21'
go-version: "^1.20"
- name: Build
run: go run build/build.go
- name: Release

View File

@@ -1,4 +1,4 @@
FROM golang:1.22-alpine as builder
FROM golang:1.20-alpine as builder
WORKDIR /app
COPY go.* .
@@ -6,8 +6,8 @@ RUN go mod download
COPY . .
RUN go build
FROM restic/restic:0.16.4
RUN apk add --no-cache rclone bash curl docker-cli
FROM restic/restic:0.15.1
RUN apk add --no-cache rclone bash
COPY --from=builder /app/autorestic /usr/bin/autorestic
ENTRYPOINT []
CMD [ "autorestic" ]

View File

@@ -34,7 +34,7 @@ Autorestic is a wrapper around the amazing [restic](https://restic.net/). While
- Backup locations to multiple backends
- Snapshot policies and pruning
- Fully encrypted
- Before/after backup hooks
- Pre/After hooks
- Exclude pattern/files
- Cron jobs for automatic backup
- Backup & Restore docker volume

View File

@@ -5,10 +5,10 @@ import (
"path/filepath"
"strings"
"github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/colors"
"github.com/cupcakearmy/autorestic/internal/flags"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/cupcakearmy/autorestic/internal/version"
"github.com/spf13/cobra"
homedir "github.com/mitchellh/go-homedir"
@@ -26,7 +26,7 @@ func CheckErr(err error) {
var cfgFile string
var rootCmd = &cobra.Command{
Version: internal.VERSION,
Version: version.VERSION,
Use: "autorestic",
Short: "CLI Wrapper for restic",
Long: "Documentation:\thttps://autorestic.vercel.app\nSupport:\thttps://discord.gg/wS7RpYTYd2",
@@ -41,7 +41,7 @@ func init() {
rootCmd.PersistentFlags().BoolVar(&flags.CI, "ci", false, "CI mode disabled interactive mode and colors and enables verbosity")
rootCmd.PersistentFlags().BoolVarP(&flags.VERBOSE, "verbose", "v", false, "verbose mode")
rootCmd.PersistentFlags().StringVar(&flags.RESTIC_BIN, "restic-bin", "restic", "specify custom restic binary")
rootCmd.PersistentFlags().StringVar(&flags.DOCKER_IMAGE, "docker-image", "cupcakearmy/autorestic:"+internal.VERSION, "specify a custom docker image")
rootCmd.PersistentFlags().StringVar(&flags.DOCKER_IMAGE, "docker-image", "cupcakearmy/autorestic:"+version.VERSION, "specify a custom docker image")
cobra.OnInitialize(initConfig)
}
@@ -59,7 +59,24 @@ func initConfig() {
os.Exit(1)
}
} else {
configPaths := getConfigPaths()
configPaths := []string{"."}
// Home
if home, err := homedir.Dir(); err == nil {
configPaths = append(configPaths, home)
}
// XDG_CONFIG_HOME
{
prefix, found := os.LookupEnv("XDG_CONFIG_HOME")
if !found {
if home, err := homedir.Dir(); err != nil {
prefix = filepath.Join(home, ".config")
}
}
xdgConfig := filepath.Join(prefix, "autorestic")
configPaths = append(configPaths, xdgConfig)
}
for _, cfgPath := range configPaths {
viper.AddConfigPath(cfgPath)
}
@@ -71,22 +88,3 @@ func initConfig() {
viper.AutomaticEnv()
}
}
func getConfigPaths() []string {
result := []string{"."}
if home, err := homedir.Dir(); err == nil {
result = append(result, home)
}
{
xdgConfigHome, found := os.LookupEnv("XDG_CONFIG_HOME")
if !found {
if home, err := homedir.Dir(); err == nil {
xdgConfigHome = filepath.Join(home, ".config")
}
}
xdgConfig := filepath.Join(xdgConfigHome, "autorestic")
result = append(result, xdgConfig)
}
return result
}

View File

@@ -1,36 +0,0 @@
package cmd
import (
"github.com/mitchellh/go-homedir"
"os"
"path/filepath"
"slices"
"testing"
)
const xdgConfigHome = "XDG_CONFIG_HOME"
func assertContains(t *testing.T, array []string, element string) {
if !slices.Contains(array, element) {
t.Errorf("Expected %s to be contained in %s", element, array)
}
}
func TestConfigResolving(t *testing.T) {
t.Run("~/.config/autorestic is used if XDG_CONFIG_HOME is not set", func(t *testing.T) {
// Override env using testing so that env gets restored after test
t.Setenv(xdgConfigHome, "")
_ = os.Unsetenv("XDG_CONFIG_HOME")
configPaths := getConfigPaths()
homeDir, _ := homedir.Dir()
expectedConfigPath := filepath.Join(homeDir, ".config/autorestic")
assertContains(t, configPaths, expectedConfigPath)
})
t.Run("XDG_CONFIG_HOME is respected if set", func(t *testing.T) {
t.Setenv(xdgConfigHome, "/foo/bar")
configPaths := getConfigPaths()
assertContains(t, configPaths, filepath.Join("/", "foo", "bar", "autorestic"))
})
}

View File

@@ -1,79 +0,0 @@
package cmd
import (
"bytes"
"fmt"
"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{}
for _, line := range lines {
if strings.Contains(line, "autorestic") && !strings.Contains(line, "grep autorestic") {
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
}

15
docs/.codedoc/build.ts Normal file
View File

@@ -0,0 +1,15 @@
import { build } from '@codedoc/core';
import { config } from './config';
import { installTheme$ } from './content/theme';
import { content } from './content';
build(config, content, installTheme$, {
resolve: {
modules: ['.codedoc/node_modules']
},
resolveLoader: {
modules: ['.codedoc/node_modules']
}
});

24
docs/.codedoc/config.ts Normal file
View File

@@ -0,0 +1,24 @@
import { configuration } from '@codedoc/core'
export const config = configuration({
src: {
base: 'markdown',
},
dest: {
html: './build',
assets: './build',
bundle: './_',
styles: './_',
},
page: {
title: {
base: 'Autorestic',
},
},
misc: {
github: {
user: 'cupcakearmy',
repo: 'autorestic',
},
},
})

View File

@@ -0,0 +1,19 @@
import { CodedocConfig } from '@codedoc/core';
import { Footer as _Footer, GitterToggle$, Watermark} from '@codedoc/core/components';
export function Footer(config: CodedocConfig, renderer: any) {
let github$;
if (config.misc?.github)
github$ = <a href={`https://github.com/${config.misc.github.user}/${config.misc.github.repo}/`}
target="_blank">GitHub</a>;
let community$;
if (config.misc?.gitter)
community$ = <GitterToggle$ room={config.misc.gitter.room}/>
if (github$ && community$) return <_Footer>{github$}<hr/>{community$}</_Footer>;
else if (github$) return <_Footer>{github$}</_Footer>;
else if (community$) return <_Footer>{community$}</_Footer>;
else return <_Footer><Watermark/></_Footer>;
}

View File

@@ -0,0 +1,21 @@
import { CodedocConfig } from '@codedoc/core';
import { Header as _Header, GithubButton, Watermark } from '@codedoc/core/components';
export function Header(config: CodedocConfig, renderer: any) {
return (
<_Header>{config.misc?.github ?
<fragment>
<GithubButton action={config.misc.github.action || 'Star'}
repo={config.misc.github.repo}
user={config.misc.github.user}
large={config.misc.github.large === true}
count={config.misc.github.count !== false}
standardIcon={config.misc.github.standardIcon !== false}/>
<br/><br/>
</fragment>
: ''}
<Watermark/>
</_Header>
)
}

View File

@@ -0,0 +1,57 @@
import { RendererLike } from '@connectv/html'
import { File } from 'rxline/fs'
import {
Page,
Meta,
ContentNav,
Fonts,
ToC,
GithubSearch$,
} from '@codedoc/core/components'
import { config } from '../config'
import { Header } from './header'
import { Footer } from './footer'
export function content(
_content: HTMLElement,
toc: HTMLElement,
renderer: RendererLike<any, any>,
file: File<string>
) {
return (
<Page
title={config.page.title.extractor(_content, config, file)}
favicon={config.page.favicon}
meta={<Meta {...config.page.meta} />}
fonts={<Fonts {...config.page.fonts} />}
scripts={config.page.scripts}
stylesheets={config.page.stylesheets}
header={<Header {...config} />}
footer={<Footer {...config} />}
toc={
<ToC
default={'open'}
search={
config.misc?.github ? (
<GithubSearch$
repo={config.misc.github.repo}
user={config.misc.github.user}
root={config.src.base}
pick={config.src.pick.source}
drop={config.src.drop.source}
/>
) : (
false
)
}
>
{toc}
</ToC>
}
>
{_content}
<ContentNav content={_content} />
</Page>
)
}

View File

@@ -0,0 +1,8 @@
import { funcTransport } from '@connectv/sdh/transport';
import { useTheme } from '@codedoc/core/transport';
import { theme } from '../theme';
export function installTheme() { useTheme(theme); }
export const installTheme$ = /*#__PURE__*/funcTransport(installTheme);

4916
docs/.codedoc/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,5 @@
{
"dependencies": {
"@codedoc/core": "^0.3.2"
}
}

18
docs/.codedoc/serve.ts Normal file
View File

@@ -0,0 +1,18 @@
import { join } from 'path';
import { serve } from '@codedoc/core';
import { config } from './config';
import { content } from './content';
import { installTheme$ } from './content/theme';
const root = join(__dirname, '../');
serve(root, config, content, installTheme$, {
resolve: {
modules: ['.codedoc/node_modules']
},
resolveLoader: {
modules: ['.codedoc/node_modules']
}
});

11
docs/.codedoc/theme.ts Normal file
View File

@@ -0,0 +1,11 @@
import { createTheme } from '@codedoc/core/transport';
export const theme = /*#__PURE__*/createTheme({
light: {
primary: '#1eb2a6'
},
dark: {
primary: '#1eb2a6'
}
});

View File

@@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "es6",
"noImplicitAny": true,
"declaration": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"alwaysStrict": true,
"sourceMap": true,
"moduleResolution": "node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"jsx": "react",
"jsxFactory": "renderer.create",
"lib": [
"es2017",
"dom"
]
},
"include": [
"./**/*"
]
}

22
docs/.codedoc/watch.ts Normal file
View File

@@ -0,0 +1,22 @@
import { exec, spawn } from 'child_process';
import { config } from './config';
const cmd = 'ts-node-dev';
const params = `--project .codedoc/tsconfig.json`
+ ` -T --watch ${config.src.base},.codedoc`
+ ` --ignore-watch .codedoc/node_modules`
+ ` .codedoc/serve`;
if (process.platform === 'win32') {
const child = exec(cmd + ' ' + params);
child.stdout?.pipe(process.stdout);
child.stderr?.pipe(process.stderr);
child.on('close', () => {});
}
else {
const child = spawn(cmd, [params], { stdio: 'inherit', shell: 'bash' });
child.on('close', () => {});
}

2
docs/.gitignore vendored
View File

@@ -1,2 +1,2 @@
node_modules
.next
build

View File

@@ -1 +0,0 @@
v20.8.0

54
docs/markdown/_toc.md Normal file
View File

@@ -0,0 +1,54 @@
[Home](/)
[Quick Start](/quick)
[Installation](/installation)
[Configuration](/config)
[Upgrade](/upgrade)
> :Collapse label=Locations
>
> [Overview](/location/overview)
> [Hooks](/location/hooks)
>
> > :Collapse label=Options
> >
> > [Overview](/location/options)
> > [Excluding Files](/location/exclude)
> > [Forget Policy](/location/forget)
> > [Copy](/location/copy)
>
> [Cron](/location/cron)
> [Docker Volumes](/location/docker)
> :Collapse label=Backend
>
> [Overview](/backend/overview)
> [Available Backends](/backend/available)
> [Options](/backend/options)
> [Environment](/backend/env)
> :Collapse label=CLI
>
> [General](/cli/general)
> [Info](/cli/info)
> [Check](/cli/check)
> [Completion](/cli/completion)
> [Backup](/cli/backup)
> [Restore](/cli/restore)
> [Forget](/cli/forget)
> [Cron](/cli/cron)
> [Exec](/cli/exec)
> [Install](/cli/install)
> [Uninstall](/cli/uninstall)
> [Upgrade](/cli/upgrade)
> :Collapse label=Migration
>
> [0.x → 1.0](/migration/0.x_1.0)
> [1.4 → 1.5](/migration/1.4_1.5)
[Examples](/examples)
[Docker](/docker)
[QA](/qa)
[Community](/community)
[Contributors](/contrib)

View File

@@ -83,3 +83,5 @@ backends:
user: user
password: pass
```
> :ToCPrevNext

View File

@@ -63,3 +63,5 @@ backends:
type: b2
path: myBucket
```
> :ToCPrevNext

View File

@@ -15,3 +15,5 @@ backend:
```
In this example, whenever `autorestic` runs `restic backup` it will append a `--tag abc --tag` to the native command.
> :ToCPrevNext

View File

@@ -16,3 +16,5 @@ backends:
## Types
We restic supports multiple types of backends. See the [full list](/backend/available) for details.
> :ToCPrevNext

View File

@@ -21,3 +21,5 @@ autorestic backup -l foo -l bar
```bash
autorestic backup -l location@backend
```
> :ToCPrevNext

View File

@@ -7,3 +7,5 @@ autorestic check
Checks locations and backends are configured properly and initializes them if they are not already.
This is mostly an internal command, but useful to verify if a backend is configured correctly.
> :ToCPrevNext

View File

@@ -13,3 +13,5 @@ Supported shells are
- powershell
To see how to install run `autorestic help completion` and follow the instructions for your specific shell
> :ToCPrevNext

View File

@@ -9,3 +9,5 @@ This command is mostly intended to be triggered by an automated system like syst
It will run cron jobs as [specified in the cron section](/location/cron) of a specific location.
The `--lean` flag will omit output like _skipping location x: not due yet_. This can be useful if you are dumping the output of the cron job to a log file and don't want to be overwhelmed by the output log.
> :ToCPrevNext

View File

@@ -11,3 +11,5 @@ autorestic exec -av -- snapshots
```
With `exec` you can basically run every cli command that you would be able to run with the restic cli. It only pre-fills path, key, etc.
> :ToCPrevNext

View File

@@ -4,8 +4,10 @@
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/options/forget).
This will prune and remove old data form the backends according to the [keep policy you have specified for the location](/location/forget).
The `--dry-run` flag will do a dry run showing what would have been deleted, but won't touch the actual data.
The `--prune` flag will also [prune the data](https://restic.readthedocs.io/en/latest/060_forget.html#removing-backup-snapshots). This is a costly operation that can take longer, however it will free up the actual space.
> :ToCPrevNext

View File

@@ -34,3 +34,5 @@ With `--restic-bin` you can specify to run a specific restic binary. This can be
```bash
autorestic --restic-bin /some/path/to/my/custom/restic/binary
```
> :ToCPrevNext

View File

@@ -14,3 +14,5 @@ autorestic info
```bash
autorestic -c path/to/some/config.yml info
```
> :ToCPrevNext

View File

@@ -5,3 +5,5 @@ Installs both restic and autorestic to `/usr/local/bin`.
```bash
autorestic install
```
> :ToCPrevNext

View File

@@ -15,3 +15,5 @@ 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`
> :ToCPrevNext

View File

@@ -5,3 +5,5 @@ Uninstalls both restic and autorestic from `/usr/local/bin`.
```bash
autorestic uninstall
```
> :ToCPrevNext

View File

@@ -7,3 +7,5 @@ autorestic upgrade
```
Updates both restic and autorestic automagically.
> :ToCPrevNext

View File

@@ -9,3 +9,5 @@ A list of community driven projects. (No official affiliation)
- Ansible Role: <https://github.com/FuzzyMistborn/ansible-role-autorestic>
- Ansible Role: <https://0xacab.org/varac-projects/ansible-role-autorestic>
- Ansible Role: <https://github.com/dbrennand/ansible-role-autorestic>
> :ToCPrevNext

View File

@@ -2,11 +2,10 @@
## Path
By default autorestic searches for a `.autorestic.yml` file in the current directory, your home folder and your XDG config folder (`~/.config/` by default):
By default autorestic searches for a `.autorestic.yml` file in the current directory and your home folder.
- `./.autorestic.yml`
- `~/.autorestic.yml`
- `~/.config/autorestic/.autorestic.yml`
You can also specify a custom file with the `-c path/to/some/config.yml`
@@ -56,8 +55,6 @@ version: 2
extras:
hooks: &foo
prevalidate:
- echo "Wake up!"
before:
- echo "Hello"
after:
@@ -86,3 +83,5 @@ locations:
forget:
<<: *bar
```
> :ToCPrevNext

View File

@@ -17,3 +17,5 @@ This amazing people helped the project!
- @TheForcer - Typos.
- @themorlan - Typos.
- @somebox - Typos.
> :ToCPrevNext

View File

@@ -36,3 +36,5 @@ locations:
to:
- somewhere-else
```
> :ToCPrevNext

View File

@@ -13,8 +13,10 @@ Autorestic is a wrapper around the amazing [restic](https://restic.net/). While
- Backup locations to multiple backends
- Snapshot policies and pruning
- Fully encrypted
- Before/after backup hooks
- Pre/After hooks
- Exclude pattern/files
- Cron jobs for automatic backup
- Backup & Restore docker volumes
- Generated completions for `[bash|zsh|fish|powershell]`
> :ToCPrevNext

View File

@@ -30,4 +30,6 @@ Fedora users can install the [autorestic](https://src.fedoraproject.org/rpms/aut
### AUR
If you are on Arch there is an [AUR Package](https://aur.archlinux.org/packages/autorestic-bin/)
~~If you are on Arch there is an [AUR Package](https://aur.archlinux.org/packages/autorestic-bin/) (looking for maintainers).~~ - Deprecated
> :ToCPrevNext

View File

@@ -1,6 +1,6 @@
# Cron
Often it is useful to trigger backups automatically. For this, we can specify a `cron` attribute to each location.
Often it is usefully to trigger backups automatically. For this we can specify a `cron` attribute to each location.
```yaml | .autorestic.yml
locations:
@@ -10,15 +10,15 @@ locations:
cron: '0 3 * * 0' # Every Sunday at 3:00
```
Here is an awesome website with [some examples](https://crontab.guru/examples.html) and an [explorer](https://crontab.guru/).
Here is a awesome website with [some examples](https://crontab.guru/examples.html) and an [explorer](https://crontab.guru/)
## Installing the cron
**This has to be done only once, regardless of how many cron jobs you have in your config file.**
**This has to be done only once, regardless of now many cron jobs you have in your config file.**
To actually enable cron jobs you need something to call `autorestic cron` on a timed schedule.
Note that the schedule has nothing to do with the `cron` attribute in each location.
My advice would be to trigger the command every 5min, but if you have a cronjob that runs only once a week, it's probably enough to schedule it once a day.
My advise would be to trigger the command every 5min, but if you have a cronjob that runs only once a week, it's probably enough to schedule it once a day.
### Crontab
@@ -50,4 +50,6 @@ To debug a cron job you can use
Now you can add as many `cron` attributes as you wish in the config file ⏱
> Also note that manually triggered backups with `autorestic backup` will not influence the cron timeline, they are intentionally not linked.
> Also note that manually triggered backups with `autorestic backup` will not influence the cron timeline, they are willingly not linked.
> :ToCPrevNext

View File

@@ -35,3 +35,5 @@ autorestic restore -l hello
```
The volume has to exists whenever backing up or restoring.
> :ToCPrevNext

View File

@@ -16,3 +16,5 @@ locations:
- '*.abc'
exclude-file: .gitignore
```
> :ToCPrevNext

View File

@@ -53,3 +53,5 @@ locations:
forget:
keep-last: 5
```
> :ToCPrevNext

View File

@@ -6,28 +6,23 @@ 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:
- `prevalidate`
- `before`
- `after`
- `failure`
- `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
locations:
my-location:
from: /data
to: my-backend
hooks:
prevalidate:
- echo "Checks"
before:
- echo "One"
- echo "Two"
- echo "Three"
after:
- echo "Bye"
- echo "Byte"
failure:
- echo "Something went wrong"
success:
@@ -36,15 +31,13 @@ locations:
## Flowchart
1. `prevalidate` hook
2. Check backup location
3. `before` hook
4. Run backup
5. `after` hook
6. - `success` hook if no errors were found
1. `before` hook
2. Run backup
3. `after` hook
4. - `success` hook if no errors were found
- `failure` hook if at least one error was encountered
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.
If the `before` hook encounters errors the backup and `after` hooks will be skipped and only the `failed` hooks will run.
## Environment variables
@@ -83,3 +76,5 @@ AUTORESTIC_LOCATION=bar
AUTORESTIC_FILES_ADDED_0=42
AUTORESTIC_FILES_ADDED_FOO=42
```
> :ToCPrevNext

View File

@@ -63,3 +63,5 @@ backends:
locations:
# ...
```
> :ToCPrevNext

View File

@@ -4,7 +4,6 @@ Locations can be seen as the input to the backup process. Generally this is simp
The paths can be relative from the config file. A location can have multiple backends, so that the data is secured across multiple servers.
Note: names of locations MUST be lower case!
```yaml | .autorestic.yml
version: 2
@@ -31,3 +30,5 @@ Paths can be absolute or relative. If relative they are resolved relative to the
## `to`
This is either a single backend or an array of backends. The backends have to be configured in the same config file.
> :ToCPrevNext

View File

@@ -22,3 +22,5 @@ remote:
```
Other than the config file there is a new `-v, --verbose` flag which shows the output of native commands, which are now hidden by default.
> :ToCPrevNext

View File

@@ -64,3 +64,5 @@ Autorestic changed the way backups are referenced. Before we took the paths as t
```bash
autorestic exec -va -- tag --add ar:location:LOCATION_NAME # Only if you have only one location
```
> :ToCPrevNext

View File

@@ -6,3 +6,5 @@ This happens when autorestic needs to write to the config file: e.g. when we are
Unfortunately during this process formatting and comments are lost because the `yaml` library used is not comment and/or format aware.
That is why autorestic will place a copy of your old config next to the one we are writing to.
> :ToCPrevNext

View File

@@ -82,3 +82,5 @@ autorestic restore -l home --from hdd --to /path/where/to/restore
```
This will restore the location `home` from the backend `hdd` to the given path.
> :ToCPrevNext

View File

@@ -1,6 +0,0 @@
const withNextra = require('nextra')({
theme: 'nextra-theme-docs',
themeConfig: './theme.config.jsx',
})
module.exports = withNextra()

2741
docs/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,14 +1,10 @@
{
"private": true,
"scripts": {
"build": "NEXT_TELEMETRY_DISABLED=1 next build",
"dev": "NEXT_TELEMETRY_DISABLED=1 next"
"build": "codedoc install && codedoc build",
"dev": "codedoc serve"
},
"dependencies": {
"next": "^13.5.3",
"nextra": "^2.13.1",
"nextra-theme-docs": "^2.13.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"packageManager": "pnpm@8.8.0"
"@codedoc/cli": "^0.3.0"
}
}

View File

@@ -1,10 +0,0 @@
{
"index": "Home",
"quick": "Quick Start",
"installation": "Installation",
"config": "Configuration",
"location": "Locations",
"backend": "Backend",
"cli": "CLI",
"migration": "Migration"
}

View File

@@ -1,6 +0,0 @@
{
"index": "Overview",
"available": "Available backends",
"options": "Options",
"env": "Environment"
}

View File

@@ -1,3 +0,0 @@
{
"general": "General"
}

View File

@@ -1,32 +0,0 @@
# 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
```

View File

@@ -1,7 +0,0 @@
{
"index": "Overview",
"hooks": "Hooks",
"options": "Options",
"cron": "Cronjobs",
"docker": "Docker volumes"
}

View File

@@ -1,3 +0,0 @@
{
"index": "Overview"
}

View File

@@ -1,4 +0,0 @@
{
"0.x_1.0": "0.x → 1.0",
"1.4_1.5": "1.4 → 1.5"
}

3044
docs/pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,24 +0,0 @@
export default {
logo: <span>Autorestic</span>,
docsRepositoryBase: 'https://github.com/cupcakearmy/autorestic/tree/master/docs',
project: {
link: 'https://github.com/cupcakearmy/autorestic',
},
sidebar: {
defaultMenuCollapseLevel: 1,
},
feedback: {
content: 'Question? An error? Give feedback →',
},
footer: {
text: (
<span>
MIT {new Date().getFullYear()} ©{' '}
<a href="https://github.com/cupcakearmy" target="_blank">
cupcakearmy
</a>
.
</span>
),
},
}

3
docs/vercel.json Normal file
View File

@@ -0,0 +1,3 @@
{
"cleanUrls": true
}

2
go.mod
View File

@@ -1,6 +1,6 @@
module github.com/cupcakearmy/autorestic
go 1.21
go 1.20
require (
github.com/blang/semver/v4 v4.0.0

View File

@@ -10,6 +10,8 @@ import (
"github.com/cupcakearmy/autorestic/internal/colors"
"github.com/cupcakearmy/autorestic/internal/flags"
"github.com/cupcakearmy/autorestic/internal/options"
"github.com/cupcakearmy/autorestic/internal/utils"
)
type BackendRest struct {
@@ -24,7 +26,7 @@ type Backend struct {
Key string `mapstructure:"key,omitempty"`
Env map[string]string `mapstructure:"env,omitempty"`
Rest BackendRest `mapstructure:"rest,omitempty"`
Options Options `mapstructure:"options,omitempty"`
Options options.Options `mapstructure:"options,omitempty"`
}
func GetBackend(name string) (Backend, bool) {
@@ -120,11 +122,15 @@ func (b Backend) validate() error {
if err != nil {
return err
}
options := ExecuteOptions{Envs: env, Silent: true}
options := utils.ExecuteOptions{
Envs: env,
Silent: true,
Global: GetConfig().Global,
}
// Check if already initialized
cmd := []string{"check"}
cmd = append(cmd, combineBackendOptions("check", b)...)
_, _, err = ExecuteResticCommand(options, cmd...)
_, _, err = utils.ExecuteResticCommand(options, cmd...)
if err == nil {
return nil
} else {
@@ -132,7 +138,7 @@ func (b Backend) validate() error {
colors.Body.Printf("Initializing backend \"%s\"...\n", b.name)
cmd := []string{"init"}
cmd = append(cmd, combineBackendOptions("init", b)...)
_, _, err := ExecuteResticCommand(options, cmd...)
_, _, err := utils.ExecuteResticCommand(options, cmd...)
return err
}
}
@@ -142,9 +148,11 @@ func (b Backend) Exec(args []string) error {
if err != nil {
return err
}
options := ExecuteOptions{Envs: env}
args = append(args, combineBackendOptions("exec", b)...)
_, out, err := ExecuteResticCommand(options, args...)
options := utils.ExecuteOptions{
Envs: env,
Global: GetConfig().Global,
}
_, out, err := utils.ExecuteResticCommand(options, args...)
if err != nil {
colors.Error.Println(out)
return err
@@ -158,7 +166,7 @@ func (b Backend) ExecDocker(l Location, args []string) (int, string, error) {
return -1, "", err
}
volume := l.From[0]
options := ExecuteOptions{
options := utils.ExecuteOptions{
Command: "docker",
Envs: env,
}
@@ -186,7 +194,7 @@ func (b Backend) ExecDocker(l Location, args []string) (int, string, error) {
// No additional setup needed
case "rclone":
// Read host rclone config and mount it into the container
code, configFile, err := ExecuteCommand(ExecuteOptions{Command: "rclone"}, "config", "file")
code, configFile, err := utils.ExecuteCommand(utils.ExecuteOptions{Command: "rclone"}, "config", "file")
if err != nil {
return code, "", err
}
@@ -201,5 +209,5 @@ func (b Backend) ExecDocker(l Location, args []string) (int, string, error) {
}
docker = append(docker, flags.DOCKER_IMAGE, "-c", strings.Join(args, " "))
return ExecuteCommand(options, docker...)
return utils.ExecuteCommand(options, docker...)
}

View File

@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
@@ -13,9 +14,10 @@ import (
"strings"
"github.com/blang/semver/v4"
"github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/colors"
"github.com/cupcakearmy/autorestic/internal/flags"
"github.com/cupcakearmy/autorestic/internal/utils"
"github.com/cupcakearmy/autorestic/internal/version"
)
const INSTALL_PATH = "/usr/local/bin"
@@ -36,7 +38,7 @@ func dlJSON(url string) (GithubRelease, error) {
return parsed, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return parsed, err
@@ -72,10 +74,9 @@ func downloadAndInstallAsset(body GithubRelease, name string) error {
// Uncompress
bz := bzip2.NewReader(resp.Body)
// Save to tmp file in the same directory as the install directory
// Linux does not support overwriting the file that is currently being running
// But it can be delete the old one and a new one moved in its place.
tmp, err := os.CreateTemp(INSTALL_PATH, "autorestic-")
// Save to tmp
// 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.
tmp, err := ioutil.TempFile(os.TempDir(), "autorestic-")
if err != nil {
return err
}
@@ -89,24 +90,22 @@ func downloadAndInstallAsset(body GithubRelease, name string) error {
to := path.Join(INSTALL_PATH, name)
defer os.Remove(tmp.Name()) // Cleanup temporary file after thread exits
mode := os.FileMode(0755)
if originalBin, err := os.Lstat(to); err == nil {
mode = originalBin.Mode()
err := os.Remove(to)
if err != nil {
if err := os.Rename(tmp.Name(), to); err != nil {
colors.Error.Printf("os.Rename() failed (%v), retrying with io.Copy()\n", err.Error())
var src *os.File
var dst *os.File
if src, err = os.Open(tmp.Name()); err != nil {
return err
}
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
}
}
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)
@@ -117,7 +116,7 @@ func downloadAndInstallAsset(body GithubRelease, name string) error {
}
func InstallRestic() error {
installed := internal.CheckIfCommandIsCallable("restic")
installed := utils.CheckIfCommandIsCallable("restic")
if installed {
colors.Body.Println("restic already installed")
return nil
@@ -131,7 +130,7 @@ func InstallRestic() error {
}
func upgradeRestic() error {
_, _, err := internal.ExecuteCommand(internal.ExecuteOptions{
_, _, err := utils.ExecuteCommand(utils.ExecuteOptions{
Command: flags.RESTIC_BIN,
}, "self-update")
return err
@@ -149,7 +148,7 @@ func Upgrade(restic bool) error {
}
// Upgrade self
current, err := semver.ParseTolerant(internal.VERSION)
current, err := semver.ParseTolerant(version.VERSION)
if err != nil {
return err
}

View File

@@ -11,23 +11,20 @@ import (
"github.com/cupcakearmy/autorestic/internal/colors"
"github.com/cupcakearmy/autorestic/internal/flags"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/cupcakearmy/autorestic/internal/options"
"github.com/cupcakearmy/autorestic/internal/utils"
"github.com/joho/godotenv"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const VERSION = "1.8.0"
type OptionMap map[string][]interface{}
type Options map[string]OptionMap
type Config struct {
Version string `mapstructure:"version"`
Extras interface{} `mapstructure:"extras"`
Locations map[string]Location `mapstructure:"locations"`
Backends map[string]Backend `mapstructure:"backends"`
Global Options `mapstructure:"global"`
Global options.Options `mapstructure:"global"`
}
var once sync.Once
@@ -132,11 +129,10 @@ func (c *Config) Describe() {
tmp = ""
hooks := map[string][]string{
"PreValidate": l.Hooks.PreValidate,
"Before": l.Hooks.Before,
"After": l.Hooks.After,
"Failure": l.Hooks.Failure,
"Success": l.Hooks.Success,
"Before": l.Hooks.Before,
"After": l.Hooks.After,
"Failure": l.Hooks.Failure,
"Success": l.Hooks.Success,
}
for hook, commands := range hooks {
if len(commands) > 0 {
@@ -185,7 +181,7 @@ func CheckConfig() error {
if c == nil {
return fmt.Errorf("config could not be loaded/found")
}
if !CheckIfResticIsCallable() {
if !utils.CheckIfResticIsCallable() {
return fmt.Errorf(`%s was not found. Install either with "autorestic install" or manually`, flags.RESTIC_BIN)
}
for name, backend := range c.Backends {
@@ -264,7 +260,7 @@ func AddFlagsToCommand(cmd *cobra.Command, backend bool) {
func (c *Config) SaveConfig() error {
file := viper.ConfigFileUsed()
if err := CopyFile(file, file+".old"); err != nil {
if err := utils.CopyFile(file, file+".old"); err != nil {
return err
}
colors.Secondary.Println("Saved a backup copy of your file next to the original.")
@@ -275,40 +271,11 @@ func (c *Config) SaveConfig() error {
return viper.WriteConfig()
}
func optionToString(option string) string {
if !strings.HasPrefix(option, "-") {
return "--" + option
}
return option
}
func appendOptionsToSlice(str *[]string, options OptionMap) {
for key, values := range options {
for _, value := range values {
// Bool
asBool, ok := value.(bool)
if ok && asBool {
*str = append(*str, optionToString(key))
continue
}
*str = append(*str, optionToString(key), fmt.Sprint(value))
}
}
}
func getOptions(options Options, keys []string) []string {
var selected []string
for _, key := range keys {
appendOptionsToSlice(&selected, options[key])
}
return selected
}
func combineBackendOptions(key string, b Backend) []string {
// Priority: backend > global
var options []string
gFlags := getOptions(GetConfig().Global, []string{key})
bFlags := getOptions(b.Options, []string{"all", key})
gFlags := GetConfig().Global.GetOptions([]string{key})
bFlags := b.Options.GetOptions([]string{"all", key})
options = append(options, gFlags...)
options = append(options, bFlags...)
return options
@@ -317,9 +284,9 @@ func combineBackendOptions(key string, b Backend) []string {
func combineAllOptions(key string, l Location, b Backend) []string {
// Priority: location > backend > global
var options []string
gFlags := getOptions(GetConfig().Global, []string{key})
bFlags := getOptions(b.Options, []string{"all", key})
lFlags := getOptions(l.Options, []string{"all", key})
gFlags := GetConfig().Global.GetOptions([]string{key})
bFlags := b.Options.GetOptions([]string{"all", key})
lFlags := l.Options.GetOptions([]string{"all", key})
options = append(options, gFlags...)
options = append(options, bFlags...)
options = append(options, lFlags...)

View File

@@ -12,6 +12,8 @@ import (
"github.com/cupcakearmy/autorestic/internal/flags"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/cupcakearmy/autorestic/internal/metadata"
"github.com/cupcakearmy/autorestic/internal/utils"
"github.com/cupcakearmy/autorestic/internal/options"
"github.com/robfig/cron"
)
@@ -33,12 +35,11 @@ const (
)
type Hooks struct {
Dir string `mapstructure:"dir"`
PreValidate HookArray `mapstructure:"prevalidate,omitempty"`
Before HookArray `mapstructure:"before,omitempty"`
After HookArray `mapstructure:"after,omitempty"`
Success HookArray `mapstructure:"success,omitempty"`
Failure HookArray `mapstructure:"failure,omitempty"`
Dir string `mapstructure:"dir"`
Before HookArray `mapstructure:"before,omitempty"`
After HookArray `mapstructure:"after,omitempty"`
Success HookArray `mapstructure:"success,omitempty"`
Failure HookArray `mapstructure:"failure,omitempty"`
}
type LocationCopy = map[string][]string
@@ -50,7 +51,7 @@ type Location struct {
To []string `mapstructure:"to,omitempty"`
Hooks Hooks `mapstructure:"hooks,omitempty"`
Cron string `mapstructure:"cron,omitempty"`
Options Options `mapstructure:"options,omitempty"`
Options options.Options `mapstructure:"options,omitempty"`
ForgetOption LocationForgetOption `mapstructure:"forget,omitempty"`
CopyOption LocationCopy `mapstructure:"copy,omitempty"`
}
@@ -102,14 +103,14 @@ func (l Location) validate() error {
if _, ok := GetBackend(copyFrom); !ok {
return fmt.Errorf(`location "%s" has an invalid backend "%s" in copy option`, l.name, copyFrom)
}
if !ArrayContains(l.To, copyFrom) {
if !utils.ArrayContains(l.To, copyFrom) {
return fmt.Errorf(`location "%s" has an invalid copy from "%s"`, l.name, copyFrom)
}
for _, copyToTarget := range copyTo {
if _, ok := GetBackend(copyToTarget); !ok {
return fmt.Errorf(`location "%s" has an invalid backend "%s" in copy option`, l.name, copyToTarget)
}
if ArrayContains(l.To, copyToTarget) {
if utils.ArrayContains(l.To, copyToTarget) {
return fmt.Errorf(`location "%s" cannot copy to "%s" as it's already a target`, l.name, copyToTarget)
}
}
@@ -124,7 +125,7 @@ func (l Location) validate() error {
return nil
}
func (l Location) ExecuteHooks(commands []string, options ExecuteOptions) error {
func (l Location) ExecuteHooks(commands []string, options utils.ExecuteOptions) error {
if len(commands) == 0 {
return nil
}
@@ -138,7 +139,7 @@ func (l Location) ExecuteHooks(commands []string, options ExecuteOptions) error
colors.Secondary.Println("\nRunning hooks")
for _, command := range commands {
colors.Body.Println("> " + command)
_, out, err := ExecuteCommand(options, "-c", command)
_, out, err := utils.ExecuteCommand(options, "-c", command)
if err != nil {
colors.Error.Println(out)
return err
@@ -177,7 +178,7 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
return errors
}
cwd, _ := GetPathRelativeToConfig(".")
options := ExecuteOptions{
options := utils.ExecuteOptions{
Command: "bash",
Dir: cwd,
Envs: map[string]string{
@@ -185,18 +186,12 @@ 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 {
errors = append(errors, err)
goto after
}
// Hooks after location validation
// Hooks
if err := l.ExecuteHooks(l.Hooks.Before, options); err != nil {
errors = append(errors, err)
goto after
@@ -228,8 +223,9 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
cmd = append(cmd, "--tag", buildTag("cron"))
}
cmd = append(cmd, "--tag", l.getLocationTags())
backupOptions := ExecuteOptions{
backupOptions := utils.ExecuteOptions{
Envs: env,
Global: GetConfig().Global,
}
var code int = 0
@@ -244,9 +240,9 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
}
cmd = append(cmd, path)
}
code, out, err = ExecuteResticCommand(backupOptions, cmd...)
code, out, err = utils.ExecuteResticCommand(backupOptions, cmd...)
case TypeVolume:
ok := CheckIfVolumeExists(l.From[0])
ok := utils.CheckIfVolumeExists(l.From[0])
if !ok {
errors = append(errors, fmt.Errorf("volume \"%s\" does not exist", l.From[0]))
continue
@@ -284,8 +280,9 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
for k, v := range env2 {
env[k+"2"] = v
}
_, _, err := ExecuteResticCommand(ExecuteOptions{
_, _, err := utils.ExecuteResticCommand(utils.ExecuteOptions{
Envs: env,
Global: GetConfig().Global,
}, "copy", md.SnapshotID)
if err != nil {
@@ -296,13 +293,12 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
}
}
// After backup hooks
// After hooks
if err := l.ExecuteHooks(l.Hooks.After, options); err != nil {
errors = append(errors, err)
}
after:
// Success/failure hooks
var commands []string
var isSuccess = len(errors) == 0
if isSuccess {
@@ -343,8 +339,9 @@ func (l Location) Forget(prune bool, dry bool) error {
if err != nil {
return nil
}
options := ExecuteOptions{
options := utils.ExecuteOptions{
Envs: env,
Global: GetConfig().Global,
}
cmd := []string{"forget", "--tag", l.getLocationTags()}
if prune {
@@ -354,7 +351,7 @@ func (l Location) Forget(prune bool, dry bool) error {
cmd = append(cmd, "--dry-run")
}
cmd = append(cmd, combineAllOptions("forget", l, backend)...)
_, _, err = ExecuteResticCommand(options, cmd...)
_, _, err = utils.ExecuteResticCommand(options, cmd...)
if err != nil {
return err
}

View File

@@ -0,0 +1,38 @@
package options
import (
"fmt"
"strings"
)
type OptionMap map[string][]interface{}
type Options map[string]OptionMap
func (o Options) GetOptions(keys []string) []string {
var selected []string
for _, key := range keys {
o[key].AppendOptionsToSlice(&selected)
}
return selected
}
func (m OptionMap) AppendOptionsToSlice(str *[]string) {
for key, values := range m {
for _, value := range values {
// Bool
asBool, ok := value.(bool)
if ok && asBool {
*str = append(*str, optionToString(key))
continue
}
*str = append(*str, optionToString(key), fmt.Sprint(value))
}
}
}
func optionToString(option string) string {
if !strings.HasPrefix(option, "-") {
return "--" + option
}
return option
}

View File

@@ -1,4 +1,4 @@
package internal
package utils
import (
"bytes"
@@ -9,6 +9,7 @@ import (
"github.com/cupcakearmy/autorestic/internal/colors"
"github.com/cupcakearmy/autorestic/internal/flags"
"github.com/cupcakearmy/autorestic/internal/options"
"github.com/fatih/color"
)
@@ -26,6 +27,7 @@ type ExecuteOptions struct {
Envs map[string]string
Dir string
Silent bool
Global options.Options
}
type ColoredWriter struct {
@@ -78,8 +80,7 @@ func ExecuteCommand(options ExecuteOptions, args ...string) (int, string, error)
func ExecuteResticCommand(options ExecuteOptions, args ...string) (int, string, error) {
options.Command = flags.RESTIC_BIN
var c = GetConfig()
var optionsAsString = getOptions(c.Global, []string{"all"})
var optionsAsString = options.Global.GetOptions([]string{"all"})
args = append(optionsAsString, args...)
return ExecuteCommand(options, args...)
}

View File

@@ -0,0 +1,3 @@
package version
const VERSION = "1.7.7"

View File

@@ -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 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
distributed under the License is distributed on an "AS IS" BASIS,