#!/usr/bin/env bash # verify-incremental-backups-using-custom-scripts.sh # Falsification script for https://heatware.net/mysql/incremental-backups-using-custom-scripts/ # # Standalone. It creates its own scratch databases named hw_w13v__* and touches nothing # else. It deliberately does NOT run DROP DATABASE: it prints the removal statement at the end # for you to run yourself, so an automated falsifier can never delete a database on your server. # Without a reachable MySQL server, or with binary logging off, every live check is SKIPped. set -u PASS=0; FAIL=0; SKIP=0; CONTENT=0 WORK="$(mktemp -d "${TMPDIR:-/tmp}/hw_w13v_mysql.XXXXXX")" ART="$(cd "$(dirname "$0")" 2>/dev/null && pwd)/incremental-backups-using-custom-scripts.html" SRC="hw_w13v_$$_src"; DST="hw_w13v_$$_dst"; DST2="hw_w13v_$$_dst2" MADE="" trap 'rm -rf "$WORK"' EXIT INT TERM pass() { PASS=$((PASS+1)); echo "PASS $*"; } fail() { FAIL=$((FAIL+1)); echo "FAIL $*"; } skip() { SKIP=$((SKIP+1)); echo "SKIP $*"; } content() { CONTENT=$((CONTENT+1)); echo "CONTENT $*"; } if ! command -v mysql >/dev/null 2>&1; then echo "SKIP mysql client not installed - no live check possible" echo "PASS=0 FAIL=0 SKIP=1 CONTENT=0" exit 0 fi MYSQL="" for c in "mysql --login-path=verify" "mysql -u root" "mysql"; do if $c -Nse "SELECT 1" >/dev/null 2>&1; then MYSQL="$c"; break; fi done if [ -z "$MYSQL" ]; then echo "SKIP no MySQL server reachable without a password (tried --login-path=verify, -u root, and defaults)" LIVE=0 else VER="$($MYSQL -Nse "SELECT VERSION()" 2>/dev/null)" echo "# server: $VER client: $(mysql --version 2>/dev/null | sed 's/.*Ver //;s/ .*//')" LIVE=1 fi echo q() { $MYSQL -Nse "$1" 2>/dev/null; } # ------------------------------- 1. SHOW MASTER STATUS is gone in 8.4, not deprecated # Article: "On 8.4 that is not a deprecation, it is a parse error" if [ "$LIVE" = "1" ]; then major_minor="$(printf '%s' "$VER" | cut -d. -f1,2)" ms_err="$($MYSQL -e "SHOW MASTER STATUS;" 2>&1 >/dev/null | head -1)" bls_ok=0 $MYSQL -e "SHOW BINARY LOG STATUS;" >/dev/null 2>&1 && bls_ok=1 case "$major_minor" in 8.4|9.*|1[0-9].*) if printf '%s' "$ms_err" | grep -q 'ERROR 1064' && [ "$bls_ok" = "1" ]; then pass "on $VER, SHOW MASTER STATUS is a syntax error and SHOW BINARY LOG STATUS is the replacement" else fail "expected SHOW MASTER STATUS to be a parse error on $VER and SHOW BINARY LOG STATUS to work; got [$ms_err] and bls_ok=$bls_ok" fi ;; *) skip "server is $VER, not 8.4+; the article's SHOW MASTER STATUS claim is specific to 8.4 and later" ;; esac else skip "no server - SHOW MASTER STATUS / SHOW BINARY LOG STATUS not checked" fi # ------------------------------------------ 2. --master-data warns, --source-data does not # Article: "it still works and warns - WARNING: --master-data is deprecated ..." if [ "$LIVE" = "1" ] && command -v mysqldump >/dev/null 2>&1; then DUMP="$(printf '%s' "$MYSQL" | sed 's/^mysql/mysqldump/')" w="$($DUMP --master-data=2 --no-data --no-create-info mysql 2>&1 >/dev/null | head -1)" if printf '%s' "$w" | grep -qi 'master-data is deprecated'; then pass "mysqldump still accepts --master-data but warns: $w" elif printf '%s' "$w" | grep -qi 'unknown option'; then fail "this client has removed --master-data entirely; the article says it still works and warns" else skip "mysqldump --master-data produced no recognisable warning here: [$w]" fi else skip "mysqldump unavailable or no server - deprecation warning not checked" fi # ---------------------------------------------- 3. mysqlcheck computes no checksum # Article: "the string 'checksum' appears zero times in its own --help output" if command -v mysqlcheck >/dev/null 2>&1; then n="$(mysqlcheck --help 2>&1 | grep -ci checksum)" if [ "${n:-1}" -eq 0 ]; then pass "mysqlcheck --help mentions 'checksum' 0 times - it is not a checksum tool" else fail "mysqlcheck --help now mentions 'checksum' $n times; the article says zero" fi else skip "mysqlcheck not installed" fi # ------------------------- 4. the whole full + incremental + restore cycle, proven by data LOGBIN="" [ "$LIVE" = "1" ] && LOGBIN="$(q "SELECT @@log_bin")" BINBASE="" [ "$LIVE" = "1" ] && BINBASE="$(q "SELECT @@log_bin_basename")" if [ "$LIVE" != "1" ]; then skip "no server - the full/incremental/restore cycle was not run" elif [ "${LOGBIN:-0}" != "1" ]; then skip "binary logging is off on this server (@@log_bin=$LOGBIN) - there is no incremental backup to take" elif ! command -v mysqldump >/dev/null 2>&1 || ! command -v mysqlbinlog >/dev/null 2>&1; then skip "mysqldump or mysqlbinlog missing - restore cycle not run" else DUMP="$(printf '%s' "$MYSQL" | sed 's/^mysql/mysqldump/')" BINLOGCMD="$(printf '%s' "$MYSQL" | sed 's/^mysql.*/mysqlbinlog/')" BINDIR="$(dirname "$BINBASE")" $MYSQL -e "CREATE DATABASE $SRC; CREATE DATABASE $DST; CREATE DATABASE $DST2;" 2>/dev/null \ && MADE="$SRC $DST $DST2" if [ -z "$MADE" ]; then skip "could not create scratch databases (no CREATE privilege?) - restore cycle not run" else $MYSQL "$SRC" -e "CREATE TABLE orders (id INT AUTO_INCREMENT PRIMARY KEY, customer VARCHAR(40), total DECIMAL(9,2));" 2>/dev/null $MYSQL "$SRC" -e "INSERT INTO orders (customer,total) VALUES ('ana',10),('bo',20),('cy',30),('di',40),('ed',50);" 2>/dev/null $MYSQL -e "FLUSH BINARY LOGS;" >/dev/null 2>&1 $DUMP --single-transaction --source-data=2 "$SRC" > "$WORK/full.sql" 2>"$WORK/dump.err" FILE="$(grep -m1 -o "SOURCE_LOG_FILE='[^']*'" "$WORK/full.sql" | cut -d"'" -f2)" POS="$(grep -m1 -o "SOURCE_LOG_POS=[0-9]*" "$WORK/full.sql" | cut -d= -f2)" if [ -z "${FILE:-}" ] || [ -z "${POS:-}" ]; then skip "mysqldump --source-data=2 wrote no CHANGE REPLICATION SOURCE line - cycle not run ($(head -1 "$WORK/dump.err" 2>/dev/null))" else pass "mysqldump --source-data=2 recorded the coordinate the incremental needs: $FILE:$POS" $MYSQL "$SRC" -e "INSERT INTO orders (customer,total) VALUES ('fi',60),('gu',70),('ha',80),('iv',90);" 2>/dev/null STOP="$($MYSQL -Nse "SHOW BINARY LOG STATUS" 2>/dev/null | awk '{print $2}')" [ -z "$STOP" ] && STOP="$($MYSQL -Nse "SHOW MASTER STATUS" 2>/dev/null | awk '{print $2}')" B="$BINDIR/$FILE" if [ ! -r "$B" ]; then skip "binary log $B is not readable by this user - restore cycle not run" else $MYSQL "$DST" < "$WORK/full.sql" 2>/dev/null $MYSQL "$DST2" < "$WORK/full.sql" 2>/dev/null base_rows="$(q "SELECT COUNT(*) FROM $DST2.orders")" $BINLOGCMD --start-position="$POS" --stop-position="$STOP" "$B" > "$WORK/incr.sql" 2>/dev/null # 4a. the old page's restore command applies nothing to the database you name $MYSQL "$DST2" < "$WORK/incr.sql" >/dev/null 2>"$WORK/wrong.err" wrong_rc=$? after_wrong="$(q "SELECT COUNT(*) FROM $DST2.orders")" if [ "${after_wrong:-0}" = "${base_rows:-0}" ] && [ "${base_rows:-0}" = "5" ]; then pass "piping the binlog slice into 'mysql ' left that database at $after_wrong rows - the database argument is ignored (mysql exit $wrong_rc: $(head -1 "$WORK/wrong.err" 2>/dev/null))" else fail "expected the named database to stay at 5 rows when the binlog slice is applied to it; it went from ${base_rows:-?} to ${after_wrong:-?}" fi # 4b. --rewrite-db redirects the events, proven by CHECKSUM TABLE $BINLOGCMD --start-position="$POS" --stop-position="$STOP" \ --rewrite-db="$SRC->$DST" "$B" 2>/dev/null | $MYSQL >/dev/null 2>&1 cs_src="$(q "CHECKSUM TABLE $SRC.orders" | awk '{print $2}')" cs_dst="$(q "CHECKSUM TABLE $DST.orders" | awk '{print $2}')" rows_dst="$(q "SELECT COUNT(*) FROM $DST.orders")" if [ -n "${cs_src:-}" ] && [ "$cs_src" = "${cs_dst:-x}" ] && [ "${rows_dst:-0}" = "9" ]; then pass "--rewrite-db applied the incremental to the restore target: $rows_dst rows and identical CHECKSUM TABLE ($cs_src) on both sides" else fail "restore not proven by data: source checksum ${cs_src:-?}, target checksum ${cs_dst:-?}, target rows ${rows_dst:-?}" fi # 4c. a position range is a slice of server history, not of one database. # Widen the window past the two restores above - restoring is itself logged. STOP2="$($MYSQL -Nse "SHOW BINARY LOG STATUS" 2>/dev/null | awk '{print $2}')" [ -z "$STOP2" ] && STOP2="$($MYSQL -Nse "SHOW MASTER STATUS" 2>/dev/null | awk '{print $2}')" dbs="$($BINLOGCMD --start-position="$POS" --stop-position="$STOP2" --base64-output=DECODE-ROWS --verbose "$B" 2>/dev/null | grep -oE '`hw_w13v_[0-9]+_[a-z0-9]+`' | sort -u | wc -l | tr -d ' ')" dbs1="$($BINLOGCMD --start-position="$POS" --stop-position="$STOP2" --database="$SRC" --base64-output=DECODE-ROWS --verbose "$B" 2>/dev/null | grep -oE '`hw_w13v_[0-9]+_[a-z0-9]+`' | sort -u | wc -l | tr -d ' ')" if [ "${dbs:-0}" -ge 2 ] && [ "${dbs1:-0}" -le 1 ]; then pass "the unfiltered window names $dbs of this script's databases; --database narrows it to $dbs1" else skip "could not demonstrate cross-database binlog content here (unfiltered=$dbs, filtered=$dbs1)" fi fi fi fi fi # ------------------------------------------------------ 5. article content greps echo if [ -f "$ART" ]; then ct() { if grep -qF "$2" "$ART"; then content "$1"; else fail "article text missing: $1"; fi; } ct "leads with the fact that the old script was a full backup" \ "The script this page used to publish was not an incremental backup." ct "states the restore procedure silently does not restore" \ "And the restore procedure it gave silently does not restore." ct "explains the silent-corruption case, not just the visible error" \ "there would have been no duplicate-key error and no visible failure — it would simply have rewritten live data while reporting that it restored a backup" ct "publishes the identical checksum as the proof of restore" \ "Nine rows on both sides and identical checksums." ct "refuses exit status as proof of a restore" \ "That comparison is the only acceptable proof that a restore worked. An exit status is not" ct "links to the compressed-backup page instead of rebuilding the pipefail argument" \ 'href="/mysql/create-compressed-backup/"' ct "corrects the mysqlcheck claim with the measured count" \ 'the string "checksum" appears zero times in its own --help output' ct "declares GTID untested" \ "GTID-based replication changes the restore procedure and was not tested here" ct "declares XtraBackup and Enterprise Backup unverified" \ "nothing on this page is a first-hand report of either" else skip "incremental-backups-using-custom-scripts.html not next to this script - content greps not run" fi echo if [ -n "$MADE" ]; then echo "# scratch databases left in place ON PURPOSE (this script never runs DROP). Remove them with:" echo "# mysql -e \"DROP DATABASE IF EXISTS $SRC; DROP DATABASE IF EXISTS $DST; DROP DATABASE IF EXISTS $DST2;\"" fi echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP CONTENT=$CONTENT" [ "$FAIL" -eq 0 ] || exit 1 exit 0