diff --git a/docker-compose-prod.yml b/docker-compose-prod.yml index c31c2b48..afcecd51 100644 --- a/docker-compose-prod.yml +++ b/docker-compose-prod.yml @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index 1fd4956e..a289e734 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/docs/maintanence.md b/docs/maintanence.md index cf4e067f..05d4bdfd 100644 --- a/docs/maintanence.md +++ b/docs/maintanence.md @@ -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 @@ -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. + diff --git a/webapp/Dockerfile b/webapp/Dockerfile index 9fa5d36a..dec1c44a 100644 --- a/webapp/Dockerfile +++ b/webapp/Dockerfile @@ -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 @@ -18,6 +21,12 @@ 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 && +RUN 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 diff --git a/webapp/scripts/crontab b/webapp/scripts/crontab new file mode 100644 index 00000000..a0083c59 --- /dev/null +++ b/webapp/scripts/crontab @@ -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 \ No newline at end of file diff --git a/webapp/scripts/entry.sh b/webapp/scripts/entry.sh new file mode 100644 index 00000000..60097910 --- /dev/null +++ b/webapp/scripts/entry.sh @@ -0,0 +1,6 @@ +#!/bin/bash +env >> /etc/environment + +# execute CMD +echo "$@" +exec "$@"