Skip to content

Instantly share code, notes, and snippets.

@pancudaniel7
Last active January 7, 2024 23:27
Show Gist options
  • Save pancudaniel7/604668a51624c199ad348aa8cf79720b to your computer and use it in GitHub Desktop.
Save pancudaniel7/604668a51624c199ad348aa8cf79720b to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script will backup your n last commits in to it to git-backups directory
BACKUP_DIR="$HOME/git-backups"
LOG_FILE="/var/log/git_backup.log"
GIT_REPO_DIR="${1:-/Users/pancudaniel/workspace/pragma-infrastructure}"
mkdir -p "$BACKUP_DIR"
TIMESTAMP=$(date +"%Y-%m-%d")
if [ -d "$GIT_REPO_DIR" ]; then
cd "$GIT_REPO_DIR" || exit 1
current_branch=$(git symbolic-ref --short HEAD)
for commit in $(git log --pretty=format:"%h" -n 5); do
backup_name="${current_branch}_${commit}_backup_$TIMESTAMP.tar.gz"
git archive --format=tar.gz -o "$BACKUP_DIR/$backup_name" "$commit"
message="Backup of commit $commit created: $backup_name"
echo "$message" | tee -a "$LOG_FILE"
done
else
message="Git repository does not exist: $GIT_REPO_DIR"
echo "$message" | tee -a "$LOG_FILE"
fi
# remove files odler then 10 days
find ~/git-backups/pragma-infrastructure/ -type f -mtime +10 -exec rm {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment