File: pg_dump.sh.epp

package info (click to toggle)
puppet-module-puppetlabs-postgresql 10.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 940 kB
  • sloc: ruby: 731; sh: 66; makefile: 2
file content (116 lines) | stat: -rw-r--r-- 3,174 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<%- |
  Boolean                                  $compress,
  Array                                    $databases,
  Optional[String[1]]                      $db_user,
  Boolean                                  $delete_before_dump,
  String[1]                                $dir,
  Enum['plain','custom','directory','tar'] $format,
  Array                                    $optional_args,
  Optional[String[1]]                      $post_script,
  Optional[String[1]]                      $pre_script,
  Integer[0]                               $rotate,
  Stdlib::Absolutepath                     $success_file_path,
| -%>
<%- if $facts['kernel'] == 'Linux' { -%>
#!/bin/bash
<%- } else { -%>
#!/bin/sh
<%- } -%>
# This file is managed by Puppet. DO NOT EDIT.
#
# A wrapper for pg_dump

# Backup config
ROTATE=<%= $rotate %>
BASEDIR="<%= $dir %>"
DIR="${BASEDIR}/$(date +%F_%H-%M-%S)"

# Pattern %FILENAME% will be replace or removed, depending
# on the pg_dump parameters.
TEMPLATE="${DIR}/%FILENAME%"

# Use a filename suffix to better distinguish different file types.
SUFFIX=".pgdump"

# Ensure backup directory exist.
mkdir -p $DIR

<%- if $facts['kernel'] == 'Linux' { -%>
set -o pipefail
<%- } -%>

<% if $pre_script { -%>
  <%- flatten($pre_script).each |$_script| { %>
<%= $_script %>
  <%- } -%>
<% } -%>

cleanup()
{
  <%- if $facts['kernel'] == 'SunOS' { -%>
    gfind "${BASEDIR}/" -mindepth 1 -maxdepth 1 -mtime +${ROTATE} -print0 | gxargs -0 -r rm -rf
  <%- } else { -%>
    find "${BASEDIR}/" -mindepth 1 -maxdepth 1 -mtime +${ROTATE} -print0 | xargs -0 -r rm -rf
  <%- } -%>
}

<% if $delete_before_dump { -%>
# Remove outdated backups unconditionally before making new backups.
cleanup
<% } -%>

_pg_args=''

<%- if $format == 'directory' { -%>
# The 'directory' format expects a target directory instead of a file.
TEMPLATE=$DIR
<%- } -%>

<%- if $db_user { -%>
_pg_args="${_pg_args} --username=<%= $db_user %>"
<%- } -%>

<%- if $optional_args { -%>
  <%- $optional_args.each |$_arg| { -%>
_pg_args="${_pg_args} <%= $_arg %>"
  <%- } -%>
<%- } -%>

<%- if $databases and $databases =~ Array and !empty($databases) { -%>
_pg_args="${_pg_args} --format=<%= $format %>"

<%# Compression is only supported by pg_dump, but not by pg_dumpall. -%>
<%- if !$compress { -%>
_pg_args="${_pg_args} --compress=0"
<%# The tar archive format does not support compression. -%>
<%- } elsif $format != 'tar' { -%>
_pg_args="${_pg_args} --compress=9"
SUFFIX="${SUFFIX}.gz"
<%- } -%>

# Dump only selected databases
  <%- $databases.each |$_db| { -%>
FILE=`echo $TEMPLATE | sed "s/%FILENAME%/<%= $_db %>$SUFFIX/;"`
pg_dump $_pg_args --file=${FILE} $@ <%= $_db %>
  <%- } -%>
<%- } else { -%>
# Dump the whole instance
FILE=`echo $TEMPLATE | sed "s/%FILENAME%/all$SUFFIX/;"`
pg_dumpall $_pg_args --file=${FILE} $@
<%- } -%>

<% unless $delete_before_dump { -%>
# Remove outdated backups only if the new backup was successful.
if [ $? -eq 0 ] ; then
    cleanup
    <%- if $success_file_path { -%>
    touch <%= $success_file_path %>
    <%- } -%>
fi
<% } -%>

<% if $post_script { -%>
  <%- flatten($post_script).each |$_script| { %>
<%= $_script %>
  <%- } -%>
<% } -%>