From fce2b07865fc4bda100460768fbdc8dea2567643 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Sun, 14 Apr 2024 12:29:49 +0100 Subject: [PATCH] scripts: add a backup and restore script --- .github/workflows/check.yml | 12 ++++++- scripts/backup.sh | 63 +++++++++++++++++++++++++++++++++++++ scripts/restore.sh | 62 ++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100755 scripts/backup.sh create mode 100644 scripts/restore.sh diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index ea5986b7..99cd7019 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -36,9 +36,19 @@ jobs: - name: git diff run: git diff --exit-code + shellcheck: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + # Shellcheck should already be installed on github runners + - name: install shellcheck + run: sudo apt install --yes shellcheck + - name: shellcheck + run: shellcheck scripts/*.sh + nix-flake-check: # Run after pre checks - needs: [license-check, flake-checker, nix-fmt] + needs: [license-check, flake-checker, nix-fmt, shellcheck] runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100755 index 00000000..d43bbccc --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +# +# SPDX-FileCopyrightText: Andrew Hayzen +# +# SPDX-License-Identifier: MPL-2.0 + +set -e + +# +# backup +# + +# Check that rsync exists +if [ ! -x "$(command -v rsync)" ]; then + echo "rsync command not found, cannot backup" + exit 1 +fi +RSYNC_ARGS="-avhP" + +HEADLESS_SYSTEM=false +USER_HOST="" + +# Check that the machine name is known +case $1 in + vps) + HEADLESS_SYSTEM=true + USER_HOST="headless@ahayzen.com" + ;; + *) + echo "Unknown machine name" + exit 1 + ;; +esac + +# Check that the target folder exists +USER_DEST=$2 +if [ ! -d "$USER_DEST" ]; then + echo "Failed to find backup target" + exit 1 +fi + +# Generate a folder for the backup and check it doesn't already exist +BACKUP_DEST="$USER_DEST/$(date --iso-8601)" +if [ -d "$BACKUP_DEST" ]; then + echo "Backup already exists at $BACKUP_DEST, won't overwrite" + exit 1 +fi +mkdir -p "$BACKUP_DEST" + +# This is a normal headless system +if [ $HEADLESS_SYSTEM ]; then + export DOCKER_COMPOSE_RUNNER_DEST="$BACKUP_DEST/docker-compose-runner" + mkdir -p "$DOCKER_COMPOSE_RUNNER_DEST" + + # Backup all of the docker data + "$(command -v rsync)" $RSYNC_ARGS "$USER_HOST:/var/lib/docker-compose-runner/" "$DOCKER_COMPOSE_RUNNER_DEST" +fi + +# Ensure the filesystem is synced +sync + +echo "Backup complete!" +date diff --git a/scripts/restore.sh b/scripts/restore.sh new file mode 100644 index 00000000..4a339e4c --- /dev/null +++ b/scripts/restore.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash +# +# SPDX-FileCopyrightText: Andrew Hayzen +# +# SPDX-License-Identifier: MPL-2.0 + +set -e + +# +# restore +# + +# Check that rsync exists +if [ ! -x "$(command -v rsync)" ]; then + echo "rsync command not found, cannot restore" + exit 1 +fi +RSYNC_ARGS="-avhP" + +HEADLESS_SYSTEM=false +USER_HOST="" + +# Check that the machine name is known +case $1 in + vps) + HEADLESS_SYSTEM=true + USER_HOST="headless@ahayzen.com" + ;; + *) + echo "Unknown machine name" + exit 1 + ;; +esac + +# Check that the source folder exists +USER_SRC=$2 +if [ ! -d "$USER_SRC" ]; then + echo "Failed to find restore source" + exit 1 +fi +RESTORE_SRC="$USER_SRC" + +# This is a normal headless system +if [ $HEADLESS_SYSTEM ]; then + export DOCKER_COMPOSE_RUNNER_SRC="$RESTORE_SRC/docker-compose-runner" + if [ ! -d "$DOCKER_COMPOSE_RUNNER_SRC" ]; then + echo "Failed to find docker-compose-runner data to restore" + exit 1 + fi + + # Stop services as we are about to mutate data + ssh "$USER_HOST" sudo systemctl stop docker-compose-runner.service + + # Restore all of the docker data + "$(command -v rsync)" $RSYNC_ARGS "$DOCKER_COMPOSE_RUNNER_SRC" "$USER_HOST:/var/lib/docker-compose-runner/" + + # Restart services + ssh $USER_HOST sudo systemctl start docker-compose-runner.service +fi + +echo "Restore complete!" +date