Blue-Maned Hawk
Blue-Maned Hawk

Reputation: 175

Archive::Tar module backs up files and folders, but not files in folders

Basic Problem.

I want to use Perl's Archive::Tar module to backup things. I am tying to use Archive::Tar to back up files and folders and files in folders. With my current code, the first two work, but the third doesn't.

Steps to reproduce:

  1. Create a sample directory.
  2. In the sample directory, create two directories and a .txt file.
  3. Using vim, write whatever in the .txt file.
  4. In one of the directories in the sample directory, repeat steps two and three.
  5. In the sample directory, create a perl script.
  6. Copy the code below into the script.
  7. Run the script. In the box, type the names of the .txt file and the two directories.
  8. Press the big compress button.
  9. Open the new .tbz file in the compressed directory.

Code.

use strict; use warnings;

use Archive::Tar;
use Tk;

my $mw = MainWindow -> new;
$mw -> Label ( -text => "Please type the files you wish to backup, separated by spaces." ) -> pack;

my $inputEntry = $mw -> Entry ( -width => 30 );
$inputEntry -> pack;

$mw -> Button ( -text => "Compress!", -command => sub { compress() } ) -> pack;

MainLoop;

sub compress {
    my $input = $inputEntry -> get;
    my @input;
    unless ( $input !~ m/ / ) {
        @input = split ( m/ /, $input );
    } else {
        @input = ( $input );
    }
    Archive::Tar -> create_archive ( "TEST.tbz", COMPRESS_BZIP, @input )    
}

Upvotes: 1

Views: 87

Answers (1)

Dan
Dan

Reputation: 130

Your steps to reproduce aren't clear and more examples could be helpful, but your script seems to work fine for me. I was able to add both folders and files, and files within folders successfully to the archive. For each file within a folder, I had to type the entire relative path eg. 'sample/file1.txt'.

You may be looking for a way to add the files within a folder automatically. Something like this would do the trick:

use strict; use warnings;

use Archive::Tar;
use Tk;

my $mw = MainWindow -> new;
$mw -> Label ( -text => "Please type the files you wish to backup, separated by spaces." ) -> pack;

my $inputEntry = $mw -> Entry ( -width => 30 );
$inputEntry -> pack;

$mw -> Button ( -text => "Compress!", -command => sub { compress() } ) -> pack;

MainLoop;

sub compress {
    my $input = $inputEntry -> get;
    my @input; 
    my @dirfiles;
    unless ( $input !~ m/ / ) {
        @input = split ( m/ /, $input );
    } else {
        @input = ( $input );
    }

    foreach(@input) { if(-d $_) { push(@dirfiles,glob "'${_}/*'"); } }
    push(@input,@dirfiles);

    Archive::Tar -> create_archive ( "TEST.tbz", COMPRESS_BZIP, @input )
}

I think there are still remaining issues, such as: This script appears to fail on files/folders with spaces in their names, even when escaped or quoted.

Upvotes: 2

Related Questions