Skip to content
This repository was archived by the owner on Apr 27, 2024. It is now read-only.

Commit 0404dbd

Browse files
Merge pull request #8 from benjamin-maynard/gcs
Version 2.2.0
2 parents 16e0ca9 + dddd1f9 commit 0404dbd

File tree

4 files changed

+192
-43
lines changed

4 files changed

+192
-43
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [v2.2.0] - 28-11-2019
8+
### Implement GCS Backend, and rename to kubernetes-cloud-mysql-backup
9+
- Added the ability to use Google Cloud Storage (GCS) as a backend storage provider (backwards compatible)
10+
- Renamed to kubernetes-cloud-mysql-backup to better reflect the function of the application
11+
- Improved environment variable processing (removed case sensitivity of Slack environment variable)
12+
- Upgraded Alpine version to 3.10 base
13+
- Switched to Python3
14+
- Documentation improvements
15+
716
## [v2.1.0] - 28-08-2019
817
### Added the ability to add a timestamp to the backup file name
918
- Ability to append timestamp to the database dump via the BACKUP_TIMESTAMP environment variable added. Thanks & credit: @kuzm1ch

Dockerfile

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
11
# Set the base image
2-
FROM alpine:3.6
2+
FROM alpine:3.10
33

44
RUN apk -v --update add \
5-
python \
5+
python3 \
66
py-pip \
77
groff \
88
less \
99
mailcap \
1010
mysql-client \
1111
curl \
12+
py-crcmod \
13+
bash \
14+
libc6-compat \
15+
gnupg \
16+
coreutils \
1217
&& \
13-
pip install --upgrade awscli s3cmd python-magic && \
18+
pip3 install --upgrade awscli s3cmd python-magic && \
1419
apk -v --purge del py-pip && \
1520
rm /var/cache/apk/*
1621

1722
# Set Default Environment Variables
1823
ENV TARGET_DATABASE_PORT=3306
1924
ENV SLACK_ENABLED=false
2025
ENV SLACK_USERNAME=kubernetes-s3-mysql-backup
26+
ENV CLOUD_SDK_VERSION=272.0.0
27+
ENV BACKUP_PROVIDER=aws
28+
29+
# Set Google Cloud SDK Path
30+
ENV PATH /google-cloud-sdk/bin:$PATH
31+
32+
# Install Google Cloud SDK
33+
RUN curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz && \
34+
tar xzf google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz && \
35+
rm google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz && \
36+
gcloud config set core/disable_usage_reporting true && \
37+
gcloud config set component_manager/disable_update_check true && \
38+
gcloud config set metrics/environment github_docker_image && \
39+
gcloud --version
2140

2241
# Copy Slack Alert script and make executable
2342
COPY resources/slack-alert.sh /

README.md

Lines changed: 120 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,61 @@
1-
# kubernetes-s3-mysql-backup
1+
# kubernetes-cloud-mysql-backup
22

3-
kubernetes-s3-mysql-backup is a container image based on Alpine Linux. This container is designed to run in Kubernetes as a cronjob to perform automatic backups of MySQL databases to Amazon S3. It was created to meet my requirements for regular and automatic database backups. Having started with a relatively basic feature set, it is gradually growing to add more and more features.
3+
kubernetes-cloud-mysql-backup is a container image based on Alpine Linux. This container is designed to run in Kubernetes as a cronjob to perform automatic backups of MySQL databases to Amazon S3 or Google Cloud Storage. It was created to meet my requirements for regular and automatic database backups. Having started with a relatively basic feature set, it is gradually growing to add more and more features.
44

5-
Currently, kubernetes-s3-mysql-backup supports the backing up of MySQL Databases. It can perform backups of multiple MySQL databases from a single database host. When triggered, a full database dump is performed using the `mysqldump` command for each configured database. The backup(s) are then uploaded to an Amazon S3 Bucket. kubernetes-s3-mysql-backup features Slack Integration, and can post messages into a channel detailing if the backup(s) were successful or not.
5+
Currently, kubernetes-cloud-mysql-backup supports the backing up of MySQL Databases. It can perform backups of multiple MySQL databases from a single database host. When triggered, a full database dump is performed using the `mysqldump` command for each configured database. The backup(s) are then uploaded to an Amazon S3 Bucket or a Google Cloud Storage Bucket. kubernetes-cloud-mysql-backup features Slack Integration, and can post messages into a channel detailing if the backup(s) were successful or not.
66

7-
Over time, kubernetes-s3-mysql-backup will be updated to support more features and functionality. I currently use this container as part of my Kubernetes Architecture which you can read about [here](https://benjamin.maynard.io/this-blog-now-runs-on-kubernetes-heres-the-architecture/).
7+
Over time, kubernetes-cloud-mysql-backup will be updated to support more features and functionality. I currently use this container as part of my Kubernetes Architecture which you can read about [here](https://benjamin.maynard.io/this-blog-now-runs-on-kubernetes-heres-the-architecture/).
88

99
All changes are captured in the [changelog](CHANGELOG.md), which adheres to [Semantic Versioning](https://semver.org/spec/vadheres2.0.0.html).
1010

1111

1212
## Environment Variables
1313

14-
The below table lists all of the Environment Variables that are configurable for kubernetes-s3-mysql-backup.
15-
16-
| Environment Variable | Purpose |
17-
| --------------------------- |------------------------------------------------------------------------------------------------------------------|
18-
| AWS_ACCESS_KEY_ID | **(Required)** AWS IAM Access Key ID. |
19-
| AWS_SECRET_ACCESS_KEY | **(Required)** AWS IAM Secret Access Key. Should have very limited IAM permissions (see below for example) and should be configured using a Secret in Kubernetes. |
20-
| AWS_DEFAULT_REGION | **(Required)** Region of the S3 Bucket (e.g. eu-west-2). |
21-
| AWS_BUCKET_NAME | **(Required)** The name of the S3 bucket. |
22-
| AWS_BUCKET_BACKUP_PATH | **(Required)** Path the backup file should be saved to in S3. E.g. `/database/myblog/backups`. **Do not put a trailing / or specify the filename.** |
23-
| TARGET_DATABASE_HOST | **(Required)** Hostname or IP address of the MySQL Host. |
24-
| TARGET_DATABASE_PORT | **(Optional)** Port MySQL is listening on (Default: 3306). |
25-
| TARGET_DATABASE_NAMES | **(Required)** Name of the databases to dump. This should be comma seperated (e.g. `database1,database2`). |
26-
| TARGET_DATABASE_USER | **(Required)** Username to authenticate to the database with. |
27-
| TARGET_DATABASE_PASSWORD | **(Required)** Password to authenticate to the database with. Should be configured using a Secret in Kubernetes. |
28-
| BACKUP_TIMESTAMP | **(Optional)** Date string to append to the backup filename ([date](http://man7.org/linux/man-pages/man1/date.1.html) format). Leave unset if using S3 Versioning and date stamp is not required. |
29-
| SLACK_ENABLED | **(Optional)** (true/false) Enable or disable the Slack Integration (Default False). |
30-
| SLACK_USERNAME | **(Optional)** (true/false) Username to use for the Slack Integration (Default: kubernetes-s3-mysql-backup). |
31-
| SLACK_CHANNEL | **(Required if Slack enabled)** Slack Channel the WebHook is configured for. |
32-
| SLACK_WEBHOOK_URL | **(Required if Slack enabled)** What is the Slack WebHook URL to post to? Should be configured using a Secret in Kubernetes. |
14+
The below table lists all of the Environment Variables that are configurable for kubernetes-cloud-mysql-backup.
15+
16+
| Environment Variable | Purpose |
17+
| --------------------------- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
18+
| BACKUP_PROVIDER | **(Optional)** The backend to use for storing the MySQL backups. Supported options are `aws` (default) or `gcp` |
19+
| AWS_ACCESS_KEY_ID | **(Required for AWS Backend)** AWS IAM Access Key ID. |
20+
| AWS_SECRET_ACCESS_KEY | **(Required for AWS Backend)** AWS IAM Secret Access Key. Should have very limited IAM permissions (see below for example) and should be configured using a Secret in Kubernetes. |
21+
| AWS_DEFAULT_REGION | **(Required for AWS Backend)** Region of the S3 Bucket (e.g. eu-west-2). |
22+
| AWS_BUCKET_NAME | **(Required for AWS Backend)** The name of the S3 bucket. |
23+
| AWS_BUCKET_BACKUP_PATH | **(Required for AWS Backend)** Path the backup file should be saved to in S3. E.g. `/database/myblog/backups`. **Do not put a trailing / or specify the filename.** |
24+
| GCP_GCLOUD_AUTH | **(Required for GCP Backend)** Base64 encoded service account key exported as JSON. Example of how to generate: `base64 ~/service-key.json` |
25+
| GCP_BUCKET_NAME | **(Required for GCP Backend)** The name of GCP GCS bucket. |
26+
| GCP_BUCKET_BACKUP_PATH | **(Required for GCP Backend)** Path the backup file should be saved to in GCS. E.g. `/database/myblog/backups`. **Do not put a trailing / or specify the filename.**|
27+
| TARGET_DATABASE_HOST | **(Required)** Hostname or IP address of the MySQL Host. |
28+
| TARGET_DATABASE_PORT | **(Optional)** Port MySQL is listening on (Default: 3306). |
29+
| TARGET_DATABASE_NAMES | **(Required)** Name of the databases to dump. This should be comma seperated (e.g. `database1,database2`). |
30+
| TARGET_DATABASE_USER | **(Required)** Username to authenticate to the database with. |
31+
| TARGET_DATABASE_PASSWORD | **(Required)** Password to authenticate to the database with. Should be configured using a Secret in Kubernetes. |
32+
| BACKUP_TIMESTAMP | **(Optional)** Date string to append to the backup filename ([date](http://man7.org/linux/man-pages/man1/date.1.html) format). Leave unset if using S3 Versioning and date stamp is not required. |
33+
| SLACK_ENABLED | **(Optional)** (true/false) Enable or disable the Slack Integration (Default False). |
34+
| SLACK_USERNAME | **(Optional)** (true/false) Username to use for the Slack Integration (Default: kubernetes-cloud-mysql-backup). |
35+
| SLACK_CHANNEL | **(Required if Slack enabled)** Slack Channel the WebHook is configured for. |
36+
| SLACK_WEBHOOK_URL | **(Required if Slack enabled)** What is the Slack WebHook URL to post to? Should be configured using a Secret in Kubernetes. |
3337

3438

3539
## Slack Integration
3640

37-
kubernetes-s3-mysql-backup supports posting into Slack after each backup job completes. The message posted into the Slack Channel varies as detailed below:
41+
kubernetes-cloud-mysql-backup supports posting into Slack after each backup job completes. The message posted into the Slack Channel varies as detailed below:
3842

3943
* If the backup job is **SUCCESSFUL**: A generic message will be posted into the Slack Channel detailing that all database backups successfully completed.
4044
* If the backup job is **UNSUCCESSFUL**: A message will be posted into the Slack Channel with a detailed error message for each database that failed.
4145

42-
In order to configure kubernetes-s3-mysql-backup to post messages into Slack, you need to create an [Incoming WebHook](https://api.slack.com/incoming-webhooks). Once generated, you can configure kubernetes-s3-mysql-backup using the environment variables detailed above.
46+
In order to configure kubernetes-cloud-mysql-backup to post messages into Slack, you need to create an [Incoming WebHook](https://api.slack.com/incoming-webhooks). Once generated, you can configure kubernetes-cloud-mysql-backup using the environment variables detailed above.
4347

44-
## Configuring the S3 Bucket & AWS IAM User
48+
## S3 Backend Configuration
4549

46-
By default, kubernetes-s3-mysql-backup performs a backup to the same path, with the same filename each time it runs. It therefore assumes that you have Versioning enabled on your S3 Bucket. A typical setup would involve S3 Versioning, with a Lifecycle Policy.
50+
The below subheadings detail how to configure kubernetes-cloud-mysql-backup to backup to an Amazon S3 backend.
51+
52+
### S3 - Configuring the S3 Bucket & AWS IAM User
53+
54+
By default, kubernetes-cloud-mysql-backup performs a backup to the same path, with the same filename each time it runs. It therefore assumes that you have Versioning enabled on your S3 Bucket. A typical setup would involve S3 Versioning, with a Lifecycle Policy.
4755

4856
If a timestamp is required on the backup file name, the BACKUP_TIMESTAMP Environment Variable can be set.
4957

50-
An IAM Users should be created, with API Credentials. An example Policy to attach to the IAM User (for a minimal permissions set) is as follows:
58+
An IAM User should be created, with API Credentials. An example Policy to attach to the IAM User (for a minimal permissions set) is as follows:
5159

5260
```
5361
{
@@ -72,9 +80,9 @@ An IAM Users should be created, with API Credentials. An example Policy to attac
7280
```
7381

7482

75-
## Example Kubernetes Cronjob
83+
### S3 - Example Kubernetes Cronjob
7684

77-
An example of how to schedule this container in Kubernetes as a cronjob is below. This would configure a database backup to run each day at 01:00am. The AWS Secret Access Key, and Target Database Password are stored in secrets.
85+
An example of how to schedule this container in Kubernetes as a cronjob is below. This would configure a database backup to run each day at 01:00am. The AWS Secret Access Key, Target Database Password and Slack Webhook URL are stored in secrets.
7886

7987
```
8088
apiVersion: v1
@@ -99,7 +107,7 @@ spec:
99107
spec:
100108
containers:
101109
- name: my-database-backup
102-
image: gcr.io/maynard-io-public/kubernetes-s3-mysql-backup
110+
image: gcr.io/maynard-io-public/kubernetes-cloud-mysql-backup
103111
imagePullPolicy: Always
104112
env:
105113
- name: AWS_ACCESS_KEY_ID
@@ -141,3 +149,84 @@ spec:
141149
key: slack_webhook_url
142150
restartPolicy: Never
143151
```
152+
153+
## GCS Backend Configuration
154+
155+
The below subheadings detail how to configure kubernetes-cloud-mysql-backup to backup to a Google GCS backend.
156+
157+
### GCS - Configuring the Service Account
158+
159+
By default, kubernetes-cloud-mysql-backup performs a backup to the same path, with the same filename each time it runs. It therefore assumes that you have Object Versioning enabled on your GCS Bucket. A typical setup would involve GCP Object Versioning, with Object Lifecycle Management configured.
160+
161+
If a timestamp is required on the backup file name, the BACKUP_TIMESTAMP Environment Variable can be set.
162+
163+
In order to backup to a GCS Bucket, you must create a Service Account in Google Cloud Platform that contains the neccesary permissions to write to the destination bucket (for example the `Storage Obect Creator` role).
164+
165+
Once created, you must create a key for the Service Account in JSON format. This key should then be base64 encoded and set in the `GCP_GCLOUD_AUTH` environment variable. For example, to encode `service_account.json` you would use the command `base64 ~/service-key.json` in your terminal and set the output as the `GCP_GCLOUD_AUTH` environment variable.
166+
167+
168+
### GCS - Example Kubernetes Cronjob
169+
170+
An example of how to schedule this container in Kubernetes as a cronjob is below. This would configure a database backup to run each day at 01:00am. The GCP Service Account Key, Target Database Password and Slack Webhook URL are stored in secrets.
171+
172+
```
173+
apiVersion: v1
174+
kind: Secret
175+
metadata:
176+
name: my-database-backup
177+
type: Opaque
178+
data:
179+
gcp_gcloud_auth: <Base64 encoded Service Account Key>
180+
database_password: <Your Database Password>
181+
slack_webhook_url: <Your Slack WebHook URL>
182+
---
183+
apiVersion: batch/v1beta1
184+
kind: CronJob
185+
metadata:
186+
name: my-database-backup
187+
spec:
188+
schedule: "0 01 * * *"
189+
jobTemplate:
190+
spec:
191+
template:
192+
spec:
193+
containers:
194+
- name: my-database-backup
195+
image: gcr.io/maynard-io-public/kubernetes-cloud-mysql-backup
196+
imagePullPolicy: Always
197+
env:
198+
- name: GCP_GCLOUD_AUTH
199+
valueFrom:
200+
secretKeyRef:
201+
name: my-database-backup
202+
key: gcp_gcloud_auth
203+
- name: GCP_BUCKET_NAME
204+
value: "<Your GCS Bucket Name>"
205+
- name: GCP_BUCKET_BACKUP_PATH
206+
value: "<Your GCS Bucket Backup Path>"
207+
- name: TARGET_DATABASE_HOST
208+
value: "<Your Target Database Host>"
209+
- name: TARGET_DATABASE_PORT
210+
value: "<Your Target Database Port>"
211+
- name: TARGET_DATABASE_NAMES
212+
value: "<Your Target Database Name(s)>"
213+
- name: TARGET_DATABASE_USER
214+
value: "<Your Target Database Username>"
215+
- name: TARGET_DATABASE_PASSWORD
216+
valueFrom:
217+
secretKeyRef:
218+
name: my-database-backup
219+
key: database_password
220+
- name: BACKUP_TIMESTAMP
221+
value: "_%Y_%m_%d"
222+
- name: SLACK_ENABLED
223+
value: "<true/false>"
224+
- name: SLACK_CHANNEL
225+
value: "#chatops"
226+
- name: SLACK_WEBHOOK_URL
227+
valueFrom:
228+
secretKeyRef:
229+
name: my-database-backup
230+
key: slack_webhook_url
231+
restartPolicy: Never
232+
```

0 commit comments

Comments
 (0)