From 1e1f4ff901342ea44f5cb2e2478a278d7610bf40 Mon Sep 17 00:00:00 2001 From: Tim Sutton Date: Thu, 16 Apr 2015 15:37:42 +0200 Subject: [PATCH] Various fixes for backup reigieme --- Dockerfile | 3 ++- README.md | 30 +++++++++++++++++++++++++----- backups-cron | 3 ++- backups.sh | 17 +++++++++++------ run.sh | 6 +++++- 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6a1f7ed..b3c180c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,5 +5,6 @@ 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 +ADD start.sh /start.sh -CMD cron -f +CMD ["/start.sh"] diff --git a/README.md b/README.md index 4a58d60..80c69bf 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ A simple docker container that runs PostGIS backups. It is intended to be used primarily with our [docker postgis](https://github.com/kartoza/docker-postgis) -docker image. By default it will create a backup once per night in a nicely -ordered directory by year / month. +docker image. By default it will create a backup once per night (at 23h00)in a +nicely ordered directory by year / month. Visit our page on the docker hub at: https://github.com/kartoza/docker-pg-backup @@ -41,12 +41,17 @@ git clone git://github.com/kartoza/docker-postgis To create a running container do: ``` -docker run --name="backups" --hostname="pg-backups" -link db -i -d kartoza/pg-backups -``` +docker run --name="backups"\ + --hostname="pg-backups" \ + --link=watchkeeper_db_1:db \ + -v backups:/backups \ + -i -d kartoza/pg-backups``` + +In this example I used a volume into which the actual backups will be +stored. # Specifying environment -**Note:** This is not implemented yet: You can also use the following environment variables to pass a user name and password etc for the database connection. @@ -65,6 +70,21 @@ Example usage: docker run -e PGUSER=bob -e PGPASSWORD=secret -link db -i -d kartoza/pg-backups ``` +One other environment variable you may like to set is a prefix for the +database dumps. + + +Variable | Expected | Default if not specified +---------|----------|-------------------------- +DUMPPREFIX | | PG_ + +Example usage: + +``` +docker run -e DUMPPREFIX=foo -link db -i -d kartoza/pg-backups +``` + + ## Credits diff --git a/backups-cron b/backups-cron index 3c25531..2a0e50a 100755 --- a/backups-cron +++ b/backups-cron @@ -1,2 +1,3 @@ -*/10 * * * * root /backups.sh 2>&1 +# Run the backups at 11pm each night +*/1 * * * * 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 index 1844a26..112789c 100755 --- a/backups.sh +++ b/backups.sh @@ -1,13 +1,17 @@ #!/bin/bash +source /pgenv.sh + +#echo "Running with these environment options" >> /var/log/cron.log +#set | grep PG >> /var/log/cron.log MYDATE=`date +%d-%B-%Y` MONTH=$(date +%B) YEAR=$(date +%Y) MYBASEDIR=/backups MYBACKUPDIR=${MYBASEDIR}/${YEAR}/${MONTH} -mkdir -p $MYBACKUPDIR -cd $MYBACKUPDIR +mkdir -p ${MYBACKUPDIR} +cd ${MYBACKUPDIR} echo "Backup running to $MYBACKUPDIR" >> /var/log/cron.log @@ -15,10 +19,11 @@ 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 ":"` +DBLIST=`psql -l | awk '{print $1}' | grep -v "+" | grep -v "Name" | grep -v "List" | grep -v "(" | grep -v "template" | grep -v "postgres" | grep -v "|" | grep -v ":"` +# echo "Databases to backup: ${DBLIST}" >> /var/log/cron.log for DB in ${DBLIST} do - echo "Backing up $DB" - FILENAME=${MYBACKUPDIR}/PG_${DB}.${MYDATE}.dmp - pg_dump -i -Fc -f ${FILENAME} -x -O ${DB}" + echo "Backing up $DB" >> /var/log/cron.log + FILENAME=${MYBACKUPDIR}/${DUMPPREFIX}_${DB}.${MYDATE}.dmp + pg_dump -i -Fc -f ${FILENAME} -x -O ${DB} done diff --git a/run.sh b/run.sh index e5efcf6..1ed7fb0 100755 --- a/run.sh +++ b/run.sh @@ -1,4 +1,8 @@ #!/bin/bash -docker run --name="backups" --hostname="pg-backups" -link db -i -d kartoza/pg-backups +docker run --name="backups"\ + --hostname="pg-backups" \ + --link=watchkeeper_db_1:db \ + -v /Users/timlinux/backups:/backups \ + -i -d kartoza/pg-backups