Wouldn't it be easier just to use "tar" (the GNU version is the accepted standard, and it is available for all common platforms).

If a perl wrapper makes it more comfortable for you, here's a version of your script that is functionally equivalent(*) to the OP, but is a lot easier (and will run a lot faster, without using very much memory at all):

#!/usr/bin/perl -w =head1 NAME backup.pl - Yet Another Script for making backups =head1 SYNOPSIS backup.pl --bakdir=s --backup=s [--ignorefile=s] Options: --bakdir - where to look for and store backup files --backup - what directory to backup --ignorefile - file which lists what subdirs not to backup =cut use strict; use warnings; use English; use Getopt::Long; use Pod::Usage; use File::Path; my $bakdir = ''; my $backup = ''; my $ignorefile = ''; GetOptions( 'bakdir=s' => \$bakdir, 'backup=s' => \$backup, 'ignorefile=s' => \$ignorefile, ); $bakdir ||= "."; $backup ||= "."; if ( $bakdir eq $backup ) { warn "We should not create a backup of $backup in $bakdir\n"; pod2usage(1); exit; } eval {mkpath($bakdir)}; if ($@) { warn "Unable to find or create directory $bakdir\n$@\n"; pod2usage(1); exit; } # create a tar.gz archive with a unique filename my @t = reverse((localtime)[0..5]); $t[0]+=1900; $t[1]++; my $t = sprintf("%4u-%02u-%02u-%02u-%02u-%02u",@t); my $newbackup = "$bakdir/$t.tar.gz"; my @cmd = qw/tar cz/; push @cmd, '-X', $ignorefile if ( length( $ignorefile ) and -f $ignorefile and -r _ ); push @cmd, '-f', $newbackup, $backup; exec @cmd;

(* footnote: it's not exactly equivalent -- I felt compelled to add a couple checks on the ARGV option values; more could still be done in this regard...)

For some reason, pod2usage did not work for me as expected, but with proper command-line args, this version does accomplish everything that the OP set out to do.

(updated code to add one more check on the "ignorefile" arg, and to remove the "debug" option, since it's not really needed here)


In reply to Re: backup script runs out of memory by graff
in thread backup script runs out of memory by gri6507

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.