Enhance backup system with comprehensive scripts and documentation

Database Backup Scripts:
- Add db-dump--standalone.sh for online database dumps (no downtime)
- Add db-restore-from-dump--standalone.sh for restoring from dumps
- Rename backup scripts with double-dash convention for clarity
- Add comprehensive header comments to all backup/restore scripts
- Document online vs offline, standalone vs swarm deployment types
- Add KEEP variable default (7) to dump script for rotation

Docker Configuration:
- Add mongo-backup volume mount to docker-compose-standalone.yaml
- Add mongo-backup volume mount to docker-compose-debug.yml
- Add container names to debug compose for consistency with standalone

Documentation:
- Add comprehensive Database Backup & Restore section to README.md
- Document all backup scripts with usage examples
- Document automated cron schedule (04:00 daily)
- Sunday: Full volume backup (KEEP=2, offline)
- Monday-Saturday: Database dumps (KEEP=7, online)
- Document backup directories and log file locations

Git Configuration:
- Add mongo-backup/ to .gitignore
- Fix missing newline at end of .gitignore

File Cleanup:
- Remove db-scheduled-backup.sh (superseded by cron jobs)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-26 11:11:17 +01:00
parent dc5698d7d6
commit 4454ea3b7e
11 changed files with 399 additions and 83 deletions

View File

@@ -17,6 +17,75 @@ Each location record is marked with a user ID.
All the actions user `withUser` to fetch user ID, which is then used in all the DB operations.
# Database Backup & Restore
The project includes multiple backup strategies for different deployment scenarios and requirements.
## Backup Scripts Overview
### Standalone Docker Deployments
**Online Backups (No Downtime):**
- `db-dump--standalone.sh` - Creates online backup of the 'utility-bills' database using mongodump
- Database stays running during backup
- Only backs up the database content, not the full volume
- Output: `./mongo-backup/utility-bills-dump-YYYY-MM-DD_HH-MM.tar.gz`
- Default retention: 7 backups (configurable via `KEEP` env var)
- Usage: `./db-dump--standalone.sh` or `KEEP=10 ./db-dump--standalone.sh`
- `db-restore-from-dump--standalone.sh` - Restores from mongodump archives
- Database stays running during restore
- **WARNING**: Drops existing collections before restore
- Usage: `./db-restore-from-dump--standalone.sh utility-bills-dump-2025-11-26_14-30.tar.gz`
**Offline Backups (With Downtime):**
- `db-backup--standalone.sh` - Creates offline backup of the complete mongo-volume directory
- Database container is stopped during backup for consistency
- Backs up the entire MongoDB data directory
- Output: `./backups/mongo-volume-backup-YYYY-MM-DD-HH-MM.tar.gz`
- Default retention: 7 backups (configurable via `KEEP` env var)
- Usage: `./db-backup--standalone.sh` or `KEEP=2 ./db-backup--standalone.sh`
### Docker Swarm Deployments
- `db-backup--swarm.sh` - Creates offline backup by scaling down the MongoDB service
- Service is scaled to 0 during backup
- Output: `./backups/mongo-volume-backup-YYYY-MM-DD-HH-MM.tar.gz`
- Usage: `./db-backup--swarm.sh`
- `db-restore-from-backup--swarm.sh` - Restores volume backup by scaling down the service
- Service is scaled to 0 during restore
- Optional `--pre-backup` flag for safety backup before restore
- Usage: `./db-restore-from-backup--swarm.sh mongo-volume-backup-2025-11-26-14-30.tar.gz`
## Automated Backup Schedule
Backups run automatically via cron at 04:00 every day:
```cron
# Sunday: Full volume backup (offline), keep 2 backups
0 4 * * 0 cd /home/knee-cola/web-pro/evidencija-rezija && KEEP=2 ./db-backup--standalone.sh
# Monday-Saturday: Database dump (online), keep 7 backups
0 4 * * 1-6 cd /home/knee-cola/web-pro/evidencija-rezija && KEEP=7 ./db-dump--standalone.sh
```
**Backup Strategy:**
- **Sundays**: Full volume backup with brief downtime (keeps 2 weeks of full backups)
- **Monday-Saturday**: Online database dumps without downtime (keeps 7 days of incremental backups)
All backup operations are logged with timestamps:
- Volume backups: `./backups/db-backup-standalone.log`
- Database dumps: `./mongo-backup/db-dump-db-standalone.log`
- Restore operations: `./mongo-backup/db-restore-from-dump.log`
## Backup Directories
- `./backups/` - Volume backup archives (full mongo-volume directory)
- `./mongo-backup/` - Database dump archives (utility-bills database only)
Both directories are excluded from git via `.gitignore`.
# Deploying
The deployment is done via Docker:
* build docker image