Skip to content

Commit

Permalink
CU-8692nw7qj: crontab for running the backup script periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
tomolopolis committed Sep 26, 2023
1 parent feede11 commit 2f979dd
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 4 deletions.
15 changes: 15 additions & 0 deletions docker-compose-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ services:
- MCT_VERSION=v2.11.2
command: /home/scripts/run.sh

# crontab - for db backup
medcattrainer-db-backup:
image: cogstacksystems/medcat-trainer:v2.11.2
restart: always
volumes:
- ./configs:/home/configs
- api-media:/home/api/media
- api-static:/home/api/static
- api-db:/home/api/db
- api-db-backup:/home/api/db-backup
env_file:
- ./envs/env
entrypoint: /home/scripts/entry.sh
command: cron -f -l 2

nginx:
container_name: medcattrainer_nginx
image: cogstacksystems/medcat-trainer-nginx:v2.11.2
Expand Down
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ services:
- MCT_VERSION=v2.11.2
command: /home/scripts/run.sh

# crontab - for db backup
medcattrainer-db-backup:
image: cogstacksystems/medcat-trainer:v2.11.2
restart: always
volumes:
- ./configs:/home/configs
- api-media:/home/api/media
- api-static:/home/api/static
- api-db:/home/api/db
- api-db-backup:/home/api/db-backup
env_file:
- ./envs/env
entrypoint: /home/scripts/entry.sh
command: cron -f -l 2

nginx:
image: cogstacksystems/medcat-trainer-nginx:v2.11.2
restart: always
Expand Down
16 changes: 13 additions & 3 deletions docs/maintanence.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,20 @@ 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
```

This is automatically done each time the service starts, and any migrations are performed, in the events of a new release
for example.
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
Expand All @@ -48,4 +57,5 @@ Found db-backup-2023-09-25__23-21-39.sqlite3 - y to confirm backup: y # you'll
Restored db-backup-2023-09-25__23-21-39.sqlite3 to /home/db/db.sqlite3
```

The `restore_db.sh` scipt will automatically restore the latest db file, if no file is specified.
The `restore_db.sh` script will automatically restore the latest db file, if no file is specified.

3 changes: 2 additions & 1 deletion envs/env
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ CONCEPT_SEARCH_SERVICE_PORT=8983
DB_DIR=/home/api/db
# currently only supports sqlite3 dbs
DB_PATH=${DB_DIR}/db.sqlite3
DB_BACKUP_DIR=/home/api/db-backup
DB_BACKUP_DIR=/home/api/db-backup

10 changes: 10 additions & 0 deletions webapp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ RUN apt-get update -y && \
# install vim as its annoying not to have an editor
RUN apt-get install -y vim

# install cron - and remove any default tabs
RUN apt-get install -y cron && which cron && rm -rf /etc/cron.*/*

# Get node and npm
RUN apt install -y nodejs && apt install -y npm

Expand All @@ -18,6 +21,13 @@ ENV PATH="/root/.cargo/bin:${PATH}"
WORKDIR /home
COPY ./ .

# copy backup crontab and chmod scripts
RUN chmod u+x /home/scripts/entry.sh && \
chmod u+x /home/scripts/crontab && \
cp /home/scripts/crontab /etc/crontab \

RUN chmod 0744 /etc/crontab && chmod u+x /home/app-backend/notify/entry.sh

# Build frontend
WORKDIR /home/frontend
RUN npm install && npm run build
Expand Down
4 changes: 4 additions & 0 deletions webapp/scripts/backup_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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}
Expand Down
3 changes: 3 additions & 0 deletions webapp/scripts/crontab
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
6 changes: 6 additions & 0 deletions webapp/scripts/entry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
env >> /etc/environment

# execute CMD
echo "$@"
exec "$@"

0 comments on commit 2f979dd

Please sign in to comment.