You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 27, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+10-11Lines changed: 10 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,13 @@
2
2
3
3
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.
4
4
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.
8
6
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/).
9
8
10
9
All changes are captured in the [changelog](CHANGELOG.md), which adheres to [Semantic Versioning](https://semver.org/spec/vadheres2.0.0.html).
11
10
11
+
12
12
## Environment Variables
13
13
14
14
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
19
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
20
| AWS_DEFAULT_REGION |**(Required)** Region of the S3 Bucket (e.g. eu-west-2). |
21
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/`. **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.**|
24
23
| TARGET_DATABASE_HOST |**(Required)** Hostname or IP address of the MySQL Host. |
25
24
| 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`).|
27
26
| TARGET_DATABASE_USER |**(Required)** Username to authenticate to the database with. |
28
27
| TARGET_DATABASE_PASSWORD |**(Required)** Password to authenticate to the database with. Should be configured using a Secret in Kubernetes. |
29
28
| SLACK_ENABLED |**(Optional)** (true/false) Enable or disable the Slack Integration (Default False). |
30
29
| SLACK_USERNAME |**(Optional)** (true/false) Username to use for the Slack Integration (Default: aws-database-backup). |
31
30
| SLACK_CHANNEL |**(Required if Slack enabled)** Slack Channel the WebHook is configured for. |
32
31
| SLACK_WEBHOOK_URL |**(Required if Slack enabled)** What is the Slack WebHook URL to post to? Should be configured using a Secret in Kubernetes. |
33
32
33
+
34
34
## Slack Integration
35
35
36
36
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
68
68
}
69
69
```
70
70
71
+
71
72
## Example Kubernetes Cronjob
72
73
73
74
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.
Copy file name to clipboardExpand all lines: resources/perform-backup.sh
+20-13Lines changed: 20 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -5,25 +5,32 @@
5
5
has_failed=false
6
6
7
7
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_NAME2>&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 ,
# 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
# 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.
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
+
18
27
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
20
29
has_failed=true
21
30
fi
22
31
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
+
27
34
28
35
29
36
# 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