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

Commit b6a30b4

Browse files
Merge pull request #3 from benjamin-maynard/dev
Version 2.0.0 - Support multiple database backups
2 parents c76ecc2 + 434c03a commit b6a30b4

3 files changed

Lines changed: 37 additions & 24 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ 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.0.0] - 16-12-2018
8+
### Fix issue with Slack Alerts
9+
- Implemented the ability to backup multiple databases from a single host
10+
- Updated the Variable Name of TARGET_DATABASE_NAME to TARGET_DATABASE_NAMES
11+
- Updated the format of AWS_BUCKET_BACKUP_PATH so that the trailing / is not required
12+
- Removed $AWS_BUCCKET_BACKUP_NAME variable (which had a typo). Database backups are now saved using their database names
13+
714
## [v1.1.1] - 16-12-2018
815
### Fix issue with Slack Alerts
916
- Fixed issue with failed Slack alerts when log messages contained special characters

README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
aws-database-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.
44

5-
Currently, aws-database-backup supports the backing up of a single MySQL Database. When triggered, a full database dump is performed using the `mysqldump` command. The backup is then uploaded to an Amazon S3 Bucket. aws-database-backup features Slack Integration, and can post messages into a channel detailing if the backup was successful or not.
6-
7-
Over time, aws-database-backup will be updated to support more features and functionality. The most immediate feature on the roadmap is the ability to perform backups of multiple databases. 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/).
5+
Currently, aws-database-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. aws-database-backup features Slack Integration, and can post messages into a channel detailing if the backup(s) were successful or not.
86

7+
Over time, aws-database-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/).
98

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

11+
1212
## Environment Variables
1313

1414
The below table lists all of the Environment Variables that are configurable for aws-database-backup.
@@ -19,18 +19,18 @@ The below table lists all of the Environment Variables that are configurable for
1919
| 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. |
2020
| AWS_DEFAULT_REGION | **(Required)** Region of the S3 Bucket (e.g. eu-west-2). |
2121
| 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/`. **Requires the trailing / and should not include the file name.** |
23-
| AWS_BUCCKET_BACKUP_NAME | **(Required)** File name of the backup file. E.g. `database_dump.sql`. |
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.** |
2423
| TARGET_DATABASE_HOST | **(Required)** Hostname or IP address of the MySQL Host. |
2524
| TARGET_DATABASE_PORT | **(Optional)** Port MySQL is listening on (Default: 3306). |
26-
| TARGET_DATABASE_NAME | **(Required)** Name of the database to dump. |
25+
| TARGET_DATABASE_NAMES | **(Required)** Name of the databases to dump. This should be comma seperated (e.g. `database1,database2`). |
2726
| TARGET_DATABASE_USER | **(Required)** Username to authenticate to the database with. |
2827
| TARGET_DATABASE_PASSWORD | **(Required)** Password to authenticate to the database with. Should be configured using a Secret in Kubernetes. |
2928
| SLACK_ENABLED | **(Optional)** (true/false) Enable or disable the Slack Integration (Default False). |
3029
| SLACK_USERNAME | **(Optional)** (true/false) Username to use for the Slack Integration (Default: aws-database-backup). |
3130
| SLACK_CHANNEL | **(Required if Slack enabled)** Slack Channel the WebHook is configured for. |
3231
| SLACK_WEBHOOK_URL | **(Required if Slack enabled)** What is the Slack WebHook URL to post to? Should be configured using a Secret in Kubernetes. |
3332

33+
3434
## Slack Integration
3535

3636
aws-database-backup supports posting into Slack after each backup job completes. The message posted into the Slack Channel varies as detailed below:
@@ -68,6 +68,7 @@ An IAM Users should be created, with API Credentials. An example Policy to attac
6868
}
6969
```
7070

71+
7172
## Example Kubernetes Cronjob
7273

7374
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.
@@ -125,14 +126,12 @@ spec:
125126
value: "<Your S3 Bucket Name>"
126127
- name: AWS_BUCKET_BACKUP_PATH
127128
value: "<Your S3 Bucket Backup Path>"
128-
- name: AWS_BUCCKET_BACKUP_NAME
129-
value: "<Your Backup File Name.sql>"
130129
- name: TARGET_DATABASE_HOST
131130
value: "<Your Target Database Host>"
132131
- name: TARGET_DATABASE_PORT
133132
value: "<Your Target Database Port>"
134-
- name: TARGET_DATABASE_NAME
135-
value: "<Your Target Database Name>"
133+
- name: TARGET_DATABASE_NAMES
134+
value: "<Your Target Database Name(s)>"
136135
- name: TARGET_DATABASE_USER
137136
value: "<Your Target Database Username>"
138137
- name: TARGET_DATABASE_PASSWORD
@@ -150,4 +149,4 @@ spec:
150149
name: SLACK_WEBHOOK_URL
151150
key: slack_webhook_url
152151
restartPolicy: Never
153-
```
152+
```

