Last active
October 15, 2025 13:42
-
-
Save johanguse/6aface218f0bda55ee8561f647dc723e to your computer and use it in GitHub Desktop.
Replace a domain in a big SQL file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| replace.py — Replace a domain in a large SQL file safely and efficiently. | |
| ------------------------------------------------------------ | |
| USAGE: | |
| python replace.py <path_to_file.sql> <old_domain> <new_domain> | |
| EXAMPLE: | |
| python replace.py ./dump.sql old-domain.com newdomain.com | |
| DESCRIPTION: | |
| This script reads a large .sql file line by line and replaces | |
| all occurrences of the specified domain. It writes the output | |
| to a NEW file with a timestamp, leaving the original intact. | |
| NOTES: | |
| - Recommended to BACK UP your .sql file before running. | |
| - Not optimized for binary SQL dumps (use plain text). | |
| - Intended for educational or personal use only. | |
| - Provided as-is, without warranty of any kind. | |
| AUTHOR: | |
| Johan Guse | |
| DATE: | |
| 2025-10-15 | |
| """ | |
| import re | |
| import os | |
| import sys | |
| import time | |
| from datetime import datetime | |
| def replace_domain_in_large_sql(filename: str, old_domain: str, new_domain: str): | |
| """Replaces all occurrences of old_domain with new_domain in a large SQL file.""" | |
| if not os.path.exists(filename): | |
| print(f"❌ Error: File not found: {filename}") | |
| sys.exit(1) | |
| start_time = time.time() | |
| timestamp = datetime.now().strftime("%Y-%m-%d_%H%M%S") | |
| # Generate output filename based on original | |
| base, ext = os.path.splitext(filename) | |
| new_filename = f"{base}_replaced_{timestamp}{ext}" | |
| count = 0 | |
| try: | |
| with open(filename, "r", encoding="utf-8", errors="ignore") as file, open( | |
| new_filename, "w", encoding="utf-8" | |
| ) as new_file: | |
| for line in file: | |
| new_line, replacements = re.subn(old_domain, new_domain, line) | |
| new_file.write(new_line) | |
| count += replacements | |
| elapsed_time = round(time.time() - start_time, 2) | |
| print(f"✅ Replaced {count} occurrence(s) of '{old_domain}' with '{new_domain}'.") | |
| print(f"📄 New file saved as: {new_filename}") | |
| print(f"⏱️ Operation completed in {elapsed_time} seconds.") | |
| except Exception as e: | |
| print(f"⚠️ An error occurred: {e}") | |
| if os.path.exists(new_filename): | |
| os.remove(new_filename) | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| if len(sys.argv) != 4: | |
| print("Usage: python replace.py <path_to_file.sql> <old_domain> <new_domain>") | |
| print("Example: python replace.py ./dump.sql example.com newexample.com") | |
| sys.exit(1) | |
| file_path = sys.argv[1] | |
| old_domain = sys.argv[2] | |
| new_domain = sys.argv[3] | |
| replace_domain_in_large_sql(file_path, old_domain, new_domain) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment