Last active
September 21, 2020 16:34
-
-
Save jbasinger/58b7c4af6adf6d64c620abbf76449149 to your computer and use it in GitHub Desktop.
An easy docker-compose that sets up an MSSQL server container with shared backup and scripts folders and a couple scripts to restore backups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.8" | |
services: | |
tools: | |
hostname: tools | |
container_name: tools | |
build: | |
context: ./tools | |
volumes: | |
- "/Volumes/dev:/code" | |
stdin_open: true | |
tty: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.8" | |
services: | |
ms-db: | |
image: mcr.microsoft.com/mssql/server:2017-latest | |
container_name: ms_db | |
ports: | |
- "1433:1433" | |
environment: | |
- "ACCEPT_EULA=Y" | |
- "SA_PASSWORD=mysecretpassword!" | |
volumes: | |
- ms_db:/var/opt/mssql | |
- ./backup:/backup | |
- ./scripts:/scripts | |
networks: | |
- database | |
volumes: | |
ms_db: | |
networks: | |
database: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM ubuntu:latest | |
ENV TERM xterm-256color | |
WORKDIR /root | |
RUN apt-get update \ | |
&& apt-get install -y \ | |
ripgrep \ | |
jq \ | |
zsh \ | |
curl \ | |
git | |
RUN sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" | |
RUN git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k | |
COPY . /root | |
WORKDIR /code | |
ENTRYPOINT ["/bin/zsh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
CONTAINER="ms_db" | |
PASS="mysecretpassword!" | |
BAKS=$(ls -r backup/*.bak) | |
for BAK in $BAKS | |
do | |
echo "Restoring backup: $BAK" | |
# echo "RESTORE FILELISTONLY FROM DISK = '/$BAK'" | |
VOLS=$(docker exec -it $CONTAINER /opt/mssql-tools/bin/sqlcmd \ | |
-S localhost -U SA -P $PASS \ | |
-Q "RESTORE FILELISTONLY FROM DISK = '/$BAK'" \ | |
| tr -s ' ' | cut -d ' ' -f 1 | sed 1,2d | tail -r | sed 1,2d | tail -r) | |
i=0 | |
SQL="RESTORE DATABASE " | |
for VOL in $VOLS | |
do | |
if [[ $i -eq 0 ]] | |
then | |
SQL+="$VOL FROM DISK = '/$BAK' WITH MOVE '$VOL' TO '/var/opt/mssql/data/$VOL.mdf'" | |
else | |
SQL+=", MOVE '$VOL' TO '/var/opt/mssql/data/$VOL.ldf'" | |
fi | |
i+=1 | |
done | |
# echo $SQL | |
docker exec -it $CONTAINER /opt/mssql-tools/bin/sqlcmd \ | |
-S localhost -U SA -P $PASS \ | |
-Q "$SQL" | |
done | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ -z "$1" ] | |
then | |
echo "Please specify a script" | |
exit 1 | |
fi | |
CONTAINER="ms_db" | |
PASS="mysecretpassword!" | |
SCRIPT=$1 | |
docker exec -it $CONTAINER /opt/mssql-tools/bin/sqlcmd \ | |
-S localhost -U SA -P $PASS \ | |
-i "/$SCRIPT" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment