Skip to content

Commit 4e2ca44

Browse files
committed
Merge branch 'release/0.1.0'
2 parents 58e9f3d + f4309e1 commit 4e2ca44

File tree

7 files changed

+169
-2
lines changed

7 files changed

+169
-2
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* eol=lf

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These owners will be the default owners for everything in the repo and
2+
# will be requested for review when someone opens a pull request.
3+
* @Speeedy01 @pascalberger @christianbumann @eoehen @georgesgoetz

Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM mcr.microsoft.com/mssql-tools:latest
2+
3+
ENV DB_SERVER="mssql" \
4+
DB_USER="SA" \
5+
DB_PASSWORD="" \
6+
DB_NAMES="" \
7+
CRON_SCHEDULE="0 1 * * sun"
8+
9+
RUN apt-get update && \
10+
apt-get install -y cron && \
11+
rm -rf /var/cache/apk/*
12+
13+
COPY backup.sh /usr/local/bin/
14+
RUN chmod +x /usr/local/bin/backup.sh
15+
16+
COPY docker-entrypoint.sh /usr/local/bin/
17+
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
18+
ENTRYPOINT ["docker-entrypoint.sh"]

GitReleaseManager.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
issue-labels-include:
2+
- Breaking change
3+
- Feature
4+
- Bug
5+
- Improvement
6+
- Documentation
7+
issue-labels-exclude:
8+
- Build
9+
issue-labels-alias:
10+
- name: Documentation
11+
header: Documentation
12+
plural: Documentation

README.md

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,82 @@
1-
# docker-mssql-backup
2-
Docker images for periodic backups from an Microsoft SQL Server container
1+
# Docker image for backup of Microsoft SQL Server
2+
3+
Docker image to create regular backups of a [Microsoft SQL Server] image.
4+
5+
## Information
6+
7+
| Service | Stats |
8+
|---------|-------------------------------------------------------------------------------------------|
9+
| 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) |
10+
| 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) |
11+
12+
## General
13+
14+
| Topic | Description |
15+
|--------|------------------------------------------------------------------------|
16+
| Image | See [Docker Hub](https://hub.docker.com/r/bbtsoftwareag/mssql-backup). |
17+
| Source | See [GitHub](https://github.com/bbtsoftware/docker-mssql-backup). |
18+
19+
## Usage
20+
21+
This container can create backups on a [Microsoft SQL Server] container.
22+
23+
**NOTE:**
24+
The backup is written to a directory `/backup` inside the [Microsoft SQL Server] container, not to a volume in the backup container.
25+
26+
### Tags
27+
28+
| Tag | Description | Size |
29+
|--------|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------|
30+
| latest | Latest master build | ![Size](https://shields.beevelop.com/docker/image/image-size/bbtsoftwareag/mssql-backup/latest.svg?style=flat-square) |
31+
| 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) |
32+
33+
### Configuration
34+
35+
These environment variables are supported:
36+
37+
| Environment variable | Default value | Description |
38+
|----------------------|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
39+
| DB_SERVER | `mssql` | Name or address of the database server to backup. Normally this should be the name of the [Microsoft SQL Server] service. |
40+
| DB_USER | `SA` | User used to connect to the database server. |
41+
| DB_PASSWORD | | Password used to connect to the database server. |
42+
| DB_NAMES | | Names of the databases for which a backup should be created. |
43+
| TZ | | Timezone to use. |
44+
| 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. |
45+
46+
## Examples
47+
48+
### Docker Compose
49+
50+
The following example will create backups of the databases `MyFirstDatabaseToRestore` and `MySecondDatabaseToRestore`
51+
running inside the `db` container every day at 01.00 CEST and stores it in the `/storage/backup` directory on the host machine.
52+
53+
```yaml
54+
version: '3.7'
55+
56+
services:
57+
db:
58+
image: mcr.microsoft.com/mssql/server
59+
volumes:
60+
- /storage/backup:/backup
61+
environment:
62+
- ACCEPT_EULA=Y
63+
- MSSQL_PID=Express
64+
- SA_PASSWORD=MySecretPassword
65+
networks:
66+
- default
67+
backup:
68+
image: bbtsoftwareag/mssql-backup
69+
environment:
70+
- TZ=Europe/Zurich
71+
- DB_SERVER=db
72+
- DB_USER=SA
73+
- DB_PASSWORD=MySecretPassword
74+
- "DB_NAMES=
75+
MyFirstDatabaseToRestore
76+
MySecondDatabaseToRestore"
77+
- CRON_SCHEDULE="0 1 * * *"
78+
networks:
79+
- default
80+
```
81+
82+
[Microsoft SQL Server]: https://hub.docker.com/_/microsoft-mssql-server

backup.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
# Validate environment variables
4+
[ -z "$DB_SERVER" ] && { echo "Required environment variable DB_SERVER not set" && exit 1; }
5+
[ -z "$DB_USER" ] && { echo "Required environment variable DB_USER not set" && exit 1; }
6+
[ -z "$DB_PASSWORD" ] && { echo "Required environment variable DB_PASSWORD not set" && exit 1; }
7+
[ -z "$DB_NAMES" ] && { echo "Required environment variable DB_NAMES not set" && exit 1; }
8+
9+
echo "Backup started at $(date "+%Y-%m-%d %H:%M:%S")"
10+
11+
CURRENT_DATE=$(date +%Y%m%d%H%M)
12+
for CURRENT_DB in $DB_NAMES
13+
do
14+
BAK_FILENAME=/backup/$CURRENT_DATE.$CURRENT_DB.bak
15+
16+
echo "Backup database $CURRENT_DB to $BAK_FILENAME on $DB_SERVER..."
17+
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"
18+
then
19+
echo "Backup of database successfully created"
20+
else
21+
echo "Error creating database backup"
22+
rm -rf "$BAK_FILENAME"
23+
fi
24+
25+
TRN_FILENAME=/backup/$CURRENT_DATE.$CURRENT_DB.trn
26+
27+
echo "Backup log of $CURRENT_DB to $TRN_FILENAME on $DB_SERVER..."
28+
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"
29+
then
30+
echo "Backup of log successfully created"
31+
else
32+
echo "Error creating log backup"
33+
rm -rf "$TRN_FILENAME"
34+
fi
35+
done
36+
37+
echo "Backup process finished at $(date "+%Y-%m-%d %H:%M:%S")"

docker-entrypoint.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
# Store environment variables to pass to cron job
4+
printenv | sed 's/^\([a-zA-Z0-9_]*\)=\(.*\)$/export \1="\2"/g' > /container_env.sh
5+
6+
# Create crontab definition
7+
echo "${CRON_SCHEDULE} . /container_env.sh; /usr/local/bin/backup.sh >> /var/log/cron.log 2>&1" > /etc/cron.d/crontab.conf
8+
9+
# Apply cron job
10+
crontab /etc/cron.d/crontab.conf
11+
12+
# Create the log file to be able to run tail
13+
touch /var/log/cron.log
14+
15+
echo "Starting cron task manager..."
16+
cron && tail -f /var/log/cron.log

0 commit comments

Comments
 (0)