Auto-update: Mon Aug 12 22:09:30 PDT 2024
This commit is contained in:
parent
e76a059f60
commit
6ec8b5ff16
1 changed files with 49 additions and 21 deletions
|
@ -14,7 +14,7 @@ declare -a TARGETS=(
|
|||
)
|
||||
|
||||
# Tables to replicate
|
||||
TABLES=("dailyweather" "hourlyweather" "short_urls" "click_logs" "locations")
|
||||
TABLES=("dailyweather" "hourlyweather" "short_urls" "click_logs" "locations" "query_tracking")
|
||||
|
||||
# PostgreSQL binaries
|
||||
PSQL="/Applications/Postgres.app/Contents/Versions/latest/bin/psql"
|
||||
|
@ -45,32 +45,59 @@ replicate_to_target() {
|
|||
run_sql $SOURCE_HOST $SOURCE_PORT $SOURCE_DB $SOURCE_USER $SOURCE_PASS "SELECT COUNT(*) FROM $table;"
|
||||
done
|
||||
|
||||
# Ensure uuid-ossp extension is created
|
||||
run_sql $target_host $target_port $target_db $target_user $target_pass "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";"
|
||||
|
||||
# Dump and restore each table
|
||||
for table in "${TABLES[@]}"; do
|
||||
echo "Replicating $table"
|
||||
|
||||
# Dump table
|
||||
PGPASSWORD=$SOURCE_PASS $PG_DUMP -h $SOURCE_HOST -p $SOURCE_PORT -U $SOURCE_USER -d $SOURCE_DB -t $table --no-owner --no-acl > ${table}_dump.sql
|
||||
if [ "$table" == "query_tracking" ]; then
|
||||
# Dump structure
|
||||
PGPASSWORD=$SOURCE_PASS $PG_DUMP -h $SOURCE_HOST -p $SOURCE_PORT -U $SOURCE_USER -d $SOURCE_DB -t $table --schema-only --no-owner --no-acl > ${table}_structure.sql
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error dumping $table"
|
||||
continue
|
||||
fi
|
||||
# Dump data
|
||||
PGPASSWORD=$SOURCE_PASS $PG_DUMP -h $SOURCE_HOST -p $SOURCE_PORT -U $SOURCE_USER -d $SOURCE_DB -t $table --data-only --no-owner --no-acl > ${table}_data.sql
|
||||
|
||||
# Drop and recreate table on target
|
||||
run_sql $target_host $target_port $target_db $target_user $target_pass "DROP TABLE IF EXISTS $table CASCADE; "
|
||||
# Drop and recreate table on target
|
||||
run_sql $target_host $target_port $target_db $target_user $target_pass "DROP TABLE IF EXISTS $table CASCADE; "
|
||||
|
||||
# Restore table
|
||||
PGPASSWORD=$target_pass $PSQL -h $target_host -p $target_port -U $target_user -d $target_db -f ${table}_dump.sql
|
||||
# Restore structure
|
||||
PGPASSWORD=$target_pass $PSQL -h $target_host -p $target_port -U $target_user -d $target_db -f ${table}_structure.sql
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error restoring $table"
|
||||
# Restore data
|
||||
PGPASSWORD=$target_pass $PSQL -h $target_host -p $target_port -U $target_user -d $target_db -f ${table}_data.sql
|
||||
|
||||
# Clean up dump files
|
||||
rm ${table}_structure.sql ${table}_data.sql
|
||||
else
|
||||
echo "$table replicated successfully"
|
||||
fi
|
||||
# Dump table
|
||||
PGPASSWORD=$SOURCE_PASS $PG_DUMP -h $SOURCE_HOST -p $SOURCE_PORT -U $SOURCE_USER -d $SOURCE_DB -t $table --no-owner --no-acl > ${table}_dump.sql
|
||||
|
||||
# Clean up dump file
|
||||
rm ${table}_dump.sql
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error dumping $table"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Clean up the dump file
|
||||
# Remove empty lines, lines with only whitespace, and lines starting with "sij"
|
||||
sed -i.bak '/^\s*$/d; /^sij/d' ${table}_dump.sql && rm ${table}_dump.sql.bak
|
||||
|
||||
# Drop and recreate table on target
|
||||
run_sql $target_host $target_port $target_db $target_user $target_pass "DROP TABLE IF EXISTS $table CASCADE; "
|
||||
|
||||
# Restore table
|
||||
PGPASSWORD=$target_pass $PSQL -h $target_host -p $target_port -U $target_user -d $target_db -f ${table}_dump.sql
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error restoring $table"
|
||||
else
|
||||
echo "$table replicated successfully"
|
||||
fi
|
||||
|
||||
# Clean up dump file
|
||||
rm ${table}_dump.sql
|
||||
fi
|
||||
done
|
||||
|
||||
# Verify replication
|
||||
|
@ -81,6 +108,7 @@ replicate_to_target() {
|
|||
done
|
||||
}
|
||||
|
||||
|
||||
# Main replication process
|
||||
for target in "${TARGETS[@]}"; do
|
||||
replicate_to_target "$target"
|
||||
|
|
Loading…
Reference in a new issue