Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T5PSS-0005TU-P5 for pgsql-docs@postgresql.org; Sat, 25 Aug 2012 23:08:00 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T5PSR-0003UA-0q for pgsql-docs@postgresql.org; Sat, 25 Aug 2012 23:08:00 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1T5PSN-0004ME-60; Sat, 25 Aug 2012 19:07:55 -0400 Date: Sat, 25 Aug 2012 19:07:55 -0400 From: Bruce Momjian To: Peter Eisentraut Cc: Josh Kupershmidt , Greg Smith , Euler Taveira de Oliveira , pgsql-docs@postgresql.org Subject: Re: somewhat wrong archive_command example Message-ID: <20120825230755.GE10814@momjian.us> References: <1316524671.9044.12.camel@fsopti579.F-Secure.com> <4E794FB5.80400@2ndQuadrant.com> <1316613536.14119.6.camel@fsopti579.F-Secure.com> <4E7A6D0D.4060106@2ndQuadrant.com> <4E7A887F.8010908@timbira.com> <20120816011347.GF8353@momjian.us> <502EA0D0.10004@gmx.net> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="PEIAKu/WMn1b1Hv9" Content-Disposition: inline In-Reply-To: <502EA0D0.10004@gmx.net> User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -2.1 (--) X-Archive-Number: 201208/42 X-Sequence-Number: 7435 --PEIAKu/WMn1b1Hv9 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Aug 17, 2012 at 03:51:44PM -0400, Peter Eisentraut wrote: > On 8/15/12 9:13 PM, Bruce Momjian wrote: > >>Few more suggestions/nitpicks: > >> 1.) IMO it's more logical to put the test for whether the $ARCHIVE > >>directory exists before the test whether ${ARCHIVE}/${FILE} exists. > >> 2.) I think the error code reporting here is not sound: > >> > >>cp ${FULLPATH} ${ARCHIVE}/${FILE} > >>if [ $? -ne 0 ] ; then > >> echo $0 Archive copy of ${FILE} failed with error $? >&2 > >> > >>at least on my OS X machine, that echo produces a message like > >>"./local_backup_script.sh Archive copy of failed with error 0", I > >>guess since $? gets reset to 0 after that if-statement. You can use a > >>temporary variable like $ERRCODE=$? to get around this. > > > >I have made all the suggestions posted and would like to add the > >attached script to our documentation as a simple example. > > Btw., is anyone else concerned about using plain cp for this? If > the cp fails half-way, it leaves a partial file around, but > subsequent file existence checks will find the file OK and skip it. > > I have occasionally used some combination of mktemp + cp + mv, which > seems to work around this problem. I am unclear why the script returns success if the file already exists --- seems if the file exists, we should throw an error, like we have always done with cp -i < /dev/null. Updated version attached. Another option in this case would be to re-issue the copy. -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + It's impossible for everything to be true. + --PEIAKu/WMn1b1Hv9 Content-Type: application/x-sh Content-Disposition: attachment; filename="local_backup_script.sh" Content-Transfer-Encoding: quoted-printable #!/bin/sh=0A=0A# archive_command script for Standalone Hot Backup=0A=0A# SE= T THIS VARIABLE TO YOUR ARCHIVE DIRECTORY=0AARCHIVE=3D"/mnt/pg_archive"=0A= =0AFULLPATH=3D"$1"=0AFILE=3D"$2"=0A=0Aif [ ! -d "$ARCHIVE" ] ; then=0A ech= o "$0: Archive directory does not exist" 1>&2=0A exit 1=0Afi=0A=0A# 'cp' = could fail and leave a partial file in place=0Acp "$FULLPATH" "$ARCHIVE/$FI= LE"=0A=0ARET=3D"$?"=0A=0Aif [ "$RET" -ne 0 ] ; then=0A echo "$0: Archive = copy of $FILE failed with return code $RET" 1>&2=0Afi=0A=0Aexit $RET=0A --PEIAKu/WMn1b1Hv9--