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

+3 -5
View File
@@ -1,11 +1,9 @@
# Remote rsync source. Example: backup@example.com:/srv/data/project/ # Remote rsync source. Example: backup@example.com:/srv/data/project/
RSYNC_SOURCE="user@example.com:/path/to/source/" RSYNC_SOURCE="user@example.com:/path/to/source/"
# Local sync destination (must be a directory inside GIT_REPO_DIR) # Local target directory for rsync; this directory is also used as git repository
RSYNC_TARGET_DIR="/path/to/monorepo/backups/source" # Must be outside the main project repository
RSYNC_TARGET_DIR="/path/to/backup-repo"
# Local monorepo path
GIT_REPO_DIR="/path/to/monorepo"
# Prefix for commit message (timestamp is appended automatically) # Prefix for commit message (timestamp is appended automatically)
COMMIT_MESSAGE_PREFIX="backup: rsync sync" COMMIT_MESSAGE_PREFIX="backup: rsync sync"
+28 -21
View File
@@ -2,11 +2,20 @@
set -euo pipefail set -euo pipefail
if [[ $# -gt 1 ]]; then if [[ $# -gt 1 ]]; then
echo "Usage: $0 [path/to/.env]" >&2 echo "Usage: $0 [path/to/env-file]" >&2
exit 2 exit 2
fi 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 if [[ ! -f "$ENV_FILE" ]]; then
echo "Env file not found: $ENV_FILE" >&2 echo "Env file not found: $ENV_FILE" >&2
exit 1 exit 1
@@ -18,7 +27,6 @@ source "$ENV_FILE"
required_vars=( required_vars=(
RSYNC_SOURCE RSYNC_SOURCE
RSYNC_TARGET_DIR RSYNC_TARGET_DIR
GIT_REPO_DIR
COMMIT_MESSAGE_PREFIX COMMIT_MESSAGE_PREFIX
) )
@@ -31,30 +39,29 @@ done
mkdir -p "$RSYNC_TARGET_DIR" mkdir -p "$RSYNC_TARGET_DIR"
repo_abs="$(cd "$GIT_REPO_DIR" && pwd)" repo_abs="$(cd "$RSYNC_TARGET_DIR" && pwd)"
target_abs="$(cd "$RSYNC_TARGET_DIR" && pwd)"
case "$target_abs" in rel_target="."
"$repo_abs"/*) ;;
*)
echo "RSYNC_TARGET_DIR must be inside GIT_REPO_DIR" >&2
exit 1
;;
esac
rel_target="${target_abs#"$repo_abs"/}" if [[ ! -d "$repo_abs/.git" ]]; then
git -C "$repo_abs" init >/dev/null
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/"
fi 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 exit 0
fi fi
timestamp="$(date '+%Y-%m-%d %H:%M:%S')" 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"