resources/perform-backup.sh

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,32 @@
55
has_failed=false
66

77

8-
# Perform the database backup. Put the output to a variable. If successful upload the backup to S3, if unsuccessful print an entry to the console and the log, and set has_failed to true.
9-
if sqloutput=$(mysqldump -u $TARGET_DATABASE_USER -h $TARGET_DATABASE_HOST -p$TARGET_DATABASE_PASSWORD -P $TARGET_DATABASE_PORT $TARGET_DATABASE_NAME 2>&1 > /tmp/$AWS_BUCCKET_BACKUP_NAME)
10-
then
11-
12-
echo -e "Database backup successfully completed for $TARGET_DATABASE_NAME at $(date +'%d-%m-%Y %H:%M:%S')."
8+
# Loop through all the defined databases, seperating by a ,
9+
for CURRENT_DATABASE in ${TARGET_DATABASE_NAMES//,/ }
10+
do
1311

14-
# Perform the upload to S3. Put the output to a variable. If successful, print an entry to the console and the log. If unsuccessful, set has_failed to true and print an entry to the console and the log
15-
if awsoutput=$(aws s3 cp /tmp/$AWS_BUCCKET_BACKUP_NAME s3://$AWS_BUCKET_NAME$AWS_BUCKET_BACKUP_PATH$AWS_BUCCKET_BACKUP_NAME 2>&1)
12+
# Perform the database backup. Put the output to a variable. If successful upload the backup to S3, if unsuccessful print an entry to the console and the log, and set has_failed to true.
13+
if sqloutput=$(mysqldump -u $TARGET_DATABASE_USER -h $TARGET_DATABASE_HOST -p$TARGET_DATABASE_PASSWORD -P $TARGET_DATABASE_PORT $CURRENT_DATABASE 2>&1 > /tmp/$CURRENT_DATABASE.sql)
1614
then
17-
echo -e "Database backup successfully uploaded for $TARGET_DATABASE_NAME at $(date +'%d-%m-%Y %H:%M:%S')."
15+
16+
echo -e "Database backup successfully completed for $CURRENT_DATABASE at $(date +'%d-%m-%Y %H:%M:%S')."
17+
18+
# Perform the upload to S3. Put the output to a variable. If successful, print an entry to the console and the log. If unsuccessful, set has_failed to true and print an entry to the console and the log
19+
if awsoutput=$(aws s3 cp /tmp/$CURRENT_DATABASE.sql s3://$AWS_BUCKET_NAME$AWS_BUCKET_BACKUP_PATH/$CURRENT_DATABASE.sql 2>&1)
20+
then
21+
echo -e "Database backup successfully uploaded for $CURRENT_DATABASE at $(date +'%d-%m-%Y %H:%M:%S')."
22+
else
23+
echo -e "Database backup failed to upload for $CURRENT_DATABASE at $(date +'%d-%m-%Y %H:%M:%S'). Error: $awsoutput" | tee -a /tmp/aws-database-backup.log
24+
has_failed=true
25+
fi
26+
1827
else
19-
echo -e "Database backup failed to upload for $TARGET_DATABASE_NAME at $(date +'%d-%m-%Y %H:%M:%S'). Error: $awsoutput" | tee -a /tmp/aws-database-backup.log
28+
echo -e "Database backup FAILED for $CURRENT_DATABASE at $(date +'%d-%m-%Y %H:%M:%S'). Error: $sqloutput" | tee -a /tmp/aws-database-backup.log
2029
has_failed=true
2130
fi
2231

23-
else
24-
echo -e "Database backup FAILED for $TARGET_DATABASE_NAME at $(date +'%d-%m-%Y %H:%M:%S'). Error: $sqloutput" | tee -a /tmp/aws-database-backup.log
25-
has_failed=true
26-
fi
32+
done
33+
2734

2835

2936
# Check if any of the backups have failed. If so, exit with a status of 1. Otherwise exit cleanly with a status of 0.

0 commit comments

Comments
 (0)