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 DOCKER_TAG ?= docker-restic-cron
.PHONY: all
all: check test-all
.PHONY: default .PHONY: default
default: build-x86 default: build-x86
@ -42,3 +45,11 @@ shell: shell-x86
.PHONY: clean .PHONY: clean
clean: clean:
docker-compose -f docker-compose-test-s3.yml down -v 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 # Run pre-backup scripts
for f in /scripts/backup/before/*; do for f in /scripts/backup/before/*; do
if [ -f $f -a -x $f ]; then if [ -f "$f" ] && [ -x "$f" ]; then
bash $f bash "$f"
fi fi
done done
# shellcheck disable=SC2086
restic \ restic \
-r $BACKUP_DEST \ -r "$BACKUP_DEST" \
$OPT_ARGUMENTS \ $OPT_ARGUMENTS \
backup \ backup \
"$PATH_TO_BACKUP" "$PATH_TO_BACKUP"
if [ -n "$CLEANUP_COMMAND" ]; then if [ -n "$CLEANUP_COMMAND" ]; then
# Clean up old snapshots via provided policy
# shellcheck disable=SC2086
restic \ restic \
-r $BACKUP_DEST \ -r "$BACKUP_DEST" \
forget \ forget \
$CLEANUP_COMMAND $CLEANUP_COMMAND
# Verify that nothing is corrupted
restic check -r "$BACKUP_DEST"
fi fi
# Run post-backup scripts # Run post-backup scripts
for f in /scripts/backup/after/*; do for f in /scripts/backup/after/*; do
if [ -f $f -a -x $f ]; then if [ -f "$f" ] && [ -x "$f" ]; then
bash $f bash "$f"
fi fi
done done

View File

@ -4,8 +4,15 @@ ENV=/env.sh
LOG=/cron.log LOG=/cron.log
HEALTH_FILE=/unhealthy HEALTH_FILE=/unhealthy
touch $ENV if [ -f "$ENV" ]; then
source $ENV # shellcheck disable=SC1090
source "$ENV"
fi
# Execute command and write output to log # 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 #! /bin/bash
set -e set -e
restore_snapshot=$1 restore_snapshot="$1"
# Run pre-restore scripts # Run pre-restore scripts
for f in /scripts/restore/before/*; do for f in /scripts/restore/before/*; do
if [ -f $f -a -x $f ]; then if [ -f "$f" ] && [ -x "$f" ]; then
bash $f bash "$f"
fi fi
done done
# shellcheck disable=SC2086
restic \ restic \
-r $BACKUP_DEST \ -r "$BACKUP_DEST" \
$OPT_ARGUMENTS \ $OPT_ARGUMENTS \
restore \ restore \
$restore_snapshot \ "$restore_snapshot" \
-t / -t /
# Run post-restore scripts # Run post-restore scripts
for f in /scripts/restore/after/*; do for f in /scripts/restore/after/*; do
if [ -f $f -a -x $f ]; then if [ -f "$f" ] && [ -x "$f" ]; then
bash $f bash "$f"
fi fi
done done

View File

@ -8,14 +8,14 @@ fi
# If no env variable set, get from command line # If no env variable set, get from command line
if [ "$OPT_ARGUMENTS" == "" ]; then if [ "$OPT_ARGUMENTS" == "" ]; then
export OPT_ARGUMENTS="$@" export OPT_ARGUMENTS="$*"
fi fi
# Init the repo # 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 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 /cron-exec.sh /restore.sh latest
fi fi

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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