DHCP Failover with Pi-hole

I

Let’s get that Pi-hole DHCP reservation syncing set up! I’ll walk you through creating a sync script to keep the DHCP reservations (and any other Pi-hole configurations) identical between your Proxmox VM and Raspberry Pi 4. Here’s how we’ll do it!


Step 1: Set up SSH key-based authentication

To allow the Raspberry Pi to pull the DHCP config from the Proxmox VM without a password:

👉 On the Raspberry Pi 4, generate an SSH key (if you haven’t already):

ssh-keygen -t rsa -b 4096 -C "pi-hole-sync"

  • Press Enter to save it to the default location (~/.ssh/id_rsa).
  • No passphrase is needed for this.

👉 Copy the key to your Proxmox Pi-hole VM:

ssh-copy-id <username>@<proxmox_vm_ip>

  • This allows the Raspberry Pi to SSH into the Proxmox VM without a password.

Step 2: Create the sync script

👉 On the Raspberry Pi, create a script (e.g., pi-hole-sync.sh):

#!/bin/bash

# Define variables
PRIMARY_PIHOLE="<proxmox_vm_ip>"
PIHOLE_CONFIG_DIR="/etc/dnsmasq.d"
LOCAL_BACKUP_DIR="/home/pi/pihole_backup"

# Create backup directory if it doesn't exist
mkdir -p $LOCAL_BACKUP_DIR

# Sync DHCP reservations and Pi-hole config files
rsync -avz --delete $PRIMARY_PIHOLE:$PIHOLE_CONFIG_DIR/ $LOCAL_BACKUP_DIR/
rsync -avz --delete $LOCAL_BACKUP_DIR/ $PIHOLE_CONFIG_DIR/

# Restart Pi-hole to apply changes
pihole restartdns

👉 Make the script executable:

chmod +x pi-hole-sync.sh


Step 3: Schedule automatic syncing with cron

👉 Open the crontab on the Raspberry Pi:

crontab -e

👉 Add a cron job to sync every 5 minutes (or whatever interval you prefer):

*/5 * * * * /path/to/pi-hole-sync.sh


Step 4: Test the setup

👉 Run the sync script manually to confirm it works:

./pi-hole-sync.sh

👉 Check the DHCP reservations on the Raspberry Pi Pi-hole to ensure they match the Proxmox VM.


Step 5: DHCP failover

👉 Now that both Pi-hole instances have the same DHCP reservations:

  • Set the Proxmox Pi-hole as the primary DHCP server.
  • Keep the Raspberry Pi Pi-hole DHCP disabled by default.
  • Use a failover script (as mentioned before) to detect when the primary goes down and enable DHCP on the Pi dynamically.

Final result:

  • DHCP reservations stay synced automatically.
  • No more IP conflicts or lost reservations during a failover!
  • Your network stays resilient even if the Proxmox node goes down.

👉 Let me know if you’d like to add monitoring or notifications to this — we can polish this home lab setup to perfection! 🚀

Failover Script

Let’s set that up! I’ll give you a failover script that’ll:
✅ Periodically check if the primary Pi-hole VM is online.
✅ Enable DHCP on the Raspberry Pi if the primary goes down.
✅ Disable DHCP on the Raspberry Pi when the primary comes back online.


Step 1: Create the failover script

👉 On the Raspberry Pi, create a new file:

nano /home/pi/dhcp-failover.sh

👉 Add the following code:

#!/bin/bash

# Primary Pi-hole VM IP address
PRIMARY_PIHOLE_IP="<proxmox_vm_ip>"

# Check if the primary Pi-hole is online
if ping -c 3 $PRIMARY_PIHOLE_IP &> /dev/null; then
    echo "$(date) - Primary Pi-hole is up."
    
    # Check if DHCP is running on the Pi - if so, disable it
    if pihole-FTL dhcp-discover | grep -q "Pi-hole DHCP server: active"; then
        echo "$(date) - Disabling DHCP on the Raspberry Pi (primary is up)."
        pihole disable-dhcp
    fi
else
    echo "$(date) - Primary Pi-hole is DOWN."
    
    # Check if DHCP is running on the Pi - if not, enable it
    if ! pihole-FTL dhcp-discover | grep -q "Pi-hole DHCP server: active"; then
        echo "$(date) - Enabling DHCP on the Raspberry Pi (primary is down)."
        pihole enable-dhcp
    fi
fi

✅ This script:

  • Pings the Proxmox Pi-hole VM.
  • Enables DHCP on the Pi-hole (Raspberry Pi) if the VM is unreachable.
  • Disables DHCP on the Pi-hole (Raspberry Pi) when the VM is back online.

👉 Save and exit the file (CTRL + X → Y → Enter).


Step 2: Make the script executable

chmod +x /home/pi/dhcp-failover.sh


Step 3: Set up a cron job for periodic checking

👉 Open the crontab:

crontab -e

👉 Add this line to check every 5 minutes:

*/5 * * * * /home/pi/dhcp-failover.sh >> /var/log/dhcp-failover.log 2>&1

✅ This logs the DHCP failover status to /var/log/dhcp-failover.log.


