Created
December 30, 2012 14:55
-
-
Save martinhbramwell/4413122 to your computer and use it in GitHub Desktop.
A bash script to compare row counts of tables in two databases on separate machines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
DROP_ROWCOUNTS="\"DROP TABLE IF EXISTS rowcounts;\"" | |
MAKE_ROWCOUNTS="\"CREATE TABLE rowcounts | |
( | |
name_server character varying(64) NOT NULL | |
, name_table character varying(64) NOT NULL | |
, count integer | |
);\"" | |
# | |
PRMRY_SVR="-d erp_db_backoffice -h 192.168.122.33" | |
PRMRY="BackOffice" | |
SCNDRY_SVR="-d erp_db_webshop -h 192.168.122.49" | |
SCNDRY="WebShop" | |
PSQL_OPTIONS="psql -U postgres -At"; | |
PGCMD="\" | |
SELECT tablename | |
FROM pg_tables | |
WHERE tableowner = 'usropenerp' | |
AND schemaname='public' | |
\"" | |
INSERTS_START="SELECT 'INSERT INTO rowcounts VALUES (" | |
INSERTS_MIDDLE=", ' || '" | |
INSERTS_END=", ' || count(*) || ');' from" | |
echo "Create the row counts table ... " | |
eval "$PSQL_OPTIONS $PRMRY_SVR -c $DROP_ROWCOUNTS" | |
eval "$PSQL_OPTIONS $PRMRY_SVR -c $MAKE_ROWCOUNTS" | |
echo "Processing secondary server...." | |
SVR=$SCNDRY_SVR | |
NAME=$SCNDRY | |
echo "Collect table names ... " | |
TABLENAMES=$(eval "$PSQL_OPTIONS $SVR -c $PGCMD") | |
echo "Record table row counts ...." | |
for TABLENAME in $TABLENAMES; do | |
PGSELECT="$PSQL_OPTIONS $SVR -c \"$INSERTS_START''$NAME''$INSERTS_MIDDLE''$TABLENAME''$INSERTS_END $TABLENAME limit 10;\"" | |
# | |
# echo "$PGSELECT" | |
# psql -U postgres -At -d erp_db_webshop -h 192.168.122.49 | |
# -c "SELECT 'INSERT INTO rowcounts VALUES (''WebShop'' | |
# , ' || '''wkf_workitem'', ' || count(*) || ');' from wkf_workitem;" | |
# | |
RESULT=$(eval "$PGSELECT") | |
PGINSERT="$PSQL_OPTIONS $PRMRY_SVR -c \"$RESULT\"" | |
eval "$PGINSERT" | |
done | |
echo "Processing primary server...." | |
SVR=$PRMRY_SVR | |
NAME=$PRMRY | |
echo "Collect table names ... " | |
TABLENAMES=$(eval "$PSQL_OPTIONS $SVR -c $PGCMD") | |
echo "Record table row counts ...." | |
for TABLENAME in $TABLENAMES; do | |
PGSELECT="$PSQL_OPTIONS $SVR -c \"$INSERTS_START''$NAME''$INSERTS_MIDDLE''$TABLENAME''$INSERTS_END $TABLENAME;\"" | |
# | |
# echo "$PGSELECT" | |
# psql -U postgres -At -d erp_db_webshop -h 192.168.122.49 | |
# -c "SELECT 'INSERT INTO rowcounts VALUES (''WebShop'' | |
# , ' || '''wkf_workitem'', ' || count(*) || ');' from wkf_workitem;" | |
# | |
RESULT=$(eval "$PGSELECT") | |
PGINSERT="$PSQL_OPTIONS $PRMRY_SVR -c \"$RESULT\"" | |
eval "$PGINSERT" | |
done | |
# | |
# | |
GET_RESULTS="\"SELECT W.name_table as \\\"Table\\\", W.count as \\\"Shop\\\", B.count as \\\"Office\\\" FROM rowcounts W, rowcounts B where W.name_server='WebShop' and W.name_table = B.name_table and W.count != B.count;\"" | |
# | |
echo $GET_RESULTS | |
eval "$PSQL_OPTIONS $PRMRY_SVR -c $GET_RESULTS" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment