Skip to content

Commit

Permalink
Merge pull request #1 from magicpages/develop
Browse files Browse the repository at this point in the history
v1.0.0
  • Loading branch information
betschki authored Aug 4, 2024
2 parents 40ec504 + e71b25b commit 0cfde6e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 36 deletions.
59 changes: 49 additions & 10 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,78 @@ jobs:
if: github.ref == 'refs/heads/develop'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: docker/setup-qemu-action@v1
- uses: docker/setup-buildx-action@v1
- uses: docker/login-action@v1
- name: Checkout code
uses: actions/checkout@v2

- name: Set up QEMU
uses: docker/setup-qemu-action@v1

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Log in to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: magicpages/mysql-onsite-offsite-backup:dev
platforms: linux/amd64,linux/arm64
platforms: linux/amd64 # Only build for amd64 during development

- name: Cleanup Docker cache
run: docker builder prune -f

release-build-and-push:
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: docker/setup-qemu-action@v1
- uses: docker/setup-buildx-action@v1
- uses: docker/login-action@v1
- name: Checkout code
uses: actions/checkout@v2

- name: Set up QEMU
uses: docker/setup-qemu-action@v1

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Log in to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- run: echo "VERSION=${GITHUB_REF/refs\/tags\/v/}" >> $GITHUB_ENV

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: magicpages/mysql-onsite-offsite-backup:${{ env.VERSION }},magicpages/mysql-onsite-offsite-backup:latest
platforms: linux/amd64,linux/arm64
platforms: linux/amd64,linux/arm64 # Build for both platforms for releases

- name: Cleanup Docker cache
run: docker builder prune -f
24 changes: 7 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,15 @@ RUN apt-get update && \
apt-get install -y mysql-client s3cmd xz-utils curl cron && \
apt-get clean

# Copy backup, cleanup, and entrypoint scripts
COPY backup.bash /usr/local/bin/backup.bash
COPY cleanup.bash /usr/local/bin/cleanup.bash
COPY entrypoint.bash /usr/local/bin/entrypoint.bash

# Make the scripts executable
RUN chmod +x /usr/local/bin/backup.bash /usr/local/bin/cleanup.bash /usr/local/bin/entrypoint.bash

# Add crontab file in the cron directory
# Copy scripts and crontab
COPY backup.bash cleanup.bash entrypoint.bash /usr/local/bin/
COPY crontab /etc/cron.d/backup-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/backup-cron

# Apply cron job
RUN crontab /etc/cron.d/backup-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Make the scripts executable and set up cron jobs
RUN chmod +x /usr/local/bin/backup.bash /usr/local/bin/cleanup.bash /usr/local/bin/entrypoint.bash && \
chmod 0644 /etc/cron.d/backup-cron && \
crontab /etc/cron.d/backup-cron && \
touch /var/log/cron.log

# Set entrypoint
ENTRYPOINT ["/usr/local/bin/entrypoint.bash"]
Expand Down
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Overview

This repository is an open-source tool designed to create and manage MySQL database backups. It allows you to store backups both onsite (e.g., on your own server or NAS) and offsite (e.g., on an S3-compatible storage). This ensures data redundancy and security, protecting against data loss in various scenarios.
This repository is an open-source tool designed to create and manage MySQL database backups. It allows you to store backups both onsite (e.g., on your own server or NAS) and offsite (e.g., on S3-compatible storage). This ensures data redundancy and security, protecting against data loss in various scenarios.

This tool was developed for the specific use case at [Magic Pages](https://magicpages.com), a managed Ghost CMS hosting provider, to maintain robust backups of all databases. However, it can be used by anyone needing a reliable MySQL backup solution.

Expand All @@ -23,14 +23,14 @@ This tool was developed for the specific use case at [Magic Pages](https://magic

## Installation

1. **Clone the Repository:**
1. **Clone the Repository**

```sh
git clone https://github.com/magicpages/mysql-onsite-offsite-backup.git
cd mysql-onsite-offsite-backup
```

2. Create and Configure the .env File:
2. **Create and Configure the .env File**

Create a .env file in the root directory and fill in the necessary environment variables:

Expand All @@ -52,16 +52,16 @@ MONTHLY_RETENTION=6
YEARLY_RETENTION=2
```

3. Build and Run the Docker Container:
3. **Build and Run the Docker Container**

```sh
docker-compose build
docker-compose up -d
docker compose up -d
```

## Usage

### Backup

The backup process is automated using a cron job configured in the Docker container. The backup script will:

1. Connect to the MySQL server.
Expand All @@ -71,16 +71,18 @@ The backup process is automated using a cron job configured in the Docker contai
5. Store the compressed dumps both locally (on your server or NAS) and offsite (on S3-compatible storage).

### Cleanup

The cleanup process is also automated using a cron job. It will:

1. Remove old backups based on the retention policy both onsite and offsite.

### Manual Trigger

You can manually trigger the backup and cleanup scripts inside the running container:

```sh
# Enter the running container
docker exec -it mysql-onsite-offsite-backup-backup-1 bash # Replace with your container name
docker exec -it mysql-onsite-offsite-backup_backup_1 bash
# Run backup script
bash /usr/local/bin/backup.bash
Expand All @@ -90,6 +92,7 @@ bash /usr/local/bin/cleanup.bash
```

## Configuration

The tool is configured via environment variables in the .env file. The following variables are required:

- MYSQL_USER: MySQL username
Expand All @@ -100,10 +103,12 @@ The tool is configured via environment variables in the .env file. The following
- S3_BUCKET_NAME: S3 bucket name
- S3_HOST_BASE: S3 host base URL
- S3_HOST_BUCKET: S3 host bucket URL pattern
- S3_BUCKET_LOCATION: S3 bucket location.
- S3_BUCKET_LOCATION: S3 bucket location
- DAILY_RETENTION: Number of daily backups to retain
- WEEKLY_RETENTION: Number of weekly backups to retain
- MONTHLY_RETENTION: Number of monthly backups to retain
- YEARLY_RETENTION: Number of yearly backups to retain

### Cron Schedule

The default cron schedule is set to run the backup and cleanup scripts at 2 AM daily (the server's local time). You can customize this by editing the crontab file in the project directory.
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3.8'

services:
backup:
build: .
image: magicpages/mysql-onsite-offsite-backup:latest
environment:
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASS: ${MYSQL_PASS}
Expand Down

0 comments on commit 0cfde6e

Please sign in to comment.