Write a Python script to back up mysql databases using mysqldump. The script should read the database names and any other parameters from a .env file using the load_dotenv function. Show all the required steps.
To back up MySQL databases using Python, you’ll need to:
- Install the necessary dependencies.
- Create a
.env
file to store your database credentials and other parameters. - Write a Python script that reads these parameters and performs the backup using the
mysqldump
command.
Here are the steps in detail:
Step 1: Install Dependencies
First, you need to install the python-dotenv
package to read the .env
file, and, optionally, mysqldump
if it’s not already installed on your server.
pip install python-dotenv
Step 2: Create a .env
File
Create a file named .env
in your project directory and add your MySQL credentials and other parameters to it. Here is an example:
MYSQL_HOST=localhost
MYSQL_USER=yourusername
MYSQL_PASSWORD=yourpassword
DATABASES=database1,database2
BACKUP_DIR=/path/to/backup/dir
Make sure to replace the placeholders with your actual credentials and paths.
Step 3: Write the Python Script
Now you can write a Python script that reads the .env
file and uses mysqldump
to back up the specified databases.
import os
from dotenv import load_dotenv
import subprocess
from datetime import datetime
# Load environment variables from .env file
load_dotenv()
# Get database credentials and parameters from .env
MYSQL_HOST = os.getenv('MYSQL_HOST')
MYSQL_USER = os.getenv('MYSQL_USER')
MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD')
DATABASES = os.getenv('DATABASES').split(',')
BACKUP_DIR = os.getenv('BACKUP_DIR')
# Ensure the backup directory exists
if not os.path.exists(BACKUP_DIR):
os.makedirs(BACKUP_DIR)
# Function to perform the backup
def backup_database(database):
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
backup_file = os.path.join(BACKUP_DIR, f"{database}_{timestamp}.sql")
dump_command = [
'mysqldump',
f'--host={MYSQL_HOST}',
f'--user={MYSQL_USER}',
f'--password={MYSQL_PASSWORD}',
database
]
try:
with open(backup_file, 'w') as f:
subprocess.run(dump_command, stdout=f, check=True)
print(f"Backup of {database} completed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error backing up {database}: {e}")
# Backup each database
for db in DATABASES:
backup_database(db)
Step 4: Run the Script
Run the Python script to perform the backups:
python backup_script.py
Make sure that mysqldump
is accessible from the command line and that the Python environment can find it. This script will create a timestamped SQL file for each database in the specified backup directory.
This is good, but change the script so that I can pass in the names of the databases as a parameter directly to the script. Keep the database names in the .env file though, and use them if the database name aren’t passed directly to the script.
To modify the script to accept database names as arguments while still using the names from the .env
file if no arguments are provided, you can follow these steps:
- Import the necessary modules to handle command-line arguments.
- Update the script to check for command-line arguments and fall back to the
.env
file if none are provided.
Here is the updated script:
Updated Python Script
import os
import sys
from dotenv import load_dotenv
import subprocess
from datetime import datetime
# Load environment variables from .env file
load_dotenv()
# Get database credentials and parameters from .env
MYSQL_HOST = os.getenv('MYSQL_HOST')
MYSQL_USER = os.getenv('MYSQL_USER')
MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD')
DEFAULT_DATABASES = os.getenv('DATABASES').split(',')
BACKUP_DIR = os.getenv('BACKUP_DIR')
# Ensure the backup directory exists
if not os.path.exists(BACKUP_DIR):
os.makedirs(BACKUP_DIR)
# Function to perform the backup
def backup_database(database):
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
backup_file = os.path.join(BACKUP_DIR, f"{database}_{timestamp}.sql")
dump_command = [
'mysqldump',
f'--host={MYSQL_HOST}',
f'--user={MYSQL_USER}',
f'--password={MYSQL_PASSWORD}',
database
]
try:
with open(backup_file, 'w') as f:
subprocess.run(dump_command, stdout=f, check=True)
print(f"Backup of {database} completed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error backing up {database}: {e}")
# Get database names from script arguments or fallback to .env file
databases = sys.argv[1:] if len(sys.argv) > 1 else DEFAULT_DATABASES
# Backup each database
for db in databases:
backup_database(db)
Running the Script
With Command-line Arguments
You can pass the database names as arguments directly:
python backup_script.py database1 database2
Using .env File
If you do not provide any arguments, the script will use the database names from the .env
file:
python backup_script.py
Summary
- The script reads MySQL credentials and default database names from the
.env
file. - It accepts database names as command-line arguments.
- If no command-line arguments are provided, it uses the database names from the
.env
file.
This approach provides flexibility, allowing you to use either command-line arguments or environment variables as per your needs.
Leave a Reply