A package to check mysql restore integrity check
Project description
MySQL restore integrity check
Module can be used to test the database integrigity after restore the database. It will compare tables and show the only that tables in tabular format, Which had diffrencial records in after restore.
Requirements
MySQL
- python3.6 or above
- tablular and os python plugin should be installed using pip3
PostgreSQL
- python3.6 or above
- tablular, os, Psycopg2 or Psycopg2-binary python plugin should be installed using pip3
How to use MySQL
$ pip3 install databaseRestoreIntegrity
$ export SRC_DB_HOST='origin_database_hostname'
$ export SRC_DB_USER='origin_database_username'
$ export SRC_DB_PASS='origin_database_password'
$ export DST_DB_HOST='restored_database_hostname'
$ export DST_DB_USER='restored_database_username'
$ export DST_DB_PASS='restored_database_password'
MySQL python script (Create and run on your local or from the host which can connect both db -- restored and active)
from databaseRestoreIntegrity import mysql
from tabulate import tabulate
import os
src_db_host = os.environ['SRC_DB_HOST']
src_db_pass = os.environ['SRC_DB_PASS']
src_db_user = os.environ['SRC_DB_USER']
dst_db_host = os.environ['DST_DB_HOST']
dst_db_pass = os.environ['DST_DB_PASS']
dst_db_user = os.environ['DST_DB_USER']
restored_db = mysql.mysqlReadData(dst_db_host, dst_db_user, dst_db_pass)
active_db = mysql.mysqlReadData(src_db_host, src_db_user, src_db_pass)
restored_db._create_tmp_file('restored_db')
active_db._create_tmp_file('active_db')
restored_db._insert_data('restored_db')
restored_db._insert_data('active_db')
def check_consistency():
unconsistent_tabels = []
conn = restored_db.connection()
check_consistency = conn.cursor()
check_consistency.execute("USE CheckDBsTableConsistency")
check_consistency.execute("select tables_name, records_count, hostname from (select tables_name, hostname, records_count from active_db union all select tables_name, hostname, records_count from restored_db) temp group by tables_name, records_count having count(*) = 1")
result = check_consistency.fetchall()
print(tabulate(result, headers=['tables_name', 'records_count'], tablefmt='psql'))
conn.close()
check_consistency()
How to use PostgreSQL
$ pip3 install databaseRestoreIntegrity
$ export SRC_DB_HOST='origin_database_hostname'
$ export SRC_DB_USER='origin_database_username'
$ export SRC_DB_PASS='origin_database_password'
$ export DST_DB_HOST='restored_database_hostname'
$ export DST_DB_USER='restored_database_username'
$ export DST_DB_PASS='restored_database_password'
$ export DB_NAME='database_name'
Note: Postgres need databsebase name to connectivity
PostgreSQL python script (Create and run on your local or from the host which can connect both db -- restored and active)
from databaseRestoreIntegrity import postgres
from tabulate import tabulate
import os
src_db_host = os.environ['SRC_DB_HOST']
src_db_pass = os.environ['SRC_DB_PASS']
src_db_user = os.environ['SRC_DB_USER']
dst_db_host = os.environ['DST_DB_HOST']
dst_db_pass = os.environ['DST_DB_PASS']
dst_db_user = os.environ['DST_DB_USER']
db_name = os.environ['DB_NAME']
restored_db = postgres.pgsqlReadData(dst_db_host, dst_db_user, dst_db_pass, db_name)
active_db = postgres.pgsqlReadData(src_db_host, src_db_user, src_db_pass, db_name)
restored_db._create_tmp_file('restored_db')
active_db._create_tmp_file('active_db')
restored_db._insert_data('restored_db')
restored_db._insert_data('active_db')
def check_consistency():
unconsistent_tabels = []
sql_query = "SELECT COALESCE(s.tables_name,t.tables_name) id, \
s.records_count records_count, t.records_count records_count, \
CASE WHEN s.records_count = t.records_count THEN 'equal' \
WHEN s.tables_name IS NULL THEN 'Table not on source' \
WHEN t.tables_name IS NULL THEN 'Table not on target' \
ELSE 'difference' \
END compare_result \
FROM CheckDBsTableConsistency.active_db s \
FULL JOIN CheckDBsTableConsistency.restored_db t ON s.tables_name=t.tables_name"
conn = restored_db.connection()
check_consistency = conn.cursor()
check_consistency.execute(sql_query)
result = check_consistency.fetchall()
print(tabulate(result, headers=['tables_name', 'records_count_active_db', 'records_count_restored_db', 'compare_result'], tablefmt='psql'))
conn.close()
check_consistency()
Copyright (c) 2018 The Python Packaging Authority
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for databaseRestoreIntegrity-1.0.9.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bd29716a36cc96dde721579890059c4a0ec320aef5b2bfbb2f24a47699d2607 |
|
MD5 | 53f56eeb4335c479de25cb15c80a90f9 |
|
BLAKE2b-256 | b8236cbdb1749931df0acf614c47802092a1b1a2dd7a42ee16dfba2cb2c4c7b9 |
Hashes for databaseRestoreIntegrity-1.0.9-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db88d768991c3c1012f7d1310fa19e7b224085234dff97c1fb166b006dd36197 |
|
MD5 | 9590e0f7bbdfde2083cb70a43b5926ed |
|
BLAKE2b-256 | 9a2cd1cf4119cf3562d35e997de441a508dd4ff87da63499fe22633204f561a8 |