#!/usr/bin/python3 # DB-Dump für motorradig2027. # Voraussetzung: mysqldump muss installiert sein (apt install mysql-client). # Die Zugangsdaten werden aus ../motorradig/.credentials gelesen (KEY=VALUE). import datetime import os import subprocess import sys CRED_FILE = os.path.join( os.path.dirname(os.path.abspath(__file__)), '..', 'motorradig', '.credentials' ) def load_credentials(path: str) -> dict: creds = {} if not os.path.isfile(path): print(f"Fehler: Credentials-Datei nicht gefunden: {path}", file=sys.stderr) sys.exit(1) with open(path, 'r', encoding='utf-8') as fh: for line in fh: line = line.strip() if not line or line.startswith('#') or '=' not in line: continue key, value = line.split('=', 1) creds[key.strip()] = value.strip() return creds def main() -> int: creds = load_credentials(CRED_FILE) db_host = creds.get('DB_HOST', creds.get('FTP_URL', '')) db_user = creds.get('DB_USERNAME', '') db_password = creds.get('DB_PASSWORD', '') db_name = creds.get('DB_NAME', db_user) missing = [] if not db_host: missing.append('DB_HOST (oder FTP_URL)') if not db_user: missing.append('DB_USERNAME') if not db_password: missing.append('DB_PASSWORD') if not db_name: missing.append('DB_NAME (oder DB_USERNAME)') if missing: print('Fehler: Fehlende Credentials: ' + ', '.join(missing), file=sys.stderr) return 1 now = datetime.datetime.now().strftime('%Y-%m-%d_%H%M%S') filename = 'motorradig_' + now + '.sql' print(f'Dumping {filename} (Host: {db_host}, DB: {db_name})') cmd = [ 'mysqldump', '--no-tablespaces', '--column-statistics=0', '--user=' + db_user, '--password=' + db_password, '--host=' + db_host, db_name, ] with open(filename, 'w', encoding='utf-8') as out: result = subprocess.run(cmd, stdout=out, stderr=subprocess.PIPE) if result.returncode != 0: print(result.stderr.decode('utf-8', 'replace'), file=sys.stderr) return result.returncode print('Fertig.') return 0 if __name__ == '__main__': sys.exit(main())