fix: improve backup script error handling
- Check if directory is a git repository before proceeding - Add fallback for git switch (use checkout if switch fails) - Handle offline/no-origin scenarios gracefully - Use local HEAD instead of origin/main for comparison - Add error handling for push operations - Continue backup locally if remote push fails
This commit is contained in:
parent
ae2e757fb2
commit
f2a5dcecda
@ -20,16 +20,26 @@ backup_repo() {
|
|||||||
|
|
||||||
cd "$repo_path"
|
cd "$repo_path"
|
||||||
|
|
||||||
|
# Check if this is a git repository
|
||||||
|
if ! git rev-parse --git-dir > /dev/null 2>&1; then
|
||||||
|
echo -e "${RED}❌ $repo_name: Not a git repository!${NC}"
|
||||||
|
cd ..
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
# Ensure we're on main
|
# Ensure we're on main
|
||||||
CURRENT_BRANCH=$(git branch --show-current)
|
CURRENT_BRANCH=$(git branch --show-current 2>/dev/null || echo "")
|
||||||
if [ "$CURRENT_BRANCH" != "main" ]; then
|
if [ "$CURRENT_BRANCH" != "main" ]; then
|
||||||
echo "Switching to main branch..."
|
echo "Switching to main branch..."
|
||||||
git switch main
|
git switch main 2>/dev/null || git checkout main
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Pull latest changes
|
# Pull latest changes
|
||||||
echo "Pulling latest changes..."
|
echo "Pulling latest changes..."
|
||||||
git pull origin main
|
if ! git pull origin main 2>/dev/null; then
|
||||||
|
echo -e "${YELLOW}⚠️ Could not pull latest changes (might be offline or no origin/main)${NC}"
|
||||||
|
# Continue anyway with local state
|
||||||
|
fi
|
||||||
|
|
||||||
# Check if working directory is clean
|
# Check if working directory is clean
|
||||||
if [ -n "$(git status --porcelain)" ]; then
|
if [ -n "$(git status --porcelain)" ]; then
|
||||||
@ -43,10 +53,10 @@ backup_repo() {
|
|||||||
LATEST_BACKUP=$(git branch -r --sort=-committerdate 2>/dev/null | grep "origin/backup/main-" | head -1 | cut -d'/' -f2 || true)
|
LATEST_BACKUP=$(git branch -r --sort=-committerdate 2>/dev/null | grep "origin/backup/main-" | head -1 | cut -d'/' -f2 || true)
|
||||||
|
|
||||||
if [ -n "$LATEST_BACKUP" ]; then
|
if [ -n "$LATEST_BACKUP" ]; then
|
||||||
MAIN_COMMIT=$(git rev-parse origin/main)
|
MAIN_COMMIT=$(git rev-parse HEAD 2>/dev/null || echo "")
|
||||||
BACKUP_COMMIT=$(git rev-parse origin/$LATEST_BACKUP 2>/dev/null || true)
|
BACKUP_COMMIT=$(git rev-parse origin/$LATEST_BACKUP 2>/dev/null || echo "")
|
||||||
|
|
||||||
if [ "$MAIN_COMMIT" = "$BACKUP_COMMIT" ]; then
|
if [ -n "$MAIN_COMMIT" ] && [ "$MAIN_COMMIT" = "$BACKUP_COMMIT" ]; then
|
||||||
echo -e "${GREEN}✅ $repo_name: Already backed up in $LATEST_BACKUP${NC}"
|
echo -e "${GREEN}✅ $repo_name: Already backed up in $LATEST_BACKUP${NC}"
|
||||||
cd ..
|
cd ..
|
||||||
return 0
|
return 0
|
||||||
@ -58,7 +68,14 @@ backup_repo() {
|
|||||||
|
|
||||||
echo "Creating backup branch: $BACKUP_BRANCH"
|
echo "Creating backup branch: $BACKUP_BRANCH"
|
||||||
git checkout -b $BACKUP_BRANCH
|
git checkout -b $BACKUP_BRANCH
|
||||||
git push -u origin $BACKUP_BRANCH
|
|
||||||
|
# Try to push, but continue even if it fails
|
||||||
|
if git push -u origin $BACKUP_BRANCH 2>/dev/null; then
|
||||||
|
echo -e "${GREEN}✅ Backup pushed to remote${NC}"
|
||||||
|
else
|
||||||
|
echo -e "${YELLOW}⚠️ Could not push backup to remote (backup exists locally only)${NC}"
|
||||||
|
fi
|
||||||
|
|
||||||
git checkout main
|
git checkout main
|
||||||
|
|
||||||
echo -e "${GREEN}✅ $repo_name: Backup created successfully${NC}"
|
echo -e "${GREEN}✅ $repo_name: Backup created successfully${NC}"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user