diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6a1f7ed --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM ubuntu:latest +MAINTAINER tim@kartoza.com + +RUN apt-get install -y postgresql-client +ADD backups-cron /etc/cron.d/backups-cron +RUN touch /var/log/cron.log +ADD backups.sh /backups.sh + +CMD cron -f diff --git a/backups-cron b/backups-cron new file mode 100755 index 0000000..3c25531 --- /dev/null +++ b/backups-cron @@ -0,0 +1,2 @@ +*/10 * * * * root /backups.sh 2>&1 +# We need a blank line here for it to be a valid cron file diff --git a/backups.sh b/backups.sh new file mode 100755 index 0000000..1844a26 --- /dev/null +++ b/backups.sh @@ -0,0 +1,24 @@ +#!/bin/bash + + +MYDATE=`date +%d-%B-%Y` +MONTH=$(date +%B) +YEAR=$(date +%Y) +MYBASEDIR=/backups +MYBACKUPDIR=${MYBASEDIR}/${YEAR}/${MONTH} +mkdir -p $MYBACKUPDIR +cd $MYBACKUPDIR + +echo "Backup running to $MYBACKUPDIR" >> /var/log/cron.log + +# +# Loop through each pg database backing it up +# + +DBLIST=`psql -l | awk '{print $1}' | grep -v "+" | grep -v "Name" | grep -v "List" | grep -v "(" | grep -v "template" | grep -v "postgres" | grep -v ":"` +for DB in ${DBLIST} +do + echo "Backing up $DB" + FILENAME=${MYBACKUPDIR}/PG_${DB}.${MYDATE}.dmp + pg_dump -i -Fc -f ${FILENAME} -x -O ${DB}" +done diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..482f0dd --- /dev/null +++ b/build.sh @@ -0,0 +1,2 @@ +#!/bin/bash +docker build -t kartoza/pg-backups . diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..1ef0ef6 --- /dev/null +++ b/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +docker run --name="cron" --hostname="cron" -link watchkeeper_db_1 -i -d kartoza/pg-backups +