Add pre-commit and lint

This commit is contained in:
ViViDboarder 2021-06-14 15:55:26 -07:00
parent 99249540fb
commit cdc1cfac30
13 changed files with 73 additions and 24 deletions

17
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,17 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-merge-conflict
- id: check-yaml
- id: end-of-file-fixer
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.7.2.1
hooks:
- id: shellcheck
- repo: https://github.com/IamTheFij/docker-pre-commit
rev: v2.0.0
hooks:
- id: docker-compose-check
- id: hadolint

View File

@ -1,5 +1,8 @@
DOCKER_TAG ?= docker-restic-cron
.PHONY: all
all: check test-all
.PHONY: default
default: build-x86
@ -42,3 +45,11 @@ shell: shell-x86
.PHONY: clean
clean:
docker-compose -f docker-compose-test-s3.yml down -v
.PHONY: install-hooks
install-hooks:
pre-commit install
.PHONY: check
check:
pre-commit run --all-files

View File

@ -3,27 +3,33 @@ set -e
# Run pre-backup scripts
for f in /scripts/backup/before/*; do
if [ -f $f -a -x $f ]; then
bash $f
if [ -f "$f" ] && [ -x "$f" ]; then
bash "$f"
fi
done
# shellcheck disable=SC2086
restic \
-r $BACKUP_DEST \
-r "$BACKUP_DEST" \
$OPT_ARGUMENTS \
backup \
"$PATH_TO_BACKUP"
if [ -n "$CLEANUP_COMMAND" ]; then
# Clean up old snapshots via provided policy
# shellcheck disable=SC2086
restic \
-r $BACKUP_DEST \
-r "$BACKUP_DEST" \
forget \
$CLEANUP_COMMAND
# Verify that nothing is corrupted
restic check -r "$BACKUP_DEST"
fi
# Run post-backup scripts
for f in /scripts/backup/after/*; do
if [ -f $f -a -x $f ]; then
bash $f
if [ -f "$f" ] && [ -x "$f" ]; then
bash "$f"
fi
done

View File

@ -4,8 +4,15 @@ ENV=/env.sh
LOG=/cron.log
HEALTH_FILE=/unhealthy
touch $ENV
source $ENV
if [ -f "$ENV" ]; then
# shellcheck disable=SC1090
source "$ENV"
fi
# Execute command and write output to log
$@ 2>> $LOG && rm -f $HEALTH_FILE || { touch $HEALTH_FILE; exit 1; }
if eval "$@" 2>> "$LOG"; then
rm -f "$HEALTH_FILE"
else
touch "$HEALTH_FILE"
exit 1;
fi

View File

@ -1,25 +1,26 @@
#! /bin/bash
set -e
restore_snapshot=$1
restore_snapshot="$1"
# Run pre-restore scripts
for f in /scripts/restore/before/*; do
if [ -f $f -a -x $f ]; then
bash $f
if [ -f "$f" ] && [ -x "$f" ]; then
bash "$f"
fi
done
# shellcheck disable=SC2086
restic \
-r $BACKUP_DEST \
-r "$BACKUP_DEST" \
$OPT_ARGUMENTS \
restore \
$restore_snapshot \
"$restore_snapshot" \
-t /
# Run post-restore scripts
for f in /scripts/restore/after/*; do
if [ -f $f -a -x $f ]; then
bash $f
if [ -f "$f" ] && [ -x "$f" ]; then
bash "$f"
fi
done

View File

@ -8,14 +8,14 @@ fi
# If no env variable set, get from command line
if [ "$OPT_ARGUMENTS" == "" ]; then
export OPT_ARGUMENTS="$@"
export OPT_ARGUMENTS="$*"
fi
# Init the repo
restic -r $BACKUP_DEST snapshots || restic -r $BACKUP_DEST init
restic -r "$BACKUP_DEST" snapshots || restic -r "$BACKUP_DEST" init
# If set to restore on start, restore if the data volume is empty
if [ "$RESTORE_ON_EMPTY_START" == "true" -a -z "$(ls -A $PATH_TO_BACKUP)" ]; then
if [ "$RESTORE_ON_EMPTY_START" == "true" ] && [ -z "$(ls -A "$PATH_TO_BACKUP")" ]; then
/cron-exec.sh /restore.sh latest
fi

View File

@ -1,7 +1,7 @@
#! /bin/bash
set -e
image=$1
image="$1"
if [ "$IN_CONTAINER" != "true" ] ; then
# Run the test script within the container
@ -11,7 +11,7 @@ if [ "$IN_CONTAINER" != "true" ] ; then
-e RESTIC_PASSWORD="Correct.Horse.Battery.Staple" \
-v "$(pwd)/test-pre-scripts.sh:/test.sh" \
-v "$(pwd)/test-pre-scripts:/scripts" \
$image \
"$image" \
bash -c "/test.sh"
else
echo "Performing backup tests"

View File

@ -1,3 +1,4 @@
#! /bin/bash
set -e
cd /data

View File

@ -1,3 +1,4 @@
#! /bin/bash
set -e
cd /data

View File

@ -1,3 +1,4 @@
#! /bin/bash
set -e
cd /data

View File

@ -1,3 +1,4 @@
#! /bin/bash
set -e
# Don't really need to do anything here

View File

@ -1,7 +1,7 @@
#! /bin/bash
set -e
image=$1
image="$1"
if [ "$IN_CONTAINER" != "true" ] ; then
# Run the test script within the container
@ -10,7 +10,7 @@ if [ "$IN_CONTAINER" != "true" ] ; then
-e SKIP_ON_START=true \
-e RESTIC_PASSWORD="Correct.Horse.Battery.Staple" \
-v "$(pwd)/test.sh:/test.sh" \
$image \
"$image" \
bash -c "/test.sh"
else
echo "Performing backup tests"
@ -31,6 +31,9 @@ else
echo "Verify backup..."
/cron-exec.sh /verify.sh || { cat /cron.log && exit 1; }
echo "Auto cleanup on second backup..."
CLEANUP_COMMAND="--prune --keep-last 1" /cron-exec.sh /backup.sh || { cat /cron.log && exit 1; }
echo "Delete test data..."
rm -fr /data/*

View File

@ -2,6 +2,6 @@
set -e
restic \
-r $BACKUP_DEST \
-r "$BACKUP_DEST" \
$OPT_ARGUMENTS \
check