Database backup and restore scheduler for mysql and postgis
2.0K
Nards DB Backup is a database backup system configurable via Docker. It supports automated backups and the automatic restoration of the last backup on startup (if configured), ensuring that the database is always aligned with the most recent backups. This project is ideal for users who use databases via Docker Compose or Kubernetes and are looking for a solution to automate backups.
A Flask application that schedules and manages database backups using cron jobs. It supports MySQL, PostgreSQL, PostGIS, MongoDB, Redis, and GraphDB, and can restore backups via command-line arguments.
Version 2.0.0 introduces the following breaking changes compared with 1.0.2:
linux/amd64 and linux/arm64; linux/arm/v7
support has been removed. Version v1.0.2 is the last version providing an
ARMv7 image. ARMv7 users should pin nards/nards-db-backup:v1.0.2 instead
of latest.v1.0.2 is the last version with the previous PostgreSQL client
compatibility; it contains PostgreSQL 15 client tools, which support dumping
older servers. Users of servers older than 14 should remain on v1.0.2
until they upgrade their database and verify backup and restore.The latest tag moves between releases and includes these breaking changes.
See CHANGELOG.md for the release notes.
Configure the application via environment variables. Create a .env file with the following variables:
DB_HOST=localhostDB_PORT=5432 # (e.g., MySQL: 3306, Postgres/PostGIS: 5432, MongoDB: 27017, GraphDB: 7200)DB_USER=userDB_PASSWORD=passwordDB_MAINTENANCE_NAME=mydb # For MongoDB, this is often 'admin' if using auth # For SQL databases; less relevant for RedisDB_TYPE=mysql|postgres|postgis|redis|mongodb|graphdb
DB_PORT is typically 7200. DB_USER and DB_PASSWORD can often be left empty for GraphDB Free if security is disabled.CRON_CONFIGS='[{"cron": "0 0 * * *", "retention_max": 90, "name": "default"}]'BACKUP_FILE_EXTENSION=.backup # Set to .backup.gz to compress backupsRESTORE_CONFIG_NAME="".graphdb-recovery-* directory next to the backup, then clears the repository and imports the requested backup. If import fails, it attempts to clear the repository again and restore that snapshot. Blank nodes and named graphs are supported.metadata.json remain in the recovery directory for manual recovery; check the logs and repository state before retrying. Store backups on persistent storage if the container may be replaced. A lost HTTP response can leave the operation outcome uncertain.GET /rest/repositories.DB_PORT=7200 for the usual GraphDB port. Provide DB_USER/DB_PASSWORD only if security is enabled in your GraphDB instance.retention_max will default to 90 and name will default to "default".Example of CRON_CONFIGS:
[{"cron": "0 0 * * *", "retention_max": 90, "name": "default"}]: creates a backup every day a midnight and keep for 90 days[{"cron": "0 * * * *", "retention_max": 24, "name": "hourly"}, {"cron": "0 0 * * *", "retention_max": 24, "name": "monthly"}]: creates a backup every hour and keep for a day in folder named 'hourly', a backup every 1 of the month and keep for 2 years in folder named 'monthly'An example of docker compose is provided into docker-compose.yml file. The same example is provided below:
services:
nards_db_backup:
image: nards/nards-db-backup:latest
build: .
environment:
DB_HOST: 'database'
DB_PORT: '5432' # postgres/postgis: 5432; mysql: 3306; graphdb: 7200; mongodb: 27017; redis: 6379
DB_USER: 'user'
DB_PASSWORD: 'password'
DB_MAINTENANCE_NAME: 'mydatabase' # For SQL databases; for Redis, typically not used or set to 0 for the default DB., for MongoDB often 'admin'
DB_TYPE: 'postgres' # postgres/postgis/mysql/mongodb/redis/graphdb
BACKUP_DIR: /backups
# BACKUP_FILE_EXTENSION: '.backup.gz' # Default: .backup (uncompressed)
CRON_CONFIGS: '[{"cron": "0 * * * *", "retention_max": 15, "name": "every"},{"cron": "0 * * * *", "retention_max": 1, "name": "hourly"}]'
# RESTORE_CONFIG_NAME: 'hourly' # When you have to restore some content
volumes:
- ./backups:/backups
HealthCheck is automatically included into Docker image and provides an url to verify if backups works correctly:
http://localhost:5000/health
Restore the database from a given configuration name or backup file path:
docker exec <container_name> flask restore <name_or_path>
If a valid name_or_path is provided it restores this file to the database as configured with environment variables, else it tries to restore the latest backup from the backup configuration name provided with RESTORE_CONFIG_NAME.
If a configuration name is provided, the application will log the chosen backup file for restore.
Automatic restore and retention only consider regular scheduler-generated files
named <database>.<14-digit timestamp><configured extension>. Recovery
directories are excluded. Restore a backup with a nonstandard name by passing
its path explicitly.
The command exits with status code 0 when the restore succeeds and with status code 1 when the restore fails or when no backup is found for the requested configuration name, so it can be safely used in scripts.
Backups compressed with .gz are decompressed in a private system temporary directory before restore. The directory is removed when the restore completes or raises an exception. The temporary filesystem must have enough free space for the uncompressed backup. An abrupt process termination can leave a nards-db-restore- directory behind; remove abandoned directories after verifying that no restore is using them.
plpgsql) and writes a companion <backup>.pre.sql with CREATE EXTENSION IF NOT EXISTS ... statements (always including postgis, plus any others like vector).<backup>.pre.sql before pg_restore so that types/functions from extensions are available.postgresql-14-pgvector for pgvector), otherwise CREATE EXTENSION will fail.There are currently no planned activities:
However, we invite the community to suggest new features and improvements! Send us your ideas and help extend the project roadmap!
You could contribute to project as you like.
You could open issue when you want to propose a feature, a fix, report a bug or anythink else!
When you report a bug I ask you to include into Issue:
You're welcome to implement features too:
git checkout -b my-new-featuregit commit -am 'Add some feature'git push origin my-new-featureYou can also mail me: [[email protected]](mailto:[email protected]?subject=[nards-db-backup] Request)
Build the Docker image:
docker build -t flask_backup_app .
Run the Docker container:
docker run -p 5000:5000 --env-file .env flask_backup_app
Install the required Python packages:
pip install -r requirements.txt
Run the application:
python app.py
You can run the test suite in two ways, either mirroring the CI pipeline with Docker Compose, or directly from your local virtual environment using pytest (with Docker-managed services).
Run tests in containers (same as GitHub Actions):
docker compose -f docker-compose.quickstart.yml down -v && docker compose -f docker-compose.quickstart.yml up --build --exit-code-from test-runner
Tear down (optional if you used --exit-code-from, containers stop automatically):
docker compose -f docker-compose.quickstart.yml down -v
Redis note: in the test compose, the redis service runs in a tiny restart loop so that the test's SHUTDOWN does not terminate the container and abort the Compose run. This applies only to the test compose.
MongoDB note: the service is named mongodb in docker-compose.quickstart.yml. The test-runner exports DB_HOST_MONGODB=mongodb, DB_PORT_MONGODB=27017, DB_USER_MONGODB=testuser, DB_PASSWORD_MONGODB=testpassword, DB_NAME_MONGODB=admin for the tests.
GraphDB note: the test stack includes a graphdb service (GraphDB Free). The GraphDB module exports and restores explicit RDF data as N-Quads. Set DB_PORT to 7200 for the test service.
Requirements:
mysqldump (from mariadb-client or mysql-client)pg_dump, pg_restore, psql (from postgresql-client)pip install -r requirements.txtRun tests from your venv; the test stack is auto-started by pytest (pytest-docker):
source venv/bin/activate && pytest -q
Notes:
tests/conftest.py automatically brings up services from tests/docker-compose.yml and exports the needed env vars; no manual setup needed.
Includes MongoDB via the mongodb service; credentials: testuser / testpassword with authSource=admin.
If you have an existing container named mysql running, you may see a name conflict. Stop it or run:
docker compose -f tests/docker-compose.yml down -v
Redis note (local venv): if reading/writing tests/redis-data/dump.rdb hits a PermissionError on your host, the RedisModule automatically falls back to copying via docker cp by detecting the Redis container. This requires the docker CLI to be available locally.
This project is licensed under the GPL v3. See the LICENSE file for details.
docker pull nards/nards-db-backup:latest