A Python script to backup all data from a SQL database into a JSON file and restore it back into the database in case of data loss or database clearing.
- Features
- Prerequisites
- Installation
- Configuration
- Usage
- Examples
- Notes & Best Practices
- Troubleshooting
- License
- Backup: Extracts all data from the specified SQL database and saves it as a structured JSON file.
- Restore: Loads data from a JSON backup file back into the SQL database.
- Database Agnostic: Supports multiple SQL databases via SQLAlchemy (e.g., SQLite, MySQL, PostgreSQL).
- Command-Line Interface: Easy to use with command-line arguments for flexibility.
Before using the script, ensure you have the following:
- Python 3.x: Installed on your system. Download from python.org.
- Database Access: Credentials and access rights to the SQL database you intend to backup or restore.
- Required Python Libraries:
SQLAlchemy: For database interactions.- Database-specific drivers (optional, depending on your database).
-
Clone the Repository:
git clone https://github.com/yourusername/db-backup-restore.git cd db-backup-restore -
Install Required Python Libraries:
It's recommended to use a virtual environment.
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install sqlalchemy
-
Install Database-Specific Drivers:
Depending on your database, install the appropriate driver:
-
SQLite: No additional driver needed (included with Python).
-
MySQL:
pip install pymysql
-
PostgreSQL:
pip install psycopg2
-
Other Databases: Refer to SQLAlchemy's Dialects for more options.
-
The script requires a Database Connection URL to connect to your SQL database. The format of this URL varies based on the database you are using.
-
SQLite:
sqlite:///path_to_your_db.sqliteExample:
sqlite:///./mydatabase.sqlite -
MySQL:
mysql+pymysql://username:password@host:port/database_nameExample:
mysql+pymysql://user:pass@localhost:3306/mydatabase -
PostgreSQL:
postgresql+psycopg2://username:password@host:port/database_nameExample:
postgresql+psycopg2://user:pass@localhost:5432/mydatabase
Replace username, password, host, port, and database_name with your actual database credentials and details.
The script provides two main functionalities: backup and restore. Use command-line arguments to specify the desired operation and necessary parameters.
Creates a JSON file containing all the data from your database.
Command Syntax:
python db_backup_restore.py backup --db-url "your_database_url" --output backup.jsonParameters:
backup: The command to initiate the backup process.--db-url: The database connection URL.--output: The path where the JSON backup file will be saved.
Loads data from a JSON backup file back into your database.
Command Syntax:
python db_backup_restore.py restore --db-url "your_database_url" --input backup.jsonParameters:
restore: The command to initiate the restore process.--db-url: The database connection URL.--input: The path to the JSON backup file.
python db_backup_restore.py backup --db-url "sqlite:///./mydatabase.sqlite" --output backup.jsonpython db_backup_restore.py restore --db-url "sqlite:///./mydatabase.sqlite" --input backup.jsonpython db_backup_restore.py backup --db-url "mysql+pymysql://user:pass@localhost:3306/mydatabase" --output backup.jsonpython db_backup_restore.py restore --db-url "postgresql+psycopg2://user:pass@localhost:5432/mydatabase" --input backup.json-
Schema Consistency: Ensure that the database schema remains unchanged between backup and restore operations. Altering tables or columns may lead to restore failures or data inconsistencies.
-
Data Types: JSON supports standard data types. Complex types (e.g., binary data) may not serialize correctly. Modify the script if necessary to handle such cases.
-
Large Databases: JSON files can become large and may not be efficient for very large databases. Consider using more optimized backup methods or splitting the backup into smaller chunks.
-
Security: Store backup files securely, especially if they contain sensitive information. Unauthorized access to these files can lead to data breaches.
-
Testing: Always test the restore process in a development or staging environment before applying it to production databases.
-
Automated Backups: For regular backups, consider scheduling the script using cron jobs (Linux/macOS) or Task Scheduler (Windows).
-
Connection Errors: Ensure that your database URL is correct and that the database server is running and accessible.
-
Missing Tables: If the restore process skips tables, verify that the tables exist in the target database and that the schema matches the backup.
-
Authentication Issues: Double-check your database credentials and ensure that the user has the necessary permissions to read/write data.
-
JSON Decoding Errors: Ensure that the backup JSON file is not corrupted and is properly formatted.
This project is licensed under the MIT License.
For any questions or issues, please open an issue in the repository or contact christopheraliu07@gmail.com