mirror of
https://github.com/stevejenkins/pihole-cloudsync.git
synced 2024-08-30 18:22:11 +00:00
Updating to try using dump instead of csv since sql keyword as a table name wrecks everything
This commit is contained in:
parent
4c22df559e
commit
8033dbebf1
@ -32,17 +32,19 @@ update='December 26, 2020'
|
|||||||
# Project Home: https://github.com/stevejenkins/pihole-cloudsync
|
# Project Home: https://github.com/stevejenkins/pihole-cloudsync
|
||||||
###########################################################################
|
###########################################################################
|
||||||
# CONSTANTS
|
# CONSTANTS
|
||||||
personal_git_dir='/usr/local/bin/my-pihole-lists'
|
personal_git_dir='/data/pihole/pihole-config'
|
||||||
pihole_dir='/etc/pihole'
|
pihole_dir='/etc/pihole'
|
||||||
gravity_db='/etc/pihole/gravity.db'
|
gravity_db='/etc/pihole/gravity.db'
|
||||||
dnsmasq_dir='/etc/dnsmasq.d'
|
dnsmasq_dir='/etc/dnsmasq.d'
|
||||||
ad_list='adlist.csv'
|
|
||||||
custom_list='custom.list'
|
custom_list='custom.list'
|
||||||
domain_list='domainlist.csv'
|
|
||||||
cname_list='05-pihole-custom-cname.conf'
|
cname_list='05-pihole-custom-cname.conf'
|
||||||
###########################################################################
|
###########################################################################
|
||||||
# SHOULDN'T NEED TO EDIT BELOW THIS LINE
|
# SHOULDN'T NEED TO EDIT BELOW THIS LINE
|
||||||
|
|
||||||
|
# List of DB tables we need to migrate between instances
|
||||||
|
DB_TABLES="adlist domainlist group domainlist_by_group"
|
||||||
|
DB_DUMP_FILE="db_dump.sql"
|
||||||
|
|
||||||
# Force sudo if not running with root privileges
|
# Force sudo if not running with root privileges
|
||||||
SUDO=''
|
SUDO=''
|
||||||
if [ "$EUID" -ne 0 ]; then
|
if [ "$EUID" -ne 0 ]; then
|
||||||
@ -73,17 +75,15 @@ if [ -n "${DOCKER_CMD}" ]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export_table () {
|
export_tables () {
|
||||||
table="$1"
|
$SUDO sqlite3 $gravity_db ".dump --preserve-rowids $DB_TABLES" > $DB_DUMP_FILE
|
||||||
|
for t in $DB_TABLES; do
|
||||||
$SUDO sqlite3 $gravity_db -header -csv "SELECT * FROM \"$table\"" >"${table}.csv"
|
sed -i "/BEGIN TRAN/a DROP TABLE IF EXISTS $t;" $DB_DUMP_FILE
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
import_table () {
|
import_tables () {
|
||||||
table="$1"
|
$SUDO sqlite3 $gravity_db ".read '|$DB_DUMP_FILE'"
|
||||||
|
|
||||||
$SUDO sqlite3 $gravity_db "DROP TABLE \"$table\";"
|
|
||||||
$SUDO sqlite3 $gravity_db -header -csv ".import \"${table}.csv\" \"$table\""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# FUNCTIONS
|
# FUNCTIONS
|
||||||
@ -103,10 +103,7 @@ push_initialize () {
|
|||||||
cd $personal_git_dir || exit
|
cd $personal_git_dir || exit
|
||||||
|
|
||||||
# Export Ad and Domain lists from Gravity database
|
# Export Ad and Domain lists from Gravity database
|
||||||
export_table "adlist"
|
export_tables
|
||||||
export_table "domainlist"
|
|
||||||
export_table "group"
|
|
||||||
export_table "domainlist_by_group"
|
|
||||||
|
|
||||||
# Add all lists to local Git repo
|
# Add all lists to local Git repo
|
||||||
$SUDO git add .
|
$SUDO git add .
|
||||||
@ -136,10 +133,7 @@ pull_initialize () {
|
|||||||
$SUDO cp $cname_list $dnsmasq_dir
|
$SUDO cp $cname_list $dnsmasq_dir
|
||||||
|
|
||||||
# Overwrite local database tables
|
# Overwrite local database tables
|
||||||
import_table "adlist"
|
import_tables
|
||||||
import_table "domainlist"
|
|
||||||
import_table "group"
|
|
||||||
import_table "domainlist_by_group"
|
|
||||||
|
|
||||||
# Restart Pi-hole to pick up changes
|
# Restart Pi-hole to pick up changes
|
||||||
$SUDO ${DOCKER} pihole -g
|
$SUDO ${DOCKER} pihole -g
|
||||||
@ -160,10 +154,7 @@ push () {
|
|||||||
cd $personal_git_dir || exit
|
cd $personal_git_dir || exit
|
||||||
|
|
||||||
# Export Ad and Domain lists from Gravity database
|
# Export Ad and Domain lists from Gravity database
|
||||||
export_table "adlist"
|
export_tables
|
||||||
export_table "domainlist"
|
|
||||||
export_table "group"
|
|
||||||
export_table "domainlist_by_group"
|
|
||||||
|
|
||||||
# Compare local files to remote Git repo
|
# Compare local files to remote Git repo
|
||||||
$SUDO git remote update > /dev/null
|
$SUDO git remote update > /dev/null
|
||||||
@ -204,10 +195,7 @@ pull () {
|
|||||||
$SUDO ${DOCKER} service pihole-FTL stop
|
$SUDO ${DOCKER} service pihole-FTL stop
|
||||||
$SUDO cp $custom_list $pihole_dir
|
$SUDO cp $custom_list $pihole_dir
|
||||||
$SUDO cp $cname_list $dnsmasq_dir
|
$SUDO cp $cname_list $dnsmasq_dir
|
||||||
import_table "adlist"
|
import_tables
|
||||||
import_table "domainlist"
|
|
||||||
import_table "group"
|
|
||||||
import_table "domainlist_by_group"
|
|
||||||
$SUDO ${DOCKER} pihole -g
|
$SUDO ${DOCKER} pihole -g
|
||||||
echo 'Done!';
|
echo 'Done!';
|
||||||
exit 0
|
exit 0
|
||||||
|
Loading…
Reference in New Issue
Block a user