Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?


by xiphias (Initiate)
on Jun 27, 2002 at 03:23 UTC ( #177618=sourcecode: print w/ replies, xml ) Need Help??

Category: Utility Scripts
Author/Contact Info Tristan Plumb
Description: Backs up files using tar into dirs in backlist.txt. Quite simple

# -- backup files listed in `backlist.txt'.

#Uses tar -c $addargs -f $filename @files

#File Format:

# Dosen't check if the files are present but just spits out 
# an error message.

$debug = 0;        #When $debug = 1 then it print debuging info

open BACK_LIST, "< backlist.txt" or
  die "Couldn't open file that lists the items to backup: backlist.txt

#Test the basedir
$basedir = <BACK_LIST>;
chomp $basedir;
if ($debug == 1) {
  print "BaseDIR: $basedir\n";

unless (-d $basedir) {
  die "The first line of backlist.txt must be directory."

#Get date and create todays dir
@time_date = localtime();                #$day_of_year = time_date(7)

if ($debug == 1) {
  print "$basedir/$time_date[7]\n";

mkdir "$basedir/$time_date[7]", 0740 or
  die "Cannot make dir: $basedir/$time_date[7]\n";

$addargs = <BACK_LIST>;
chomp $addargs;

$filename = <BACK_LIST>;
chomp $filename;

@files = <BACK_LIST>;
chomp @files;

if ($debug == 1) {
  print "@files\n";
  print "tar -c $addargs -f $basedir/$time_date[7]/$filename @files\n"

system("tar -c $addargs -f $basedir/$time_date[7]/$filename @files");

#>>>>>>>>>>>>>>Close the file<<<<<<<<<<<<<
close BACK_LIST;

Comment on backfiles
Download Code
Replies are listed 'Best First'.
(jeffa) Re: backfiles
by jeffa (Bishop) on Jun 27, 2002 at 19:52 UTC
    No CPAN modules? This version tars and gzips and allows the user to override a lot of default names. Instead of placing all arguments in another file, this one accepts long style arguments. Since the number of files to archive could be large, it does accept a textfile containing the list of files to archive.
    #!/usr/bin/perl -w use strict; use Archive::Tar; use Getopt::Long; use Pod::Usage; use vars qw($base $dir $name $comp $list @file $debug $help); GetOptions( 'base=s' => \$base, 'dir=s' => \$dir, 'name=s' => \$name, 'comp=s' => \$comp, 'list=s' => \$list, 'file=s' => \@file, 'debug!' => \$debug, 'help|?' => \$help, ); pod2usage(-verbose=>1) if $help; pod2usage(-verbose=>2) unless $name and $list or @file; $base ||= '.'; $comp ||= 9; $dir = (localtime)[7]; print "BaseDIR: $base\n" if $debug; die "$base is not a directory" unless -d $base; if ($list) { open FH,'<',$list or die "couldn't open $list: $!"; chomp(@file = <FH>); } my $tardir = "$base/$dir"; print "archive is $tardir/$name\n" if $debug; print "files are @file\n" if $debug; if (-d "$tardir") { print "$tardir already exists\n" if $debug; } else { mkdir "$tardir", 0740 or die "cannot mkdir $tardir"; print "created $tardir\n" if $debug; } my $tar = Archive::Tar->create_archive("$tardir/$name",$comp,@file); __END__ =head1 NAME -- backup files listed in text file or command line =head1 SYNOPSIS -list|-file [-dir -name -args -debug -help] Options: -name required - name of tar file: -file required - specify files to archive or ... -list text file containing list of files to srchive -base dir to store tar directory: default is . -dir dir to store tar file: day of year is default -comp compression level: default is 9 -debug prints debugging messages -help this message Examples: -list=backlist.txt -name=foo.tar -file=foo.txt -file=bar.txt -name=foo.tar =cut

Back to Code Catacombs

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://177618]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2015-10-06 17:40 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (156 votes), past polls