Browse Source

Add pre-commit and lint

github-tests
ViViDboarder 3 months ago
parent
commit
cdc1cfac30
  1. 17
      .pre-commit-config.yaml
  2. 11
      Makefile
  3. 18
      backup.sh
  4. 13
      cron-exec.sh
  5. 15
      restore.sh
  6. 6
      start.sh
  7. 4
      tests/test-pre-scripts.sh
  8. 1
      tests/test-pre-scripts/backup/after/post-backup.sh
  9. 1
      tests/test-pre-scripts/backup/before/pre-backup.sh
  10. 1
      tests/test-pre-scripts/restore/after/post-restore.sh
  11. 1
      tests/test-pre-scripts/restore/before/pre-restore.sh
  12. 7
      tests/test.sh
  13. 2
      verify.sh

17
.pre-commit-config.yaml

@ -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

11
Makefile

@ -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

18
backup.sh

@ -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

13
cron-exec.sh

@ -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

15
restore.sh

@ -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

6
start.sh

@ -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

4
tests/test-pre-scripts.sh

@ -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"

1
tests/test-pre-scripts/backup/after/post-backup.sh

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

1
tests/test-pre-scripts/backup/before/pre-backup.sh

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

1
tests/test-pre-scripts/restore/after/post-restore.sh

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

1
tests/test-pre-scripts/restore/before/pre-restore.sh

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

7
tests/test.sh

@ -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/*

2
verify.sh

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

Loading…
Cancel
Save