Debian Codegraph @

pg_getwal

raw
1 #!/bin/sh
2
3 # retrieve a WAL file from a pg_receivewal archive
4 #
5 # Copyright (C) 2021-2022 Christoph Berg <[email protected]>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16
17 set -eu
18
19 binroot="/usr/lib/postgresql/"
20 #redhat# binroot="/usr/pgsql-"
21
22 if [ -z "${2:-}" ]; then
23 echo "Syntax: $0 /path/to/wal/%f %p" >&2
24 exit 1
25 fi
26
27 file="$1"
28 path="$2"
29
30 # sanity-check the first argument
31 waldir="$(dirname $file)"
32 if ! [ -d "$waldir" ]; then
33 echo "$0: $waldir is not a directory" >&2
34 exit 129
35 fi
36
37 if [ -f "$file.gz" ]; then
38 gunzip < "$file.gz" > "$path" || exit 129
39
40 elif [ -f "$file.lz4" ]; then
41 unlz4 < "$file.lz4" > "$path" || exit 129
42
43 elif [ -f "$file" ]; then
44 cp "$file" "$path" || exit 129
45
46 elif [ -f "$file.gz.partial" ] || [ -f "$file.lz4.partial" ]; then
47 if [ -s "$file.gz.partial" ]; then
48 gunzip < "$file.gz.partial" > "$path" || exit 129
49 elif [ -s "$file.lz4.partial" ]; then
50 unlz4 < "$file.lz4.partial" > "$path" || exit 129
51 else
52 # .gz.partial starts completely empty, gunzip doesn't like that
53 touch "$path" || exit 129
54 fi
55
56 # expand file to original size
57 version=$(cat PG_VERSION) || exit 129
58 wal_file_size=$(LC_ALL=C "$binroot$version/bin/pg_controldata" . | awk '/^Bytes per WAL segment:/ { print $5 }') || exit 129
59 [ "$wal_file_size" -gt 0 ] || exit 129
60 truncate --size="$wal_file_size" "$path" || exit 129
61
62 elif [ -f "$file.partial" ]; then
63 cp "$file.partial" "$path" || exit 129
64
65 else
66 # file not found, exit silently in order not to spam the server log with errors
67 exit 1
68 fi
69
70 exit 0
71
72 : <<=cut
73
74 =head1 NAME
75
76 pg_getwal - retrieve a WAL file from a pg_receivewal archive
77
78 =head1 SYNOPSIS
79
80 B<pg_getwal> I</path/to/wal/%f> I<%p>
81
82 =head1 DESCRIPTION
83
84 B<pg_getwal> retrieves and decompresses files from a WAL archive maintained by
85 B<pg_receivewal> and B<pg_backupcluster>. It is put into PostgreSQL's
86 B<restore_command> by B<pg_restorecluster>.
87
88 =head1 SEE ALSO
89
90 L<pg_restorecluster(1)>, L<pg_backupcluster(1)>.
91
92 =head1 AUTHOR
93
94 Christoph Berg L<E<lt>[email protected]<gt>>
95
96 =cut