Skip to content

Commit 459b9aa

Browse files
authored
Merge pull request #1 from silinternational/develop
Inital commit
2 parents 5eea1d2 + 8557b33 commit 459b9aa

File tree

12 files changed

+211
-0
lines changed

12 files changed

+211
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Vagrant folder
2+
.vagrant/
3+
4+
# phpstorm project files
5+
.idea/
6+
/nbproject/
7+
# local config files
8+
local.env
9+
10+
vendor/
11+
*.aes

Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM alpine:3.6
2+
3+
RUN apk update \
4+
&& apk add --no-cache rsyslog rsyslog-tls \
5+
ca-certificates openssl \
6+
bash \
7+
postgresql \
8+
postgresql-client \
9+
python py-pip \
10+
&& update-ca-certificates \
11+
&& pip install s3cmd python-magic
12+
13+
COPY dockerbuild/rsyslog.conf /etc/rsyslog.conf
14+
15+
RUN wget https://raw.githubusercontent.com/silinternational/runny/0.2/runny -O /usr/local/bin/runny \
16+
&& chmod +x /usr/local/bin/runny
17+
18+
COPY application/ /data/
19+
WORKDIR /data
20+
21+
ENTRYPOINT ["./entrypoint.sh"]
22+
CMD ["crond -f"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 SIL International
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
start: restore backup
2+
3+
restore: db
4+
docker-compose up -d restore
5+
6+
backup: db
7+
docker-compose up -d backup
8+
9+
db:
10+
docker-compose up -d db adminer
11+
12+
clean:
13+
docker-compose kill
14+
docker system prune -f

application/backup.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env sh
2+
3+
for dbName in ${DB_NAMES}; do
4+
logger -p user.info "backing up ${dbName}..."
5+
6+
start=$(date +%s)
7+
runny $(PGPASSWORD=${DB_PASSWORD} pg_dump --host=${DB_HOST} --username=${DB_USER} --create --clean ${DB_OPTIONS} --dbname=${dbName} > /tmp/${dbName}.sql)
8+
end=$(date +%s)
9+
10+
logger -p user.info "${dbName} backed up ($(stat -c %s /tmp/${dbName}.sql) bytes) in $(expr ${end} - ${start}) seconds."
11+
12+
runny gzip -f /tmp/${dbName}.sql
13+
runny s3cmd put /tmp/${dbName}.sql.gz ${S3_BUCKET}
14+
# runny aws s3 cp /tmp/${dbName}.sql.gz ${S3_BUCKET}
15+
16+
logger -p user.info "${dbName} backup stored in ${S3_BUCKET}."
17+
done

application/entrypoint.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env sh
2+
3+
# hostname:port:database:username:password
4+
echo ${DB_HOST}:*:*:${DB_USER}:${DB_PASSWORD} > /root/.pgpass
5+
chmod 600 /root/.pgpass
6+
7+
if [ "${LOGENTRIES_KEY}" ]; then
8+
sed -i /etc/rsyslog.conf -e "s/LOGENTRIESKEY/${LOGENTRIES_KEY}/"
9+
rsyslogd
10+
sleep 10 # ensure rsyslogd is running before we may need to send logs to it
11+
else
12+
logger -p user.error "Missing LOGENTRIES_KEY environment variable"
13+
fi
14+
15+
# default to every day at 2 am when no schedule is provided
16+
echo "${CRON_SCHEDULE:=0 2 * * *} runny /data/${MODE}.sh" >> /etc/crontabs/root
17+
18+
runny $1

application/restore.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env sh
2+
3+
for dbName in ${DB_NAMES}; do
4+
logger -p user.info "restoring ${dbName}..."
5+
6+
runny s3cmd get -f ${S3_BUCKET}/${dbName}.sql.gz /tmp/${dbName}.sql.gz
7+
runny gunzip -f /tmp/${dbName}.sql.gz
8+
9+
start=$(date +%s)
10+
runny psql --host=${DB_HOST} --username=${DB_USER} ${DB_OPTIONS} < /tmp/${dbName}.sql
11+
end=$(date +%s)
12+
13+
logger -p user.info "${dbName} restored in $(expr ${end} - ${start}) seconds."
14+
done

docker-compose.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
version: '2'
2+
services:
3+
data:
4+
image: silintl/data-volume:latest
5+
volumes:
6+
- ./application:/data
7+
8+
# See https://hub.docker.com/_/postgres/ for details of the postgres image.
9+
# POSTGRES_PASSWORD - superuser password for PostgreSQL
10+
# POSTGRES_USER - superuser (default is 'postgres')
11+
# POSTGRES_DB - name of default database (default is value of POSTGRES_USER)
12+
db:
13+
image: postgres:9.6-alpine
14+
volumes_from:
15+
- data
16+
ports:
17+
- "5432"
18+
environment:
19+
POSTGRES_PASSWORD: r00tp@ss!
20+
21+
adminer:
22+
image: adminer:4.6.3
23+
ports:
24+
- "8080:8080"
25+
26+
# DB_HOST - hostname of the database server
27+
# DB_USER - user that accesses the database
28+
# DB_PASSWORD - password for the DB_USER
29+
# DB_NAMES - list of databases to back up/restore
30+
restore:
31+
build: ./
32+
volumes_from:
33+
- data
34+
env_file:
35+
- ./local.env
36+
environment:
37+
DB_HOST: db
38+
DB_USER: postgres
39+
DB_PASSWORD: r00tp@ss!
40+
DB_NAMES: world
41+
MODE: restore
42+
CRON_SCHEDULE: "25 * * * *"
43+
44+
backup:
45+
build: ./
46+
volumes_from:
47+
- data
48+
env_file:
49+
- ./local.env
50+
environment:
51+
DB_HOST: db
52+
DB_USER: postgres
53+
DB_PASSWORD: r00tp@ss!
54+
DB_NAMES: world
55+
MODE: backup
56+
CRON_SCHEDULE: "20 * * * *"

dockerbuild/rsyslog.conf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# if you experience problems, check:
2+
# http://www.rsyslog.com/troubleshoot
3+
4+
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
5+
6+
#
7+
# Configure TLS (logentries-specific example: https://docs.logentries.com/docs/rsyslog/)
8+
#
9+
$DefaultNetstreamDriverCAFile /etc/ssl/certs/ca-certificates.crt
10+
$ActionSendStreamDriver gtls
11+
$ActionSendStreamDriverMode 1
12+
$ActionSendStreamDriverAuthMode x509/name
13+
$ActionSendStreamDriverPermittedPeer *.logentries.com
14+
15+
$template LogentriesFormat,"LOGENTRIESKEY %msg%\n"
16+
*.emerg,*.alert,*.crit,*.err,*.warning,user.* @@data.logentries.com:443;LogentriesFormat

local.env.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
LOGENTRIES_KEY=
2+
AWS_ACCESS_KEY=
3+
AWS_SECRET_KEY=
4+
S3_BUCKET=

0 commit comments

Comments
 (0)