Step 4: Test the failover

👉 Run the script manually to verify it works:

/home/pi/dhcp-failover.sh

👉 Then, simulate a failure by shutting down the Proxmox Pi-hole VM and checking if the Raspberry Pi takes over DHCP.


Final result:

  • Primary Pi-hole VM on Proxmox runs DHCP under normal conditions.
  • Secondary Pi-hole (Raspberry Pi) automatically enables DHCP if the primary goes offline.
  • DHCP is disabled again when the primary comes back online — no IP conflicts, just smooth failover!

👉 Let me know if you want to add email alerts or system monitoring to this! 🚀

Local Copy of WordPress Site (ChatGPT)

Yes! You can create a local copy of your WordPress site for backup, testing, or development purposes. Here are the steps:

Method 1: Using Localhost (XAMPP, LocalWP, or Docker)

If you want to run WordPress locally, you can use tools like XAMPP, LocalWP, or Docker.

Step 1: Backup Your Live Site

  1. Download Website Files
    • Use an FTP client (like FileZilla) or your hosting’s file manager to download all files from the public_html (or WordPress root) directory.
  2. Export the Database
    • In your hosting’s control panel, open phpMyAdmin.
    • Select your WordPress database and click Export → Choose SQL format → Click Go.

Step 2: Set Up a Local Server

  • Install XAMPP (Windows/Mac/Linux) or LocalWP (easier for beginners).
  • Start Apache and MySQL services.

Step 3: Copy Files to Local Server

  • Move your WordPress files to C:\xampp\htdocs\your-site (if using XAMPP).
  • For LocalWP, simply create a new site and replace its wp-content folder with your backup.

Step 4: Import the Database

  • Open phpMyAdmin at http://localhost/phpmyadmin/.
  • Create a new database.
  • Import your .sql file.

Step 5: Update wp-config.php

Edit the wp-config.php file to match your local database credentials:

define('DB_NAME', 'your_local_db');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');

Step 6: Fix URLs in Database

Your local site URL may differ from the live site, so update it:

  1. Run this SQL query in phpMyAdmin: UPDATE wp_options SET option_value = 'http://localhost/your-site' WHERE option_name = 'siteurl' OR option_name = 'home';
  2. Alternatively, use the Better Search Replace plugin to change URLs.

Now, visit http://localhost/your-site to access your local WordPress copy!

Would you like to automate backups or sync changes between local and live sites?

Samba Setup (ChatGPT)

This is a copy and paste from ChatGPT (https://chatgpt.com/share/677bd14a-ef84-8002-93fc-b3456d8a9df8). The formatting and loss of copy buttons is a problem.

Setting up a file server on Ubuntu using an external USB drive is a straightforward process. Here’s a step-by-step guide:


1. Prepare Your USB Drive

  1. Plug in the USB Drive: Connect the external USB drive to your Ubuntu machine.
  2. Identify the Drive: Open a terminal and run:bashCopy codelsblk Find the USB drive in the list. It will likely be named something like /dev/sdX (e.g., /dev/sdb1).
  3. Format the Drive (if needed): If the USB drive isn’t already formatted, format it with a suitable file system (e.g., ext4 for Linux systems):bashCopy codesudo mkfs.ext4 /dev/sdX1 Replace /dev/sdX1 with your USB drive’s identifier.
  4. Create a Mount Point:bashCopy codesudo mkdir /mnt/usb
  5. Mount the Drive:bashCopy codesudo mount /dev/sdX1 /mnt/usb Add the drive to /etc/fstab for automatic mounting:bashCopy codeecho "/dev/sdX1 /mnt/usb ext4 defaults 0 0" | sudo tee -a /etc/fstab

2. Install and Configure Samba (File Sharing for Windows/Linux)

  1. Install Samba:bashCopy codesudo apt update sudo apt install samba -y
  2. Configure Samba: Open the Samba configuration file:bashCopy codesudo nano /etc/samba/smb.conf Add a shared folder configuration at the end:iniCopy code[USB_Share] path = /mnt/usb read only = no browsable = yes writable = yes guest ok = no valid users = your_username
  3. Set Samba User: Add your user to Samba:bashCopy codesudo smbpasswd -a your_username
  4. Restart Samba Service:bashCopy codesudo systemctl restart smbd

3. Configure Permissions

Ensure your user has ownership of the USB mount point:

bashCopy codesudo chown -R your_username:your_username /mnt/usb

4. Access the File Server

  • From Linux: Use the smb:// protocol in your file manager.
  • From Windows:
    • Open the File Explorer.
    • Type \\<Ubuntu_IP_Address>\USB_Share in the address bar.
    • Enter your Samba username and password.

Optional: Install and Configure NFS (for Linux Sharing)

If you prefer NFS for Linux clients:

  1. Install the NFS server:bashCopy codesudo apt install nfs-kernel-server -y
  2. Edit the export file:bashCopy codesudo nano /etc/exports Add:plaintextCopy code/mnt/usb *(rw,sync,no_subtree_check)
  3. Restart the NFS service:bashCopy codesudo systemctl restart nfs-kernel-server