postgresql: Added postgresql::server::backup class for database backups.
This commit is contained in:
parent
183147594a
commit
39a60084c4
2 changed files with 76 additions and 0 deletions
|
@ -102,3 +102,44 @@ class postgresql::server($datadir="/srv/pgsql") {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
# Install PostgreSQL daily backup job
|
||||
#
|
||||
# === Global variables
|
||||
#
|
||||
# $datadir:
|
||||
# Directory where PostgreSQL backups are stored. Defaults
|
||||
# to /srv/pgsql-backup
|
||||
#
|
||||
# $maxage:
|
||||
# How long to keep MariaDB backups. Defaults to 7 days.
|
||||
#
|
||||
class postgresql::server::backup($datadir="/srv/pgsql-backup", $maxage="7") {
|
||||
|
||||
file { $datadir:
|
||||
ensure => directory,
|
||||
mode => "0700",
|
||||
owner => "postgres",
|
||||
group => "postgres",
|
||||
}
|
||||
|
||||
file { "/usr/local/sbin/pgsql-backup":
|
||||
ensure => present,
|
||||
content => template("postgresql/pgsql-backup.erb"),
|
||||
mode => "0755",
|
||||
owner => "root",
|
||||
group => "root",
|
||||
require => File[$datadir],
|
||||
}
|
||||
|
||||
cron { "pgsql-backup":
|
||||
command => "/usr/local/sbin/pgsql-backup",
|
||||
environment => [ "MAILTO=root", ],
|
||||
user => "postgres",
|
||||
hour => "0",
|
||||
minute => "20",
|
||||
require => File["/usr/local/sbin/pgsql-backup"],
|
||||
}
|
||||
|
||||
}
|
||||
|
|
35
postgresql/templates/pgsql-backup.erb
Executable file
35
postgresql/templates/pgsql-backup.erb
Executable file
|
@ -0,0 +1,35 @@
|
|||
#!/bin/sh
|
||||
|
||||
umask 077
|
||||
|
||||
if [ `whoami` != postgres ]; then
|
||||
echo "ERR: Script needs to be run as postgres user" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
DESTDIR="<%= @datadir %>"
|
||||
MAXAGE="<%= @maxage %>"
|
||||
|
||||
DATE=`date "+%Y-%m-%d"`
|
||||
|
||||
if [ ! -d ${DESTDIR} ]; then
|
||||
echo "ERR: PostgreSQL backup directory [${DESTDIR}] does not exist" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cd ${DESTDIR} && {
|
||||
find . -xdev -mindepth 2 -maxdepth 2 -type f -mtime +7 -execdir rm -f -- {} \;
|
||||
find . -xdev -depth -mindepth 1 -maxdepth 1 -type d -empty -execdir rmdir -- {} \;
|
||||
}
|
||||
|
||||
DESTDIR=${DESTDIR}/${DATE}
|
||||
mkdir -p ${DESTDIR}
|
||||
|
||||
for db in `psql -l -t | sed -n 's/^ \([^ ]\+\) .*/\1/p'`; do
|
||||
case ${db} in
|
||||
template*)
|
||||
continue
|
||||
;;
|
||||
esac
|
||||
pg_dump ${db} | gzip > ${DESTDIR}/${db}.${DATE}.gz
|
||||
done
|
Loading…
Add table
Reference in a new issue