Skip to content

Commit 23843d2

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 9d167bc commit 23843d2

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

doc/src/sgml/backup.sgml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -552,15 +552,16 @@ tar -cf backup.tar /usr/local/pgsql/data
552552
character in the command. The simplest useful command is something
553553
like:
554554
<programlisting>
555-
archive_command = 'cp -i %p /mnt/server/archivedir/%f &lt;/dev/null'
555+
archive_command = 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f' # Unix
556+
archive_command = 'copy "%p" "C:\\server\\archivedir\\%f"' # Windows
556557
</programlisting>
557558
which will copy archivable WAL segments to the directory
558559
<filename>/mnt/server/archivedir</>. (This is an example, not a
559560
recommendation, and might not work on all platforms.) After the
560561
<literal>%p</> and <literal>%f</> parameters have been replaced,
561562
the actual command executed might look like this:
562563
<programlisting>
563-
cp -i pg_xlog/00000001000000A900000065 /mnt/server/archivedir/00000001000000A900000065 &lt;/dev/null
564+
test ! -f /mnt/server/archivedir/00000001000000A900000065 &amp;&amp; cp pg_xlog/00000001000000A900000065 /mnt/server/archivedir/00000001000000A900000065
564565
</programlisting>
565566
A similar command will be generated for each new file to be archived.
566567
</para>
@@ -590,17 +591,19 @@ cp -i pg_xlog/00000001000000A900000065 /mnt/server/archivedir/00000001000000A900
590591
preserve the integrity of your archive in case of administrator error
591592
(such as sending the output of two different servers to the same archive
592593
directory).
594+
</para>
595+
596+
<para>
593597
It is advisable to test your proposed archive command to ensure that it
594598
indeed does not overwrite an existing file, <emphasis>and that it returns
595-
nonzero status in this case</>. We have found that <literal>cp -i</> does
596-
this correctly on some platforms but not others. If the chosen command
597-
does not itself handle this case correctly, you should add a command
598-
to test for pre-existence of the archive file. For example, something
599-
like:
600-
<programlisting>
601-
archive_command = 'test ! -f .../%f &amp;&amp; cp %p .../%f'
602-
</programlisting>
603-
works correctly on most Unix variants.
599+
nonzero status in this case</>.
600+
The example command above for Unix ensures this by including a separate
601+
<command>test</> step. On some Unix platforms, <command>cp</> has
602+
switches such as <option>-i</> that can be used to do the same thing
603+
less verbosely, but you should not rely on these without verifying that
604+
the right exit status is returned. (In particular, GNU <command>cp</>
605+
will return status zero when <option>-i</> is used and the target file
606+
already exists, which is <emphasis>not</> the desired behavior.)
604607
</para>
605608

606609
<para>

0 commit comments

Comments
 (0)