Debian Codegraph @

pg_renamecluster

raw
1 #!/usr/bin/perl -wT
2
3 # Rename a PostgreSQL cluster
4 #
5 # (C) 2014-2021 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 use strict;
18 use warnings;
19 use PgCommon;
20 use Getopt::Long;
21 use POSIX;
22
23 # untaint environment
24 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
25 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
26
27 if (@ARGV != 3) {
28 print "Usage: $0 [OPTIONS] <version> <old cluster name> <new cluster name>\n";
29 exit 1;
30 }
31
32 my ($version) = $ARGV[0] =~ /^(\d+\.?\d+)$/;
33 my ($oldcluster) = $ARGV[1] =~ /^([-.\w]+)$/;
34 my ($newcluster) = $ARGV[2] =~ /^([-.\w]+)$/;
35 if ($newcluster =~ /-/ and -t 1) {
36 print "Warning: cluster names containing dashes (-) will cause problems when running from systemd. Continuing anyway\n";
37 }
38
39 error "Old and new name must be different"
40 if ($oldcluster eq $newcluster);
41 error "specified cluster $version $oldcluster does not exist"
42 unless (cluster_exists $version, $oldcluster);
43 error "target cluster $version $newcluster already exists"
44 if (cluster_exists $version, $newcluster);
45 my %info = cluster_info ($version, $oldcluster);
46 validate_cluster_owner \%info;
47
48 # stopping old cluster, so that we notice early when there are still
49 # connections
50 if ($info{'running'}) {
51 print "Stopping cluster $version $oldcluster ...\n";
52 my @argv = ('pg_ctlcluster', $version, $oldcluster, 'stop');
53 error "Could not stop cluster" if system @argv;
54 }
55
56 # Arguments: <string>, <from>, <to>
57 sub strrepl {
58 my ($s, $f, $t) = @_;
59 $s =~ s/\b\Q$f\E\b/$t/g;
60 return $s;
61 }
62
63 # rename config directory
64 my $olddir = "$PgCommon::confroot/$version/$oldcluster";
65 my $newdir = "$PgCommon::confroot/$version/$newcluster";
66 rename $olddir, $newdir or error "Could not rename config directory $olddir: $!";
67
68 # adapt paths to configuration files
69 my %c = read_cluster_conf_file $version, $newcluster, 'postgresql.conf', 0;
70 if ($c{hba_file}) {
71 PgCommon::set_conf_value $version, $newcluster, 'postgresql.conf', 'hba_file',
72 strrepl($c{hba_file}, $oldcluster, $newcluster);
73 }
74 if ($c{ident_file}) {
75 PgCommon::set_conf_value $version, $newcluster, 'postgresql.conf', 'ident_file',
76 strrepl($c{ident_file}, $oldcluster, $newcluster);
77 }
78 if ($c{external_pid_file}) {
79 PgCommon::set_conf_value $version, $newcluster, 'postgresql.conf', 'external_pid_file',
80 strrepl($c{external_pid_file}, $oldcluster, $newcluster);
81 }
82
83 # update cluster_name
84 if ($c{cluster_name}) {
85 PgCommon::set_conf_value $version, $newcluster, 'postgresql.conf', 'cluster_name',
86 strrepl ($c{cluster_name}, $oldcluster, $newcluster);
87 }
88
89
90 # rename data directory
91 if ($info{pgdata}) {
92 my $newpgdata = strrepl ($info{pgdata}, $oldcluster, $newcluster);
93 if ($info{pgdata} ne $newpgdata) {
94 rename $info{pgdata}, $newpgdata or
95 error "Could not rename data directory $info{pgdata}: $!";
96 PgCommon::set_conf_value $version, $newcluster, 'postgresql.conf',
97 'data_directory', $newpgdata;
98 }
99 }
100
101 # rename stats_temp_directory
102 my $statstempdir = $info{config}->{stats_temp_directory};
103 if ($statstempdir) {
104 my $newstatstempdir = strrepl ($statstempdir, $oldcluster, $newcluster);
105 if ($statstempdir ne $newstatstempdir) {
106 PgCommon::set_conf_value $version, $newcluster, 'postgresql.conf',
107 'stats_temp_directory', $newstatstempdir;
108 if (-d $statstempdir) {
109 rename $statstempdir, $newstatstempdir or
110 error "Could not rename stats temp directory $statstempdir}: $!";
111 }
112 }
113 }
114
115 # rename old log files
116 my $logdir = "/var/log/postgresql";
117 if (opendir LOG, $logdir) {
118 while (my $logfile = readdir LOG) {
119 next unless $logfile =~ /^(\Qpostgresql-$version-$oldcluster.log\E.*)/;
120 $logfile = $1; # untaint
121 my $f = strrepl ($logfile, $oldcluster, $newcluster);
122 rename "$logdir/$logfile", "$logdir/$f" or error "rename $logdir/$logfile: $!";
123 }
124 closedir LOG;
125 }
126
127 # notify systemd about the new cluster
128 if (not exists $ENV{'PG_CLUSTER_CONF_ROOT'} and -d '/run/systemd/system') {
129 if ($> == 0) {
130 system 'systemctl daemon-reload';
131 } elsif (-t 1) {
132 print "Warning: systemd does not know about the new cluster yet. Operations like \"service postgresql start\" will not handle it. To fix, run:\n";
133 print " sudo systemctl daemon-reload\n";
134 }
135 }
136
137 # start cluster if it was running before
138 if ($info{'running'}) {
139 print "Starting cluster $version $newcluster ...\n";
140 my @argv = ('pg_ctlcluster', $version, $newcluster, 'start');
141 error "Could not start cluster" if system @argv;
142 }
143
144 __END__
145
146 =head1 NAME
147
148 pg_renamecluster - rename a PostgreSQL cluster
149
150 =head1 SYNOPSIS
151
152 B<pg_renamecluster> I<version> I<oldname> I<newname>
153
154 =head1 DESCRIPTION
155
156 B<pg_renamecluster> changes the name of a PostgreSQL cluster, i. e. the name of
157 the config directory in /etc/postgresql/I<version>/ along with the data
158 directory in /var/lib/postgresql/I<version>/. Existing log files in
159 /var/log/postgresql/ are also renamed. The cluster is stopped and started for
160 the operation.
161
162 The following B<postgresql.conf> config options are updated to refer to the
163 changed path names: B<data_directory>, B<hba_file>, B<ident_file>,
164 B<external_pid_file>, B<stats_temp_directory>, B<cluster_name>.
165
166 =head1 OPTIONS
167
168 None.
169
170 =head1 SEE ALSO
171
172 L<pg_createcluster(8)>, L<pg_dropcluster(8)>, L<pg_lsclusters(1)>, L<pg_wrapper(1)>
173
174 =head1 AUTHOR
175
176 Christoph Berg L<E<lt>[email protected]<gt>>