Dump and restore DBmarlin PostgreSQL repository DB
Requires DBmarlin 4.11.0 or above.
The DBmarlin repository database is PostgreSQL but simply using the native pg_dump and pg_restore will not leave you with a working database due to TimeScaleDB hypertables and materialized hypertables which need special treatment. We provide wrapper scripts in the scripts directory to take care of this.
On source machine​
Where you are exporting from:
- Stop Tomcat with
./stop.sh -tso no new data is being written to PostgreSQL - Start PostgreSQL with
./start.sh -p(if it isn’t already running). - Use the wrapper script
scripts/dbadmin/pg_dump.shIt takes 1 param which is the dump directory like this.
cd scripts/dbadmin
./pg_dump.sh [dump_dir]
# E.g.
# ./pg_dump.sh /mnt/volume1/pgdump
On the destination machine​
Where you are importing to:
-
scporsftpthe dump file from the source machine to this machine. -
Do a clean install DBmarlin using the exact same version as the one you exported from. Complete the full install including running
configure.sh -
Start PostgreSQL but leave Tomcat stopped.
start.sh -p -
Use the wrapper script
scripts/dbadmin/pg_restore.sh. It takes 1 param which is the dump file like this.cd scripts/dbadmin
./pg_restore.sh [dump_file]
# E.g.
# ./pg_restore.sh /mnt/volume1/pgdump/dbmarlin-postgresql.dumpThe reason you need to use the wrapper script is to avoid the potential pitfalls with using pg_dump and pg_restore with TimeScaleDB (see below).
-
Start up Tomcat and Nginx and check all 3 processes are running.
cd /opt/dbmarlin
./start.sh -t && ./start.sh -n
./status.sh
nginx (Running)
tomcat (Running)
postgres (Running) -
Check logs under
dbmarlin/tomcat/logsanddbmarlin/postgresql/data/logfor any errors.
Files provided​
These are the wrapper scripts provided. Only #1 and #2 are called directly and you can find them under scripts/dbadmin.
pg_dump.sh- wrapper forpg_dumpwith correct params andLD_LIBRARY_PATHset.pg_restore.sh- wrapper forpg_restorewith correct params andLD_LIBRARY_PATHset and several important post restore scripts which are needed for TimeScaleDB to avoid problems.drop_ts_insert_blocker.sql- Called automatically bypg_restore.sh. It removes triggers on TimeScaleDB chunks which shouldn’t be there.timescaledb_pre_restore.sql- Called automatically bypg_restore.sh. Disables TimeScaleDB jobs during restore.timescaledb_post_restore.sql- Called automatically bypg_restore.sh. Re-enables TimeScaleDB jobs.
Problems using pg_dump and pg_restore with TimeScaleDB​
This is just for background information and provides and explanation of why the wrapper scripts are used instead of just calling pg_dump and pg_restore directly.
The pg_restore.sh wrapper script does serval things to avoid problems restoring TimeScaleDB table using pg_restore.
-
It calls
timescaledb_pre_restore.sqlbeforepg_restoreandtimescaledb_post_restore.sqlafterwards. This is to disable the TimeScale jobs from running while data is importing. -
It calls
drop_ts_insert_blocker.sqlafter thepg_restore. This drops the triggerts_insert_blockerfrom all chunks of the hypertables and continuous aggregates (it shouldn't be there - only on the parent tables). This is a TimeScaleDB issue. See https://github.com/timescale/timescaledb-docker/issues/86 and https://github.com/timescale/timescaledb/issues/1298#issuecomment-505865516 for more details. If you don't run this you will see the following errors in the logs.Hint: Make sure the TimescaleDB extension has been preloaded.
org.postgresql.util.PSQLException: ERROR: invalid INSERT on the root table of hypertable "_hyper_4_9_chunk"It also drops the trigger
ts_cagg_invalidation_triggerfrom all chunks of the hypertables and continuous aggregates (it will be there after pg_restore which prevents it being created on the parent which is where it should be). -
It then extracts all the failed
CREATEandALTERstatements from the pg_restore log file and amends them so they are correct, saves aspost_restore.sqland then runs it.- Changes
ALTER TABLE ONLYtoALTER TABLEsinceONLYoption won't work. This allows the Primary Key and Foreign Keys on TimeScaleDB tables to be created. - Reruns the
CREATE TRIGGER ts_cagg_invalidation_triggeron the TimeScaleDB tables which will succeed this time now it was removed from the underlying chunks.
- Changes