Debian Codegraph @

pg_dropcluster

raw
1 #!/usr/bin/perl -wT
2
3 # Completely delete a PostgreSQL cluster. Fails if there is still a server
4 # process attached.
5 #
6 # (C) 2005-2009 Martin Pitt <[email protected]>
7 # (C) 2015-2025 Christoph Berg <[email protected]>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18
19 use strict;
20 use warnings;
21 use Getopt::Long;
22 use PgCommon;
23
24 # untaint environment
25 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
26 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
27
28 my $stopserver = 0;
29 exit 1 unless GetOptions ('stop-server' => \$stopserver, 'stop' => \$stopserver);
30
31 # command line options
32 if ($#ARGV != 1) {
33 print "Usage: $0 [--stop] <version> <cluster>\n";
34 exit 1;
35 }
36
37 my ($version) = $ARGV[0] =~ /^(\d+\.?\d+)$/;
38 my ($cluster) = $ARGV[1] =~ /^([-.\w]+)$/;
39 error 'invalid version' unless defined $version;
40 error 'invalid cluster name' unless defined $cluster;
41
42 my %info;
43 my $c; # configuration directory
44 my $startconf;
45
46 if (cluster_exists $version, $cluster) {
47 %info = cluster_info ($version, $cluster);
48 validate_cluster_owner \%info if ($info{pgdata} && -d $info{pgdata}); # ignore missing data directory
49
50 if ($info{'running'}) {
51 if ($stopserver) {
52 if ($info{'pgdata'} && -d $info{'pgdata'}) {
53 if (system ('pg_ctlcluster', $version, $cluster, 'stop')) {
54 error 'could not stop server, aborting';
55 }
56 } else {
57 print STDERR "Warning: corrupted cluster: data directory does not exist any more, but server is still running; you have to manually kill the postgres process\n";
58 }
59 } else {
60 error 'This cluster is still running. Stop it or supply the --stop option';
61 }
62 }
63 $c = $info{'configdir'};
64 $startconf = $info{'start'};
65 } else {
66 $c = "/etc/postgresql/$version/$cluster";
67
68 # check if we have a broken cluster, clean up if necessary
69 -d $c or error 'specified cluster does not exist';
70 }
71
72 # disable systemd-enabled clusters
73 if (not exists $ENV{'PG_CLUSTER_CONF_ROOT'} and -d '/run/systemd/system') {
74 if ($> == 0) {
75 system "systemctl", "disable", "postgresql\@$version-$cluster";
76 system "systemctl", "disable", "--now", "pg_receivewal\@$version-$cluster";
77 system "systemctl", "disable", "--now", "pg_basebackup\@$version-$cluster.timer";
78 system "systemctl", "disable", "--now", "pg_dump\@$version-$cluster.timer";
79 }
80 }
81
82 if ($info{'pgdata'} && -d $info{'pgdata'}) {
83 # remove custom wal directory
84 if ($info{waldir} and -d $info{waldir}) {
85 my $walowner = (stat($info{waldir}))[4];
86 if (defined $walowner and $walowner == $info{owneruid}) {
87 my $result = system 'rm', '-r', '--', $info{waldir};
88 if ($result) {
89 print STDERR "Warning: could not remove wal directory $info{waldir}";
90 }
91 } else {
92 print STDERR "Warning: wal directory $info{waldir} is not owned by uid $info{owneruid}, not removing\n";
93 }
94 }
95
96 # get catalog version
97 my $catversion = "*"; # fall back to glob if pg_controldata is missing
98 my $pg_controldata = get_program_path 'pg_controldata', $version;
99 if (-e $pg_controldata and -e "$info{pgdata}/global/pg_control") {
100 eval { # ignore errors from pg_controldata, we just want to drop the cluster
101 my $controldata = get_cluster_controldata $version, $cluster;
102 $catversion = $controldata->{'Catalog version number'};
103 };
104 }
105
106 # remove tablespace subdirectories that belong to our version
107 for my $link (glob "$info{pgdata}/pg_tblspc/*") {
108 next unless (-l $link);
109 my $tblspc = readlink $link;
110 my $tblspcowner = (stat($tblspc))[4];
111 if (defined $tblspcowner and $tblspcowner == $info{owneruid}) {
112 if ($version >= 9.0) {
113 for my $dir (glob "$tblspc/PG_${version}_$catversion") {
114 my $dirowner = (stat($dir))[4];
115 if (defined $dirowner and $dirowner == $info{owneruid}) {
116 my $result = system 'rm', '-r', '--', ($dir =~ /(.*)/); # untaint
117 if ($result) {
118 print STDERR "Warning: could not remove tablespace directory $dir";
119 }
120 } else {
121 print STDERR "Warning: tablespace subdirectory $dir (in tablespace linked from $link) is not owned by uid $info{owneruid}, not removing\n";
122 }
123 }
124 } else { # before 9.0
125 if (open my $fh, '<', "$tblspc/PG_VERSION") {
126 my $v = <$fh>;
127 chomp $v;
128 close $fh;
129 if ($v eq $version) {
130 $tblspc =~ /(.*)/; # untaint
131 my $result = system "rm -r -- $1/*";
132 } else {
133 print STDERR "Warning: tablespace directory $tblspc (linked from $link) is from PostgreSQL version $v, not removing\n";
134 }
135 } else {
136 print STDERR "Warning: tablespace directory $tblspc (linked from $link) is not a PostgreSQL directory, not removing\n";
137 }
138 }
139 } else {
140 print STDERR "Warning: tablespace directory $tblspc (linked from $link) is not owned by uid $info{owneruid}, not considering\n";
141 }
142 }
143
144 # remove pgdata
145 my $result = system 'rm', '-r', $info{'pgdata'};
146 if ($result) {
147 if (! -w ($info{'pgdata'} . '/..')) {
148 error 'you might need to run this program with root privileges';
149 }
150 exit $result;
151 }
152 } else {
153 print STDERR "Warning: corrupted cluster: data directory does not exist\n";
154 }
155
156 # remove config
157 unlink $c.'/pg_hba.conf', $c.'/pg_ident.conf', $c.'/postgresql.conf',
158 $c.'/start.conf', $c.'/log', $c.'/autovacuum_log', $c.'/pgdata',
159 $c.'/environment', $c.'/pg_ctl.conf';
160 rmdir $_ foreach (map { /(.*)/ && $1 } glob "$c/*"); # remove empty conf.d directories
161
162 unlink $info{'logfile'} if defined ($info{'logfile'});
163 if ($info{'socketdir'} and $info{'socketdir'} !~ /^(\/tmp|\/var\/run\/postgresql)\/?$/) {
164 rmdir $info{'socketdir'};
165 }
166 rmdir $c;
167 rmdir "/etc/postgresql/$version";
168 rmdir "/var/lib/postgresql/$version/$cluster";
169 rmdir "/var/lib/postgresql/$version";
170 unlink "/var/log/postgresql/postgresql-$version-$cluster.log";
171 # remove logrotated files
172 foreach my $f (</var/log/postgresql/postgresql-$version-$cluster.log.[1-9]*>) {
173 unlink ($f =~ /(.*)/); # untaint
174 }
175
176 # remove stats_temp_directory
177 my $statstempdir = $info{config}->{stats_temp_directory};
178 if ($statstempdir) {
179 my $statsowner = (stat($statstempdir))[4];
180 if (defined $statsowner and defined $info{owneruid} and $statsowner == $info{owneruid}) {
181 foreach my $f (<$statstempdir/*.stat>) {
182 unlink ($f =~ /(.*)/); # untaint
183 }
184 rmdir $statstempdir;
185 }
186 }
187
188 # notify systemd when an autostarted cluster went away
189 if (not exists $ENV{'PG_CLUSTER_CONF_ROOT'} and $startconf and $startconf eq 'auto' and -d '/run/systemd/system') {
190 if ($> == 0) {
191 system 'systemctl daemon-reload';
192 } elsif (-t 1) {
193 print "Warning: systemd was not informed about the removed cluster yet. Operations like \"service postgresql start\" might fail. To fix, run:\n";
194 print " sudo systemctl daemon-reload\n";
195 }
196 }
197
198 # notify apt about the new cluster
199 if (not exists $ENV{'PG_CLUSTER_CONF_ROOT'} and $> == 0) {
200 system "/usr/share/postgresql-common/pg_updateaptconfig";
201 }
202
203 exit 0;
204
205 __END__
206
207 =head1 NAME
208
209 pg_dropcluster - completely delete a PostgreSQL cluster
210
211 =head1 SYNOPSIS
212
213 B<pg_dropcluster> [B<--stop>] I<cluster-version> I<cluster-name>
214
215 =head1 DESCRIPTION
216
217 This program removes all files that belong to a given PostgreSQL cluster; that
218 includes the data, wal, and tablespace directories, the log file, and all configuration files that
219 were created by L<pg_createcluster(1)>. If the configuration directory
220 (C</etc/postgresql/>I<version>C</>I<cluster>) is empty after this, it is
221 removed as well.
222 An empty socket directory other than B</var/run/postgresql> or B</tmp> is
223 also removed.
224
225 Usually a cluster which still has a running server attached will not be
226 deleted. To override this, the B<--stop> option forces a server shutdown
227 before the files are removed.
228
229 =head1 SEE ALSO
230
231 L<pg_createcluster(1)>, L<pg_ctlcluster(1)>
232
233 =head1 AUTHOR
234
235 Martin Pitt L<E<lt>[email protected]<gt>>
236