#!/bin/sh # MigrateMyDatabase (migratemydatabase.eu) check tool for Linux and macOS: runs the read-only migration scan script and # the read-only data check against ONE database with the database client that is already installed, and writes ONE file # to upload. Nothing to install: POSIX sh plus the client of your database. # # What it does # 1. Collector: runs mmdb-.sql (structure of the database from the system catalog: no table data, no code). # 2. Data check (optional, asked; --no-datacheck skips it): runs mmdb--datacheck.sql, which COUNTS rows that # violate untrusted/disabled constraints or hold values another engine cannot store. Counts only, never rows. It # reads table data, so it can take a while on large tables: run it off-hours. # 3. Writes -mmdb-result.txt in the output folder: the collector output followed by the data check output # (only the MMDB1|… lines), and prints a short summary. # What it does NOT do # - It changes nothing in the database: both scripts are read-only (see their own headers below). The only session # settings are the ones the scripts set themselves (and, for Oracle with --schema, ALTER SESSION SET CURRENT_SCHEMA). # - It sends nothing anywhere. It has no network code of its own; only your database client talks to your database. # - It never writes your password to a file you keep or to the output. Passwords are read without echo (or from # MMDB_PASSWORD / the client's own variable) and handed to the client as follows: MySQL/MariaDB through a temporary # option file (mode 0600, removed at exit), PostgreSQL through PGPASSWORD, SQL Server through SQLCMDPASSWORD, # Oracle on the standard input of sqlplus (CONNECT), so never on a command line visible to other users. # Why a script and not a program: you can read every line of this file before you run it and see that it only reads, # changes nothing and sends nothing; a compiled .exe cannot be checked that way. SHA256SUMS shows your copy is ours. # Files it reads: the SQL below (identical to the files published at https://migratemydatabase.eu/sql/, see # SHA256SUMS there; --show-sql prints one). Files it writes: a temporary folder (removed at exit) and the # result file. # # Clients used (found on PATH, or give --client ), exactly as the script headers document: # SQL Server sqlcmd (mssql-tools18 / mssql-tools) sqlcmd -S [,port] -d -E|-U -i -y 0 # (+ -b -r1 for error codes, -f 65001 for UTF-8, -C with --trust-cert) # PostgreSQL psql -X -At -v ON_ERROR_STOP=1 -f (PGCLIENTENCODING=UTF8) # MySQL / MariaDB mysql or mariadb --batch --skip-column-names --raw --default-character-set=utf8mb4 < # Oracle sqlplus -s -L /nolog, CONNECT on standard input, then @ (NLS_LANG=AMERICAN_AMERICA.AL32UTF8) # Rights: the same as the scripts (see their headers): SQL Server VIEW DEFINITION (db_owner works) + SELECT for the data # check; PostgreSQL CONNECT (+ SELECT on the tables for the data check); MySQL SELECT, SHOW VIEW, TRIGGER, EVENT and # SHOW_ROUTINE; Oracle the schema owner (or SELECT ANY DICTIONARY + SELECT ANY TABLE). # # Usage: sh mmdb-check.sh (asks for everything it needs) # sh mmdb-check.sh --engine postgresql --host db1 --database shop --user reader [--no-datacheck] [--out DIR] # --engine sqlserver|postgresql|mysql|mariadb|oracle --host H --port P --database D --user U # --auth sql|windows (SQL Server: windows = integrated/Kerberos, -E) --trust-cert (SQL Server -C, MariaDB client) # --service S (Oracle service name) --connect C (Oracle connect identifier / TNS alias instead of host/port/service) # --schema S (Oracle: another schema than your own) --client PATH --out DIR (default: current folder) # --datacheck / --no-datacheck (default: ask, yes) --show-sql FILE --help # Exit codes: 0 ok · 1 wrong parameters · 2 client not found · 3 cannot connect / log in · 4 collector failed or # incomplete · 5 data check failed (the file holds the collector part) · 6 written, but rights are missing (listed) set -u umask 077 HERE=$(cd "$(dirname "$0")" 2>/dev/null && pwd) ENGINE= HOST= PORT= DB= USR= AUTH= SERVICE= CONNECT= SCHEMA= TRUST=0 CLIENT= OUTDIR=. DC= SHOWSQL= say() { printf '%s\n' "$*" >&2; } die() { code=$1; shift; say "ERROR: $*"; exit "$code"; } usage() { sed -n '/^# Usage:/,/^# Exit codes/p' "$0" | sed 's/^# \{0,1\}//' >&2; } need() { [ $# -ge 2 ] || die 1 "$1 needs a value"; } while [ $# -gt 0 ]; do case $1 in --engine) need "$@"; ENGINE=$2; shift ;; --host|--server) need "$@"; HOST=$2; shift ;; --port) need "$@"; PORT=$2; shift ;; --database|-d) need "$@"; DB=$2; shift ;; --user|-u) need "$@"; USR=$2; shift ;; --auth) need "$@"; AUTH=$2; shift ;; --service) need "$@"; SERVICE=$2; shift ;; --connect) need "$@"; CONNECT=$2; shift ;; --schema) need "$@"; SCHEMA=$2; shift ;; --trust-cert) TRUST=1 ;; --client) need "$@"; CLIENT=$2; shift ;; --out) need "$@"; OUTDIR=$2; shift ;; --datacheck) DC=1 ;; --no-datacheck) DC=0 ;; --show-sql) need "$@"; SHOWSQL=$2; shift ;; -h|--help) usage; exit 0 ;; *) usage; die 1 "unknown option $1" ;; esac shift done # the SQL scripts: appended to the end of this file by the website build, after the final "exit" (the shell never # reads them; awk copies one out unchanged); a copy without them (the source in the repository) reads the file next to it sql_text() { if grep -qx "#@@MMDB-SQL-BEGIN $1" "$0" 2>/dev/null; then LC_ALL=C awk -v b="#@@MMDB-SQL-BEGIN $1" '$0 == b { on = 1; next } on && $0 == "#@@MMDB-SQL-END" { exit } on' "$0" elif [ -f "$HERE/$1" ]; then cat "$HERE/$1" else return 1; fi } # ---------- questions (only on a terminal) ---------- TTY=0; [ -t 0 ] && TTY=1 ask() { # ask VAR "question" [default]; without a terminal the default is taken (or it stops) if [ $TTY = 0 ]; then [ -n "${3:-}" ] || die 1 "missing parameter: $2 (run it in a terminal to be asked, or see --help)" eval "$1=\$3"; return fi printf '%s%s: ' "$2" "${3:+ [$3]}" >&2 IFS= read -r ans || ans= [ -n "$ans" ] || ans=${3:-} eval "$1=\$ans" } ask_secret() { printf '%s: ' "$1" >&2 stty -echo 2>/dev/null; IFS= read -r PW || PW=; stty echo 2>/dev/null; printf '\n' >&2 } main() { if [ -n "$SHOWSQL" ]; then sql_text "$SHOWSQL" || die 1 "no embedded script $SHOWSQL"; exit 0; fi [ -n "$ENGINE" ] || ask ENGINE "Database engine (sqlserver, postgresql, mysql, mariadb, oracle)" case $ENGINE in sqlserver|postgresql|mysql|mariadb|oracle) ;; *) die 1 "unknown engine '$ENGINE'" ;; esac BASE=$ENGINE; [ "$ENGINE" = mariadb ] && BASE=mysql # client if [ -z "$CLIENT" ]; then case $ENGINE in sqlserver) cands="sqlcmd /opt/mssql-tools18/bin/sqlcmd /opt/mssql-tools/bin/sqlcmd" ;; postgresql) cands="psql" ;; mysql) cands="mysql mariadb" ;; mariadb) cands="mariadb mysql" ;; oracle) cands="sqlplus" ;; esac for c in $cands; do if command -v "$c" >/dev/null 2>&1; then CLIENT=$(command -v "$c"); break; fi done if [ -z "$CLIENT" ] && [ $TTY = 1 ]; then ask CLIENT "Client program not found on PATH ($cands). Full path to it"; fi [ -n "$CLIENT" ] || die 2 "no database client found ($cands). Install the client of your database or give --client ." fi [ -x "$CLIENT" ] || command -v "$CLIENT" >/dev/null 2>&1 || die 2 "client '$CLIENT' not found or not executable" # connection details case $ENGINE in oracle) if [ -z "$CONNECT" ]; then [ -n "$HOST" ] || ask HOST "Database host" localhost [ -n "$SERVICE" ] || ask SERVICE "Service name (for example ORCLPDB1)" CONNECT="//$HOST:${PORT:-1521}/$SERVICE" fi [ -n "$USR" ] || ask USR "User (the schema owner)" ;; *) [ -n "$HOST" ] || ask HOST "Database host" localhost [ -n "$DB" ] || ask DB "Database name" if [ "$ENGINE" = sqlserver ]; then [ -n "$AUTH" ] || { if [ -n "$USR" ]; then AUTH=sql; else ask AUTH "Authentication: sql (login and password) or windows (integrated)" sql; fi; } case $AUTH in sql|windows) ;; *) die 1 "--auth sql|windows" ;; esac fi if [ "${AUTH:-sql}" = sql ]; then [ -n "$USR" ] || ask USR "User"; fi ;; esac # password: never on a command line PW=${MMDB_PASSWORD:-} if [ -z "$PW" ] && [ "${AUTH:-sql}" = sql ]; then case $ENGINE in postgresql) PW=${PGPASSWORD:-} ;; sqlserver) PW=${SQLCMDPASSWORD:-} ;; mysql|mariadb) PW=${MYSQL_PWD:-} ;; esac if [ -z "$PW" ] && [ $TTY = 1 ]; then ask_secret "Password for $USR (empty: none / .pgpass / socket)"; fi fi if [ -z "$DC" ]; then if [ $TTY = 1 ]; then say "Also run the data check? It reads table data with COUNT queries and can take a while on large tables" printf '%s' "- run it off-hours. [Y/n]: " >&2 IFS= read -r ans || ans= case $ans in [nN]*) DC=0 ;; *) DC=1 ;; esac else DC=1; fi fi mkdir -p "$OUTDIR" || die 1 "cannot create $OUTDIR" TMP=$(mktemp -d "${TMPDIR:-/tmp}/mmdb-check.XXXXXX") || die 1 "cannot create a temporary folder" trap 'rm -rf "$TMP"' EXIT trap 'rm -rf "$TMP"; exit 130' INT TERM if [ "$BASE" = mysql ] && [ -n "$PW" ]; then esc=$(printf '%s' "$PW" | sed 's/\\/\\\\/g; s/"/\\"/g') printf '[client]\npassword="%s"\n' "$esc" > "$TMP/my.cnf" fi NAME=$DB; [ "$ENGINE" = oracle ] && NAME=${SCHEMA:-$USR} NAME=$(printf '%s' "$NAME" | tr -c 'A-Za-z0-9._-' '_') RESULT="$OUTDIR/$NAME-mmdb-result.txt" say "MigrateMyDatabase check tool – $ENGINE ($(basename "$CLIENT")) – read-only" step 1 "Structure (mmdb-$BASE.sql)" "mmdb-$BASE.sql" collector || exit $? WARN=$(warnings "$TMP/collector") cp "$TMP/collector" "$TMP/bundle" RC=0 DCOK=0 if [ "$DC" = 1 ]; then if step 2 "Data check (mmdb-$BASE-datacheck.sql)" "mmdb-$BASE-datacheck.sql" datacheck; then cat "$TMP/datacheck" >> "$TMP/bundle"; DCOK=1 n=$(awk -F'|' '($2 == "fkorphans" || $2 == "checkviol") && $7 != "" { e++ } END { print e + 0 }' "$TMP/datacheck") [ "$n" -gt 0 ] && WARN="${WARN:+$WARN } - $n data check quer(ies) could not run (shown in the file): the user needs SELECT on those tables." else step_rc=$?; [ $step_rc = 3 ] && RC=3 || RC=5 say "The file is written with the structure part only." fi fi cp "$TMP/bundle" "$RESULT" || die 1 "cannot write $RESULT" chmod 644 "$RESULT" 2>/dev/null summary if [ -n "$WARN" ]; then say ""; say "Warning – the user lacks some rights, so parts are missing:"; printf '%s\n' "$WARN" >&2 [ $RC = 0 ] && RC=6 fi say "" say "Result: $RESULT" say "Nothing was sent anywhere. Upload this file in the scan at https://migratemydatabase.eu, or send it with your report request." exit $RC } # ---------- run one script: step