#!/bin/sh set -e # Define your backup root directory BACKUP_ROOT="/Backup" # Only run if we are inside /etc and it's a git repo if [ -d .git ]; then # Check if there were any changes committed in the last git commit (or check working tree) # Here we check if the HEAD commit modified any files CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r HEAD) if [ -n "$CHANGED_FILES" ]; then # Generate date-encoded folder (e.g., 2026-11-12-02-20) TIMESTAMP=$(date +%Y-%m-%d-%H-%M) DEST="$BACKUP_ROOT/$TIMESTAMP/etc" # Create destination directory mkdir -p "$DEST" # Copy only the files that were changed in this commit, maintaining directory structure echo "$CHANGED_FILES" | while read -r file; do if [ -f "$file" ]; then mkdir -p "$DEST/$(dirname "$file")" cp -p "$file" "$DEST/$file" fi done echo "etckeeper backup: Copied changed files to $DEST" fi fi