Syncing lists between devices

I have two raspberry pis on my network running Pi-hole, but keeping the whitelist and blacklist entries in sync between them was a chore, so I wrote pihole-sync:

It’s simple to use: just clone, add details of your devices to settings.py (see the README), then run python3 sync.py. Enjoy!

2 Likes

Pretty cool aproach to it.

One question though… after sync, how do you get the Pi-hole instance to read/apply the changes, if any?

Probably a script that watches the file(s) for changes, then runs pihole -g to update gravity when it detects a change.

The changes are applied automatically. The script uses the PHP API, which handles the dnsmasq refresh. (Under the hood, the API is just shelling out to sudo pihole -w and sudo pihole -b - see here.)

1 Like

I see the function (now that I fully reviewed the code).

Well done !

On a new pi install, I cloned the repo and I’m getting a script error. I’m a python newbie. Any guidance?
python3 sync.py

File “sync.py”, line 18
address: str
^
SyntaxError: invalid syntax

Thx

1 Like

Good catch! This was my fault. The script relies on features introduced in Python 3.6, but the latest Raspbian distribution ships with Python 3.5.3. So this script for aiding administration of Raspberry Pis won’t run on an out-of-the-box Raspberry Pi. Doh! :laughing:

I just pushed a fix to Github. If you do a git pull you should find it now works. Apologies for the confusion!

confirmed good. thank you!

+1 script works.
sad part: getting another error. It attempts to sync one whitelist entry, then throws an error.

quick fix is defining argument (missed one).
line 95 list_api_arg = “”

python3 sync.py

  • Adding s3.amazonaws.com to WHITELIST on 192.168.100.100
    Traceback (most recent call last):
    File “sync.py”, line 185, in
    sync_lists(hosts)
    File “sync.py”, line 176, in sync_lists
    sync_count = _sync_list(hosts, list_type)
    File “sync.py”, line 167, in _sync_list
    h.add_list_entry(entry, list_type)
    File “sync.py”, line 95, in add_list_entry
    list_api_arg
    UnboundLocalError: local variable ‘list_api_arg’ referenced before assignment

Thanks, good catch! Have updated the code and pushed the fix to Github.

I was able to get your script working with my 2 Pi-holes… but it did not function as I expected when I removed a domain from the whitelist on Pi-hole 1. Is the procedure for removing items from lists to manually remove them from each host? Kind of defeats the purpose of a sync script. :slight_smile:

Would you consider editing the script to designate one host as the “primary” and then all other hosts as “secondary?” That way the sync always goes in one direction.