48 lines
1.7 KiB
Bash
48 lines
1.7 KiB
Bash
# sqlite db replace function
|
|
replace_in_sqlite_db() {
|
|
local DB_PATH="$1"
|
|
local OLD_STRING="$2"
|
|
local NEW_STRING="$3"
|
|
|
|
# Check if the database file exists
|
|
if [ ! -f "$DB_PATH" ]; then
|
|
echo "Error: Database file '$DB_PATH' does not exist."
|
|
exit 1
|
|
fi
|
|
|
|
echo "Starting safe replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..."
|
|
|
|
# Create a backup of the original database
|
|
local BACKUP_PATH="${DB_PATH}.bak"
|
|
cp "$DB_PATH" "$BACKUP_PATH"
|
|
|
|
# Dump the database to SQL
|
|
local DUMP_PATH="${DB_PATH}.sql"
|
|
sqlite3 "$DB_PATH" ".dump" > "$DUMP_PATH"
|
|
|
|
# Replace strings in the SQL dump
|
|
sed -i "s|$OLD_STRING|$NEW_STRING|g" "$DUMP_PATH"
|
|
|
|
# Create a new database from the modified SQL dump
|
|
local NEW_DB_PATH="${DB_PATH}.new"
|
|
sqlite3 "$NEW_DB_PATH" < "$DUMP_PATH"
|
|
|
|
# Replace the original database with the new one
|
|
mv "$NEW_DB_PATH" "$DB_PATH"
|
|
|
|
# Clean up temporary files
|
|
rm "$DUMP_PATH"
|
|
|
|
echo "Replacement completed in '$DB_PATH'. Original database backed up to '$BACKUP_PATH'."
|
|
}
|
|
|
|
#set script directory
|
|
scriptdir="$(dirname "$(realpath "$0")")"
|
|
|
|
#example usage: replace_in_sqlite_db "database.sqlite" "Europe/Amsterdam" "UTC"
|
|
cp $scriptdir/stacks/npm/data/database-pre.sqlite $scriptdir/stacks/npm/data/database.sqlite
|
|
replace_in_sqlite_db "$scriptdir/stacks/npm/data/database.sqlite" "sdgserver.online" "?domain?"
|
|
replace_in_sqlite_db "$scriptdir/stacks/npm/data/database.sqlite" "192.168.2.132" "?localip?"
|
|
replace_in_sqlite_db "$scriptdir/stacks/npm/data/database.sqlite" "z5fGWz2i0q" "?adminpass?"
|
|
replace_in_sqlite_db "$scriptdir/stacks/npm/data/database.sqlite" "0.0.0.0/0" "?localip?"
|