Skip to content
Snippets Groups Projects
Commit f3392a42 authored by Picasoft Registry's avatar Picasoft Registry
Browse files

Postgresbackup

parent 1ba1105e
No related branches found
No related tags found
No related merge requests found
FROM registry.picasoft.net:5000/pica-debian
MAINTAINER antoine@barbare.me
RUN apt-get update && \
apt-get install -y --no-install-recommends postgresql-client cron && \
apt-get clean && \
mkdir /backup
ENV CRON_TIME="0 0 * * *"
ADD run.sh /run.sh
VOLUME ["/backup"]
CMD ["/run.sh"]
# postgres-backup
This image runs postgresdump to backup data using cronjob to folder `/backup`
## Usage:
docker run -d \
--env POSTGRES_HOST=postgres.host \
--env POSTGRES_PORT=27017 \
--env POSTGRES_USER=admin \
--env POSTGRES_PASS=password \
--volume host.folder:/backup
tutum/postgres-backup
## Parameters
POSTGRES_HOST the host/ip of your postgres database
POSTGRES_PORT the port number of your postgres database
POSTGRES_USER the username of your postgres database
POSTGRES_PASS the password of your postgres database
CRON_TIME the interval of cron job to run postgresdump. `0 0 * * *` by default, which is every day at 00:00
POSTGRES_DB the DB to backup
MAX_BACKUPS the number of backups to keep. When reaching the limit, the old backup will be discarded. No limit by default
INIT_BACKUP if set, create a backup when the container starts
INIT_RESTORE_LATEST if set, restores latest backup
## Restore from a backup
See the list of backups, you can run:
docker exec postgres-backup ls /backup
To restore database from a certain backup, simply run:
docker exec tutum-backup /restore.sh /backup/2015.08.06.171901
#!/bin/bash
[ -z "${POSTGRES_HOST}" ] && { echo "=> POSTGRES_HOST cannot be empty" && exit 1; }
[ -z "${POSTGRES_PORT}" ] && { echo "=> POSTGRES_PORT cannot be empty" && exit 1; }
[ -z "${POSTGRES_USER}" ] && { echo "=> POSTGRES_USER cannot be empty" && exit 1; }
[ -z "${POSTGRES_PASS}" ] && { echo "=> POSTGRES_PASS cannot be empty" && exit 1; }
[ -z "${POSTGRES_DB}" ] && { echo "=> POSTGRES_DB cannot be empty" && exit 1; }
BACKUP_CMD="pg_dump -w -c > /backup/"'${BACKUP_NAME}'
echo "=> Creating backup script"
rm -f /backup.sh
cat <<EOF >> /backup.sh
#!/bin/bash
MAX_BACKUPS=${MAX_BACKUPS}
export PGHOST=$POSTGRES_HOST
export PGPORT=$POSTGRES_PORT
export PGDATABASE=$POSTGRES_DB
export PGUSER=$POSTGRES_USER
export PGPASSWORD=$POSTGRES_PASS
BACKUP_NAME=\$(date +\%Y.\%m.\%d.\%H\%M\%S).sql
echo "=> Backup started: \${BACKUP_NAME}"
if ${BACKUP_CMD} ;then
echo " Backup succeeded"
else
echo " Backup failed"
rm -rf /backup/\${BACKUP_NAME}
fi
if [ -n "\${MAX_BACKUPS}" ]; then
while [ \$(ls /backup -N1 | wc -l) -gt \${MAX_BACKUPS} ];
do
BACKUP_TO_BE_DELETED=\$(ls /backup -N1 | sort | head -n 1)
echo " Backup \${BACKUP_TO_BE_DELETED} is deleted"
rm -rf /backup/\${BACKUP_TO_BE_DELETED}
done
fi
echo "=> Backup done"
EOF
chmod +x /backup.sh
echo "=> Creating restore script"
rm -f /restore.sh
cat <<EOF >> /restore.sh
#!/bin/bash
export PGHOST=$POSTGRES_HOST
export PGPORT=$POSTGRES_PORT
export PGDATABASE=$POSTGRES_DB
export PGUSER=$POSTGRES_USER
export PGPASSWORD=$POSTGRES_PASS
echo "=> Restore database from \$1"
if pg_restore -w -d $POSTGRES_DB < \$1 ;then
echo " Restore succeeded"
else
echo " Restore failed"
fi
echo "=> Done"
EOF
chmod +x /restore.sh
touch /postgres_backup.log
tail -F /postgres_backup.log &
if [ -n "${INIT_BACKUP}" ]; then
echo "=> Create a backup on the startup"
/backup.sh
elif [ -n "${INIT_RESTORE_LATEST}" ]; then
echo "=> Restore lates backup"
until nc -z $POSTGRES_HOST $POSTGRES_PORT
do
echo "waiting database container..."
sleep 1
done
ls -d -1 /backup/* | tail -1 | xargs /restore.sh
fi
echo "${CRON_TIME} /backup.sh >> /postgres_backup.log 2>&1" > /crontab.conf
crontab /crontab.conf
echo "=> Running cron job"
exec cron -f
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment