From d17c2855ac0138c5009350d2539954c701df4eec Mon Sep 17 00:00:00 2001 From: clerie Date: Fri, 14 Feb 2025 13:09:59 +0100 Subject: [PATCH] pkgs/clerie-backup: Add script to unify backup configs --- flake.nix | 1 + pkgs/clerie-backup/clerie-backup.sh | 95 +++++++++++++++++++++++++++++ pkgs/clerie-backup/default.nix | 9 +++ pkgs/overlay.nix | 1 + 4 files changed, 106 insertions(+) create mode 100755 pkgs/clerie-backup/clerie-backup.sh create mode 100644 pkgs/clerie-backup/default.nix diff --git a/flake.nix b/flake.nix index 8fe1f74..aaae56e 100644 --- a/flake.nix +++ b/flake.nix @@ -132,6 +132,7 @@ pkgs = localNixpkgs.${system}; in { inherit (pkgs) + clerie-backup clerie-keys clerie-system-upgrade clerie-merge-nixfiles-update diff --git a/pkgs/clerie-backup/clerie-backup.sh b/pkgs/clerie-backup/clerie-backup.sh new file mode 100755 index 0000000..c2106db --- /dev/null +++ b/pkgs/clerie-backup/clerie-backup.sh @@ -0,0 +1,95 @@ +#!/usr/bin/env bash + +set -euo pipefail + +REPO= +ACTION= + +if [[ $# -lt 2 ]]; then + echo "Command not specified" + echo + echo "clerie-backup REPO ACTION" + echo + echo "ACTION: restic,backup" + echo + echo "Available REPOs (/etc/clerie-backup/):" + echo + if [[ -d "/etc/clerie-backup" ]]; then + find "/etc/clerie-backup/" -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | sort -d + fi + exit 1 +fi + +REPO="$1" +shift + +ACTION="$1" +shift + +CONFIG_DIR="/etc/clerie-backup/${REPO}" +if [[ ! -d "${CONFIG_DIR}" ]]; then + echo "Config dir ${CONFIG_DIR} for ${REPO} does not exist" + exit 1 +fi + +ISSUE_EXIST= +if [[ ! -f "${CONFIG_DIR}/repo_password" ]]; then + echo "File ${CONFIG_DIR}/repo_password not found" + ISSUE_EXIST=1 +fi +if [[ ! -f "${CONFIG_DIR}/repo_url" ]]; then + echo "File ${CONFIG_DIR}/repo_url not found" + ISSUE_EXIST=1 +fi +if [[ ! -f "${CONFIG_DIR}/auth_username" ]]; then + echo "File ${CONFIG_DIR}/auth_password not found" + ISSUE_EXIST=1 +fi +if [[ ! -f "${CONFIG_DIR}/auth_username" ]]; then + echo "File ${CONFIG_DIR}/auth_password not found" + ISSUE_EXIST=1 +fi +if [[ -n "${ISSUE_EXIST}" ]]; then + exit 1 +fi + +RESTIC_PASSWORD_FILE="${CONFIG_DIR}/repo_password" +export RESTIC_PASSWORD_FILE +RESTIC_REPOSITORY="rest:$(cat "${CONFIG_DIR}/repo_url")" +export RESTIC_REPOSITORY +RESTIC_REST_USERNAME="$(cat "${CONFIG_DIR}/auth_username")" +export RESTIC_REST_USERNAME +RESTIC_REST_PASSWORD="$(cat "${CONFIG_DIR}/auth_password")" +export RESTIC_REST_PASSWORD +RESTIC_PROGRESS_FPS="0.1" +export RESTIC_PROGRESS_FPS +RESTIC_CACHE_DIR="/var/cache/restic" +export RESTIC_CACHE_DIR + +case "${ACTION}" in +restic) + restic "$@" + ;; +backup) + ISSUE_EXIST= + if [[ ! -f "${CONFIG_DIR}/excludes" ]]; then + echo "File ${CONFIG_DIR}/excludes not found" + ISSUE_EXIST=1 + fi + if [[ ! -f "${CONFIG_DIR}/files" ]]; then + echo "File ${CONFIG_DIR}/files not found" + ISSUE_EXIST=1 + fi + if [[ -n "${ISSUE_EXIST}" ]]; then + exit 1 + fi + + restic snapshots --latest 1 || restic init + + restic backup --exclude-file "${CONFIG_DIR}/excludes" --files-from "${CONFIG_DIR}/files" + ;; +*) + echo "Unsupported ACTION: ${ACTION}" + exit 1 + ;; +esac diff --git a/pkgs/clerie-backup/default.nix b/pkgs/clerie-backup/default.nix new file mode 100644 index 0000000..6ac17b3 --- /dev/null +++ b/pkgs/clerie-backup/default.nix @@ -0,0 +1,9 @@ +{ pkgs, ... }: + +pkgs.writeShellApplication { + name = "clerie-backup"; + text = builtins.readFile ./clerie-backup.sh; + runtimeInputs = with pkgs; [ + restic + ]; +} diff --git a/pkgs/overlay.nix b/pkgs/overlay.nix index d574b4e..b6db58e 100644 --- a/pkgs/overlay.nix +++ b/pkgs/overlay.nix @@ -1,4 +1,5 @@ final: prev: { + clerie-backup = final.callPackage ./clerie-backup {}; clerie-keys = final.callPackage ./clerie-keys {}; clerie-system-upgrade = final.callPackage ./clerie-system-upgrade/clerie-system-upgrade.nix {}; clerie-merge-nixfiles-update = final.callPackage ./clerie-update-nixfiles/clerie-merge-nixfiles-update.nix {};