Python MySQL

Recover Corrupted MySQL Data (ibdata1)

When InnoDB fails and `innodb_force_recovery` doesn't work, this script is your last resort. It scans the raw `ibdata1` binary file for printable strings (like JSON, emails, or configs) using Regex. It does not repair the DB structure, but helps you salvage text content.

import re
import sys

def main():
    if len(sys.argv) < 2:
        print("Usage: python recover_ibdata.py ibdata1")
        sys.exit(1)

    filename = sys.argv[1]
    min_length = 4  # Minimum string length to extract

    print(f"Scanning {filename}... This may take a while.")
    
    try:
        with open(filename, "rb") as file:
            data = file.read()
            
            # Regex to find printable ASCII characters
            # Modify this pattern if you need specific data formats
            pattern = b"[^\x00-\x1F\x7F-\xFF]{" + str(min_length).encode() + b",}"
            
            strings = re.findall(pattern, data)
            
            output_file = "recovered_data.txt"
            with open(output_file, "w", encoding="utf-8") as f:
                for s in strings:
                    try:
                        line = s.decode('utf-8', 'ignore')
                        f.write(line + "\n")
                    except:
                        pass

        print(f"Success! Found {len(strings)} strings.")
        print(f"Saved to {output_file}")

    except FileNotFoundError:
        print("Error: File not found.")
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    main()

Usage:

python3 recover_ibdata.py ibdata1

The script produces a recovered_data.txt file containing every readable text string found in the binary blob.