Skip to content

Commit

Permalink
initial public release
Browse files Browse the repository at this point in the history
  • Loading branch information
tpo committed May 18, 2021
0 parents commit 792fb30
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 0 deletions.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Postgres DB backup
==================

Purpose
-------

Dump all DBs in a cluster to a directory.

Usage
-----

(taken from `dump_postgres_dbs --help`)

```
usage: dump_postgres_dbs [--ignore-not-accepting-connections] [--ignore-does-not-exist] [/path/to/backup/dir]
dump_postgres_dbs --help
--ignore-not-accepting-connections
Ignore pg_dump errors resulting from DBs that do not
allow to be connected to.
--ignore-does-not-exist
Ignore pg_dump errors resulting from DBs that do not
exist.
/path/to/backup/dir
Directory where `pg_dump`s of DBs will be saved.
Default is /var/lib/postgresql/backup
```

How to release a new version of this package
--------------------------------------------

```
apt-get install dpkg-dev
debchange --distribution unstable --no-auto-nmu --maintmaint --increment "see git log" && \
dpkg-buildpackage -b -tc -rfakeroot && \
rm ../backup-pg_*_*.changes && \
rm ../backup-pg_*_*.buildinfo && \
mv ../backup-pg_*_all.deb . && \
git add backup-pg_*_all.deb && \
git commit backup-pg_*_all.deb
```

Authorship
----------

`dump_postgres_dbs` started it's life as http://forritan.blogspot.ch/2012/10/postgresql-backup-script.html,
was later improved by Pirmin Kalberer and then by Tomáš Pospíšek
Binary file added backup-pg_0.1-10_all.deb
Binary file not shown.
2 changes: 2 additions & 0 deletions debian/backup-pg.dirs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/var/lib/postgresql/backup
/var/log
1 change: 1 addition & 0 deletions debian/backup-pg.examples
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
etc/cron.d/
1 change: 1 addition & 0 deletions debian/backup-pg.install
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/usr/sbin/dump_postgres_dbs
55 changes: 55 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
backup-pg (0.1-10) unstable; urgency=medium

* prepare for public release

-- Tomas Pospisek <[email protected]> Tue, 18 May 2021 15:03:36 +0200

backup-pg (0.1-9) unstable; urgency=medium

* see git log

-- Tomas Pospisek <[email protected]> Mon, 18 Jan 2021 17:33:47 +0100

backup-pg (0.1-8) unstable; urgency=medium

* see git log

-- Tomas Pospisek <[email protected]> Thu, 07 Jan 2021 15:34:42 +0100

backup-pg (0.1-7) unstable; urgency=medium

* see git log

-- Tomas Pospisek <[email protected]> Mon, 04 Jan 2021 17:32:51 +0100

backup-pg (0.1-6) unstable; urgency=medium

* see git log

-- Tomas Pospisek <[email protected]> Mon, 04 Jan 2021 16:48:09 +0100

backup-pg (0.1-5) unstable; urgency=medium

* see git log

-- Tomas Pospisek <[email protected]> Thu, 15 Jun 2017 14:05:12 +0200

backup-pg (0.1-4) unstable; urgency=medium

* see git log

-- Tomas Pospisek <[email protected]> Thu, 19 Feb 2015 10:33:20 +0100

backup-pg (0.1-3) unstable; urgency=medium

* see git log

-- Tomas Pospisek <[email protected]> Tue, 17 Feb 2015 11:52:22 +0100

backup-pg (0.1-1) unstable; urgency=low

* Initial release
* split out of the dubackup package
* see the git log for changes

-- Tomas Pospisek <[email protected]> Tue, 10 Feb 2015 14:45:05 +0100
1 change: 1 addition & 0 deletions debian/compat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9
15 changes: 15 additions & 0 deletions debian/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Source: backup-pg
Section: sourcepole
Priority: optional
Maintainer: Tomas Pospisek <[email protected]>
Build-Depends: debhelper (>= 9)
Standards-Version: 3.9.5
Vcs-Git: [email protected]:sourcepole/sp-sysadmin.git
Vcs-Browser: https://gitlab.sourcepole.ch/sourcepole/sp-sysadmin

Package: backup-pg
Architecture: all
# depend on util-linux for ionice
Depends: ${shlibs:Depends}, ${misc:Depends}, postgresql-client, util-linux
Description: Sourcepole's postgres DB backup
see /usr/share/doc/backup-pg/README for docu
1 change: 1 addition & 0 deletions debian/docs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
README.md
31 changes: 31 additions & 0 deletions debian/rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#DH_VERBOSE = 1

# see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

# see FEATURE AREAS in dpkg-buildflags(1)
# see ENVIRONMENT in dpkg-buildflags(1)

# main packaging script based on dh7 syntax
%:
dh $@

