Last active
September 14, 2025 19:22
-
-
Save Ruzzz/f2e2eba143439191115483c2bea03c22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # Restore settings of Git, GPG, SSH and direnv settings | |
| # Example: ./restore-git-opts.sh ~/git-opts-my-team ~/Projects/MyTeam | |
| # | |
| set -euo pipefail | |
| if [ $# -lt 2 ]; then | |
| echo "Usage: $0 <settings_path> <projects_dir>" | |
| exit 1 | |
| fi | |
| OPTS_DIR="$1" | |
| PROJECTS_DIR="$2" | |
| if [ ! -d "$OPTS_DIR" ]; then | |
| echo "[!] Settings $OPTS_DIR not found!" | |
| exit 1 | |
| fi | |
| # Restore GPG keys | |
| echo "[*] Restoring GPG keys" | |
| shopt -s nullglob | |
| for pub in "$OPTS_DIR"/gpg/*_pub.gpg; do | |
| keyid=$(basename "$pub" | sed 's/_pub.gpg//') | |
| if gpg --list-keys "$keyid" &>/dev/null; then | |
| echo " [=] Public GPG key already exists: $keyid (skipped)" | |
| else | |
| echo " [+] Importing public key: $keyid" | |
| gpg --import "$pub" | |
| fi | |
| done | |
| for priv in "$OPTS_DIR"/gpg/*_priv.gpg; do | |
| keyid=$(basename "$priv" | sed 's/_priv.gpg//') | |
| if gpg --list-secret-keys "$keyid" &>/dev/null; then | |
| echo " [=] Private GPG key already exists: $keyid (skipped)" | |
| else | |
| echo " [+] Importing private key: $keyid" | |
| gpg --import "$priv" | |
| fi | |
| done | |
| shopt -u nullglob | |
| # Restore SSH keys | |
| echo "[*] Restoring SSH keys" | |
| mkdir -p ~/.ssh | |
| chmod 700 ~/.ssh | |
| shopt -s nullglob | |
| for keyfile in "$OPTS_DIR"/ssh/*; do | |
| keyname=$(basename "$keyfile") | |
| dest="$HOME/.ssh/$keyname" | |
| if [ -f "$dest" ]; then | |
| echo " [=] SSH file already exists: $keyname (skipped)" | |
| else | |
| echo " [+] Copying SSH file: $keyname" | |
| cp "$keyfile" "$dest" | |
| chmod 600 "$dest" | |
| fi | |
| done | |
| shopt -u nullglob | |
| # Restore global Git config | |
| if [ -f "$OPTS_DIR/git/gitconfig" ]; then | |
| if [ -f "$HOME/.gitconfig" ]; then | |
| echo "[=] ~/.gitconfig already exists (skipped)" | |
| else | |
| echo "[+] Restoring ~/.gitconfig" | |
| cp "$OPTS_DIR/git/gitconfig" "$HOME/.gitconfig" | |
| fi | |
| fi | |
| # Restore project Git configs (always overwrite) | |
| echo "[*] Restoring project .git/config files" | |
| if [ -d "$OPTS_DIR/git/projects" ]; then | |
| (cd "$OPTS_DIR/git/projects" && find . -type f -path "*/config") | while read -r relpath; do | |
| project_dir="$PROJECTS_DIR/${relpath%/config}" | |
| dest="$project_dir/config" | |
| src="$OPTS_DIR/git/projects/$relpath" | |
| mkdir -p "$project_dir" | |
| echo " [+] Restoring $dest" | |
| cp -f "$src" "$dest" | |
| done | |
| fi | |
| # Restore direnv .envrc files | |
| echo "[*] Restoring .envrc files" | |
| if [ -d "$OPTS_DIR/direnv" ]; then | |
| (cd "$OPTS_DIR/direnv" && find . -type f -name ".envrc") | while read -r relpath; do | |
| dest="$PROJECTS_DIR/$relpath" | |
| mkdir -p "$(dirname "$dest")" | |
| if [ -f "$dest" ]; then | |
| echo " [=] .envrc already exists: $dest (skipped)" | |
| else | |
| echo " [+] Restoring $dest" | |
| cp "$OPTS_DIR/direnv/$relpath" "$dest" | |
| fi | |
| done | |
| fi | |
| echo "[✔] Restore complete" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment