From owner-freebsd-questions@FreeBSD.ORG Fri Aug 17 12:42:53 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 7E99A16A417 for ; Fri, 17 Aug 2007 12:42:53 +0000 (UTC) (envelope-from xfb52@dial.pipex.com) Received: from astro.systems.pipex.net (astro.systems.pipex.net [62.241.163.6]) by mx1.freebsd.org (Postfix) with ESMTP id 4AD1713C45D for ; Fri, 17 Aug 2007 12:42:53 +0000 (UTC) (envelope-from xfb52@dial.pipex.com) Received: from [192.168.23.2] (62-31-10-181.cable.ubr05.edin.blueyonder.co.uk [62.31.10.181]) by astro.systems.pipex.net (Postfix) with ESMTP id 3FE7FE0001E0; Fri, 17 Aug 2007 13:42:51 +0100 (BST) Message-ID: <46C597C7.3000300@dial.pipex.com> Date: Fri, 17 Aug 2007 13:42:47 +0100 From: Alex Zbyslaw User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-GB; rv:1.7.13) Gecko/20061205 X-Accept-Language: en MIME-Version: 1.0 To: Eric Crist , Questions User References: <1A4196AD-1749-40BC-8F41-3F4E4715096C@gmail.com> <46C47129.4090206@u.washington.edu> In-Reply-To: <46C47129.4090206@u.washington.edu> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: Subject: Re: mysqldump/gzip shell scripting question... X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 17 Aug 2007 12:42:53 -0000 Eric Crist wrote: > First off, I don't care if you send example in perl, php, or sh, but > we're not a python shop here, so those recommendation will not be > useful... > > I'm trying to write a shell script that scans our databases for > tables starting with archive_ which are created by other > scripts/departments, etc. This script needs to perform a mysqldump of > that table, and then gzip it. It's MUCH quick to pipe directly to > gzip, than perform the dump, then gzip that. The problem is, this > table to filesystem dump is also going to drop those archive_* > tables. We would like to know that the mysqldump worked before we do > this. The problem we're having, as I'm sure others have run into (at > least according to Google), is that a command such as the following > leaves no apparent easy way to capture the exit status of the > mysqldump command: > > # mysqldump -u $USER -p$PASS $DBHOST $DATABASE $TABLE | gzip > > $TABLE.sql.gz > This rough perl should do the trick: open (MYSQL, "/tmp/fail|") || die "mysqldump failed"; open (GZIP, "|gzip > /tmp/test.gz") || die "gzip failed"; while ($ret = read MYSQL, $buf, 4096) { print GZIP $buf || die "gzip write failed: $!"; } die "gzip write failed: $!" if (!defined($ret)); close(MYSQL) || die "mysql close failed"; close (GZIP) || die "gzip close failed"; and for testing /tmp/fail was executable and contained: #!/bin/sh - cat /etc/motd /etc/motd /etc/motd /etc/motd /etc/motd /etc/motd /etc/motd /etc/motd exit 1 With exit 1, perl dies on the MYSQL close, with exit 0 perl exits normally. If this works for you then /tmp/fail gets replaced with your mysqldump command and you'll need some params to pass in the name of the gzipped file. You can play with sysread instead of read, and vary the buffer size. No idea how it compares for speed to straight shell piping to gzip. hth, --Alex