Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Dockerfiles
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Picasoft
Technique
Dockerfiles
Commits
f3392a42
Commit
f3392a42
authored
8 years ago
by
Picasoft Registry
Browse files
Options
Downloads
Patches
Plain Diff
Postgresbackup
parent
1ba1105e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
postgres-backup/Dockerfile
+13
-0
13 additions, 0 deletions
postgres-backup/Dockerfile
postgres-backup/README.md
+35
-0
35 additions, 0 deletions
postgres-backup/README.md
postgres-backup/run.sh
+83
-0
83 additions, 0 deletions
postgres-backup/run.sh
with
131 additions
and
0 deletions
postgres-backup/Dockerfile
0 → 100644
+
13
−
0
View file @
f3392a42
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"]
This diff is collapsed.
Click to expand it.
postgres-backup/README.md
0 → 100644
+
35
−
0
View file @
f3392a42
# 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
This diff is collapsed.
Click to expand it.
postgres-backup/run.sh
0 → 100755
+
83
−
0
View file @
f3392a42
#!/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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment