mirror of
https://github.com/ditkrg/db-backup-s3.git
synced 2026-01-22 13:56:47 +00:00
- Updated `backup.sh` to allow backing up multiple databases by introducing the `DATABASE_NAMES` environment variable. - Modified `env.sh` to handle the new `DATABASE_NAMES` variable and ensure compatibility with existing `DATABASE_NAME`. - Updated `docker-compose.yaml` and `README.md` to reflect changes and provide examples for backing up single and multiple databases. - Added a new binary file `umb_pm_dev.bacpac` for database backup. Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
57 lines
1.5 KiB
Bash
57 lines
1.5 KiB
Bash
#! /bin/sh
|
|
|
|
set -eu
|
|
set -o pipefail
|
|
|
|
source ./env.sh
|
|
source ./helpers.sh
|
|
|
|
for CURRENT_DATABASE in $DATABASE_NAMES_LIST; do
|
|
DATABASE_NAME="$CURRENT_DATABASE"
|
|
|
|
echo "Creating backup of $DATABASE_NAME database..."
|
|
backup
|
|
|
|
timestamp=$(date +"%Y-%m-%dT%H:%M:%S")
|
|
|
|
# MSSQL uses .bak extension, other databases use .dump
|
|
if [ "$DATABASE_SERVER" = "mssql" ]; then
|
|
local_file="${MSSQL_DATA_DIR}/db.bak"
|
|
s3_uri_base="s3://${S3_BUCKET}/${S3_PREFIX}/${DATABASE_NAME}_${timestamp}.bak"
|
|
else
|
|
local_file="db.dump"
|
|
s3_uri_base="s3://${S3_BUCKET}/${S3_PREFIX}/${DATABASE_NAME}_${timestamp}.dump"
|
|
fi
|
|
|
|
if [ -n "${PASSPHRASE:-}" ]; then
|
|
echo "Encrypting backup..."
|
|
gpg --symmetric --batch --passphrase "$PASSPHRASE" "$local_file"
|
|
rm "$local_file"
|
|
local_file="${local_file}.gpg"
|
|
s3_uri="${s3_uri_base}.gpg"
|
|
else
|
|
s3_uri="$s3_uri_base"
|
|
fi
|
|
|
|
echo "Uploading backup of $DATABASE_NAME to $S3_BUCKET..."
|
|
aws $aws_args s3 cp "$local_file" "$s3_uri"
|
|
rm "$local_file"
|
|
done
|
|
|
|
echo "Backup complete."
|
|
|
|
if [ -n "${BACKUP_KEEP_DAYS:-}" ]; then
|
|
sec=$((86400*BACKUP_KEEP_DAYS))
|
|
date_from_remove=$(date -d "@$(($(date +%s) - sec))" +%Y-%m-%d)
|
|
backups_query="Contents[?LastModified<='${date_from_remove} 00:00:00'].{Key: Key}"
|
|
|
|
echo "Removing old backups from $S3_BUCKET..."
|
|
aws $aws_args s3api list-objects \
|
|
--bucket "${S3_BUCKET}" \
|
|
--prefix "${S3_PREFIX}" \
|
|
--query "${backups_query}" \
|
|
--output text \
|
|
| xargs -n1 -t -I 'KEY' aws $aws_args s3 rm s3://"${S3_BUCKET}"/'KEY'
|
|
echo "Removal complete."
|
|
fi
|