#!/usr/bin/env bash # # Installation script for Porkbun Dynamic DNS systemd service # # This script will: # 1. Verify prerequisites # 2. Copy service files to systemd directory # 3. Enable and start the timer # 4. Show status and usage information set -euo pipefail # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Get script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" echo "======================================" echo "Porkbun DDNS Service Installer" echo "======================================" echo "" # Check if running as root if [ "$EUID" -eq 0 ]; then echo -e "${RED}Error: Do not run this script as root or with sudo${NC}" echo "The script will request sudo privileges when needed." exit 1 fi # Verify required files exist echo "Checking required files..." REQUIRED_FILES=( "$SCRIPT_DIR/updateDNS.sh" "$SCRIPT_DIR/.env" "$SCRIPT_DIR/porkbun-ddns.service" "$SCRIPT_DIR/porkbun-ddns.timer" ) for file in "${REQUIRED_FILES[@]}"; do if [ ! -f "$file" ]; then echo -e "${RED}Error: Required file not found: $file${NC}" exit 1 fi done echo -e "${GREEN}✓ All required files found${NC}" echo "" # Check if updateDNS.sh is executable if [ ! -x "$SCRIPT_DIR/updateDNS.sh" ]; then echo "Making updateDNS.sh executable..." chmod +x "$SCRIPT_DIR/updateDNS.sh" echo -e "${GREEN}✓ Made updateDNS.sh executable${NC}" fi # Check for required commands echo "Checking dependencies..." MISSING_DEPS=() for cmd in curl jq systemctl; do if ! command -v "$cmd" &> /dev/null; then MISSING_DEPS+=("$cmd") fi done if [ ${#MISSING_DEPS[@]} -gt 0 ]; then echo -e "${RED}Error: Missing required dependencies: ${MISSING_DEPS[*]}${NC}" echo "Please install them and try again." exit 1 fi echo -e "${GREEN}✓ All dependencies installed${NC}" echo "" # Verify .env file has required variables echo "Validating .env configuration..." if ! grep -q "^API_KEY=" "$SCRIPT_DIR/.env" || \ ! grep -q "^SECRET_KEY=" "$SCRIPT_DIR/.env" || \ ! grep -q "^DOMAIN=" "$SCRIPT_DIR/.env" || \ ! grep -q "^MACHINE_ID=" "$SCRIPT_DIR/.env"; then echo -e "${YELLOW}Warning: .env file may be incomplete${NC}" echo "Required variables: API_KEY, SECRET_KEY, DOMAIN, MACHINE_ID" echo "" read -p "Continue anyway? (y/N) " -n 1 -r echo "" if [[ ! $REPLY =~ ^[Yy]$ ]]; then exit 1 fi else echo -e "${GREEN}✓ .env configuration looks good${NC}" fi echo "" # Test the script echo "Testing DNS update script..." if "$SCRIPT_DIR/updateDNS.sh"; then echo -e "${GREEN}✓ Script test passed${NC}" else echo -e "${RED}Error: Script test failed${NC}" echo "Please fix the script configuration before installing the service." exit 1 fi echo "" # Check if service is already installed if systemctl list-unit-files | grep -q "porkbun-ddns.timer"; then echo -e "${YELLOW}Warning: porkbun-ddns service is already installed${NC}" read -p "Reinstall? (y/N) " -n 1 -r echo "" if [[ ! $REPLY =~ ^[Yy]$ ]]; then exit 0 fi echo "Stopping existing service..." sudo systemctl stop porkbun-ddns.timer 2>/dev/null || true sudo systemctl disable porkbun-ddns.timer 2>/dev/null || true fi # Create temporary service files with correct paths echo "Preparing service files..." TEMP_SERVICE=$(mktemp) TEMP_TIMER=$(mktemp) # Replace paths in service file sed "s|/home/kacper/Documents/platformIO/email/porkbun|$SCRIPT_DIR|g" \ "$SCRIPT_DIR/porkbun-ddns.service" > "$TEMP_SERVICE" # Update user/group in service file CURRENT_USER=$(whoami) CURRENT_GROUP=$(id -gn) sed -i "s|User=kacper|User=$CURRENT_USER|g" "$TEMP_SERVICE" sed -i "s|Group=kacper|Group=$CURRENT_GROUP|g" "$TEMP_SERVICE" # Copy timer file as-is cp "$SCRIPT_DIR/porkbun-ddns.timer" "$TEMP_TIMER" # Install service files echo "Installing systemd service files..." sudo cp "$TEMP_SERVICE" /etc/systemd/system/porkbun-ddns.service sudo cp "$TEMP_TIMER" /etc/systemd/system/porkbun-ddns.timer # Clean up temp files rm "$TEMP_SERVICE" "$TEMP_TIMER" # Set proper permissions sudo chmod 644 /etc/systemd/system/porkbun-ddns.service sudo chmod 644 /etc/systemd/system/porkbun-ddns.timer echo -e "${GREEN}✓ Service files installed${NC}" echo "" # Reload systemd echo "Reloading systemd daemon..." sudo systemctl daemon-reload echo -e "${GREEN}✓ Systemd reloaded${NC}" echo "" # Enable and start timer echo "Enabling and starting porkbun-ddns.timer..." sudo systemctl enable porkbun-ddns.timer sudo systemctl start porkbun-ddns.timer echo -e "${GREEN}✓ Service enabled and started${NC}" echo "" # Show status echo "======================================" echo "Installation Complete!" echo "======================================" echo "" echo "Service Status:" sudo systemctl status porkbun-ddns.timer --no-pager -l || true echo "" echo -e "${GREEN}The Porkbun DDNS service is now running!${NC}" echo "" echo "Useful commands:" echo " View timer status: systemctl status porkbun-ddns.timer" echo " View service logs: journalctl -u porkbun-ddns.service -f" echo " List timers: systemctl list-timers" echo " Run manually: sudo systemctl start porkbun-ddns.service" echo " Stop service: sudo systemctl stop porkbun-ddns.timer" echo " Uninstall: ./uninstall.sh" echo "" echo "The service will update DNS records every 5 minutes." echo "Check logs with: journalctl -u porkbun-ddns.service -n 50"