mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-11-15 02:42:58 +00:00
Only migrate files once (domain and adlist lists druing initial creation of gravity.db auditlog.list on database upgrade from version 1 to 2.
Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
parent
efe8216445
commit
acc50b709e
3 changed files with 46 additions and 59 deletions
|
@ -15,7 +15,16 @@ upgrade_gravityDB(){
|
||||||
version="$(sqlite3 "$1" "SELECT \"value\" FROM \"info\" WHERE \"property\" = 'version';")"
|
version="$(sqlite3 "$1" "SELECT \"value\" FROM \"info\" WHERE \"property\" = 'version';")"
|
||||||
|
|
||||||
if [[ "$version" == "1" ]]; then
|
if [[ "$version" == "1" ]]; then
|
||||||
|
# This migration script upgrades the gravity.db file by
|
||||||
|
# adding the domain_auditlist table
|
||||||
sqlite3 "$1" < "/etc/.pihole/advanced/Scripts/database_migration/gravity/1_to_2.sql"
|
sqlite3 "$1" < "/etc/.pihole/advanced/Scripts/database_migration/gravity/1_to_2.sql"
|
||||||
version=2
|
version=2
|
||||||
|
|
||||||
|
# Store audit domains in database table
|
||||||
|
if [ -e "${auditFile}" ]; then
|
||||||
|
echo -e " ${INFO} Migrating content of ${auditFile} into new database"
|
||||||
|
# database_table_from_file is defined in gravity.sh
|
||||||
|
database_table_from_file "domain_auditlist" "${auditFile}"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,20 +81,13 @@ CREATE TABLE gravity
|
||||||
domain TEXT PRIMARY KEY
|
domain TEXT PRIMARY KEY
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE domain_auditlist
|
|
||||||
(
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
domain TEXT UNIQUE NOT NULL,
|
|
||||||
date_added INTEGER NOT NULL DEFAULT (cast(strftime('%s', 'now') as int))
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE info
|
CREATE TABLE info
|
||||||
(
|
(
|
||||||
property TEXT PRIMARY KEY,
|
property TEXT PRIMARY KEY,
|
||||||
value TEXT NOT NULL
|
value TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
INSERT INTO info VALUES("version","2");
|
INSERT INTO info VALUES("version","1");
|
||||||
|
|
||||||
CREATE VIEW vw_gravity AS SELECT domain
|
CREATE VIEW vw_gravity AS SELECT domain
|
||||||
FROM gravity
|
FROM gravity
|
||||||
|
|
29
gravity.sh
29
gravity.sh
|
@ -17,8 +17,7 @@ coltable="/opt/pihole/COL_TABLE"
|
||||||
source "${coltable}"
|
source "${coltable}"
|
||||||
regexconverter="/opt/pihole/wildcard_regex_converter.sh"
|
regexconverter="/opt/pihole/wildcard_regex_converter.sh"
|
||||||
source "${regexconverter}"
|
source "${regexconverter}"
|
||||||
readonly databaseMigrationScript="/etc/.pihole/advanced/Scripts/database_migration/gravity-db.sh"
|
source "/etc/.pihole/advanced/Scripts/database_migration/gravity-db.sh"
|
||||||
source "${databaseMigrationScript}"
|
|
||||||
|
|
||||||
basename="pihole"
|
basename="pihole"
|
||||||
PIHOLE_COMMAND="/usr/local/bin/${basename}"
|
PIHOLE_COMMAND="/usr/local/bin/${basename}"
|
||||||
|
@ -116,7 +115,6 @@ database_table_from_file() {
|
||||||
else
|
else
|
||||||
# Apply format for white-, blacklist, regex, and adlist tables
|
# Apply format for white-, blacklist, regex, and adlist tables
|
||||||
# Read file line by line
|
# Read file line by line
|
||||||
if [[ "${table}" == "auditlist" ]]; then
|
|
||||||
local rowid
|
local rowid
|
||||||
declare -i rowid
|
declare -i rowid
|
||||||
rowid=1
|
rowid=1
|
||||||
|
@ -124,25 +122,16 @@ database_table_from_file() {
|
||||||
do
|
do
|
||||||
# Only add non-empty lines
|
# Only add non-empty lines
|
||||||
if [[ -n "${domain}" ]]; then
|
if [[ -n "${domain}" ]]; then
|
||||||
|
if [[ "${table}" == "auditlist" ]]; then
|
||||||
# Auditlist table format
|
# Auditlist table format
|
||||||
echo "${rowid},\"${domain}\",${timestamp}" >> "${tmpFile}"
|
echo "${rowid},\"${domain}\",${timestamp}" >> "${tmpFile}"
|
||||||
rowid+=1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
else
|
else
|
||||||
local rowid
|
|
||||||
declare -i rowid
|
|
||||||
rowid=1
|
|
||||||
grep -v '^ *#' < "${source}" | while IFS= read -r domain
|
|
||||||
do
|
|
||||||
# Only add non-empty lines
|
|
||||||
if [[ -n "${domain}" ]]; then
|
|
||||||
# White-, black-, and regexlist format
|
# White-, black-, and regexlist format
|
||||||
echo "${rowid},\"${domain}\",1,${timestamp},${timestamp},\"Migrated from ${source}\"" >> "${tmpFile}"
|
echo "${rowid},\"${domain}\",1,${timestamp},${timestamp},\"Migrated from ${source}\"" >> "${tmpFile}"
|
||||||
|
fi
|
||||||
rowid+=1
|
rowid+=1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
|
||||||
inputfile="${tmpFile}"
|
inputfile="${tmpFile}"
|
||||||
fi
|
fi
|
||||||
# Store domains in database table specified by ${table}
|
# Store domains in database table specified by ${table}
|
||||||
|
@ -170,12 +159,9 @@ database_table_from_file() {
|
||||||
migrate_to_database() {
|
migrate_to_database() {
|
||||||
# Create database file only if not present
|
# Create database file only if not present
|
||||||
if [ ! -e "${gravityDBfile}" ]; then
|
if [ ! -e "${gravityDBfile}" ]; then
|
||||||
|
# Create new database file - note that this will be created in version 1
|
||||||
echo -e " ${INFO} Creating new gravity database"
|
echo -e " ${INFO} Creating new gravity database"
|
||||||
generate_gravity_database
|
generate_gravity_database
|
||||||
fi
|
|
||||||
|
|
||||||
# Check if gravity database needs to be updated
|
|
||||||
upgrade_gravityDB "${gravityDBfile}"
|
|
||||||
|
|
||||||
# Migrate list files to new database
|
# Migrate list files to new database
|
||||||
if [ -e "${adListFile}" ]; then
|
if [ -e "${adListFile}" ]; then
|
||||||
|
@ -198,11 +184,10 @@ migrate_to_database() {
|
||||||
echo -e " ${INFO} Migrating content of ${regexFile} into new database"
|
echo -e " ${INFO} Migrating content of ${regexFile} into new database"
|
||||||
database_table_from_file "regex" "${regexFile}"
|
database_table_from_file "regex" "${regexFile}"
|
||||||
fi
|
fi
|
||||||
if [ -e "${auditFile}" ]; then
|
|
||||||
# Store audit domains in database
|
|
||||||
echo -e " ${INFO} Migrating content of ${auditFile} into new database"
|
|
||||||
database_table_from_file "domain_auditlist" "${auditFile}"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Check if gravity database needs to be updated
|
||||||
|
upgrade_gravityDB "${gravityDBfile}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Determine if DNS resolution is available before proceeding
|
# Determine if DNS resolution is available before proceeding
|
||||||
|
|
Loading…
Reference in a new issue