stevejenkins / pihole-cloudsync

Syncs blocklists, blacklists, and whitelists across multiple Pi-holes using a private GitHub repo
MIT License
517 stars 104 forks source link

FYI - group management sync will not fully work due to current schema #26

Open dispo2 opened 3 years ago

dispo2 commented 3 years ago

I have raised a 'feature request' on the pihole discourse for this but just FYI the future group sync feature (which I have been looking at coding) will not fully work due to the use of the group keyword for the table name (only this table is affected).

The use of a keyword results in a syntax error when trying to export the table to CSV.

A workaround is to manually create the groups to match but I'm not sure if this will cause issues as internally the DB's will be different.

Coding wise the change is very easy with additional commands for the 3 group tables and 2 client tables but the client side would raise issues if the piholes are in different locations and therefore see different clients. This might require the client sync aspect to be optional via command line flag ??

dispo2 commented 3 years ago

quick update - manually creating the groups on all 'slave' piholes does appear to work as a quick fix however they need to be created / displayed in the same order as on the master.

dispo2 commented 3 years ago

so I have a workaround fix for exporting group in pushes, just enclose the table name so its 'group' (thanks discourse) however this does not appear to work with the .import command as it still raises syntax errors so pulls do not work.

dispo2 commented 3 years ago

So the feature request didnt get anywhere. The only way I have been able to get this to work is make the group import a 3 stage process not 2, so now its a drop group, import to temp, rename temp to group.

$SUDO sqlite3 $gravity_db "DROP TABLE 'group';" $SUDO sqlite3 $gravity_db -header -csv ".import group.csv group_tmp" $SUDO sqlite3 $gravity_db "ALTER TABLE group_tmp RENAME TO 'group';"

This is required because .import produces syntax errors when working with group or group enclosed in '' "" `` etc. I couldnt get any to work. Its messy but at least the import and rename works.

dispo2 commented 3 years ago

going around in circles with this - I let this run through a push/pull cycle and it failed on one machine but worked on another. The fail was during the pull process with sqlite3 being unable to rename the group_tmp table due to conflicts (apparently) within the views.

sqlite> ALTER TABLE group_tmp RENAME TO 'group'; Error: error in view vw_whitelist: no such table: main.group {this is after dropping the original group table}

I'll play some more but I've seen this previously and have no idea what's going on. This only ever appears with the 'group' table so I'm pretty sure its the name of the table forcing extra steps that is the issue but there's not a lot I can do about that. From reading some documentation it appears that renaming tables in sqlite can be problematic due to the way sqlite stores the schema compared to say full sql.

The only main difference I can find between the systems is sqlite version. On the push and failing pull machine its SQLite 3.27.2 2019-02-25 16:06:06

On the working pull machine its SQLite 3.16.2 2017-01-06 16:32:41

So at the moment I am back to manually syncing groups on the various pihole before attempting to pull and have disabled the above drop/import/rename code within my testing.

At least (for me) the groups rarely change

dispo2 commented 3 years ago

Firstly thank you very much for this code. I love how it works and how easy it makes syncing my piholes.

I now have working code for group and client sync thanks to various info's and help from the pihole discourse. The fix required various changes to the cloudsync code (which I was only playing with to see if I could get this working).

I am happy to share what I have, it appears to fully work with group and client sync for all the push/pull functions. Client sync is optional via changing a variable within the script. I didnt want to just paste it here as its not my underlying code.

Let me know and I will post it here or do a pull request, though I did end up making quite a few changes.

Key things I had to change

Other things

bast69 commented 3 years ago

Hi @dispo2 , do you think it's possible to share your awesome work ?

dispo2 commented 3 years ago

This is my modified script which I haven't updated it since Feb 2021, it's pretty solid but use at your own risk. I use this across three pihole, two on the same LAN which sync clients, the third is in the cloud which does not.

All credit to @stevejenkins for the original code, I have made various changes to enable group and client sync, which are enabled by default but can be individually disabled as required by editing variables at the top of the scipt.

This post will of course be deleted if requested by steve as this is not my code.

The use / syntax of the code is unchanged so use --initpush / push, --initpull / pull as normal. All push / push functions are via the github software and are unchanged from the original script so creation of repositories and userid / key access still has to be configured before the script will work.

If for some reason a pull messes up or clients / groups get out of sync on a 'slave' then a delete of groups / clients and an initpull / pull usually fixes it.

There is no risk to the 'master' as the only change is that additional tables are pushed to github. This is version='5.0gc4' to reflect that its pihole-cloudsync V5 with group/client sync additions revision 4

pihole-cloudsync.sh.txt

Delete the .txt extension to use.