This repository provides two Python scripts for backing up your SQLite database:
- backup_db.py: For general Python/SQLite projects.
- backup_db_django.py: For Django projects, provided as a management command template (you can rename it when adding to your project).
- General Standalone Script (
backup_db.py
) - Django Management Command (
backup_db_django.py
→ custom name) - Script Selection Guide
- License & Author
- Safe backup of a SQLite database using Python’s standard library.
- Optionally include a media folder in the backup.
- Creates a timestamped ZIP archive.
- Simple CLI usage.
- Python 3.6 or newer.
- No external dependencies.
-
Clone the repository:
git clone https://github.com/legeRise/simple-db-backup-script.git cd simple-db-backup-script
-
Run the backup script:
-
Backup just the database:
python backup_db.py /path/to/your/database.sqlite3
-
Backup database and media folder:
python backup_db.py /path/to/your/database.sqlite3 --media /path/to/media_folder
-
Specify a custom backup directory:
python backup_db.py /path/to/your/database.sqlite3 --backup-dir /path/to/backups
-
python backup_db.py mydb.sqlite3 --media ./media
Creates a ZIP file in db_backups/
containing your database and all media files.
- Database not found: Check your database path.
- Media folder missing: The script warns and skips if the folder does not exist.
- Permissions: Make sure you have access to the files/folders.
This script is provided as backup_db_django.py
in this repository. When adding it to your Django project, you can rename it to any command name you prefer (e.g., db_backup.py
). The command name will match the filename you choose.
-
Copy the Script:
- Copy
backup_db_django.py
from this repo into your Django app at:your_app/management/commands/<your_command_name>.py
(Create the folders if they do not exist.)
your_project/ your_app/ management/ commands/ db_backup.py # or any name you prefer
- Copy
-
Rename the Script (Recommended):
- Rename
backup_db_django.py
to something meaningful for your project, e.g.,db_backup.py
ordatabase_backup.py
. - The management command name will match the filename you use.
- Rename
-
Adjust the Script (if needed):
- Ensure the script correctly references your database and media folder. By default, it uses Django settings.
- If you name the file
db_backup.py
, run:python manage.py db_backup
- If you name it
database_backup.py
, run:python manage.py database_backup
From your Django project root, run:
python manage.py <your_command_name>
- This will back up your SQLite database and your media folder into a ZIP file.
- By default, the backup ZIP will be created in
db_backups/
.
- Pass options as defined in the script:
python manage.py <your_command_name> --help
- Copy
backup_db_django.py
to your app's management/commands directory. - Rename it to your desired command name (e.g.,
db_backup.py
). - Run:
python manage.py db_backup
- Find your backup ZIP in
db_backups/
.
- App not found: Confirm you placed the script in an installed app.
- Database/media folder path issues: Adjust script to match your project’s settings.
- Permissions: Run with appropriate access.
Use case | Script to use | How to run |
---|---|---|
General Python/SQLite project | backup_db.py |
python backup_db.py <db_path> [options] |
Django project | <your_command_name>.py |
python manage.py <your_command_name> [options] |
- License: MIT
- Author: legeRise
Tip:
- For standalone use, stick with
backup_db.py
. - For Django, copy
backup_db_django.py
as a management command and rename it as you wish for easy backups viamanage.py
.