Skip to content

Commit

Permalink
Merge pull request #531 from PAWECOGmbH/development
Browse files Browse the repository at this point in the history
Added the backup strategy
  • Loading branch information
ptruessel authored Oct 21, 2024
2 parents c2a8f2e + ddb1140 commit 84e0b69
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 6 deletions.
2 changes: 1 addition & 1 deletion compose-dev.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
lucee:
image: ${LUCEE_IMAGE}
image: ${LUCEE_IMAGE}:${LUCEE_IMAGE_VERSION}
ports:
- "${LUCEE_PORT}:80"
restart: always
Expand Down
2 changes: 1 addition & 1 deletion compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
lucee:
image: ${LUCEE_IMAGE}
image: ${LUCEE_IMAGE}:${LUCEE_IMAGE_VERSION}
ports:
- "${LUCEE_PORT}:80"
restart: always
Expand Down
28 changes: 28 additions & 0 deletions config/backup/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

# Activates the automatic export of variables
set -a

# Load the .env file
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
source "$PROJECT_ROOT/.env"

# Deactivates the automatic export of variables
set +a

# Checks whether the /backup folder exists and creates it if required
if [ ! -d "/backup" ]; then
mkdir -p /backup
fi

# Backup database
docker compose -f compose-backup.yml run db_backup
scp -i ${SSH_KEY_PATH} /backup/database.tar.gz ${SERVER_USER}@${SERVER_IP}:${REMOTE_BACKUP_PATH}

# Backup userdata
docker compose -f compose-backup.yml run userdata_backup
scp -i ${SSH_KEY_PATH} /backup/userdata.tar.gz ${SERVER_USER}@${SERVER_IP}:${REMOTE_BACKUP_PATH}

# Backup Lucee image
docker compose -f compose-backup.yml run lucee_image_backup
scp -i ${SSH_KEY_PATH} /backup/image_${LUCEE_IMAGE}_${LUCEE_IMAGE_VERSION}.tar ${SERVER_USER}@${SERVER_IP}:${REMOTE_BACKUP_PATH}
20 changes: 20 additions & 0 deletions config/backup/compose-backup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
services:
db_backup:
image: alpine
volumes:
- ${COMPOSE_PROJECT_NAME}_db_volume:/volume
- /backup:/backup
command: tar -czf /backup/database.tar.gz -C /volume .

userdata_backup:
image: alpine
volumes:
- ${COMPOSE_PROJECT_NAME}_userdata_volume:/volume
- /backup:/backup
command: tar -czf /backup/userdata.tar.gz -C /volume .

lucee_image_backup:
image: alpine
volumes:
- /backup:/backup
command: docker save -o /backup/image_${LUCEE_IMAGE}_${LUCEE_IMAGE_VERSION}.tar ${LUCEE_IMAGE}:${LUCEE_IMAGE_VERSION}
48 changes: 48 additions & 0 deletions config/backup/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
***Backup for the Production Environment***

**Purpose**

This directory contains the configurations and scripts for backing up and restoring the database, user data, and the Lucee image. These backups are intended only for the production environment.

By utilizing Docker Compose and shell scripts, the backup process is automated to ensure consistent and reliable data backups with minimal manual intervention.

**Structure**

- .env: This file contains environment variables specifically for backup and restoration in the production environment. Variables like volume names, image versions, and SSH credentials are defined here.

- compose-backup.yml: This file defines the container services needed to create backups. Each service performs a backup for the database, user data, or the Lucee image.

- backup.sh: A shell script that automates the process of backing up the database, user data, and Lucee image. It uses Docker Compose and secure copying (SCP) to transfer backups to the designated backup server.

- restore.sh: A shell script that automates the process of restoring the database, user data, and Lucee image. It retrieves backups from the backup server and restores them to the appropriate volumes.

**Usage**

*Backup*

To create a backup, run the following command from this directory:

sh backup.sh

This will:
1. Backup the database volume.
2. Backup the user data volume.
3. Backup the Lucee image.
4. Securely transfer all backups to the remote backup server.

