-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #153 from CogStack/db-backup-process
CU-8692nw7qj: backup and restore scripts sqlite db sh scripts
- Loading branch information
Showing
16 changed files
with
189 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Maintanence | ||
|
||
MedCATtrainer is actively maintained. To ensure you receive the latest | ||
security patches of the software and its dependencies you should regularly | ||
be upgrading to the latest release. | ||
|
||
The latest stable releases update the `docker-compose.yml` and `docker-compose-prod.yml` files. | ||
|
||
To update these docker compose files, either copy them directly from the [repo](https://github.com/CogStack/MedCATtrainer) | ||
or update the cloned files via: | ||
|
||
```shell | ||
$ cd MedCATtrainer | ||
$ git pull | ||
$ docker-compose up | ||
# alternatively for prod releases use: | ||
$ docker-compose -f docker-compose-prod.yml up | ||
``` | ||
|
||
MedCATtrainer follows [Semver](https://semver.org/), so patch and minor release should always be backwards compatible, | ||
whereas major releases, e.g. v1.x vs 2.x versions signify breaking changes. | ||
|
||
Neccessary Django DB migrations will automatically applied between releases, which should largely be invisible to an end admin | ||
or annotation user. Nevertheless, migrating ORM / DB models, then rolling back a release can cause issues if values are defaulted | ||
or removed from a later version. | ||
|
||
## Backup and Restore | ||
|
||
### Backup | ||
Before updating to a new release, a backup will be created in the `DB_BACKUP_DIR`, as configured in `envs/env`. | ||
A further crontab runs the same backup script at 10pm every night. This does not cause any downtime and will look like | ||
this in the logs: | ||
```shell | ||
medcattrainer-medcattrainer-db-backup-1 | Found backup dir location: /home/api/db-backup and DB_PATH: /home/api/db/db.sqlite3 | ||
medcattrainer-medcattrainer-db-backup-1 | Backed up existing DB to /home/api/db-backup/db-backup-2023-09-26__23-26-01.sqlite3 | ||
medcattrainer-medcattrainer-db-backup-1 | To restore this backup use $ ./restore.sh /home/api/db-backup/db-backup-2023-09-26__23-26-01.sqlite3 | ||
``` | ||
|
||
A backup is also automatically performed each time the service starts, and any migrations are performed, in the events of a new release | ||
introducing a breaking change and corrupting a DB. | ||
|
||
### Restore | ||
If a DB is corrupted or needs to be restored to an existing backed up db use the following commands, whilst the service is running: | ||
|
||
```shell | ||
$ docker ps | ||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | ||
a2489b0c681b cogstacksystems/medcat-trainer-nginx:v2.11.2 "/docker-entrypoint.…" 4 days ago Up 4 days 80/tcp, 0.0.0.0:8001->8000/tcp, :::8001->8000/tcp medcattrainer-nginx-1 | ||
20fed153d798 solr:8 "docker-entrypoint.s…" 4 days ago Up 4 days 0.0.0.0:8983->8983/tcp, :::8983->8983/tcp mct_solr | ||
2b250a0975fe cogstacksystems/medcat-trainer:v2.11.2 "/home/run.sh" 4 days ago Up 4 days medcattrainer-medcattrainer-1 | ||
$ docker exec -it 2b250a0975fe bash | ||
root@2b250a0975fe:/home/api# cd .. | ||
$ restore_db.sh db-backup-2023-09-25__23-21-39.sqlite3 # run the restore.sh script | ||
Found backup dir location: /home/api/db-backup, found db path: home/api/db/db.sqlite3 | ||
DB file to restore: db-backup-2023-09-25__23-21-39.sqlite3 | ||
Found db-backup-2023-09-25__23-21-39.sqlite3 - y to confirm backup: y # you'll need tp confirm this is the correct file to restore. | ||
Restored db-backup-2023-09-25__23-21-39.sqlite3 to /home/db/db.sqlite3 | ||
``` | ||
|
||
The `restore_db.sh` script will automatically restore the latest db file, if no file is specified. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/bin/sh | ||
|
||
if [ -n "${DB_BACKUP_DIR}" ] && [ -f "${DB_PATH}" ]; then | ||
echo "Found backup dir location: ${DB_BACKUP_DIR} and DB_PATH: ${DB_PATH}" | ||
if [ ! -d "${DB_BACKUP_DIR}" ]; then | ||
mkdir DB_BACKUP_DIR | ||
else | ||
# remove backups older than 90 days. | ||
echo "Checking age of current backups - removing any older than 90 days.." | ||
find ${DB_BACKUP_DIR} -mtime +90 -type f -delete | ||
fi | ||
BACKUP_NAME=db-backup-$(date +"%Y-%m-%d__%H-%M-%S").sqlite3 | ||
cp $DB_PATH ${DB_BACKUP_DIR}/${BACKUP_NAME} | ||
echo "Backed up existing DB to ${DB_BACKUP_DIR}/${BACKUP_NAME}" | ||
echo "To restore this backup use $ /home/scripts/restore.sh ${DB_BACKUP_DIR}/${BACKUP_NAME}" | ||
else | ||
echo "No DB_BACKUP_DIR env var found or DB_PATH . This should be set in env vars. No backups will be created" | ||
return 0 | ||
fi | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SHELL=/bin/bash | ||
BASH_ENV=/etc/envrionment | ||
0 22 * * * root /home/scripts/backup_db.sh > /proc/1/fd/1 2>/proc/1/fd/2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
env >> /etc/environment | ||
|
||
# execute CMD | ||
echo "$@" | ||
exec "$@" |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/bin/sh | ||
|
||
DB_RESTORE_FILE=$1 | ||
BACKUP_DIR=${DB_BACKUP_DIR} | ||
|
||
if [ -n "$BACKUP_DIR" ] && [ -n "$DB_PATH" ]; then | ||
echo "Found backup dir location: ${BACKUP_DIR}, found db path: ${DB_PATH}" | ||
if [ -z "$DB_RESTORE_FILE" ]; then | ||
echo "No specific backup specified. Restoring latest backup in $BACKUP_DIR" | ||
DB_RESTORE_FILE=$(ls -Art ${BACKUP_DIR}/ | tail -n 1) | ||
fi | ||
echo "DB file to restore: ${DB_RESTORE_FILE}" | ||
read -p "Found $DB_RESTORE_FILE - y to confirm backup: " choice | ||
case "$choice" in | ||
y|Y ) | ||
cp $BACKUP_DIR/$DB_RESTORE_FILE $DB_PATH | ||
echo "Restored $DB_RESTORE_FILE to $DB_PATH" | ||
;; | ||
n|N ) | ||
echo " - exiting";; | ||
* ) | ||
echo "Invalid choice - exiting";; | ||
esac | ||
else | ||
echo "No BACKUP_DIR and DB_PATH found. Set to location of the backups and the location of DB sqlite3 file to be restored." | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters