Skip to content

Commit

Permalink
add --format flag to specify the config format
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Jun 30, 2020
1 parent 1006fcf commit d2ebc7b
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 13 deletions.
115 changes: 108 additions & 7 deletions README.md
Expand Up @@ -19,13 +19,13 @@ With resticprofile:
* You can run a shell command if an error occurred (at any time)
* You can send a backup stream via _stdin_
* You can start restic at a lower or higher priority (Priority Class in Windows, *nice* in all unixes) and/or _ionice_ (only available on Linux)
* Check you have enough memory before starting a backup. I've had some backups that literally killed a server (with swap disabled)
* Check you have enough memory before starting a backup. (I've had some backups that literally killed a server with swap disabled)

The configuration file accepts various formats:
* [TOML](https://github.com/toml-lang/toml) : configuration file with extension _.toml_ and _.conf_ to keep compatibility with versions before 0.6.0
* [JSON](https://en.wikipedia.org/wiki/JSON) : configuration file with extension _.json_
* [YAML](https://en.wikipedia.org/wiki/YAML) : configuration file with extension _.yaml_
* HCL: **experimental support**, configuration file with extension _.hcl_
* [HCL](https://github.com/hashicorp/hcl): **experimental support**, configuration file with extension _.hcl_

For the rest of the documentation, I'll be showing examples using the TOML file configuration format (because it was the only one supported before version 0.6.0) but you can pick your favourite: they all work with resticprofile :-)

Expand All @@ -37,9 +37,9 @@ It's been actively tested on macOS X and Linux, and regularly tested on Windows.

**This is at _beta_ stage. Please don't use it in production yet. Even though I'm using it on my servers, I cannot guarantee all combinations of configuration are going to work properly for you.**

## Installation
## Installation (macOS, Linux & other unixes)

Here's a simple script to download the binary automatically for you. It works on mac OS X, FreeBSD, OpenBSD and Linux:
Here's a simple script to download the binary automatically for you. It works on mac OS X, FreeBSD, OpenBSD and Linux. It should also work in Windows if you start it from any `bash` command line (WSL, git bash, etc.):

```
$ curl -sfL https://raw.githubusercontent.com/creativeprojects/resticprofile/master/install.sh | sh
Expand All @@ -58,6 +58,16 @@ $ sudo ./install.sh -b /usr/local/bin
It will install resticprofile in `/usr/local/bin/`


### Installation for Windows using bash

You can use the same script if you're using bash in Windows (via WSL, git bash, etc.)

```
$ curl -LO https://raw.githubusercontent.com/creativeprojects/resticprofile/master/install.sh
$ ./install.sh
```
It will create a `bin` directory under your current directory and place `resticprofile.exe` in it.

### Manual installation (Windows)

- Download the package corresponding to your system and CPU from the [release page](https://github.com/creativeprojects/resticprofile/releases)
Expand Down Expand Up @@ -440,7 +450,8 @@ Usage of resticprofile:
resticprofile [resticprofile flags] [command] [restic flags]
resticprofile flags:
-c, --config string configuration file (default "profiles.conf")
-c, --config string configuration file (default "profiles")
-f, --format string file format of the configuration (default is to use the file extension)
-h, --help display this help
-n, --name string profile name (default "default")
--no-ansi disable ansi control characters (disable console colouring)
Expand All @@ -464,6 +475,7 @@ There are not many options on the command line, most of the options are in the c

* **[-h]**: Display quick help
* **[-c | --config] configuration_file**: Specify a configuration file other than the default
* **[-f | --format] configuration_format**: Specify the configuration file format: `toml`, `yaml`, `json` or `hcl`
* **[-n | --name] profile_name**: Profile section to use from the configuration file
* **[-q | --quiet]**: Force resticprofile and restic to be quiet (override any configuration from the profile)
* **[-v | --verbose]**: Force resticprofile and restic to be verbose (override any configuration from the profile)
Expand All @@ -474,9 +486,9 @@ There are not many options on the command line, most of the options are in the c
## Minimum memory required

restic can be memory hungry. I'm running a few servers with no swap (I know: it is _bad_) and I managed to kill some of them during a backup.
Anyway, for that matter I've introduced a parameter in the `global` section called `min-memory`. The **default value is 100MB**. You can disable it by using a value of `0`.
For that matter I've introduced a parameter in the `global` section called `min-memory`. The **default value is 100MB**. You can disable it by using a value of `0`.

It compares against `(total - used)` which is probably the best way to know how much memory is available (that is including the disk buffers/cache).
It compares against `(total - used)` which is probably the best way to know how much memory is available (that is including the memory used for disk buffers/cache).

## Configuration file reference

Expand Down Expand Up @@ -751,6 +763,95 @@ stdin:

```

Also here's an example of a configuration file in HCL:
``` hcl
global {
priority = "low"
ionice = true
ionice-class = 2
ionice-level = 6
# don't start if the memory available is < 1000MB
min-memory = 1000
}
groups {
all = ["src", "self"]
}
default {
repository = "/tmp/backup"
password-file = "key"
run-before = "echo Profile started!"
run-after = "echo Profile finished!"
run-after-fail = "echo An error occured!"
}
src {
inherit = "default"
initialize = true
lock = "/tmp/backup/resticprofile-profile-src.lock"
snapshots = {
tag = [ "test", "dev" ]
}
backup = {
run-before = [ "echo Starting!", "ls -al ~/go/src" ]
run-after = "echo All Done!"
exclude = [ "/**/.git" ]
exclude-caches = true
tag = [ "test", "dev" ]
source = [ "~/go/src" ]
check-before = true
}
retention = {
before-backup = false
after-backup = true
keep-last = 3
compact = false
prune = true
}
check = {
check-unused = true
with-cache = false
}
}
self {
inherit = "default"
initialize = false
snapshots = {
tag = [ "self" ]
}
backup = {
source = "./"
tag = [ "self" ]
}
}
# sending stream through stdin
stdin = {
inherit = "default"
snapshots = {
tag = [ "stdin" ]
}
backup = {
stdin = true
stdin-filename = "stdin-test"
tag = [ "stdin" ]
}
}
```

## Using resticprofile and systemd

systemd is a common service manager in use by many Linux distributions.
Expand Down
12 changes: 8 additions & 4 deletions config/config.go
Expand Up @@ -60,10 +60,14 @@ func newConfig(format string) *Config {
}

// LoadFile loads configuration from file
func LoadFile(configFile string) (*Config, error) {
format := filepath.Ext(configFile)
if strings.HasPrefix(format, ".") {
format = format[1:]
// Leave format blank for auto-detection from the file extension
func LoadFile(configFile, format string) (*Config, error) {
if format == "" {
// use file extension as format
format = filepath.Ext(configFile)
if strings.HasPrefix(format, ".") {
format = format[1:]
}
}
file, err := os.Open(configFile)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions flags.go
Expand Up @@ -13,6 +13,7 @@ type commandLineFlags struct {
quiet bool
verbose bool
config string
format string
name string
noAnsi bool
theme string
Expand Down Expand Up @@ -40,10 +41,13 @@ func loadFlags() (*pflag.FlagSet, commandLineFlags) {
flagset.BoolVarP(&flags.quiet, "quiet", "q", constants.DefaultQuietFlag, "display only warnings and errors")
flagset.BoolVarP(&flags.verbose, "verbose", "v", constants.DefaultVerboseFlag, "display all debugging information")
flagset.StringVarP(&flags.config, "config", "c", constants.DefaultConfigurationFile, "configuration file")
flagset.StringVarP(&flags.format, "format", "f", "", "file format of the configuration (default is to use the file extension)")
flagset.StringVarP(&flags.name, "name", "n", constants.DefaultProfileName, "profile name")

flagset.BoolVar(&flags.noAnsi, "no-ansi", false, "disable ansi control characters (disable console colouring)")
flagset.StringVar(&flags.theme, "theme", constants.DefaultTheme, "console colouring theme (dark, light, none)")

// Deprecated since 0.7.0
flagset.BoolVar(&flags.selfUpdate, "self-update", false, "auto update of resticprofile (does not update restic)")
_ = flagset.MarkHidden("self-update")

Expand Down
4 changes: 2 additions & 2 deletions main.go
Expand Up @@ -21,7 +21,7 @@ import (

// These fields are populated by the goreleaser build
var (
version = "0.8.0"
version = "0.8.1"
commit = ""
date = ""
builtBy = ""
Expand Down Expand Up @@ -73,7 +73,7 @@ func main() {
}
clog.Infof("using configuration file: %s", configFile)

c, err := config.LoadFile(configFile)
c, err := config.LoadFile(configFile, flags.format)
if err != nil {
clog.Error("cannot load configuration file:", err)
os.Exit(1)
Expand Down

0 comments on commit d2ebc7b

Please sign in to comment.