diff --git a/bash/.bashrc b/bash/.bashrc index 1a5afff..c45c12c 100644 --- a/bash/.bashrc +++ b/bash/.bashrc @@ -124,3 +124,44 @@ export QT_IM_MODULE=fcitx export SDL_IM_MODULE=fcitx export WLR_XWAYLAND=$HOME/.local/bin/Xwayland-noshm $MACHINE_START_SWAY && [[ -z "$DISPLAY" && $(tty) == /dev/tty1 ]] && exec sway + +# Miscellaneous utilities +function clamp_filename() { + [[ -z "$1" ]] && return 1 + + local len="$1" + + for file in *; do + local filename_len=$(echo "$file" | wc -c) + + if [ $filename_len -gt $len ]; then + local extension="${file##*.}" + local filename="${filename%.*}" + if [ ${#extension} -gt 5 ]; then + filename="$file" + extension="" + fi + local clamped_filename="$(echo "$file" | cut -c1-$((len - ${#extension})) | iconv -f utf8 -t utf8 -c -)" + if [ ${#extension} -gt 0 ]; then + clamped_filename="$clamped_filename.$extension" + fi + echo "File '$PWD/$file' clamped to '$PWD/$clamped_filename'" + mv "$file" "$clamped_filename" + file="$clamped_filename" + fi + + if [ -d "$file" ]; then + pushd "$file" > /dev/null + clamp_filename "$1" + popd > /dev/null + fi + done +} + +function open_nas_decrypt() { + gocryptfs -passfile /dev/stdin "$MACHINE_NAS_CIPHER_PATH" "$MACHINE_NAS_DECRYPT_PATH" <<< "$(pass gocrypt show "$MACHINE_NAS_CIPHER_PASS")" +} + +function close_nas_decrypt() { + fusermount -u "$MACHINE_NAS_DECRYPT_PATH" +} diff --git a/restic-backup/.local/bin/restic-backup b/restic-backup/.local/bin/restic-backup new file mode 100755 index 0000000..7bb5577 --- /dev/null +++ b/restic-backup/.local/bin/restic-backup @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +set -e + +source ~/.config/restic-backup/config.sh + +cleanup() { + pass gocrypt close || true > /dev/null 2>&1 +} + +trap cleanup EXIT + +pass gocrypt close > /dev/null 2>&1 || true +PASS_GOCRYPT_CLOSE_TIMEOUT=2147483648 pass gocrypt open + +for dir in ${RESTIC_BACKUP_SUBVOL_PATHS[@]}; do + parent="$(dirname $dir)" + snap="$parent/$(basename $dir)-restic" + echo "snap = $snap" + sudo btrfs subvol delete "$snap" > /dev/null 2>&1 || true + sudo btrfs subvol snap "$dir" "$snap" + restic-wrapper --verbose backup "$snap" + sudo btrfs subvol delete "$snap" || true +done diff --git a/restic-backup/.local/bin/restic-wrapper b/restic-backup/.local/bin/restic-wrapper new file mode 100755 index 0000000..84adee2 --- /dev/null +++ b/restic-backup/.local/bin/restic-wrapper @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set -e + +source ~/.config/restic-backup/config.sh +pass gocrypt open || true +sudo restic --password-file /dev/stdin -r "$RESTIC_BACKUP_REPO" $@ <<< $(pass show "$RESTIC_BACKUP_PASS")