*Restore*
To restore from a backup, run the following command:

sh restore.sh

This will:

1. Retrieve the latest backups from the remote backup server.
2. Restore the database volume.
3. Restore the user data volume.
4. Load the Lucee image into Docker.

**Notes**
- The backups created by this process are only for the **production environment**. Please ensure that the environment variables in the `.env` file are configured correctly before running any backups or restores.

- Make sure to update the `.env` file with the correct values (such as volume names, SSH keys, and server IP) specific to the production setup.
29 changes: 29 additions & 0 deletions config/backup/restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Activates the automatic export of variables
set -a

# Load the .env file
source /.env

# Deactivates the automatic export of variables
set +a

# Checks whether the /restore folder exists and creates it if required
if [ ! -d "/restore" ]; then
mkdir -p /restore
fi

# Restore database
scp -i ${SSH_KEY_PATH} ${SERVER_USER}@${SERVER_IP}:${REMOTE_BACKUP_PATH}/database.tar.gz /restore/
docker run --rm -v ${COMPOSE_PROJECT_NAME}_db_volume:/volume -v /restore:/backup alpine sh -c "cd /volume && tar -xzf /backup/database.tar.gz"
docker restart ${MYSQL_CONTAINER_NAME}

# Restore userdata
scp -i ${SSH_KEY_PATH} ${SERVER_USER}@${SERVER_IP}:${REMOTE_BACKUP_PATH}/userdata.tar.gz /restore/
docker run --rm -v ${COMPOSE_PROJECT_NAME}_userdata_volume:/volume -v /restore:/backup alpine sh -c "cd /volume && tar -xzf /backup/userdata.tar.gz"
docker restart ${LUCEE_CONTAINER_NAME}

# Restore Lucee image
scp -i ${SSH_KEY_PATH} ${SERVER_USER}@${SERVER_IP}:${REMOTE_BACKUP_PATH}/image_${LUCEE_IMAGE}_${LUCEE_IMAGE_VERSION}.tar /restore/
docker load -i /restore/image_${LUCEE_IMAGE}_${LUCEE_IMAGE_VERSION}.tar
22 changes: 18 additions & 4 deletions config/example.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
## Project name
COMPOSE_PROJECT_NAME=saaster # Must be unique on docker host.

## Lucee settings (default: lucee/lucee:6.0-nginx)
LUCEE_IMAGE=lucee/lucee:6.0-nginx # For the initial setup, please leave it as is, then use your own image name afterward.
## Lucee settings
# Keep the Lucee image name for setup, then replace with your own later.
# Default: lucee/lucee:6.0-nginx
LUCEE_IMAGE=lucee/lucee
LUCEE_IMAGE_VERSION=6.0-nginx
LUCEE_CONTAINER_NAME=saaster_lucee # Must be unique on docker host.
LUCEE_PORT=8080 # Must be unique on docker host.
LUCEE_ADMIN_PASSWORD=defaultpass
Expand All @@ -22,8 +25,19 @@ FLYWAY_DB_FOLDER=core # core or myapp
FLYWAY_MIGRATION_TYPE=migrate # migrate or repair
FLYWAY_CONTAINER_NAME=saaster_flyway # Must be unique on docker host.

## Inbucket settings (dev)
## Inbucket settings (development environment)
INBUCKET_CONTAINER_NAME=saaster_inbucket # Will be used as the SMTP server in Lucee.
INBUCKET_SMTP_PORT=2500 # Must be unique on docker host. In Lucee Admin please set to 2500.
INBUCKET_WEB_PORT=9000 # Must be unique on docker host.
INBUCKET_POP3_PORT=1100 # Must be unique on docker host.
INBUCKET_POP3_PORT=1100 # Must be unique on docker host.


# Backup settings (production environment)

# Backup path on the remote server
REMOTE_BACKUP_PATH=/backups

# Server information for SCP Transfer
SERVER_USER=root
SERVER_IP=xxx.xxx.xxx.xxx
SSH_KEY_PATH=~/.ssh/id_rsa

0 comments on commit 84e0b69

Please sign in to comment.