From 792fb30bb708ac2de278bdbfc3448949a0e84da1 Mon Sep 17 00:00:00 2001 From: Tomas Pospisek Date: Tue, 18 May 2021 15:39:27 +0200 Subject: [PATCH] initial public release --- README.md | 52 ++++++++++++++++++ backup-pg_0.1-10_all.deb | Bin 0 -> 3584 bytes debian/backup-pg.dirs | 2 + debian/backup-pg.examples | 1 + debian/backup-pg.install | 1 + debian/changelog | 55 +++++++++++++++++++ debian/compat | 1 + debian/control | 15 ++++++ debian/docs | 1 + debian/rules | 31 +++++++++++ debian/source/format | 1 + etc/cron.d/backup-pg | 4 ++ usr/sbin/dump_postgres_dbs | 108 +++++++++++++++++++++++++++++++++++++ 13 files changed, 272 insertions(+) create mode 100644 README.md create mode 100644 backup-pg_0.1-10_all.deb create mode 100644 debian/backup-pg.dirs create mode 100644 debian/backup-pg.examples create mode 100644 debian/backup-pg.install create mode 100644 debian/changelog create mode 100644 debian/compat create mode 100644 debian/control create mode 100644 debian/docs create mode 100755 debian/rules create mode 100644 debian/source/format create mode 100644 etc/cron.d/backup-pg create mode 100755 usr/sbin/dump_postgres_dbs diff --git a/README.md b/README.md new file mode 100644 index 0000000..0b81d9e --- /dev/null +++ b/README.md @@ -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 diff --git a/backup-pg_0.1-10_all.deb b/backup-pg_0.1-10_all.deb new file mode 100644 index 0000000000000000000000000000000000000000..7854631980e4ffa0d85e7ae8a4625c0fb7530269 GIT binary patch literal 3584 zcmbu>S5On&x(46?0qN3vmEJ;tKD@c{~V~nOelO zmp{nB8aVJ3M9qM{8zFkWSMuuDoV5j*ebGtO>5wlgGc({?WIPsZf^lprWqQsxSOmQeZezy-Fmtt#0z4plzpB%Vs zaODk+0tGyQy=GdP6bM(xQ=flv%!U0RM}J!DMp7}}Cdc<@ww0ELn&+mjOqso->LCM@ zKmA8uYXaTlO8Ljm;05XiWa|rY`;`Mt!Ss$sd zdyvFcE_n@X03#iisLI_fr{aeOP6sNCuEyg|$MKuG;=`8I+|Bn5tmp<%)p_UT ziP8^sk9yb{Ej%?NePJ+s`?r|GFJ}*FTV;#Bg?=d4GG6w^xA^7^cwqHmG@0i~nC?q_ zQCrmx{TW^ZE~r-BOOYCqmpx1DTN5Ts2R=Bf4bRy4;)QHFx;MY>%aeiKDSt2*(;X@s zLb%d9jP1%Q(Yv7s?^0%n-?{#%B{D_BpuxoW$vrWRKaBLJ=G&r20MelpOmGlIHyIh9&c}x>0L@YklowxZ1MQ-HB22F`Uvxt|qLmMA_o8yRQ}ut-Jbe zZHlKB>j_(}WHYi{*F4;%_AXqZFKVrw1jX&#Xt?XRdXw;n$D5u$L%HqRm1p#;YK8Bhg_k2gbce=LylzcLM3`-`+VjtTo zVK#he_I}|Hw&{NO>nj~5MDLm(Fv}STgT-}n`$njQLT;FDnE0p+Mp127k6TI0`e}t2?HpCk+~*hfvn`p@*|$cJ>|&TGN>Q7PhwiB`-X>N#GV9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g4pynHW!317%;Vx){`<3H>QR~}?kwyIGT64D}*G|jr#U%gBb>@3_$QT0MSr+Z53W7l7A z)cfsAD)}r^@HvHMH_sv&MHwX?lq5(#x!aiMA9HpPZo+717|FA5auOF{^U3tba2iQi zC6z1bht)Y;!+Dc}jOOA?V}ztxi(&!sBi|C| zGTp+mXrl9w^~`Gi6#UZSqw-cGHpUw$b6&PEF~MJ}9PP(0!^RS04m=+H zQVgj+wOWCn94M8DJh$RE6%#j=3e-1e=80qpgn#k zq1%&3?KDXJf^8OBhRwDa`diYApjY*cx^&@~++sQ_vLih4C|Ts%%mI=~EfAwbQT=(% z|J%#-{TjLK=S}l}{@l+|kdzsd9Z%4D4=k%ws&Xvo-AA&iY^^1OO=Uj(sN;hP;VC!P z2td;BN4Kqr3HW@Vw$ybCY~|trh1X8!-~~*JS<%VNfi+Z#O#?4#PMBS|RO%IG-5)eE zel~vd4nEnRvg+G=f#`if1-56xP@3mFS;iklfmKia9&3j9zV@=1U>+AC*M{7xL`AXw wypv(uYcCl_tMj|0^<$_bapF(CmClJ729V^l{^_QGrrjaA|J{B61@`-yBme*a literal 0 HcmV?d00001 diff --git a/debian/backup-pg.dirs b/debian/backup-pg.dirs new file mode 100644 index 0000000..bbe451b --- /dev/null +++ b/debian/backup-pg.dirs @@ -0,0 +1,2 @@ +/var/lib/postgresql/backup +/var/log diff --git a/debian/backup-pg.examples b/debian/backup-pg.examples new file mode 100644 index 0000000..c45bb0a --- /dev/null +++ b/debian/backup-pg.examples @@ -0,0 +1 @@ +etc/cron.d/ diff --git a/debian/backup-pg.install b/debian/backup-pg.install new file mode 100644 index 0000000..5c0c938 --- /dev/null +++ b/debian/backup-pg.install @@ -0,0 +1 @@ +/usr/sbin/dump_postgres_dbs diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..103ab64 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,55 @@ +backup-pg (0.1-10) unstable; urgency=medium + + * prepare for public release + + -- Tomas Pospisek Tue, 18 May 2021 15:03:36 +0200 + +backup-pg (0.1-9) unstable; urgency=medium + + * see git log + + -- Tomas Pospisek Mon, 18 Jan 2021 17:33:47 +0100 + +backup-pg (0.1-8) unstable; urgency=medium + + * see git log + + -- Tomas Pospisek Thu, 07 Jan 2021 15:34:42 +0100 + +backup-pg (0.1-7) unstable; urgency=medium + + * see git log + + -- Tomas Pospisek Mon, 04 Jan 2021 17:32:51 +0100 + +backup-pg (0.1-6) unstable; urgency=medium + + * see git log + + -- Tomas Pospisek Mon, 04 Jan 2021 16:48:09 +0100 + +backup-pg (0.1-5) unstable; urgency=medium + + * see git log + + -- Tomas Pospisek Thu, 15 Jun 2017 14:05:12 +0200 + +backup-pg (0.1-4) unstable; urgency=medium + + * see git log + + -- Tomas Pospisek Thu, 19 Feb 2015 10:33:20 +0100 + +backup-pg (0.1-3) unstable; urgency=medium + + * see git log + + -- Tomas Pospisek 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 Tue, 10 Feb 2015 14:45:05 +0100 diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..ec63514 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +9 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..3c6ab45 --- /dev/null +++ b/debian/control @@ -0,0 +1,15 @@ +Source: backup-pg +Section: sourcepole +Priority: optional +Maintainer: Tomas Pospisek +Build-Depends: debhelper (>= 9) +Standards-Version: 3.9.5 +Vcs-Git: git@git.sourcepole.ch: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 diff --git a/debian/docs b/debian/docs new file mode 100644 index 0000000..b43bf86 --- /dev/null +++ b/debian/docs @@ -0,0 +1 @@ +README.md diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..18d6cf5 --- /dev/null +++ b/debian/rules @@ -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: diff --git a/debian/source/format b/debian/source/format new file mode 100644 index 0000000..163aaf8 --- /dev/null +++ b/debian/source/format @@ -0,0 +1 @@ +3.0 (quilt) diff --git a/etc/cron.d/backup-pg b/etc/cron.d/backup-pg new file mode 100644 index 0000000..5375539 --- /dev/null +++ b/etc/cron.d/backup-pg @@ -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" diff --git a/usr/sbin/dump_postgres_dbs b/usr/sbin/dump_postgres_dbs new file mode 100755 index 0000000..f5b870d --- /dev/null +++ b/usr/sbin/dump_postgres_dbs @@ -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