Debian Codegraph @

templates/pg_dump.sh.epp

raw
1 <%- |
2 Boolean $compress,
3 Array $databases,
4 Optional[String[1]] $db_user,
5 Boolean $delete_before_dump,
6 String[1] $dir,
7 Enum['plain','custom','directory','tar'] $format,
8 Array $optional_args,
9 Optional[String[1]] $post_script,
10 Optional[String[1]] $pre_script,
11 Integer[0] $rotate,
12 Stdlib::Absolutepath $success_file_path,
13 | -%>
14 <%- if $facts['kernel'] == 'Linux' { -%>
15 #!/bin/bash
16 <%- } else { -%>
17 #!/bin/sh
18 <%- } -%>
19 # This file is managed by Puppet. DO NOT EDIT.
20 #
21 # A wrapper for pg_dump
22
23 # Backup config
24 ROTATE=<%= $rotate %>
25 BASEDIR="<%= $dir %>"
26 DIR="${BASEDIR}/$(date +%F_%H-%M-%S)"
27
28 # Pattern %FILENAME% will be replace or removed, depending
29 # on the pg_dump parameters.
30 TEMPLATE="${DIR}/%FILENAME%"
31
32 # Use a filename suffix to better distinguish different file types.
33 SUFFIX=".pgdump"
34
35 # Ensure backup directory exist.
36 mkdir -p $DIR
37
38 <%- if $facts['kernel'] == 'Linux' { -%>
39 set -o pipefail
40 <%- } -%>
41
42 <% if $pre_script { -%>
43 <%- flatten($pre_script).each |$_script| { %>
44 <%= $_script %>
45 <%- } -%>
46 <% } -%>
47
48 cleanup()
49 {
50 <%- if $facts['kernel'] == 'SunOS' { -%>
51 gfind "${BASEDIR}/" -mindepth 1 -maxdepth 1 -mtime +${ROTATE} -print0 | gxargs -0 -r rm -rf
52 <%- } else { -%>
53 find "${BASEDIR}/" -mindepth 1 -maxdepth 1 -mtime +${ROTATE} -print0 | xargs -0 -r rm -rf
54 <%- } -%>
55 }
56
57 <% if $delete_before_dump { -%>
58 # Remove outdated backups unconditionally before making new backups.
59 cleanup
60 <% } -%>
61
62 _pg_args=''
63
64 <%- if $format == 'directory' { -%>
65 # The 'directory' format expects a target directory instead of a file.
66 TEMPLATE=$DIR
67 <%- } -%>
68
69 <%- if $db_user { -%>
70 _pg_args="${_pg_args} --username=<%= $db_user %>"
71 <%- } -%>
72
73 <%- if $optional_args { -%>
74 <%- $optional_args.each |$_arg| { -%>
75 _pg_args="${_pg_args} <%= $_arg %>"
76 <%- } -%>
77 <%- } -%>
78
79 <%- if $databases and $databases =~ Array and !empty($databases) { -%>
80 _pg_args="${_pg_args} --format=<%= $format %>"
81
82 <%# Compression is only supported by pg_dump, but not by pg_dumpall. -%>
83 <%- if !$compress { -%>
84 _pg_args="${_pg_args} --compress=0"
85 <%# The tar archive format does not support compression. -%>
86 <%- } elsif $format != 'tar' { -%>
87 _pg_args="${_pg_args} --compress=9"
88 SUFFIX="${SUFFIX}.gz"
89 <%- } -%>
90
91 # Dump only selected databases
92 <%- $databases.each |$_db| { -%>
93 FILE=`echo $TEMPLATE | sed "s/%FILENAME%/<%= $_db %>$SUFFIX/;"`
94 pg_dump $_pg_args --file=${FILE} $@ <%= $_db %>
95 <%- } -%>
96 <%- } else { -%>
97 # Dump the whole instance
98 FILE=`echo $TEMPLATE | sed "s/%FILENAME%/all$SUFFIX/;"`
99 pg_dumpall $_pg_args --file=${FILE} $@
100 <%- } -%>
101
102 <% unless $delete_before_dump { -%>
103 # Remove outdated backups only if the new backup was successful.
104 if [ $? -eq 0 ] ; then
105 cleanup
106 <%- if $success_file_path { -%>
107 touch <%= $success_file_path %>
108 <%- } -%>
109 fi
110 <% } -%>
111
112 <% if $post_script { -%>
113 <%- flatten($post_script).each |$_script| { %>
114 <%= $_script %>
115 <%- } -%>
116 <% } -%>