For the sole purpose of creating a comprehensive dump backup of a MySQL table, I have developed an additional Python script. Below is the script for your reference:
import csv
import mysql.connector
# MySQL database connection configuration
db_config = {
'host': 'your_host',
'user': 'your_username',
'password': 'your_password',
'database': 'your_database',
}
# SQL SELECT query
sql_query = "SELECT * FROM your_table"
# Output CSV file
output_csv_file = 'output.csv'
try:
# Connect to the MySQL database
connection = mysql.connector.connect(**db_config)
if connection.is_connected():
print("Connected to MySQL database")
# Create a cursor object to execute SQL queries
cursor = connection.cursor()
# Execute the SQL query
cursor.execute(sql_query)
# Fetch all rows from the result set
rows = cursor.fetchall()
# Write the rows to a CSV file
with open(output_csv_file, 'w', newline='') as csvfile:
csv_writer = csv.writer(csvfile)
# Write the header row with column names
csv_writer.writerow([i[0] for i in cursor.description])
# Write the data rows
csv_writer.writerows(rows)
print(f"Data has been exported to {output_csv_file}")
except mysql.connector.Error as e:
print(f"Error: {e}")
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")