Skip to content

Commit 26996cf

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 6c320b8 commit 26996cf

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

doc/src/sgml/backup.sgml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ tar -cf backup.tar /usr/local/pgsql/data
530530
character in the command. The simplest useful command is something
531531
like
532532
<programlisting>
533-
archive_command = 'cp -i %p /mnt/server/archivedir/%f &lt;/dev/null'
533+
archive_command = 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f' # Unix
534+
archive_command = 'copy "%p" "C:\\server\\archivedir\\%f"' # Windows
534535
</programlisting>
535536
which will copy archivable WAL segments to the directory
536537
<filename>/mnt/server/archivedir</>. (This is an example, not a
@@ -562,17 +563,19 @@ archive_command = 'cp -i %p /mnt/server/archivedir/%f &lt;/dev/null'
562563
preserve the integrity of your archive in case of administrator error
563564
(such as sending the output of two different servers to the same archive
564565
directory).
566+
</para>
567+
568+
<para>
565569
It is advisable to test your proposed archive command to ensure that it
566570
indeed does not overwrite an existing file, <emphasis>and that it returns
567-
nonzero status in this case</>. We have found that <literal>cp -i</> does
568-
this correctly on some platforms but not others. If the chosen command
569-
does not itself handle this case correctly, you should add a command
570-
to test for pre-existence of the archive file. For example, something
571-
like
572-
<programlisting>
573-
archive_command = 'test ! -f .../%f &amp;&amp; cp %p .../%f'
574-
</programlisting>
575-
works correctly on most Unix variants.
571+
nonzero status in this case</>.
572+
The example command above for Unix ensures this by including a separate
573+
<command>test</> step. On some Unix platforms, <command>cp</> has
574+
switches such as <option>-i</> that can be used to do the same thing
575+
less verbosely, but you should not rely on these without verifying that
576+
the right exit status is returned. (In particular, GNU <command>cp</>
577+
will return status zero when <option>-i</> is used and the target file
578+
already exists, which is <emphasis>not</> the desired behavior.)
576579
</para>
577580

578581
<para>

0 commit comments

Comments
 (0)