homarr-dashboard/update.sh

195 lines
4.5 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
#======================================================================#
# Homarr Update Script #
# Pulls latest changes from Gitea and restarts services #
#======================================================================#
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
REPO_URL="https://gitea.lemonlink.eu/homelab/homarr-dashboard"
print_header() {
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} Homarr Dashboard Update Script ${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
}
print_success() {
echo -e "${GREEN}$1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}$1${NC}"
}
print_info() {
echo -e "${BLUE} $1${NC}"
}
# Check if running in git repo
check_git_repo() {
if [ ! -d .git ]; then
print_error "Not a git repository!"
print_info "Please run this script from the homarr-dashboard directory"
exit 1
fi
}
# Backup current state
backup_data() {
print_info "Creating backup..."
BACKUP_DIR="backups/$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"
if [ -d "homarr/appdata" ]; then
cp -r homarr/appdata "$BACKUP_DIR/" 2>/dev/null || true
fi
if [ -f ".env" ]; then
cp .env "$BACKUP_DIR/" 2>/dev/null || true
fi
print_success "Backup created at $BACKUP_DIR"
}
# Pull latest changes
pull_updates() {
print_info "Pulling latest changes from Gitea..."
# Fetch latest changes
git fetch origin
# Check if there are updates
LOCAL=$(git rev-parse HEAD)
REMOTE=$(git rev-parse origin/main 2>/dev/null || git rev-parse origin/master)
if [ "$LOCAL" = "$REMOTE" ]; then
print_success "Already up to date!"
return 0
fi
# Stash local changes if any
if ! git diff-index --quiet HEAD --; then
print_warning "Local changes detected, stashing..."
git stash
fi
# Pull updates
git pull origin main 2>/dev/null || git pull origin master
print_success "Updated to latest version"
# Show changes
echo ""
print_info "Recent changes:"
git log --oneline -5
}
# Update Docker images
update_containers() {
echo ""
print_info "Pulling latest Docker images..."
docker compose pull
print_success "Docker images updated"
}
# Restart services
restart_services() {
echo ""
print_info "Restarting services..."
docker compose up -d --remove-orphans
print_success "Services restarted"
}
# Cleanup old images
cleanup() {
echo ""
print_info "Cleaning up old Docker images..."
docker image prune -f
print_success "Cleanup complete"
}
# Check service status
check_status() {
echo ""
print_info "Service Status:"
echo "----------------------------------------"
docker compose ps
echo "----------------------------------------"
# Check if services are healthy
if docker compose ps | grep -q "Up"; then
echo ""
print_success "All services are running!"
# Get IP for access info
IP=$(hostname -I | awk '{print $1}')
echo ""
echo -e "${GREEN}Access your dashboard:${NC}"
echo -e " • Homarr: http://$IP:7575"
echo -e " • Dash.: http://$IP:3001"
else
echo ""
print_error "Some services are not running!"
print_info "Check logs with: docker compose logs"
fi
}
# Main function
main() {
print_header
# Check prerequisites
if ! command -v docker &> /dev/null; then
print_error "Docker not found!"
exit 1
fi
if ! command -v git &> /dev/null; then
print_error "Git not found!"
exit 1
fi
check_git_repo
# Show current version
CURRENT_COMMIT=$(git rev-parse --short HEAD)
print_info "Current version: $CURRENT_COMMIT"
# Confirm update
read -p "Proceed with update? (y/N): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_info "Update cancelled"
exit 0
fi
# Execute update steps
backup_data
pull_updates
update_containers
restart_services
cleanup
check_status
echo ""
print_success "Update complete! 🎉"
echo ""
print_info "If you encounter issues, restore from backup:"
echo " cp -r backups/$(ls -t backups | head -1)/* ."
}
# Run main function
main