Skip to content

Commit a2624c6

Browse files
committed
Don't use "cp -i" in the example WAL archive_command.
This is a dangerous example to provide because on machines with GNU cp, it will silently do the wrong thing and risk archive corruption. Worse, during the 9.0 cycle somebody "improved" the discussion by removing the warning that used to be there about that, and instead leaving the impression that the command would work as desired on most Unixen. It doesn't. Try to rectify the damage by providing an example that is safe most everywhere, and then noting that you can try cp -i if you want but you'd better test that. In back-patching this to all supported branches, I also added an example command for Windows, which wasn't provided before 9.0.
1 parent ddef31c commit a2624c6

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

doc/src/sgml/backup.sgml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ tar -cf backup.tar /usr/local/pgsql/data
579579
character in the command. The simplest useful command is something
580580
like:
581581
<programlisting>
582-
archive_command = 'cp -i %p /mnt/server/archivedir/%f &lt;/dev/null' # Unix
582+
archive_command = 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f' # Unix
583583
archive_command = 'copy "%p" "C:\\server\\archivedir\\%f"' # Windows
584584
</programlisting>
585585
which will copy archivable WAL segments to the directory
@@ -588,7 +588,7 @@ archive_command = 'copy "%p" "C:\\server\\archivedir\\%f"' # Windows
588588
<literal>%p</> and <literal>%f</> parameters have been replaced,
589589
the actual command executed might look like this:
590590
<programlisting>
591-
cp -i pg_xlog/00000001000000A900000065 /mnt/server/archivedir/00000001000000A900000065 &lt;/dev/null
591+
test ! -f /mnt/server/archivedir/00000001000000A900000065 &amp;&amp; cp pg_xlog/00000001000000A900000065 /mnt/server/archivedir/00000001000000A900000065
592592
</programlisting>
593593
A similar command will be generated for each new file to be archived.
594594
</para>
@@ -617,18 +617,19 @@ cp -i pg_xlog/00000001000000A900000065 /mnt/server/archivedir/00000001000000A900
617617
preserve the integrity of your archive in case of administrator error
618618
(such as sending the output of two different servers to the same archive
619619
directory).
620+
</para>
621+
622+
<para>
620623
It is advisable to test your proposed archive command to ensure that it
621624
indeed does not overwrite an existing file, <emphasis>and that it returns
622-
nonzero status in this case</>. On many Unix platforms, <command>cp
623-
-i</> causes copy to prompt before overwriting a file, and
624-
<literal>&lt; /dev/null</> causes the prompt (and overwriting) to
625-
fail. If your platform does not support this behavior, you should
626-
add a command to test for the existence of the archive file. For
627-
example, something like:
628-
<programlisting>
629-
archive_command = 'test ! -f /mnt/server/archivedir/%f &amp;&amp; cp %p /mnt/server/archivedir/%f'
630-
</programlisting>
631-
works correctly on most Unix variants.
625+
nonzero status in this case</>.
626+
The example command above for Unix ensures this by including a separate
627+
<command>test</> step. On some Unix platforms, <command>cp</> has
628+
switches such as <option>-i</> that can be used to do the same thing
629+
less verbosely, but you should not rely on these without verifying that
630+
the right exit status is returned. (In particular, GNU <command>cp</>
631+
will return status zero when <option>-i</> is used and the target file
632+
already exists, which is <emphasis>not</> the desired behavior.)
632633
</para>
633634

634635
<para>

0 commit comments

Comments
 (0)