check folders and target git repo

This commit is contained in:
trnq1ll0 committed 2026-03-08 12:58:50 +01:00
1 parent 89c6a001c7
commit 35eb53ccea
2 files changed
+31 -26

No files matched your search

+28 -21
View File
@@ -2,11 +2,20 @@
set -euo pipefail
if [[ $# -gt 1 ]]; then
echo "Usage: $0 [path/to/.env]" >&2
echo "Usage: $0 [path/to/env-file]" >&2
exit 2
fi
ENV_FILE="${1:-$(dirname "$0")/.env}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
if [[ $# -eq 1 ]]; then
ENV_FILE="$1"
elif [[ -f "$SCRIPT_DIR/backup_rsync_git.env" ]]; then
ENV_FILE="$SCRIPT_DIR/backup_rsync_git.env"
else
ENV_FILE="$SCRIPT_DIR/.env"
fi
if [[ ! -f "$ENV_FILE" ]]; then
echo "Env file not found: $ENV_FILE" >&2
exit 1
@@ -18,7 +27,6 @@ source "$ENV_FILE"
required_vars=(
RSYNC_SOURCE
RSYNC_TARGET_DIR
GIT_REPO_DIR
COMMIT_MESSAGE_PREFIX
)
@@ -31,30 +39,29 @@ done
mkdir -p "$RSYNC_TARGET_DIR"
repo_abs="$(cd "$GIT_REPO_DIR" && pwd)"
target_abs="$(cd "$RSYNC_TARGET_DIR" && pwd)"
repo_abs="$(cd "$RSYNC_TARGET_DIR" && pwd)"
case "$target_abs" in
"$repo_abs"/*) ;;
*)
echo "RSYNC_TARGET_DIR must be inside GIT_REPO_DIR" >&2
exit 1
;;
esac
rel_target="."
rel_target="${target_abs#"$repo_abs"/}"
if [[ -n "${RSYNC_RSH:-}" ]]; then
rsync -az --delete -e "$RSYNC_RSH" "$RSYNC_SOURCE" "$RSYNC_TARGET_DIR/"
else
rsync -az --delete "$RSYNC_SOURCE" "$RSYNC_TARGET_DIR/"
if [[ ! -d "$repo_abs/.git" ]]; then
git -C "$repo_abs" init >/dev/null
fi
git -C "$GIT_REPO_DIR" add -A -- "$rel_target"
git_target() {
git --git-dir="$repo_abs/.git" --work-tree="$repo_abs" "$@"
}
if git -C "$GIT_REPO_DIR" diff --cached --quiet -- "$rel_target"; then
if [[ -n "${RSYNC_RSH:-}" ]]; then
rsync -az --delete -e "$RSYNC_RSH" "$RSYNC_SOURCE" "$repo_abs/"
else
rsync -az --delete "$RSYNC_SOURCE" "$repo_abs/"
fi
git_target add -A -- "$rel_target"
if git_target diff --cached --quiet -- "$rel_target"; then
exit 0
fi
timestamp="$(date '+%Y-%m-%d %H:%M:%S')"
git -C "$GIT_REPO_DIR" commit -m "$COMMIT_MESSAGE_PREFIX $timestamp" -- "$rel_target"
git_target commit -m "$COMMIT_MESSAGE_PREFIX $timestamp" -- "$rel_target"