From 535d8305710e5bd45ecb4661691e87c27a753ff0 Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Thu, 26 Mar 2020 10:18:08 +0100 Subject: [PATCH 1/6] Add code owners --- .github/CODEOWNERS | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..220c752 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,3 @@ +# These owners will be the default owners for everything in the repo and +# will be requested for review when someone opens a pull request. +* @Speeedy01 @pascalberger @christianbumann @eoehen @georgesgoetz \ No newline at end of file From 2c7141d1708567a45fb5db8e6669a78a92945448 Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Thu, 26 Mar 2020 10:18:59 +0100 Subject: [PATCH 2/6] Define repo settings --- .gitattributes | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..44b4224 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* eol=lf \ No newline at end of file From 8f673ea9b4d966e80e4d7dce9b61758cade49f8f Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Thu, 26 Mar 2020 10:17:01 +0100 Subject: [PATCH 3/6] Add configuration for GitReleaseManager --- GitReleaseManager.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 GitReleaseManager.yaml diff --git a/GitReleaseManager.yaml b/GitReleaseManager.yaml new file mode 100644 index 0000000..721c1ba --- /dev/null +++ b/GitReleaseManager.yaml @@ -0,0 +1,12 @@ +issue-labels-include: +- Breaking change +- Feature +- Bug +- Improvement +- Documentation +issue-labels-exclude: +- Build +issue-labels-alias: + - name: Documentation + header: Documentation + plural: Documentation \ No newline at end of file From 9da37b486a918702209dfab8e5068ba9135960ab Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Tue, 24 Mar 2020 22:49:21 +0100 Subject: [PATCH 4/6] Implement basic backup functionality --- Dockerfile | 17 +++++++++ README.md | 83 ++++++++++++++++++++++++++++++++++++++++++-- backup.sh | 37 ++++++++++++++++++++ docker-entrypoint.sh | 16 +++++++++ 4 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 backup.sh create mode 100644 docker-entrypoint.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fd6452c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM mcr.microsoft.com/mssql-tools:latest + +ENV DB_SERVER="mssql" \ + DB_USER="SA" \ + DB_PASSWORD="" \ + DB_NAMES="" \ + CRON_SCHEDULE="0 1 * * sun" + +RUN apt-get update && \ + apt-get install -y cron && \ + rm -rf /var/cache/apk/* + +COPY backup.sh /usr/local/bin/ +RUN chmod +x /usr/local/bin/backup.sh + +COPY docker-entrypoint.sh /usr/local/bin/ +ENTRYPOINT ["docker-entrypoint.sh"] \ No newline at end of file diff --git a/README.md b/README.md index 94bb8af..94555e8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,81 @@ -# docker-mssql-backup -Docker images for periodic backups from an Microsoft SQL Server container +# Docker image for backup of Microsoft SQL Server + +Docker image to create regular backups of a [Microsoft SQL Server] image. + +## Information + +| Service | Stats | +|---------|-------------------------------------------------------------------------------------------| +| Docker | [![Build](https://img.shields.io/docker/cloud/build/bbtsoftwareag/mssql-backup.svg?style=flat-square)](https://hub.docker.com/r/bbtsoftwareag/mssql-backup/builds) [![Pulls](https://img.shields.io/docker/pulls/bbtsoftwareag/mssql-backup.svg?style=flat-square)](https://hub.docker.com/r/bbtsoftwareag/mssql-backup) [![Stars](https://img.shields.io/docker/stars/bbtsoftwareag/mssql-backup.svg?style=flat-square)](https://hub.docker.com/r/bbtsoftwareag/mssql-backup) [![Automated](https://img.shields.io/docker/cloud/automated/bbtsoftwareag/mssql-backup.svg?style=flat-square)](https://hub.docker.com/r/bbtsoftwareag/mssql-backup/builds) | +| GitHub | [![Last commit](https://img.shields.io/github/last-commit/bbtsoftware/docker-mssql-backup.svg?style=flat-square)](https://github.com/bbtsoftware/docker-mssql-backup/commits/master) [![Issues](https://img.shields.io/github/issues-raw/bbtsoftware/docker-mssql-backup.svg?style=flat-square)](https://github.com/bbtsoftware/docker-mssql-backup/issues) [![PR](https://img.shields.io/github/issues-pr-raw/bbtsoftware/docker-mssql-backup.svg?style=flat-square)](https://github.com/bbtsoftware/docker-mssql-backup/pulls) [![Size](https://img.shields.io/github/repo-size/bbtsoftware/docker-mssql-backup.svg?style=flat-square)](https://github.com/bbtsoftware/docker-mssql-backup/) [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/bbtsoftware/docker-mssql-backup/blob/master/LICENSE) | + +## General + +| Topic | Description | +|--------|------------------------------------------------------------------------| +| Image | See [Docker Hub](https://hub.docker.com/r/bbtsoftwareag/mssql-backup). | +| Source | See [GitHub](https://github.com/bbtsoftware/docker-mssql-backup). | + +## Usage + +This container can create backups on a [Microsoft SQL Server] container. + +**NOTE:** +The backup is written to a directory `/backup` inside the [Microsoft SQL Server] container, not to a volume in the backup container. + +### Tags + +| Tag | Description | Size | +|--------|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------| +| latest | Latest master build | ![Size](https://shields.beevelop.com/docker/image/image-size/bbtsoftwareag/mssql-backup/latest.svg?style=flat-square) | + +### Configuration + +These environment variables are supported: + +| Environment variable | Default value | Description | +|----------------------|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| DB_SERVER | `mssql` | Name or address of the database server to backup. Normally this should be the name of the [Microsoft SQL Server] service. | +| DB_USER | `SA` | User used to connect to the database server. | +| DB_PASSWORD | | Password used to connect to the database server. | +| DB_NAMES | | Names of the databases for which a backup should be created. | +| TZ | | Timezone to use. | +| CRON_SCHEDULE | `0 1 * * sun` | Cron schedule for running backups. NOTE: There is no check if there's already a backup running when starting the backup job. Therefore time interval needs to be longer than the maximum expected backup time for all databases. | + +## Examples + +### Docker Compose + +The following example will create backups of the databases `MyFirstDatabaseToRestore` and `MySecondDatabaseToRestore` +running inside the `db` container every day at 01.00 CEST and stores it in the `/storage/backup` directory on the host machine. + +```yaml +version: '3.7' + +services: + db: + image: mcr.microsoft.com/mssql/server + volumes: + - /storage/backup:/backup + environment: + - ACCEPT_EULA=Y + - MSSQL_PID=Express + - SA_PASSWORD=MySecretPassword + networks: + - default + backup: + image: bbtsoftwareag/mssql-backup + environment: + - TZ=Europe/Zurich + - DB_SERVER=db + - DB_USER=SA + - DB_PASSWORD=MySecretPassword + - "DB_NAMES= + MyFirstDatabaseToRestore + MySecondDatabaseToRestore" + - CRON_SCHEDULE="0 1 * * *" + networks: + - default +``` + +[Microsoft SQL Server]: https://hub.docker.com/_/microsoft-mssql-server diff --git a/backup.sh b/backup.sh new file mode 100644 index 0000000..2f15ef7 --- /dev/null +++ b/backup.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +# Validate environment variables +[ -z "$DB_SERVER" ] && { echo "Required environment variable DB_SERVER not set" && exit 1; } +[ -z "$DB_USER" ] && { echo "Required environment variable DB_USER not set" && exit 1; } +[ -z "$DB_PASSWORD" ] && { echo "Required environment variable DB_PASSWORD not set" && exit 1; } +[ -z "$DB_NAMES" ] && { echo "Required environment variable DB_NAMES not set" && exit 1; } + +echo "Backup started at $(date "+%Y-%m-%d %H:%M:%S")" + +CURRENT_DATE=$(date +%Y%m%d%H%M) +for CURRENT_DB in $DB_NAMES +do + BAK_FILENAME=/backup/$CURRENT_DATE.$CURRENT_DB.bak + + echo "Backup database $CURRENT_DB to $BAK_FILENAME on $DB_SERVER..." + if /opt/mssql-tools/bin/sqlcmd -S "$DB_SERVER" -U "$DB_USER" -P "$DB_PASSWORD" -Q "BACKUP DATABASE [$CURRENT_DB] TO DISK = N'$BAK_FILENAME' WITH NOFORMAT, NOINIT, NAME = '$CURRENT_DB-full', SKIP, NOUNLOAD, STATS = 10" + then + echo "Backup of database successfully created" + else + echo "Error creating database backup" + rm -rf "$BAK_FILENAME" + fi + + TRN_FILENAME=/backup/$CURRENT_DATE.$CURRENT_DB.trn + + echo "Backup log of $CURRENT_DB to $TRN_FILENAME on $DB_SERVER..." + if /opt/mssql-tools/bin/sqlcmd -S "$DB_SERVER" -U "$DB_USER" -P "$DB_PASSWORD" -Q "BACKUP LOG [$CURRENT_DB] TO DISK = N'$TRN_FILENAME' WITH NOFORMAT, NOINIT, NAME = '$CURRENT_DB-log', SKIP, NOUNLOAD, STATS = 10" + then + echo "Backup of log successfully created" + else + echo "Error creating log backup" + rm -rf "$TRN_FILENAME" + fi +done + +echo "Backup process finished at $(date "+%Y-%m-%d %H:%M:%S")" \ No newline at end of file diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100644 index 0000000..690df49 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# Store environment variables to pass to cron job +printenv | sed 's/^\([a-zA-Z0-9_]*\)=\(.*\)$/export \1="\2"/g' > /container_env.sh + +# Create crontab definition +echo "${CRON_SCHEDULE} . /container_env.sh; /usr/local/bin/backup.sh >> /var/log/cron.log 2>&1" > /etc/cron.d/crontab.conf + +# Apply cron job +crontab /etc/cron.d/crontab.conf + +# Create the log file to be able to run tail +touch /var/log/cron.log + +echo "Starting cron task manager..." +cron && tail -f /var/log/cron.log \ No newline at end of file From 20a57cf809cc668cd3a57d314593c4c143131d6e Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Wed, 1 Apr 2020 16:37:32 +0200 Subject: [PATCH 5/6] Add tag for release 0.1.0 --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 94555e8..79605cd 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ The backup is written to a directory `/backup` inside the [Microsoft SQL Server] | Tag | Description | Size | |--------|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------| | latest | Latest master build | ![Size](https://shields.beevelop.com/docker/image/image-size/bbtsoftwareag/mssql-backup/latest.svg?style=flat-square) | +| 0.1.0 | Release [0.1.0](https://github.com/bbtsoftware/docker-mssql-backup/releases/tag/0.1.0) | ![Size](https://shields.beevelop.com/docker/image/image-size/bbtsoftwareag/mssql-backup/0.1.0.svg?style=flat-square) | ### Configuration From f4309e1a06784a59c9cab1c48f0e2ca62f275f56 Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Wed, 1 Apr 2020 17:42:38 +0200 Subject: [PATCH 6/6] Fix execution of entrypoint script --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index fd6452c..d83a5cd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,4 +14,5 @@ COPY backup.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/backup.sh COPY docker-entrypoint.sh /usr/local/bin/ +RUN chmod +x /usr/local/bin/docker-entrypoint.sh ENTRYPOINT ["docker-entrypoint.sh"] \ No newline at end of file