Skip to content

Commit

Permalink
Merge branch 'release/0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalberger committed Apr 1, 2020
2 parents 58e9f3d + f4309e1 commit 4e2ca44
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* eol=lf
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["docker-entrypoint.sh"]
12 changes: 12 additions & 0 deletions GitReleaseManager.yaml
Original file line number Diff line number Diff line change
@@ -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
84 changes: 82 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,82 @@
# 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) |
| 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

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
37 changes: 37 additions & 0 deletions backup.sh
Original file line number Diff line number Diff line change
@@ -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")"
16 changes: 16 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4e2ca44

Please sign in to comment.