override_dh_fixperms:
dh_fixperms
touch -t 197001010000 debian/backup-pg/var/log/backup-pg.success
chown postgres debian/backup-pg/var/log/backup-pg.success

# we don't need those steps
override_dh_auto_configure:
override_dh_auto_build:
override_dh_auto_test:
override_dh_auto_install:
override_dh_usrlocal:
override_dh_strip:
override_dh_makeshlibs:
override_dh_shlibdeps:
override_dh_perl:
1 change: 1 addition & 0 deletions debian/source/format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0 (quilt)
4 changes: 4 additions & 0 deletions etc/cron.d/backup-pg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PATH=/usr/sbin:/usr/bin:/sbin:/bin

28 2 * * * root touch /var/log/dump_postgres_dbs.log && chown postgres /var/log/dump_postgres_dbs.log
29 2 * * * postgres ionice -c3 dump_postgres_dbs 2> /var/log/dump_postgres_dbs.log || echo "backup failed, see /var/log/dump_postgres_dbs.log"
108 changes: 108 additions & 0 deletions usr/sbin/dump_postgres_dbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/bin/bash
# PostgreSQL backup script
# Run as postgres user
# Based on http://forritan.blogspot.ch/2012/10/postgresql-backup-script.html

help() {
echo 'usage: dump_postgres_dbs [--ignore-not-accepting-connections] [--ignore-does-not-exist] [/path/to/backup/dir]'
echo ' dump_postgres_dbs --help'
echo
echo ' --ignore-not-accepting-connections'
echo
echo ' Ignore pg_dump errors resulting from DBs that do not'
echo ' allow to be connected to.'
echo
echo ' --ignore-does-not-exist'
echo
echo ' Ignore pg_dump errors resulting from DBs that do not'
echo ' exist.'
echo
echo ' /path/to/backup/dir'
echo
echo ' Directory where `pg_dump`s of DBs will be saved.'
echo ' Default is /var/lib/postgresql/backup'
echo
exit 1
}

[ "$1" == "--help" ] && help
[ "$1" == "--ignore-not-accepting-connections" ] && NOT_ACCEPTING_CONNECTIONS=IGNORE && shift
[ "$1" == "--ignore-does-not-exist" ] && DOES_NOT_EXIST=IGNORE && shift

set -e # stop on error

BACKUP_PATH=${1:-"/var/lib/postgresql/backup"}

log() {
# prefix all log messages with dashes so they become
# visually easily distinguishable from psql errors
echo "------------------ $1" >&2
}

log "Start of dump_postgres_dbs: `date -R`"

# For all running PostgreSQL clusters
#
# See below, the end of the loop for the input. If we do it this way then
# the body of the loop is NOT executed in a subshell and thus setting the
# variables in the body of the loop affects their value outside of the
# loop.
#
# The problem is documented here:
# http://mywiki.wooledge.org/BashFAQ/024
#
while read cluster; do

# Create backup path
mkdir -p $BACKUP_PATH/$cluster

# Global stuff from cluster: roles etc.
pg_dumpall --cluster $cluster --globals-only >$BACKUP_PATH/$cluster/globals.sql

error=0
# And then each database (except templates)
psql --cluster $cluster --no-align --tuples-only \
--command="SELECT datname from pg_database WHERE datname NOT IN ('postgres','template0','template1') AND datallowconn" \
| while read databasename; do
log "Start of $databasename DB dump"

if pg_dump_output=$( pg_dump --cluster $cluster \
--format=c $databasename \
--file="$BACKUP_PATH/$cluster/$databasename.dump" \
2>&1 );
then
log "Dump of $databasename SUCCESSFUL"

else
log "$pg_dump_output"

# pg_dump: error: connection to database "foobar" failed: FATAL: database "foobar" does not exist
if echo "$pg_dump_output" | grep -q "does not exist" && \
[ "$DOES_NOT_EXIST" == "IGNORE" ]; then
log "Dump of $databasename IGNORED - was deleted before we could dump it"

# pg_dump: error: connection to database "barbaz" failed: FATAL: database "barbaz" is not currently accepting connections
elif echo "$pg_dump_output" | grep -q "is not currently accepting connections" && \
[ "$NOT_ACCEPTING_CONNECTIONS" == "IGNORE" ]; then
log "Dump of $databasename IGNORED - was disabled before we could dump it"

else
log "Dump of $databasename FAILED"
error=1

fi
fi
done

done < <( awk -- '{ print $1"/"$2 }' <(pg_lsclusters --no-header | grep online) )

if [ "$error" = 0 ]; then
touch /var/log/backup-pg.success
status=SUCCESSFUL
else
status=FAILED
fi

log "End of dump_postgres_dbs: $status: `date -R`, error: $error"

exit $error

0 comments on commit 792fb30

Please sign in to comment.