Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync gravity.db by dumping and restoring #7

Merged
merged 1 commit into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions roles/sync/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
- name: Install sqlite3
apt:
force_apt_get: yes
name: sqlite3
become: yes

- name: Generate ssh keypair
community.crypto.openssh_keypair:
comment: pihole-sync-{{ inventory_hostname }}
Expand Down
10 changes: 8 additions & 2 deletions roles/sync/templates/pihole_sync.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ target="{{ ansible_user }}@{{ sync_target }}"
sync_dir="{{ ansible_user_dir }}/{{ sync_dir.path }}"
pihole_dir="{{ ansible_user_dir }}/pihole"

if [[ $(ip a | grep {{ sync_target }}) ]]; then
nice -n 19 sqlite3 $pihole_dir/pihole/gravity.db ".backup $sync_dir/gravity.dump"
fi

if [[ ! $(ip a | grep {{ sync_target }}) ]]; then
RSYNC_GRAVITY=$(rsync -a --info=name -e "ssh $key $host_key_check" $target:$pihole_dir/pihole/gravity.db $sync_dir)
sleep 60

RSYNC_GRAVITY=$(rsync -a --info=name -e "ssh $key $host_key_check" $target:$sync_dir/gravity.dump $sync_dir)
if [ $? -eq 0 ]; then
if [ -n "$RSYNC_GRAVITY" ]; then
docker stop pihole
sudo cp --preserve=timestamps $sync_dir/gravity.db $pihole_dir/pihole
sudo sqlite3 $pihole_dir/pihole/gravity.db ".restore $sync_dir/gravity.dump"
docker start pihole
fi
fi
Expand Down