Skip to content

Commit

Permalink
Merge branch 'release/0.2.0' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalberger committed Sep 15, 2020
2 parents 4e2ca44 + 006c0db commit 12b1b00
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,3 +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
* @bbtsoftware/dev-services
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2
updates:
- package-ecosystem: docker
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
FROM mcr.microsoft.com/mssql-tools:latest
LABEL MAINTAINER="BBT Software AG <[email protected]>"

ENV DB_SERVER="mssql" \
DB_USER="SA" \
DB_PASSWORD="" \
DB_NAMES="" \
CRON_SCHEDULE="0 1 * * sun"
CRON_SCHEDULE="0 1 * * sun" \
BACKUP_CLEANUP=false \
BACKUP_AGE=7

RUN apt-get update && \
apt-get install -y cron && \
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ 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.
For using the cleanup feature attach the same `/backup` volume in the `bbtsoftwareag/mssql-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) |
| 0.2.0 | Release [0.2.0](https://github.com/bbtsoftware/docker-mssql-backup/releases/tag/0.2.0) | ![Size](https://shields.beevelop.com/docker/image/image-size/bbtsoftwareag/mssql-backup/0.2.0.svg?style=flat-square) |

### Configuration

Expand All @@ -42,6 +44,8 @@ These environment variables are supported:
| 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. |
| BACKUP_CLEANUP | `false` | Set to "true" if you want to let the cronjob remove files older than $BACKUP_AGE days |
| BACKUP_AGE | `7` | Number of days to keep backups in backup directory |

## Examples

Expand All @@ -61,20 +65,23 @@ services:
environment:
- ACCEPT_EULA=Y
- MSSQL_PID=Express
- SA_PASSWORD=MySecretPassword
- SA_PASSWORD=MySecre(12)tPassword
networks:
- default
backup:
image: bbtsoftwareag/mssql-backup
# for using the cleanup feature, use the backup volume from db.
# volumes:
# - /storage/backup:/backup
environment:
- TZ=Europe/Zurich
- DB_SERVER=db
- DB_USER=SA
- DB_PASSWORD=MySecretPassword
- DB_PASSWORD=MySecre(12)tPassword
- "DB_NAMES=
MyFirstDatabaseToRestore
MySecondDatabaseToRestore"
- CRON_SCHEDULE="0 1 * * *"
- CRON_SCHEDULE=0 1 * * *
networks:
- default
```
Expand Down
19 changes: 18 additions & 1 deletion backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ 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

# backup database files
BAK_FILENAME=/backup/$CURRENT_DATE.$CURRENT_DB.bak

echo "Backup database $CURRENT_DB to $BAK_FILENAME on $DB_SERVER..."
Expand All @@ -22,6 +24,7 @@ do
rm -rf "$BAK_FILENAME"
fi

# backup log files
TRN_FILENAME=/backup/$CURRENT_DATE.$CURRENT_DB.trn

echo "Backup log of $CURRENT_DB to $TRN_FILENAME on $DB_SERVER..."
Expand All @@ -32,6 +35,20 @@ do
echo "Error creating log backup"
rm -rf "$TRN_FILENAME"
fi

# cleanup old backup files
if [ "$BACKUP_CLEANUP" = true ]; then
echo ""
echo "Backup cleanup is activated"
find /backup -type f -name "*.$CURRENT_DB.bak" -mtime +"$BACKUP_AGE" -exec echo {} " is deleted" \;
find /backup -type f -name "*.$CURRENT_DB.bak" -mtime +"$BACKUP_AGE" -exec rm {} \;

find /backup -type f -name "*.$CURRENT_DB.trn" -mtime +"$BACKUP_AGE" -exec echo {} " is deleted" \;
find /backup -type f -name "*.$CURRENT_DB.trn" -mtime +"$BACKUP_AGE" -exec rm {} \;
else
echo "Backup files cleanup is disabled"
fi

done

echo "Backup process finished at $(date "+%Y-%m-%d %H:%M:%S")"
echo "Backup process finished at $(date "+%Y-%m-%d %H:%M:%S")"
7 changes: 6 additions & 1 deletion docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
# Store environment variables to pass to cron job
printenv | sed 's/^\([a-zA-Z0-9_]*\)=\(.*\)$/export \1="\2"/g' > /container_env.sh

# Remove quotes from CRON_SCHEDULE
cronSchedule=${CRON_SCHEDULE}
cronSchedule=${cronSchedule%\"}
cronSchedule=${cronSchedule#\"}

# 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
echo "$cronSchedule . /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
Expand Down

0 comments on commit 12b1b00

Please sign in to comment.