Skip to content

Instantly share code, notes, and snippets.

@carlansell94
Created November 19, 2023 15:50
Show Gist options
  • Save carlansell94/e075137df4d371e8ed711ebaee23e767 to your computer and use it in GitHub Desktop.
Save carlansell94/e075137df4d371e8ed711ebaee23e767 to your computer and use it in GitHub Desktop.
A docker stack template to create a Wordpress/mariadb stack with added SSH backup capability
# A docker stack template to create a Wordpress/mariadb stack with added SSH backup capability.
#
# Backups are managed by offen/docker-volume-backup. Check the documentation for more options.
# Backups are configured to run at 23:00 every Sunday, change the BACKUP_CRON_EXPRESSION to change the backup frequency.
# This particular file is set to send backups to an external server using SSH.
#
# Required environment variables (should be self-explanatory):
# MYSQL_USER
# MYSQL_PASSWORD
# MYSQL_DATABASE
# SSH_HOST
# SSH_USER
# SSH_PATH
#
# An SSH key must be set up on the host, available at /root/.ssh/id_rsa. Change this path if the key is located elsewhere.
# Information on how to generate SSH keys and copy them to the receiving server can be found here: https://qubitsandbytes.co.uk/linux/setting-up-passwordless-ssh
# When running, the Wordpress UI is available at http://{server_ip}:9094.
version: '3'
services:
main_app:
image: wordpress:latest
container_name: wordpress
ports:
- target: 80
published: 9094
protocol: tcp
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: ${MYSQL_USER}
WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
volumes:
- wordpress:/var/www/html
db:
image: mariadb:latest
container_name: wp_database
labels:
- docker-volume-backup.archive-pre=/bin/sh -c 'mariadb-dump -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE --compact > /tmp/backup/db.sql'
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/tmp/backup
backup_1: &backup_service
image: offen/docker-volume-backup:v2
environment: &backup_environment
BACKUP_CRON_EXPRESSION: "00 23 * * 0"
BACKUP_STOP_CONTAINER_LABEL: data_backup
SSH_HOST_NAME: ${SSH_HOST_NAME}
SSH_PORT: 22
SSH_USER: ${SSH_USER}
SSH_REMOTE_PATH: ${SSH_PATH}
volumes:
- wordpress:/backup/wordpress-backup:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- /root/.ssh/id_rsa:/root/.ssh/id_rsa:ro
backup_2:
<<: *backup_service
environment:
<<: *backup_environment
BACKUP_STOP_CONTAINER_LABEL: db_backup
BACKUP_FILENAME: db-%Y-%m-%dT%H-%M-%S.tar.gz
volumes:
- db:/backup/db-backup:ro
- /root/.ssh/id_rsa:/root/.ssh/id_rsa:ro
volumes:
wordpress:
db:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment