Compare commits
36 Commits
push-gatew
...
main
Author | SHA1 | Date |
---|---|---|
IamTheFij | 4b16dea34e | |
IamTheFij | 7b313b8f9b | |
IamTheFij | bea338c27a | |
IamTheFij | 1d0d6b3fe6 | |
IamTheFij | 985572d737 | |
IamTheFij | cff06cd1c6 | |
IamTheFij | 90cd0ec9e0 | |
IamTheFij | a0db27be1a | |
IamTheFij | cddc290ee0 | |
IamTheFij | d049228980 | |
IamTheFij | 390074e048 | |
IamTheFij | e0542a68e5 | |
IamTheFij | 4ddcea9f7d | |
IamTheFij | a2823e09ad | |
IamTheFij | b1fe2537e2 | |
IamTheFij | f3ecabf4fe | |
IamTheFij | 4c7baa46a7 | |
IamTheFij | 1d6957f45f | |
IamTheFij | 0de267a4cf | |
IamTheFij | 28f081c8d0 | |
IamTheFij | 9cdf37c680 | |
IamTheFij | 84095f9875 | |
IamTheFij | 03cc5e8fa6 | |
IamTheFij | 129dc75d4c | |
IamTheFij | f32b5e4afa | |
IamTheFij | a19ad94027 | |
IamTheFij | 8b9844465c | |
IamTheFij | 95fea5ef30 | |
IamTheFij | 8a8bc23376 | |
IamTheFij | fed9224c17 | |
IamTheFij | 47f1c14c48 | |
IamTheFij | bd3151e6f8 | |
IamTheFij | 3e550d29ca | |
IamTheFij | 0074cced26 | |
IamTheFij | 9c5d597ab4 | |
IamTheFij | 57e933c20d |
|
@ -4,7 +4,7 @@ name: test
|
|||
|
||||
steps:
|
||||
- name: test
|
||||
image: golang:1.17
|
||||
image: golang:1.21
|
||||
environment:
|
||||
VERSION: ${DRONE_TAG:-${DRONE_COMMIT}}
|
||||
commands:
|
||||
|
@ -13,7 +13,7 @@ steps:
|
|||
- make test
|
||||
|
||||
- name: check
|
||||
image: iamthefij/drone-pre-commit:personal
|
||||
image: iamthefij/drone-pre-commit@sha256:30fa17489b86d7a4c3ad9c3ce2e152c25d82b8671e5609d322c6cae0baed89cd
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
|
@ -32,7 +32,7 @@ trigger:
|
|||
|
||||
steps:
|
||||
- name: build all binaries
|
||||
image: golang:1.17
|
||||
image: golang:1.21
|
||||
environment:
|
||||
VERSION: ${DRONE_TAG:-${DRONE_COMMIT}}
|
||||
commands:
|
||||
|
|
|
@ -20,4 +20,9 @@ dist/
|
|||
|
||||
# Built executable
|
||||
restic-scheduler
|
||||
resticscheduler
|
||||
data/
|
||||
|
||||
# Itest temp dirs
|
||||
itest/data
|
||||
itest/repo
|
||||
|
|
|
@ -16,7 +16,6 @@ linters:
|
|||
- contextcheck
|
||||
- cyclop
|
||||
- decorder
|
||||
- depguard
|
||||
- dupl
|
||||
- durationcheck
|
||||
- errchkjson
|
||||
|
@ -32,10 +31,8 @@ linters:
|
|||
- gocognit
|
||||
- goconst
|
||||
- gocritic
|
||||
# - gocyclo # Using cyclop
|
||||
- godot
|
||||
# - goerr113 # Using errorlint
|
||||
- gofmt
|
||||
- gofumpt
|
||||
- goheader
|
||||
- goimports
|
||||
- gomnd
|
||||
|
@ -45,7 +42,6 @@ linters:
|
|||
- gosec
|
||||
- grouper
|
||||
- importas
|
||||
# - ireturn
|
||||
- lll
|
||||
- maintidx
|
||||
- makezero
|
||||
|
@ -60,11 +56,9 @@ linters:
|
|||
- paralleltest
|
||||
- prealloc
|
||||
- predeclared
|
||||
# - promlinter # Not common enough
|
||||
- revive
|
||||
- rowserrcheck
|
||||
- sqlclosecheck
|
||||
# - stylecheck # Using revive
|
||||
- tagliatelle
|
||||
- tenv
|
||||
- testpackage
|
||||
|
@ -72,27 +66,11 @@ linters:
|
|||
- tparallel
|
||||
- unconvert
|
||||
- unparam
|
||||
- varnamelen
|
||||
- wastedassign
|
||||
- whitespace
|
||||
- wrapcheck
|
||||
- wsl
|
||||
|
||||
disable:
|
||||
- gochecknoglobals
|
||||
- godox
|
||||
- forbidigo
|
||||
# Deprecated
|
||||
- golint
|
||||
- interfacer
|
||||
- maligned
|
||||
- scopelint
|
||||
- ifshort
|
||||
- varcheck
|
||||
- structcheck
|
||||
- deadcode
|
||||
- exhaustivestruct
|
||||
|
||||
linters-settings:
|
||||
gomnd:
|
||||
settings:
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v3.4.0
|
||||
rev: v4.5.0
|
||||
hooks:
|
||||
- id: check-added-large-files
|
||||
- id: check-yaml
|
||||
|
@ -11,10 +11,8 @@ repos:
|
|||
- id: end-of-file-fixer
|
||||
- id: check-merge-conflict
|
||||
- repo: https://github.com/dnephin/pre-commit-golang
|
||||
rev: v0.4.0
|
||||
rev: v0.5.1
|
||||
hooks:
|
||||
- id: go-fmt
|
||||
- id: go-imports
|
||||
- id: golangci-lint
|
||||
args:
|
||||
- --timeout=3m
|
||||
|
|
19
Dockerfile
19
Dockerfile
|
@ -1,18 +1,23 @@
|
|||
FROM alpine:3
|
||||
FROM alpine:3.18
|
||||
|
||||
RUN apk add --no-cache \
|
||||
bash~=5 \
|
||||
consul~=1.12 \
|
||||
mariadb-client~=10.6 \
|
||||
mariadb-connector-c~=3.1 \
|
||||
rclone~=1.58 \
|
||||
redis~=7.0 \
|
||||
restic~=0.13 \
|
||||
consul~=1 \
|
||||
mariadb-client~=10 \
|
||||
mariadb-connector-c~=3 \
|
||||
nomad~=1 \
|
||||
postgresql15-client~=15 \
|
||||
rclone~=1.62 \
|
||||
redis~=7 \
|
||||
restic~=0.15 \
|
||||
sqlite~=3 \
|
||||
tzdata~=2023 \
|
||||
;
|
||||
|
||||
ARG TARGETOS
|
||||
ARG TARGETARCH
|
||||
COPY ./dist/resticscheduler-$TARGETOS-$TARGETARCH /bin/resticscheduler
|
||||
|
||||
HEALTHCHECK CMD ["wget", "-O", "-", "http://localhost:8080/health"]
|
||||
|
||||
ENTRYPOINT [ "/bin/resticscheduler" ]
|
||||
|
|
8
Makefile
8
Makefile
|
@ -2,7 +2,7 @@ APP_NAME = resticscheduler
|
|||
VERSION ?= $(shell git describe --tags --dirty)
|
||||
GOFILES = *.go
|
||||
# Multi-arch targets are generated from this
|
||||
TARGET_ALIAS = $(APP_NAME)-linux-amd64 $(APP_NAME)-linux-arm $(APP_NAME)-linux-arm64 $(APP_NAME)-darwin-amd64 $(APP_NAME)-darwin-arm64
|
||||
TARGET_ALIAS = $(APP_NAME)-linux-amd64 $(APP_NAME)-linux-arm $(APP_NAME)-linux-arm64
|
||||
TARGETS = $(addprefix dist/,$(TARGET_ALIAS))
|
||||
.QUOTE = "
|
||||
CURRENT_GOARCH = $(shell go env | awk -F "=" '/GOARCH/ { gsub(/$(.QUOTE)/,"", $$2); print $$2}')
|
||||
|
@ -29,9 +29,13 @@ build: $(APP_NAME)
|
|||
# Run all tests
|
||||
.PHONY: test
|
||||
test:
|
||||
go test -coverprofile=coverage.out # -short
|
||||
go test -v -coverprofile=coverage.out # -short
|
||||
go tool cover -func=coverage.out
|
||||
|
||||
.PHONY: itest
|
||||
itest: docker-build
|
||||
./itest/run.sh
|
||||
|
||||
# Installs pre-commit hooks
|
||||
.PHONY: install-hooks
|
||||
install-hooks:
|
||||
|
|
18
go.mod
18
go.mod
|
@ -1,11 +1,11 @@
|
|||
module git.iamthefij.com/iamthefij/restic-scheduler
|
||||
|
||||
go 1.17
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/go-test/deep v1.0.8
|
||||
github.com/hashicorp/hcl/v2 v2.11.1
|
||||
github.com/prometheus/client_golang v1.12.1
|
||||
github.com/prometheus/client_golang v1.14.0
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/zclconf/go-cty v1.8.0
|
||||
)
|
||||
|
@ -16,13 +16,13 @@ require (
|
|||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/google/go-cmp v0.5.5 // indirect
|
||||
github.com/google/go-cmp v0.5.8 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
|
||||
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
|
||||
github.com/prometheus/client_model v0.2.0 // indirect
|
||||
github.com/prometheus/common v0.32.1 // indirect
|
||||
github.com/prometheus/procfs v0.7.3 // indirect
|
||||
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
|
||||
golang.org/x/text v0.3.6 // indirect
|
||||
google.golang.org/protobuf v1.26.0 // indirect
|
||||
github.com/prometheus/client_model v0.3.0 // indirect
|
||||
github.com/prometheus/common v0.37.0 // indirect
|
||||
github.com/prometheus/procfs v0.8.0 // indirect
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
google.golang.org/protobuf v1.28.1 // indirect
|
||||
)
|
||||
|
|
34
go.sum
34
go.sum
|
@ -71,9 +71,11 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2
|
|||
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
|
||||
github.com/go-kit/log v0.2.0/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
|
||||
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
|
||||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
|
||||
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
|
||||
|
@ -118,8 +120,9 @@ github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
|||
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
|
||||
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
|
||||
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
|
||||
|
@ -177,24 +180,28 @@ github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP
|
|||
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
|
||||
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
|
||||
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
|
||||
github.com/prometheus/client_golang v1.12.1 h1:ZiaPsmm9uiBeaSMRznKsCDNtPCS0T3JVDGF+06gjBzk=
|
||||
github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY=
|
||||
github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw=
|
||||
github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y=
|
||||
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
|
||||
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
|
||||
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
|
||||
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
|
||||
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
|
||||
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
|
||||
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
|
||||
github.com/prometheus/common v0.32.1 h1:hWIdL3N2HoUx3B8j3YN9mWor0qhY/NlEKZEaXxuIRh4=
|
||||
github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
|
||||
github.com/prometheus/common v0.37.0 h1:ccBbHCgIiT9uSoFY0vX8H3zsNR5eLt17/RQLUvn8pXE=
|
||||
github.com/prometheus/common v0.37.0/go.mod h1:phzohg0JFMnBEFGxTDbfu3QyL5GI8gTQJFhYO5B3mfA=
|
||||
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
||||
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
|
||||
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
|
||||
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
|
||||
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
|
||||
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
|
||||
github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo=
|
||||
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
|
||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
|
@ -291,12 +298,15 @@ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81R
|
|||
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
|
||||
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
|
@ -306,6 +316,7 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
|
|||
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
|
@ -342,17 +353,22 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
|
@ -399,7 +415,6 @@ golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc
|
|||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
|
||||
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
|
||||
|
@ -475,8 +490,9 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
|
|||
google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4=
|
||||
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
|
||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
|
||||
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
#! /bin/sh
|
||||
set -ex
|
||||
|
||||
# Create flat file
|
||||
echo "Hello" > /data/test.txt
|
||||
|
||||
# Create Sqlite database
|
||||
touch /data/test_database.db
|
||||
sqlite3 /data/test_database.db <<-EOF
|
||||
CREATE TABLE test_table (
|
||||
id INTEGER PRIMARY KEY,
|
||||
data TEXT NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO test_table(data)
|
||||
VALUES ("Test row");
|
||||
EOF
|
||||
|
||||
# Create MySql database
|
||||
until mysql --host "$MYSQL_HOST" --user "$MYSQL_USER" --password="$MYSQL_PWD" --execute "SHOW DATABASES;"; do
|
||||
sleep 1
|
||||
done
|
||||
mysql --host "$MYSQL_HOST" --user "$MYSQL_USER" --password="$MYSQL_PWD" main <<EOF
|
||||
CREATE TABLE test_table (
|
||||
id INTEGER AUTO_INCREMENT PRIMARY KEY,
|
||||
data TEXT NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO test_table(data)
|
||||
VALUES ("Test row");
|
||||
EOF
|
||||
|
||||
# Create Postgres database
|
||||
export PGPASSWORD="$PGSQL_PASS"
|
||||
until psql --host "$PGSQL_HOST" --username "$PGSQL_USER" --command "SELECT datname FROM pg_database;"; do
|
||||
sleep 1
|
||||
done
|
||||
psql -v ON_ERROR_STOP=1 --host "$PGSQL_HOST" --username "$PGSQL_USER" main <<EOF
|
||||
CREATE TABLE test_table (
|
||||
id SERIAL PRIMARY KEY,
|
||||
data TEXT NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO test_table(data)
|
||||
VALUES ('Test row');
|
||||
EOF
|
|
@ -0,0 +1,57 @@
|
|||
---
|
||||
version: "3.9"
|
||||
|
||||
services:
|
||||
mysql:
|
||||
image: mysql
|
||||
environment:
|
||||
MYSQL_ROOT_PASSWORD: shhh
|
||||
MYSQL_DATABASE: main
|
||||
|
||||
postgres:
|
||||
image: postgres
|
||||
environment:
|
||||
POSTGRES_PASSWORD: shhh
|
||||
POSTGRES_DB: main
|
||||
|
||||
bootstrap:
|
||||
image: resticscheduler
|
||||
entrypoint: /bootstrap-tests.sh
|
||||
environment:
|
||||
MYSQL_HOST: mysql
|
||||
MYSQL_USER: root
|
||||
MYSQL_PWD: shhh
|
||||
PGSQL_HOST: postgres
|
||||
PGSQL_USER: postgres
|
||||
PGSQL_PASS: shhh
|
||||
volumes:
|
||||
- ./bootstrap-tests.sh:/bootstrap-tests.sh
|
||||
- ./data:/data
|
||||
|
||||
main:
|
||||
image: resticscheduler
|
||||
environment:
|
||||
MYSQL_HOST: mysql
|
||||
MYSQL_USER: root
|
||||
MYSQL_PWD: shhh
|
||||
PGSQL_HOST: postgres
|
||||
PGSQL_USER: postgres
|
||||
PGSQL_PASS: shhh
|
||||
volumes:
|
||||
- ./repo:/repo
|
||||
- ./data:/data
|
||||
- ./test-backup.hcl:/test-backup.hcl
|
||||
|
||||
validate:
|
||||
image: resticscheduler
|
||||
entrypoint: /validate-tests.sh
|
||||
environment:
|
||||
MYSQL_HOST: mysql
|
||||
MYSQL_USER: root
|
||||
MYSQL_PWD: shhh
|
||||
PGSQL_HOST: postgres
|
||||
PGSQL_USER: postgres
|
||||
PGSQL_PASS: shhh
|
||||
volumes:
|
||||
- ./validate-tests.sh:/validate-tests.sh
|
||||
- ./data:/data
|
|
@ -0,0 +1,35 @@
|
|||
#! /bin/bash
|
||||
set -ex
|
||||
|
||||
cd "$(dirname "$0")"
|
||||
mkdir -p ./repo ./data
|
||||
|
||||
echo Clean everything
|
||||
docker-compose down -v
|
||||
rm -fr ./repo/* ./data/*
|
||||
sleep 5
|
||||
|
||||
echo Boostrap databases and data
|
||||
docker-compose up -d mysql postgres
|
||||
docker-compose run bootstrap
|
||||
sleep 1
|
||||
|
||||
echo Run backup job
|
||||
docker-compose run main -backup IntegrationTest -once /test-backup.hcl
|
||||
|
||||
echo Clean data
|
||||
docker-compose down -v
|
||||
docker-compose up -d mysql postgres
|
||||
rm -fr ./data/*
|
||||
sleep 15
|
||||
|
||||
echo Run restore
|
||||
docker-compose run main -restore IntegrationTest -once /test-backup.hcl
|
||||
sleep 1
|
||||
|
||||
echo Validate data
|
||||
docker-compose run validate
|
||||
|
||||
echo Clean all again
|
||||
docker-compose down -v
|
||||
rm -fr ./repo/* ./data/*
|
|
@ -0,0 +1,38 @@
|
|||
job "IntegrationTest" {
|
||||
schedule = "@daily"
|
||||
|
||||
config {
|
||||
repo = "/repo"
|
||||
passphrase = "shh"
|
||||
}
|
||||
|
||||
mysql "MySQL" {
|
||||
hostname = env("MYSQL_HOST")
|
||||
database = "main"
|
||||
username = env("MYSQL_USER")
|
||||
password = env("MYSQL_PWD")
|
||||
dump_to = "/tmp/mysql.sql"
|
||||
}
|
||||
|
||||
postgres "Postgres" {
|
||||
hostname = env("PGSQL_HOST")
|
||||
database = "main"
|
||||
username = env("PGSQL_USER")
|
||||
password = env("PGSQL_PASS")
|
||||
create = true
|
||||
dump_to = "/tmp/psql.sql"
|
||||
}
|
||||
|
||||
sqlite "SQLite" {
|
||||
path = "/data/test_database.db"
|
||||
dump_to = "/data/test_database.db.bak"
|
||||
}
|
||||
|
||||
backup {
|
||||
paths = ["/data"]
|
||||
|
||||
restore_opts {
|
||||
Target = "/"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#! /bin/sh
|
||||
set -ex
|
||||
|
||||
# Check flat file
|
||||
test -f /data/test.txt
|
||||
grep "^Hello" /data/test.txt
|
||||
|
||||
# Check Sqlite database
|
||||
test -f /data/test_database.db
|
||||
sqlite3 /data/test_database.db "select data from test_table where id = 1" | grep "^Test row"
|
||||
|
||||
# Check MySql database
|
||||
mysql --host "$MYSQL_HOST" --user "$MYSQL_USER" --password="$MYSQL_PWD" main <<EOF | grep "^Test row"
|
||||
select data from test_table where id = 1;
|
||||
EOF
|
||||
|
||||
# Check Postgres database
|
||||
export PGPASSWORD="$PGSQL_PASS"
|
||||
psql --host "$PGSQL_HOST" --user "$PGSQL_USER" main <<EOF | grep "Test row"
|
||||
select data from test_table where id = 1;
|
||||
EOF
|
79
job.go
79
job.go
|
@ -52,14 +52,17 @@ func (r ResticConfig) Validate() error {
|
|||
type Job struct {
|
||||
Name string `hcl:"name,label"`
|
||||
Schedule string `hcl:"schedule"`
|
||||
Config ResticConfig `hcl:"config,block"`
|
||||
Config *ResticConfig `hcl:"config,block"`
|
||||
Tasks []JobTask `hcl:"task,block"`
|
||||
Backup BackupFilesTask `hcl:"backup,block"`
|
||||
Forget *ForgetOpts `hcl:"forget,block"`
|
||||
|
||||
// Meta Tasks
|
||||
MySQL []JobTaskMySQL `hcl:"mysql,block"`
|
||||
Sqlite []JobTaskSqlite `hcl:"sqlite,block"`
|
||||
// NOTE: Now that these are also available within a task
|
||||
// these could be removed to make task order more obvious
|
||||
MySQL []JobTaskMySQL `hcl:"mysql,block"`
|
||||
Postgres []JobTaskPostgres `hcl:"postgres,block"`
|
||||
Sqlite []JobTaskSqlite `hcl:"sqlite,block"`
|
||||
|
||||
// Metrics and health
|
||||
healthy bool
|
||||
|
@ -73,6 +76,24 @@ func (j Job) validateTasks() error {
|
|||
}
|
||||
}
|
||||
|
||||
for _, mysql := range j.MySQL {
|
||||
if err := mysql.Validate(); err != nil {
|
||||
return fmt.Errorf("job %s has an invalid task: %w", j.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, pg := range j.Postgres {
|
||||
if err := pg.Validate(); err != nil {
|
||||
return fmt.Errorf("job %s has an invalid task: %w", j.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, sqlite := range j.Sqlite {
|
||||
if err := sqlite.Validate(); err != nil {
|
||||
return fmt.Errorf("job %s has an invalid task: %w", j.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -82,7 +103,11 @@ func (j Job) Validate() error {
|
|||
}
|
||||
|
||||
if _, err := cron.ParseStandard(j.Schedule); err != nil {
|
||||
return fmt.Errorf("job %s has an invalid schedule: %v: %w", j.Name, err, ErrInvalidConfigValue)
|
||||
return fmt.Errorf("job %s has an invalid schedule: %w: %w", j.Name, err, ErrInvalidConfigValue)
|
||||
}
|
||||
|
||||
if j.Config == nil {
|
||||
return fmt.Errorf("job %s is missing restic config: %w", j.Name, ErrMissingField)
|
||||
}
|
||||
|
||||
if err := j.Config.Validate(); err != nil {
|
||||
|
@ -93,18 +118,6 @@ func (j Job) Validate() error {
|
|||
return err
|
||||
}
|
||||
|
||||
for _, mysql := range j.MySQL {
|
||||
if err := mysql.Validate(); err != nil {
|
||||
return fmt.Errorf("job %s has an invalid task: %w", j.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, sqlite := range j.Sqlite {
|
||||
if err := sqlite.Validate(); err != nil {
|
||||
return fmt.Errorf("job %s has an invalid task: %w", j.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := j.Backup.Validate(); err != nil {
|
||||
return fmt.Errorf("job %s has an invalid backup config: %w", j.Name, err)
|
||||
}
|
||||
|
@ -120,6 +133,10 @@ func (j Job) AllTasks() []ExecutableTask {
|
|||
allTasks = append(allTasks, mysql.GetPreTask())
|
||||
}
|
||||
|
||||
for _, pg := range j.Postgres {
|
||||
allTasks = append(allTasks, pg.GetPreTask())
|
||||
}
|
||||
|
||||
for _, sqlite := range j.Sqlite {
|
||||
allTasks = append(allTasks, sqlite.GetPreTask())
|
||||
}
|
||||
|
@ -140,6 +157,10 @@ func (j Job) AllTasks() []ExecutableTask {
|
|||
allTasks = append(allTasks, mysql.GetPostTask())
|
||||
}
|
||||
|
||||
for _, pg := range j.Postgres {
|
||||
allTasks = append(allTasks, pg.GetPostTask())
|
||||
}
|
||||
|
||||
for _, sqlite := range j.Sqlite {
|
||||
allTasks = append(allTasks, sqlite.GetPostTask())
|
||||
}
|
||||
|
@ -154,6 +175,10 @@ func (j Job) BackupPaths() []string {
|
|||
paths = append(paths, t.DumpToPath)
|
||||
}
|
||||
|
||||
for _, t := range j.Postgres {
|
||||
paths = append(paths, t.DumpToPath)
|
||||
}
|
||||
|
||||
for _, t := range j.Sqlite {
|
||||
paths = append(paths, t.DumpToPath)
|
||||
}
|
||||
|
@ -197,7 +222,7 @@ func (j Job) Logger() *log.Logger {
|
|||
return GetLogger(j.Name)
|
||||
}
|
||||
|
||||
func (j Job) RunRestore() error {
|
||||
func (j Job) RunRestore(snapshot string) error {
|
||||
logger := j.Logger()
|
||||
restic := j.NewRestic()
|
||||
|
||||
|
@ -213,6 +238,10 @@ func (j Job) RunRestore() error {
|
|||
Env: nil,
|
||||
}
|
||||
|
||||
if backupTask, ok := exTask.(BackupFilesTask); ok {
|
||||
backupTask.snapshot = snapshot
|
||||
}
|
||||
|
||||
if err := exTask.RunRestore(taskCfg); err != nil {
|
||||
return fmt.Errorf("failed running job %s: %w", j.Name, err)
|
||||
}
|
||||
|
@ -251,8 +280,10 @@ func (j Job) Run() {
|
|||
result.LastError = err
|
||||
} else {
|
||||
Metrics.SnapshotCurrentCount.WithLabelValues(j.Name).Set(float64(len(snapshots)))
|
||||
latestSnapshot := snapshots[len(snapshots)-1]
|
||||
Metrics.SnapshotLatestTime.WithLabelValues(j.Name).Set(float64(latestSnapshot.Time.Unix()))
|
||||
if len(snapshots) > 0 {
|
||||
latestSnapshot := snapshots[len(snapshots)-1]
|
||||
Metrics.SnapshotLatestTime.WithLabelValues(j.Name).Set(float64(latestSnapshot.Time.Unix()))
|
||||
}
|
||||
}
|
||||
|
||||
if result.Success {
|
||||
|
@ -276,8 +307,8 @@ func (j Job) NewRestic() *Restic {
|
|||
}
|
||||
|
||||
type Config struct {
|
||||
// GlobalConfig *ResticConfig `hcl:"global_config,block"`
|
||||
Jobs []Job `hcl:"job,block"`
|
||||
DefaultConfig *ResticConfig `hcl:"default_config,block"`
|
||||
Jobs []Job `hcl:"job,block"`
|
||||
}
|
||||
|
||||
func (c Config) Validate() error {
|
||||
|
@ -286,6 +317,12 @@ func (c Config) Validate() error {
|
|||
}
|
||||
|
||||
for _, job := range c.Jobs {
|
||||
// Use default restic config if no job config is provided
|
||||
// TODO: Maybe merge values here
|
||||
if job.Config == nil {
|
||||
job.Config = c.DefaultConfig
|
||||
}
|
||||
|
||||
if err := job.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
115
job_test.go
115
job_test.go
|
@ -7,8 +7,8 @@ import (
|
|||
main "git.iamthefij.com/iamthefij/restic-scheduler"
|
||||
)
|
||||
|
||||
func ValidResticConfig() main.ResticConfig {
|
||||
return main.ResticConfig{
|
||||
func ValidResticConfig() *main.ResticConfig {
|
||||
return &main.ResticConfig{
|
||||
Passphrase: "shh",
|
||||
Repo: "./data",
|
||||
Env: nil,
|
||||
|
@ -92,6 +92,7 @@ func TestJobValidation(t *testing.T) {
|
|||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Postgres: []main.JobTaskPostgres{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
},
|
||||
expectedErr: nil,
|
||||
|
@ -106,6 +107,7 @@ func TestJobValidation(t *testing.T) {
|
|||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Postgres: []main.JobTaskPostgres{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
},
|
||||
expectedErr: main.ErrMissingField,
|
||||
|
@ -120,6 +122,7 @@ func TestJobValidation(t *testing.T) {
|
|||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Postgres: []main.JobTaskPostgres{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
},
|
||||
expectedErr: main.ErrInvalidConfigValue,
|
||||
|
@ -129,11 +132,12 @@ func TestJobValidation(t *testing.T) {
|
|||
job: main.Job{
|
||||
Name: "Test job",
|
||||
Schedule: "@daily",
|
||||
Config: main.ResticConfig{}, //nolint:exhaustruct
|
||||
Config: &main.ResticConfig{}, //nolint:exhaustruct
|
||||
Tasks: []main.JobTask{},
|
||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Postgres: []main.JobTaskPostgres{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
},
|
||||
expectedErr: main.ErrMutuallyExclusive,
|
||||
|
@ -144,10 +148,13 @@ func TestJobValidation(t *testing.T) {
|
|||
Name: "Test job",
|
||||
Schedule: "@daily",
|
||||
Config: ValidResticConfig(),
|
||||
Tasks: []main.JobTask{{}},
|
||||
Tasks: []main.JobTask{
|
||||
{}, //nolint:exhaustruct
|
||||
},
|
||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Postgres: []main.JobTaskPostgres{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
},
|
||||
expectedErr: main.ErrMissingField,
|
||||
|
@ -161,7 +168,10 @@ func TestJobValidation(t *testing.T) {
|
|||
Tasks: []main.JobTask{},
|
||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
MySQL: []main.JobTaskMySQL{{}},
|
||||
MySQL: []main.JobTaskMySQL{
|
||||
{}, //nolint:exhaustruct
|
||||
},
|
||||
Postgres: []main.JobTaskPostgres{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
},
|
||||
expectedErr: main.ErrMissingField,
|
||||
|
@ -176,7 +186,10 @@ func TestJobValidation(t *testing.T) {
|
|||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Sqlite: []main.JobTaskSqlite{{}},
|
||||
Postgres: []main.JobTaskPostgres{},
|
||||
Sqlite: []main.JobTaskSqlite{
|
||||
{}, //nolint:exhaustruct
|
||||
},
|
||||
},
|
||||
expectedErr: main.ErrMissingField,
|
||||
},
|
||||
|
@ -207,34 +220,82 @@ func TestConfigValidation(t *testing.T) {
|
|||
}{
|
||||
{
|
||||
name: "Valid job",
|
||||
config: main.Config{Jobs: []main.Job{{
|
||||
Name: "Valid job",
|
||||
Schedule: "@daily",
|
||||
Config: ValidResticConfig(),
|
||||
Tasks: []main.JobTask{},
|
||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
}}},
|
||||
config: main.Config{
|
||||
DefaultConfig: nil,
|
||||
Jobs: []main.Job{{
|
||||
Name: "Valid job",
|
||||
Schedule: "@daily",
|
||||
Config: ValidResticConfig(),
|
||||
Tasks: []main.JobTask{},
|
||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Postgres: []main.JobTaskPostgres{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
}},
|
||||
},
|
||||
expectedErr: nil,
|
||||
},
|
||||
{
|
||||
name: "No jobs",
|
||||
config: main.Config{Jobs: []main.Job{}},
|
||||
name: "Valid job with default config",
|
||||
config: main.Config{
|
||||
DefaultConfig: ValidResticConfig(),
|
||||
Jobs: []main.Job{{
|
||||
Name: "Valid job",
|
||||
Schedule: "@daily",
|
||||
Config: nil,
|
||||
Tasks: []main.JobTask{},
|
||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Postgres: []main.JobTaskPostgres{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
}},
|
||||
},
|
||||
expectedErr: nil,
|
||||
},
|
||||
{
|
||||
name: "No jobs",
|
||||
config: main.Config{
|
||||
DefaultConfig: nil,
|
||||
Jobs: []main.Job{},
|
||||
},
|
||||
expectedErr: main.ErrNoJobsFound,
|
||||
},
|
||||
{
|
||||
name: "Invalid name",
|
||||
config: main.Config{Jobs: []main.Job{{
|
||||
Name: "",
|
||||
Schedule: "@daily",
|
||||
Config: ValidResticConfig(),
|
||||
Tasks: []main.JobTask{},
|
||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
}}},
|
||||
config: main.Config{
|
||||
DefaultConfig: nil,
|
||||
Jobs: []main.Job{{
|
||||
Name: "",
|
||||
Schedule: "@daily",
|
||||
Config: ValidResticConfig(),
|
||||
Tasks: []main.JobTask{},
|
||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Postgres: []main.JobTaskPostgres{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
}},
|
||||
},
|
||||
expectedErr: main.ErrMissingField,
|
||||
},
|
||||
{
|
||||
name: "Missing config",
|
||||
config: main.Config{
|
||||
DefaultConfig: nil,
|
||||
Jobs: []main.Job{{
|
||||
Name: "",
|
||||
Schedule: "@daily",
|
||||
Config: nil,
|
||||
Tasks: []main.JobTask{},
|
||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Postgres: []main.JobTaskPostgres{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
}},
|
||||
},
|
||||
expectedErr: main.ErrMissingField,
|
||||
},
|
||||
}
|
||||
|
|
141
main.go
141
main.go
|
@ -29,8 +29,12 @@ func ParseConfig(path string) ([]Job, error) {
|
|||
Functions: map[string]function.Function{
|
||||
"env": function.New(&function.Spec{
|
||||
Params: []function.Parameter{{
|
||||
Name: "var",
|
||||
Type: cty.String,
|
||||
Name: "var",
|
||||
Type: cty.String,
|
||||
AllowNull: false,
|
||||
AllowUnknown: false,
|
||||
AllowDynamicType: false,
|
||||
AllowMarked: false,
|
||||
}},
|
||||
VarParam: nil,
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
|
@ -40,8 +44,12 @@ func ParseConfig(path string) ([]Job, error) {
|
|||
}),
|
||||
"readfile": function.New(&function.Spec{
|
||||
Params: []function.Parameter{{
|
||||
Name: "path",
|
||||
Type: cty.String,
|
||||
Name: "path",
|
||||
Type: cty.String,
|
||||
AllowNull: false,
|
||||
AllowUnknown: false,
|
||||
AllowDynamicType: false,
|
||||
AllowMarked: false,
|
||||
}},
|
||||
VarParam: nil,
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
|
@ -90,6 +98,10 @@ func ReadJobs(paths []string) ([]Job, error) {
|
|||
}
|
||||
}
|
||||
|
||||
if len(allJobs) == 0 {
|
||||
return allJobs, fmt.Errorf("No jobs found in provided configuration: %w", ErrJobNotFound)
|
||||
}
|
||||
|
||||
return allJobs, nil
|
||||
}
|
||||
|
||||
|
@ -136,6 +148,10 @@ func FilterJobs(jobs []Job, names []string) ([]Job, error) {
|
|||
}
|
||||
|
||||
func runBackupJobs(jobs []Job, names string) error {
|
||||
if names == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
namesSlice := strings.Split(names, ",")
|
||||
|
||||
if len(namesSlice) == 0 {
|
||||
|
@ -152,7 +168,11 @@ func runBackupJobs(jobs []Job, names string) error {
|
|||
return filterJobErr
|
||||
}
|
||||
|
||||
func runRestoreJobs(jobs []Job, names string) error {
|
||||
func runRestoreJobs(jobs []Job, names string, snapshot string) error {
|
||||
if names == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
namesSlice := strings.Split(names, ",")
|
||||
|
||||
if len(namesSlice) == 0 {
|
||||
|
@ -161,7 +181,7 @@ func runRestoreJobs(jobs []Job, names string) error {
|
|||
|
||||
jobs, filterJobErr := FilterJobs(jobs, namesSlice)
|
||||
for _, job := range jobs {
|
||||
if err := job.RunRestore(); err != nil {
|
||||
if err := job.RunRestore(snapshot); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -169,17 +189,90 @@ func runRestoreJobs(jobs []Job, names string) error {
|
|||
return filterJobErr
|
||||
}
|
||||
|
||||
func main() {
|
||||
showVersion := flag.Bool("version", false, "Display the version and exit")
|
||||
backup := flag.String("backup", "", "Run backup jobs now. Names are comma separated and `all` will run all.")
|
||||
restore := flag.String("restore", "", "Run restore jobs now. Names are comma separated and `all` will run all.")
|
||||
once := flag.Bool("once", false, "Run jobs specified using -backup and -restore once and exit")
|
||||
healthCheckAddr := flag.String("addr", "0.0.0.0:8080", "address to bind health check API")
|
||||
func runUnlockJobs(jobs []Job, names string) error {
|
||||
if names == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
namesSlice := strings.Split(names, ",")
|
||||
|
||||
if len(namesSlice) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
jobs, filterJobErr := FilterJobs(jobs, namesSlice)
|
||||
for _, job := range jobs {
|
||||
if err := job.NewRestic().Unlock(UnlockOpts{RemoveAll: true}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return filterJobErr
|
||||
}
|
||||
|
||||
type Flags struct {
|
||||
showVersion bool
|
||||
backup string
|
||||
restore string
|
||||
unlock string
|
||||
restoreSnapshot string
|
||||
once bool
|
||||
healthCheckAddr string
|
||||
metricsPushGateway string
|
||||
}
|
||||
|
||||
func readFlags() Flags {
|
||||
flags := Flags{} //nolint:exhaustruct
|
||||
flag.BoolVar(&flags.showVersion, "version", false, "Display the version and exit")
|
||||
flag.StringVar(&flags.backup, "backup", "", "Run backup jobs now. Names are comma separated. `all` will run all.")
|
||||
flag.StringVar(&flags.restore, "restore", "", "Run restore jobs now. Names are comma separated. `all` will run all.")
|
||||
flag.StringVar(&flags.unlock, "unlock", "", "Unlock job repos now. Names are comma separated. `all` will run all.")
|
||||
flag.BoolVar(&flags.once, "once", false, "Run jobs specified using -backup and -restore once and exit")
|
||||
flag.StringVar(&flags.healthCheckAddr, "addr", "0.0.0.0:8080", "address to bind health check API")
|
||||
flag.StringVar(&flags.metricsPushGateway, "push-gateway", "", "url of push gateway service for batch runs (optional)")
|
||||
flag.StringVar(&JobBaseDir, "base-dir", JobBaseDir, "Base dir to create intermediate job files like SQL dumps.")
|
||||
flag.StringVar(&flags.restoreSnapshot, "snapshot", "latest", "the snapshot to restore")
|
||||
flag.Parse()
|
||||
|
||||
return flags
|
||||
}
|
||||
|
||||
func runSpecifiedJobs(jobs []Job, backupJobs, restoreJobs, unlockJobs, snapshot string) error {
|
||||
// Run specified job unlocks
|
||||
if err := runUnlockJobs(jobs, unlockJobs); err != nil {
|
||||
return fmt.Errorf("Failed running unlock for jobs: %w", err)
|
||||
}
|
||||
|
||||
// Run specified backup jobs
|
||||
if err := runBackupJobs(jobs, backupJobs); err != nil {
|
||||
return fmt.Errorf("Failed running backup jobs: %w", err)
|
||||
}
|
||||
|
||||
// Run specified restore jobs
|
||||
if err := runRestoreJobs(jobs, restoreJobs, snapshot); err != nil {
|
||||
return fmt.Errorf("Failed running restore jobs: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func maybePushMetrics(metricsPushGateway string) error {
|
||||
if metricsPushGateway != "" {
|
||||
fmt.Println("Pushing metrics to push gateway")
|
||||
|
||||
if err := Metrics.PushToGateway(metricsPushGateway); err != nil {
|
||||
return fmt.Errorf("Failed pushing metrics after jobs run: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flags := readFlags()
|
||||
|
||||
// Print version if flag is provided
|
||||
if *showVersion {
|
||||
if flags.showVersion {
|
||||
fmt.Println("restic-scheduler version:", version)
|
||||
|
||||
return
|
||||
|
@ -198,27 +291,21 @@ func main() {
|
|||
log.Fatalf("Failed to read jobs from files: %v", err)
|
||||
}
|
||||
|
||||
if len(jobs) == 0 {
|
||||
log.Fatal("No jobs found in provided configuration")
|
||||
}
|
||||
|
||||
// Run specified backup jobs
|
||||
if err := runBackupJobs(jobs, *backup); err != nil {
|
||||
log.Fatalf("Failed running backup jobs: %v", err)
|
||||
}
|
||||
|
||||
// Run specified restore jobs
|
||||
if err := runRestoreJobs(jobs, *restore); err != nil {
|
||||
log.Fatalf("Failed running restore jobs: %v", err)
|
||||
if err := runSpecifiedJobs(jobs, flags.backup, flags.restore, flags.unlock, flags.restoreSnapshot); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Exit if only running once
|
||||
if *once {
|
||||
if flags.once {
|
||||
if err := maybePushMetrics(flags.metricsPushGateway); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
_ = RunHTTPHandlers(*healthCheckAddr)
|
||||
_ = RunHTTPHandlers(flags.healthCheckAddr)
|
||||
}()
|
||||
|
||||
// TODO: Add healthcheck handler using Job.Healthy()
|
||||
|
|
|
@ -18,9 +18,9 @@ func TestMain(m *testing.M) {
|
|||
if testResult == 0 && testing.CoverMode() != "" {
|
||||
c := testing.Coverage()
|
||||
if c < MinCoverage {
|
||||
fmt.Printf("Tests passed but coverage failed at %0.2f and minimum to pass is %0.2f\n", c, MinCoverage)
|
||||
fmt.Printf("WARNING: Tests passed but coverage failed at %0.2f and minimum to pass is %0.2f\n", c, MinCoverage)
|
||||
|
||||
testResult = -1
|
||||
testResult = 0
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,6 @@ func TestReadJobs(t *testing.T) {
|
|||
t.Parallel()
|
||||
|
||||
jobs, err := main.ReadJobs([]string{"./test/sample.hcl"})
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error reading jobs: %v", err)
|
||||
}
|
||||
|
@ -52,6 +51,7 @@ func TestRunJobs(t *testing.T) {
|
|||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Postgres: []main.JobTaskPostgres{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
}
|
||||
|
||||
|
|
24
metrics.go
24
metrics.go
|
@ -1,7 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/push"
|
||||
)
|
||||
|
||||
type ResticMetrics struct {
|
||||
|
@ -9,12 +12,25 @@ type ResticMetrics struct {
|
|||
JobFailureCount *prometheus.GaugeVec
|
||||
SnapshotCurrentCount *prometheus.GaugeVec
|
||||
SnapshotLatestTime *prometheus.GaugeVec
|
||||
Registry *prometheus.Registry
|
||||
}
|
||||
|
||||
func (m ResticMetrics) PushToGateway(url string) error {
|
||||
err := push.New(url, "batch").
|
||||
Gatherer(m.Registry).
|
||||
Add()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error pushing to registry %s: %w", url, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func InitMetrics() *ResticMetrics {
|
||||
labelNames := []string{"job"}
|
||||
|
||||
metrics := &ResticMetrics{
|
||||
Registry: prometheus.NewRegistry(),
|
||||
JobStartTime: prometheus.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "restic_job_start_time",
|
||||
|
@ -57,10 +73,10 @@ func InitMetrics() *ResticMetrics {
|
|||
),
|
||||
}
|
||||
|
||||
prometheus.MustRegister(metrics.JobStartTime)
|
||||
prometheus.MustRegister(metrics.JobFailureCount)
|
||||
prometheus.MustRegister(metrics.SnapshotCurrentCount)
|
||||
prometheus.MustRegister(metrics.SnapshotLatestTime)
|
||||
metrics.Registry.MustRegister(metrics.JobStartTime)
|
||||
metrics.Registry.MustRegister(metrics.JobFailureCount)
|
||||
metrics.Registry.MustRegister(metrics.SnapshotCurrentCount)
|
||||
metrics.Registry.MustRegister(metrics.SnapshotLatestTime)
|
||||
|
||||
return metrics
|
||||
}
|
||||
|
|
57
restic.go
57
restic.go
|
@ -11,8 +11,10 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
var ErrRestic = errors.New("restic error")
|
||||
var ErrRepoNotFound = errors.New("repository not found or uninitialized")
|
||||
var (
|
||||
ErrRestic = errors.New("restic error")
|
||||
ErrRepoNotFound = errors.New("repository not found or uninitialized")
|
||||
)
|
||||
|
||||
func lineIn(needle string, haystack []string) bool {
|
||||
for _, line := range haystack {
|
||||
|
@ -72,6 +74,16 @@ func (NoOpts) ToArgs() []string {
|
|||
return []string{}
|
||||
}
|
||||
|
||||
type UnlockOpts struct {
|
||||
RemoveAll bool `hcl:"RemoveAll,optional"`
|
||||
}
|
||||
|
||||
func (uo UnlockOpts) ToArgs() (args []string) {
|
||||
args = maybeAddArgBool(args, "--remove-all", uo.RemoveAll)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
type BackupOpts struct {
|
||||
Exclude []string `hcl:"Exclude,optional"`
|
||||
Include []string `hcl:"Include,optional"`
|
||||
|
@ -193,9 +205,10 @@ type ResticGlobalOpts struct {
|
|||
TLSClientCertFile string `hcl:"TlsClientCertFile,optional"`
|
||||
LimitDownload int `hcl:"LimitDownload,optional"`
|
||||
LimitUpload int `hcl:"LimitUpload,optional"`
|
||||
Options map[string]string `hcl:"Options,optional"`
|
||||
VerboseLevel int `hcl:"VerboseLevel,optional"`
|
||||
Options map[string]string `hcl:"Options,optional"`
|
||||
CleanupCache bool `hcl:"CleanupCache,optional"`
|
||||
InsecureTLS bool `hcl:"InsecureTls,optional"`
|
||||
NoCache bool `hcl:"NoCache,optional"`
|
||||
NoLock bool `hcl:"NoLock,optional"`
|
||||
}
|
||||
|
@ -209,6 +222,7 @@ func (glo ResticGlobalOpts) ToArgs() (args []string) {
|
|||
args = maybeAddArgInt(args, "--limit-upload", glo.LimitUpload)
|
||||
args = maybeAddArgInt(args, "--verbose", glo.VerboseLevel)
|
||||
args = maybeAddArgBool(args, "--cleanup-cache", glo.CleanupCache)
|
||||
args = maybeAddArgBool(args, "--insecure-tls", glo.InsecureTLS)
|
||||
args = maybeAddArgBool(args, "--no-cache", glo.NoCache)
|
||||
args = maybeAddArgBool(args, "--no-lock", glo.NoLock)
|
||||
|
||||
|
@ -273,7 +287,11 @@ func (e *ResticError) Unwrap() error {
|
|||
return e.OriginalError
|
||||
}
|
||||
|
||||
func (rcmd Restic) RunRestic(command string, options CommandOptions, commandArgs ...string) ([]string, error) {
|
||||
func (rcmd Restic) RunRestic(
|
||||
command string,
|
||||
options CommandOptions,
|
||||
commandArgs ...string,
|
||||
) (*CapturedCommandLogWriter, error) {
|
||||
args := []string{}
|
||||
if rcmd.GlobalOpts != nil {
|
||||
args = rcmd.GlobalOpts.ToArgs()
|
||||
|
@ -285,22 +303,22 @@ func (rcmd Restic) RunRestic(command string, options CommandOptions, commandArgs
|
|||
|
||||
cmd := exec.Command("restic", args...)
|
||||
|
||||
output := NewCapturedLogWriter(rcmd.Logger)
|
||||
cmd.Stdout = output
|
||||
cmd.Stderr = output
|
||||
output := NewCapturedCommandLogWriter(rcmd.Logger)
|
||||
cmd.Stdout = output.Stdout
|
||||
cmd.Stderr = output.Stderr
|
||||
cmd.Env = rcmd.BuildEnv()
|
||||
cmd.Dir = rcmd.Cwd
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
responseErr := ErrRestic
|
||||
if lineIn("Is there a repository at the following location?", output.Lines) {
|
||||
if lineIn("Is there a repository at the following location?", output.Stderr.Lines) {
|
||||
responseErr = ErrRepoNotFound
|
||||
}
|
||||
|
||||
return output.Lines, NewResticError(command, output.Lines, responseErr)
|
||||
return output, NewResticError(command, output.AllLines(), responseErr)
|
||||
}
|
||||
|
||||
return output.Lines, nil
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (rcmd Restic) Backup(files []string, opts BackupOpts) error {
|
||||
|
@ -327,6 +345,12 @@ func (rcmd Restic) Check() error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (rcmd Restic) Unlock(unlockOpts UnlockOpts) error {
|
||||
_, err := rcmd.RunRestic("unlock", unlockOpts)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type Snapshot struct {
|
||||
UID int `json:"uid"`
|
||||
GID int `json:"gid"`
|
||||
|
@ -341,15 +365,20 @@ type Snapshot struct {
|
|||
}
|
||||
|
||||
func (rcmd Restic) ReadSnapshots() ([]Snapshot, error) {
|
||||
lines, err := rcmd.RunRestic("snapshots", GenericOpts{"--json"})
|
||||
output, err := rcmd.RunRestic("snapshots", GenericOpts{"--json"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
snapshots := new([]Snapshot)
|
||||
if len(output.Stdout.Lines) == 0 {
|
||||
return nil, fmt.Errorf("no snapshot output to parse: %w", ErrRestic)
|
||||
}
|
||||
|
||||
if err = json.Unmarshal([]byte(lines[0]), snapshots); err != nil {
|
||||
return nil, fmt.Errorf("failed parsing snapshot results from %s: %w", lines[0], err)
|
||||
singleLineOutput := strings.Join(output.Stdout.Lines, "")
|
||||
|
||||
snapshots := new([]Snapshot)
|
||||
if err = json.Unmarshal([]byte(singleLineOutput), snapshots); err != nil {
|
||||
return nil, fmt.Errorf("failed parsing snapshot results from %s: %w", singleLineOutput, err)
|
||||
}
|
||||
|
||||
return *snapshots, nil
|
||||
|
|
|
@ -32,6 +32,7 @@ func TestGlobalOptions(t *testing.T) {
|
|||
LimitUpload: 1,
|
||||
VerboseLevel: 1,
|
||||
CleanupCache: true,
|
||||
InsecureTLS: true,
|
||||
NoCache: true,
|
||||
NoLock: true,
|
||||
Options: map[string]string{
|
||||
|
@ -48,6 +49,7 @@ func TestGlobalOptions(t *testing.T) {
|
|||
"--limit-upload", "1",
|
||||
"--verbose", "1",
|
||||
"--cleanup-cache",
|
||||
"--insecure-tls",
|
||||
"--no-cache",
|
||||
"--no-lock",
|
||||
"--option", "key='a long value'",
|
||||
|
@ -150,6 +152,20 @@ func TestForgetOpts(t *testing.T) {
|
|||
AssertEqual(t, "args didn't match", expected, args)
|
||||
}
|
||||
|
||||
func TestUnlockOpts(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
args := main.UnlockOpts{
|
||||
RemoveAll: true,
|
||||
}.ToArgs()
|
||||
|
||||
expected := []string{
|
||||
"--remove-all",
|
||||
}
|
||||
|
||||
AssertEqual(t, "args didn't match", expected, args)
|
||||
}
|
||||
|
||||
func TestBuildEnv(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
@ -221,7 +237,7 @@ func TestResticInterface(t *testing.T) {
|
|||
}
|
||||
|
||||
// Write test file to the data dir
|
||||
err := os.WriteFile(dataFile, []byte("testing"), 0644)
|
||||
err := os.WriteFile(dataFile, []byte("testing"), 0o644)
|
||||
AssertEqualFail(t, "unexpected error writing to test file", nil, err)
|
||||
|
||||
// Make sure no existing repo is found
|
||||
|
@ -281,7 +297,7 @@ func TestResticInterface(t *testing.T) {
|
|||
AssertEqualFail(t, "unexpected error checking repo", nil, err)
|
||||
|
||||
// Change the data file
|
||||
err = os.WriteFile(dataFile, []byte("unexpected"), 0644)
|
||||
err = os.WriteFile(dataFile, []byte("unexpected"), 0o644)
|
||||
AssertEqualFail(t, "unexpected error writing to test file", nil, err)
|
||||
|
||||
// Check that data wrote
|
||||
|
@ -297,4 +313,8 @@ func TestResticInterface(t *testing.T) {
|
|||
value, err = os.ReadFile(restoredDataFile)
|
||||
AssertEqualFail(t, "unexpected error reading from test file", nil, err)
|
||||
AssertEqualFail(t, "incorrect value in test file", "testing", string(value))
|
||||
|
||||
// Try to unlock the repo (repo shouldn't really be locked, but this should still run without error
|
||||
err = restic.Unlock(main.UnlockOpts{}) //nolint:exhaustruct
|
||||
AssertEqualFail(t, "unexpected error unlocking repo", nil, err)
|
||||
}
|
||||
|
|
15
scheduler.go
15
scheduler.go
|
@ -13,8 +13,10 @@ import (
|
|||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
var jobResultsLock = sync.Mutex{}
|
||||
var jobResults = map[string]JobResult{}
|
||||
var (
|
||||
jobResultsLock = sync.Mutex{}
|
||||
jobResults = map[string]JobResult{}
|
||||
)
|
||||
|
||||
type JobResult struct {
|
||||
JobName string
|
||||
|
@ -67,9 +69,12 @@ func healthHandleFunc(writer http.ResponseWriter, request *http.Request) {
|
|||
|
||||
func RunHTTPHandlers(addr string) error {
|
||||
http.HandleFunc("/health", healthHandleFunc)
|
||||
http.Handle("/metrics", promhttp.Handler())
|
||||
http.Handle("/metrics", promhttp.HandlerFor(
|
||||
Metrics.Registry,
|
||||
promhttp.HandlerOpts{Registry: Metrics.Registry}, //nolint:exhaustruct
|
||||
))
|
||||
|
||||
return fmt.Errorf("error on healthcheck: %w", http.ListenAndServe(addr, nil)) //#nosec: g114
|
||||
return fmt.Errorf("error on http server: %w", http.ListenAndServe(addr, nil)) //#nosec: g114
|
||||
}
|
||||
|
||||
func ScheduleAndRunJobs(jobs []Job) error {
|
||||
|
@ -110,6 +115,8 @@ func ScheduleAndRunJobs(jobs []Job) error {
|
|||
defer func() {
|
||||
ctx := scheduler.Stop()
|
||||
<-ctx.Done()
|
||||
|
||||
fmt.Println("All jobs successfully stopped")
|
||||
}()
|
||||
|
||||
return nil
|
||||
|
|
29
shell.go
29
shell.go
|
@ -5,6 +5,7 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -39,6 +40,7 @@ func NewCapturedLogWriter(logger *log.Logger) *CapturedLogWriter {
|
|||
return &CapturedLogWriter{Lines: []string{}, logger: logger}
|
||||
}
|
||||
|
||||
// Write writes the provided byte slice to the logger and stores each captured line.
|
||||
func (w *CapturedLogWriter) Write(content []byte) (n int, err error) {
|
||||
message := string(content)
|
||||
for _, line := range strings.Split(message, "\n") {
|
||||
|
@ -49,6 +51,33 @@ func (w *CapturedLogWriter) Write(content []byte) (n int, err error) {
|
|||
return len(content), nil
|
||||
}
|
||||
|
||||
// LinesMergedWith returns a slice of lines from this logger merged with another.
|
||||
func (w CapturedLogWriter) LinesMergedWith(other CapturedLogWriter) []string {
|
||||
allLines := []string{}
|
||||
allLines = append(allLines, w.Lines...)
|
||||
allLines = append(allLines, other.Lines...)
|
||||
|
||||
sort.Strings(allLines)
|
||||
|
||||
return allLines
|
||||
}
|
||||
|
||||
type CapturedCommandLogWriter struct {
|
||||
Stdout *CapturedLogWriter
|
||||
Stderr *CapturedLogWriter
|
||||
}
|
||||
|
||||
func NewCapturedCommandLogWriter(logger *log.Logger) *CapturedCommandLogWriter {
|
||||
return &CapturedCommandLogWriter{
|
||||
Stdout: NewCapturedLogWriter(logger),
|
||||
Stderr: NewCapturedLogWriter(logger),
|
||||
}
|
||||
}
|
||||
|
||||
func (cclw CapturedCommandLogWriter) AllLines() []string {
|
||||
return cclw.Stdout.LinesMergedWith(*cclw.Stderr)
|
||||
}
|
||||
|
||||
func RunShell(script string, cwd string, env map[string]string, logger *log.Logger) error {
|
||||
cmd := exec.Command("sh", "-c", strings.TrimSpace(script)) //nolint:gosec
|
||||
|
||||
|
|
192
tasks.go
192
tasks.go
|
@ -67,7 +67,7 @@ func (t *JobTaskScript) SetName(name string) {
|
|||
t.name = name
|
||||
}
|
||||
|
||||
// JobTaskMySQL is a sqlite backup task that performs required pre and post tasks.
|
||||
// JobTaskMySQL is a MySQL backup task that performs required pre and post tasks.
|
||||
type JobTaskMySQL struct {
|
||||
Port int `hcl:"port,optional"`
|
||||
Name string `hcl:"name,label"`
|
||||
|
@ -89,11 +89,16 @@ func (t JobTaskMySQL) Validate() error {
|
|||
return fmt.Errorf("task %s is missing dump_to path: %w", t.Name, ErrMissingField)
|
||||
}
|
||||
|
||||
if s, err := os.Stat(t.DumpToPath); err != nil {
|
||||
if stat, err := os.Stat(t.DumpToPath); err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return fmt.Errorf("task %s: invalid dump_to: could not stat path: %v: %w", t.Name, err, ErrInvalidConfigValue)
|
||||
return fmt.Errorf(
|
||||
"task %s: invalid dump_to: could not stat path: %s: %w",
|
||||
t.Name,
|
||||
t.DumpToPath,
|
||||
ErrInvalidConfigValue,
|
||||
)
|
||||
}
|
||||
} else if s.Mode().IsDir() {
|
||||
} else if stat.Mode().IsDir() {
|
||||
return fmt.Errorf("task %s: dump_to cannot be a directory: %w", t.Name, ErrInvalidConfigValue)
|
||||
}
|
||||
|
||||
|
@ -155,12 +160,20 @@ func (t JobTaskMySQL) GetPostTask() ExecutableTask {
|
|||
command = append(command, "--host", t.Hostname)
|
||||
}
|
||||
|
||||
if t.Port != 0 {
|
||||
command = append(command, "--port", fmt.Sprintf("%d", t.Port))
|
||||
}
|
||||
|
||||
if t.Username != "" {
|
||||
command = append(command, "--user", t.Username)
|
||||
}
|
||||
|
||||
if t.Password != "" {
|
||||
command = append(command, "--password", t.Password)
|
||||
command = append(command, fmt.Sprintf("--password=%s", t.Password))
|
||||
}
|
||||
|
||||
if t.Database != "" {
|
||||
command = append(command, t.Database)
|
||||
}
|
||||
|
||||
command = append(command, "<", t.DumpToPath)
|
||||
|
@ -174,6 +187,144 @@ func (t JobTaskMySQL) GetPostTask() ExecutableTask {
|
|||
}
|
||||
}
|
||||
|
||||
// JobTaskPostgres is a postgres backup task that performs required pre and post tasks.
|
||||
type JobTaskPostgres struct {
|
||||
Port int `hcl:"port,optional"`
|
||||
Name string `hcl:"name,label"`
|
||||
Hostname string `hcl:"hostname,optional"`
|
||||
Database string `hcl:"database,optional"`
|
||||
Username string `hcl:"username,optional"`
|
||||
Password string `hcl:"password,optional"`
|
||||
Tables []string `hcl:"tables,optional"`
|
||||
DumpToPath string `hcl:"dump_to"`
|
||||
NoTablespaces bool `hcl:"no_tablespaces,optional"`
|
||||
Clean bool `hcl:"clean,optional"`
|
||||
Create bool `hcl:"create,optional"`
|
||||
}
|
||||
|
||||
func (t JobTaskPostgres) Paths() []string {
|
||||
return []string{t.DumpToPath}
|
||||
}
|
||||
|
||||
func (t JobTaskPostgres) Validate() error {
|
||||
if t.DumpToPath == "" {
|
||||
return fmt.Errorf("task %s is missing dump_to path: %w", t.Name, ErrMissingField)
|
||||
}
|
||||
|
||||
if stat, err := os.Stat(t.DumpToPath); err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return fmt.Errorf(
|
||||
"task %s: invalid dump_to: could not stat path: %s: %w",
|
||||
t.Name,
|
||||
t.DumpToPath,
|
||||
ErrInvalidConfigValue,
|
||||
)
|
||||
}
|
||||
} else if stat.Mode().IsDir() {
|
||||
return fmt.Errorf("task %s: dump_to cannot be a directory: %w", t.Name, ErrInvalidConfigValue)
|
||||
}
|
||||
|
||||
if len(t.Tables) > 0 && t.Database == "" {
|
||||
return fmt.Errorf(
|
||||
"task %s is invalid. Must specify a database to use tables: %w",
|
||||
t.Name,
|
||||
ErrMissingField,
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//nolint:cyclop
|
||||
func (t JobTaskPostgres) GetPreTask() ExecutableTask {
|
||||
command := []string{"pg_dump"}
|
||||
if t.Database == "" {
|
||||
command = []string{"pg_dumpall"}
|
||||
}
|
||||
|
||||
command = append(command, "--file", t.DumpToPath)
|
||||
|
||||
if t.Hostname != "" {
|
||||
command = append(command, "--host", t.Hostname)
|
||||
}
|
||||
|
||||
if t.Port != 0 {
|
||||
command = append(command, "--port", fmt.Sprintf("%d", t.Port))
|
||||
}
|
||||
|
||||
if t.Username != "" {
|
||||
command = append(command, "--username", t.Username)
|
||||
}
|
||||
|
||||
if t.NoTablespaces {
|
||||
command = append(command, "--no-tablespaces")
|
||||
}
|
||||
|
||||
if t.Clean {
|
||||
command = append(command, "--clean")
|
||||
}
|
||||
|
||||
if t.Create {
|
||||
command = append(command, "--create")
|
||||
}
|
||||
|
||||
for _, table := range t.Tables {
|
||||
command = append(command, "--table", table)
|
||||
}
|
||||
|
||||
if t.Database != "" {
|
||||
command = append(command, t.Database)
|
||||
}
|
||||
|
||||
env := map[string]string{}
|
||||
if t.Password != "" {
|
||||
env["PGPASSWORD"] = t.Password
|
||||
}
|
||||
|
||||
return JobTaskScript{
|
||||
name: t.Name,
|
||||
env: env,
|
||||
Cwd: ".",
|
||||
OnBackup: strings.Join(command, " "),
|
||||
OnRestore: "",
|
||||
}
|
||||
}
|
||||
|
||||
func (t JobTaskPostgres) GetPostTask() ExecutableTask {
|
||||
command := []string{"psql"}
|
||||
|
||||
if t.Hostname != "" {
|
||||
command = append(command, "--host", t.Hostname)
|
||||
}
|
||||
|
||||
if t.Port != 0 {
|
||||
command = append(command, "--port", fmt.Sprintf("%d", t.Port))
|
||||
}
|
||||
|
||||
if t.Username != "" {
|
||||
command = append(command, "--username", t.Username)
|
||||
}
|
||||
|
||||
if t.Database != "" {
|
||||
command = append(command, t.Database)
|
||||
}
|
||||
|
||||
command = append(command, "<", t.DumpToPath)
|
||||
|
||||
env := map[string]string{}
|
||||
if t.Password != "" {
|
||||
env["PGPASSWORD"] = t.Password
|
||||
}
|
||||
|
||||
return JobTaskScript{
|
||||
name: t.Name,
|
||||
env: env,
|
||||
Cwd: ".",
|
||||
OnBackup: "",
|
||||
OnRestore: strings.Join(command, " "),
|
||||
}
|
||||
}
|
||||
|
||||
// JobTaskSqlite is a sqlite backup task that performs required pre and post tasks.
|
||||
type JobTaskSqlite struct {
|
||||
Name string `hcl:"name,label"`
|
||||
|
@ -190,11 +341,16 @@ func (t JobTaskSqlite) Validate() error {
|
|||
return fmt.Errorf("task %s is missing dump_to path: %w", t.Name, ErrMissingField)
|
||||
}
|
||||
|
||||
if s, err := os.Stat(t.DumpToPath); err != nil {
|
||||
if stat, err := os.Stat(t.DumpToPath); err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return fmt.Errorf("task %s: invalid dump_to: could not stat path: %v: %w", t.Name, err, ErrInvalidConfigValue)
|
||||
return fmt.Errorf(
|
||||
"task %s: invalid dump_to: could not stat path: %s: %w",
|
||||
t.Name,
|
||||
t.DumpToPath,
|
||||
ErrInvalidConfigValue,
|
||||
)
|
||||
}
|
||||
} else if s.Mode().IsDir() {
|
||||
} else if stat.Mode().IsDir() {
|
||||
return fmt.Errorf("task %s: dump_to cannot be a directory: %w", t.Name, ErrInvalidConfigValue)
|
||||
}
|
||||
|
||||
|
@ -226,6 +382,7 @@ type BackupFilesTask struct {
|
|||
BackupOpts *BackupOpts `hcl:"backup_opts,block"`
|
||||
RestoreOpts *RestoreOpts `hcl:"restore_opts,block"`
|
||||
name string
|
||||
snapshot string
|
||||
}
|
||||
|
||||
func (t BackupFilesTask) RunBackup(cfg TaskConfig) error {
|
||||
|
@ -248,8 +405,11 @@ func (t BackupFilesTask) RunRestore(cfg TaskConfig) error {
|
|||
t.RestoreOpts = &RestoreOpts{} //nolint:exhaustruct
|
||||
}
|
||||
|
||||
// TODO: Make the snapshot configurable
|
||||
if err := cfg.Restic.Restore("latest", *t.RestoreOpts); err != nil {
|
||||
if t.snapshot == "" {
|
||||
t.snapshot = "latest"
|
||||
}
|
||||
|
||||
if err := cfg.Restic.Restore(t.snapshot, *t.RestoreOpts); err != nil {
|
||||
err = fmt.Errorf("failed restoring paths: %w", err)
|
||||
cfg.Logger.Print(err)
|
||||
|
||||
|
@ -277,14 +437,16 @@ func (t *BackupFilesTask) Validate() error {
|
|||
|
||||
// JobTask represents a single task within a backup job.
|
||||
type JobTask struct {
|
||||
Name string `hcl:"name,label"`
|
||||
PreScripts []JobTaskScript `hcl:"pre_script,block"`
|
||||
PostScripts []JobTaskScript `hcl:"post_script,block"`
|
||||
MySQL []JobTaskMySQL `hcl:"mysql,block"`
|
||||
Sqlite []JobTaskSqlite `hcl:"sqlite,block"`
|
||||
Name string `hcl:"name,label"`
|
||||
PreScripts []JobTaskScript `hcl:"pre_script,block"`
|
||||
PostScripts []JobTaskScript `hcl:"post_script,block"`
|
||||
MySQL []JobTaskMySQL `hcl:"mysql,block"`
|
||||
Postgres []JobTaskPostgres `hcl:"postgres,block"`
|
||||
Sqlite []JobTaskSqlite `hcl:"sqlite,block"`
|
||||
}
|
||||
|
||||
func (t JobTask) Validate() error {
|
||||
// NOTE: Might make task types mutually exclusive because order is confusing even if deterministic
|
||||
if t.Name == "" {
|
||||
return fmt.Errorf("task is missing a name: %w", ErrMissingField)
|
||||
}
|
||||
|
|
|
@ -163,7 +163,29 @@ func TestJobTaskSql(t *testing.T) {
|
|||
" --user user --password=pass --no-tablespaces db table1 table2",
|
||||
postBackup: "",
|
||||
preRestore: "",
|
||||
postRestore: "mysql --host host --user user --password pass < ./simple.sql",
|
||||
postRestore: "mysql --host host --port 3306 --user user --password=pass db < ./simple.sql",
|
||||
},
|
||||
{
|
||||
name: "psql all",
|
||||
task: main.JobTaskPostgres{
|
||||
Name: "simple",
|
||||
Hostname: "host",
|
||||
Port: 6543,
|
||||
Username: "user",
|
||||
Password: "pass",
|
||||
Database: "db",
|
||||
NoTablespaces: true,
|
||||
Create: true,
|
||||
Clean: true,
|
||||
Tables: []string{"table1", "table2"},
|
||||
DumpToPath: "./simple.sql",
|
||||
},
|
||||
validationErr: nil,
|
||||
preBackup: "pg_dump --file ./simple.sql --host host --port 6543 --username user --no-tablespaces" +
|
||||
" --clean --create --table table1 --table table2 db",
|
||||
postBackup: "",
|
||||
preRestore: "",
|
||||
postRestore: "psql --host host --port 6543 --username user db < ./simple.sql",
|
||||
},
|
||||
// Sqlite
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue