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 to replicate
|
||||||
TABLES=("dailyweather" "hourlyweather" "short_urls" "click_logs" "locations")
|
TABLES=("dailyweather" "hourlyweather" "short_urls" "click_logs" "locations" "query_tracking")
|
||||||
|
|
||||||
# PostgreSQL binaries
|
# PostgreSQL binaries
|
||||||
PSQL="/Applications/Postgres.app/Contents/Versions/latest/bin/psql"
|
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;"
|
run_sql $SOURCE_HOST $SOURCE_PORT $SOURCE_DB $SOURCE_USER $SOURCE_PASS "SELECT COUNT(*) FROM $table;"
|
||||||
done
|
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
|
# Dump and restore each table
|
||||||
for table in "${TABLES[@]}"; do
|
for table in "${TABLES[@]}"; do
|
||||||
echo "Replicating $table"
|
echo "Replicating $table"
|
||||||
|
|
||||||
# Dump table
|
if [ "$table" == "query_tracking" ]; then
|
||||||
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
|
# 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
|
# Dump data
|
||||||
echo "Error dumping $table"
|
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
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Drop and recreate table on target
|
# Drop and recreate table on target
|
||||||
run_sql $target_host $target_port $target_db $target_user $target_pass "DROP TABLE IF EXISTS $table CASCADE; "
|
run_sql $target_host $target_port $target_db $target_user $target_pass "DROP TABLE IF EXISTS $table CASCADE; "
|
||||||
|
|
||||||
# Restore table
|
# Restore structure
|
||||||
PGPASSWORD=$target_pass $PSQL -h $target_host -p $target_port -U $target_user -d $target_db -f ${table}_dump.sql
|
PGPASSWORD=$target_pass $PSQL -h $target_host -p $target_port -U $target_user -d $target_db -f ${table}_structure.sql
|
||||||
|
|
||||||
if [ $? -ne 0 ]; then
|
# Restore data
|
||||||
echo "Error restoring $table"
|
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
|
else
|
||||||
echo "$table replicated successfully"
|
# Dump table
|
||||||
fi
|
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
|
if [ $? -ne 0 ]; then
|
||||||
rm ${table}_dump.sql
|
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
|
done
|
||||||
|
|
||||||
# Verify replication
|
# Verify replication
|
||||||
|
@ -81,6 +108,7 @@ replicate_to_target() {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Main replication process
|
# Main replication process
|
||||||
for target in "${TARGETS[@]}"; do
|
for target in "${TARGETS[@]}"; do
|
||||||
replicate_to_target "$target"
|
replicate_to_target "$target"
|
||||||
|
|
Loading…
Reference in a new issue