Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Appropriate usage of IO::CaptureOutput for a mysqldump and gzip?

by loothi (Novice)
on Oct 15, 2012 at 16:19 UTC ( #999113=perlquestion: print w/ replies, xml ) Need Help??
loothi has asked for the wisdom of the Perl Monks concerning the following question:


Messing about with this module IO::CaptureOutput.

I want to do a mysqldump and redirect the raw SQL through gzip. I used to do this with the system command

system("mysqldump --add-drop-table -h $global_host -u $user -p'".$pass."' $db_name  | gzip > $backup_file")

...but if the mysqldump fails, I was still getting a success exit code so I thought I'd split it into the two commands so it's more robust for debugging. Is this method below a decent way to do this? I haven't written any code in ages and am wondering if anyone can check this for me (just the concept, as it's just a snippet and I'd use strict etc in the real script).

#!/usr/bin/perl use IO::CaptureOutput qw/capture_exec capture/; sub mysqldump { system('mysqldump --add-drop-table -h localhost -u myuser -pmypass + mydb '); } capture \&mysqldump, undef, \$stderr, 'outfile.sql'; if ($stderr) { print "FAILED: $stderr\n"; }else{ @args = ('gzip','outfile.sql'); ($stdout, $stderr, $success, $exit_code) = capture_exec(@args) +; if (!$success) { print "Failed gzip: $stderr\n"; } else { print "Success gzip\n"; } }
ta. Loothi.

Comment on Appropriate usage of IO::CaptureOutput for a mysqldump and gzip?
Select or Download Code
Re: Appropriate usage of IO::CaptureOutput for a mysqldump and gzip?
by remiah (Hermit) on Oct 15, 2012 at 19:02 UTC

    Hello loothi.

    I am afraid for the database size. Dumps becomes very large and the buffer also becomes large. How about using --result-file and --log-error option of mysqldump?

    For capturing output of other programes, I see Capture::Tiny examples here at monk many times. I would like to give it a try, but, not yet.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://999113]
Approved by herveus
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2014-07-10 11:34 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (207 votes), past polls