Skip to content

Instantly share code, notes, and snippets.

@zheng022
Created April 22, 2026 12:06
Show Gist options
  • Select an option

  • Save zheng022/acfea5b1271205be826f90ccd7ebfdb8 to your computer and use it in GitHub Desktop.

Select an option

Save zheng022/acfea5b1271205be826f90ccd7ebfdb8 to your computer and use it in GitHub Desktop.
#!/bin/bash
#/ Usage: ghe-storage-verify-mysql-migration <backup-path> <destination-path>
#/ Verifies that two MySQL datadir trees are identical by comparing md5sums
#/ of every file beneath each path.
#/
#/ Example (after ghe-storage-migrate-mysql):
#/ ghe-storage-verify-mysql-migration /data/user/mysql-backup/github_enterprise /data/multi-disk/db/mysql/github_enterprise
set -e
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
grep '^#/' <"$0" | cut -c4-
exit 2
fi
if [ "$#" -ne 2 ]; then
grep '^#/' <"$0" | cut -c4- >&2
exit 1
fi
BACKUP_PATH="$1"
DEST_PATH="$2"
[ "$(whoami)" = "root" ] || {
exec sudo -u root "$0" "$@"
}
if [ ! -d "$BACKUP_PATH" ]; then
echo "Error: backup path not found: $BACKUP_PATH" >&2
exit 1
fi
if [ ! -d "$DEST_PATH" ]; then
echo "Error: destination path not found: $DEST_PATH" >&2
exit 1
fi
checksum_tree() {
local root=$1
# Produce "<md5> <relative-path>" lines, sorted, for deterministic diff.
( cd "$root" && find . -type f -print0 \
| sort -z \
| xargs -0 md5sum )
}
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
BACKUP_SUMS="$TMPDIR/backup.md5"
DEST_SUMS="$TMPDIR/dest.md5"
echo "Computing md5sums under $BACKUP_PATH..."
checksum_tree "$BACKUP_PATH" > "$BACKUP_SUMS"
echo "Computing md5sums under $DEST_PATH..."
checksum_tree "$DEST_PATH" > "$DEST_SUMS"
echo "Comparing..."
if diff -u "$BACKUP_SUMS" "$DEST_SUMS"; then
echo "OK: $BACKUP_PATH and $DEST_PATH are identical ($(wc -l < "$BACKUP_SUMS") files)."
exit 0
else
echo "MISMATCH: differences found between $BACKUP_PATH and $DEST_PATH." >&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment