Monks, I have updated my script. It now supports
the following options:

Submitting your own configuration file as parameter.
Including directories to be synced.
Excluding directories to be synced.
Automatically syncing of subdirs in dirs
unless they are excluded.
Working with incremental backups.
Creating ISO images using mkisofs.
Burning ISO images using NeroCmd (Windows)
Writing a logfile which is also burned or copied.


The configuration file has the following format:
inc - <dirname> #Directory(and subs) to be synced.
exc - <dirname> #Directory(and subs) not to be synced.
opt - <option> = <value> #option to set in program.


It is probably still horrid code, but it does what I
want it to do. I realize that there are already programs
that can do this, perhaps even better and with more
options, but when I will reuse code instead of writing
it, I will not learn. I think a carpenter will not
master his trade without actually carpenting,
and mastering this trade is my goal. Still I thank
all those who supported my endless questions as well
as giving tips, hints and advice. Thank you.

Regards,


Detonite

#!C:/PERL/BIN/PERL.EXE -w use strict; use warnings; use File::Copy; use File::Path; use Date::Simple; ################################################# # Sync.pl # # # # Wordt gebruikt om dagelijks directories met # # files synchroon te houden # # tussen de server en een andere opslag. # # # # First draft : 17-08-04 # # # # Last update : 19-08-04 09:00 am # ################################################# ####### GLOBAL VARS ######### my $dest = 'c:/'; my @dirstack = (); my @excdirs = (); my ($totalsize,$copied) = ('0','0'); my $currfile = ''; my $vandaag = Date::Simple->new; my (@errmsg,@filemsg,@dirmsg) = ((),(),()); my %option = (); $vandaag = $vandaag->format("%d-%m-%y"); my $logfile = ">>./SyncLog_$vandaag.txt"; open LOG, $logfile or die "Kan logbestand [./$logfile] niet openen : $ +!\n"; select LOG; $option{cfgfile} = $ARGV[1] unless !$ARGV[1]; #parameter? instellen + dan my $cfgfile = $option{cfgfile}; $cfgfile = "./Files.txt" unless $cfgfile; #geen parameter? pak +standaard. open DIRLIST,$cfgfile or die "Kan configuratiebestand [$cfgfile] niet +openen : $!\n"; &parseOpt; #Verwerk opties in $cfgfile $dest = $option{destdir} unless !$option{destdir}; #Stel destination d +irectory in seek(DIRLIST,0,0); #reset DIRLIST fh nadat parseOpt er doorheen gelope +n is unless (!$option{incremental} or !$option{tempdir}) { rmtree($option{tempdir}); # deltree tempdir $dest = $option{tempdir}; } while(<DIRLIST>) { chomp; syncDir(parseCfg($_)); } close DIRLIST; while(scalar(@dirstack)) { my $dir = pop(@dirstack); syncDir($dir) unless(!$dir); } print("*****************BACKUP LOG VOOR $vandaag*****************\n\n\ +n"); print "*************************** OPTIES **************************\n +"; foreach my $key(keys %option) {print "$key = $option{$key}\n";} print "*************************************************************\n +\n"; print "******************** DIRECTORY MELDINGEN ********************\n +"; while(scalar(@dirmsg)) { my $msg = pop(@dirmsg); print "$msg\n" unless(!$msg); } print "*************************************************************\n +\n"; print "********************** FILE MELDINGEN ***********************\n +"; while(scalar(@filemsg)) { my $msg = pop(@filemsg); print "$msg\n" unless(!$msg); } print "*************************************************************\n +\n"; print "********************** FOUT MELDINGEN ***********************\n +"; while(scalar(@errmsg)) { my $msg = pop(@errmsg); print "$msg\n" unless(!$msg); } print "*************************************************************\n +\n"; print("\n$totalsize bytes gekopieerd.\n$copied files gekopieerd."); close LOG; copy($logfile,$option{tempdir}.$logfile); ### Bereken totaal aantal MB gekopieerd, maak image en brand op CD $totalsize /= 1000000; if($copied and !($totalsize > 4600)) #files gekopieerd? iso maken wann +eer niet groter dan dvd { system($option{isoprog}.$option{tempdir}); system(qq[$option{burnprog} --write --real --drivename $option{burndr +ive} --image $option{isoname} --speed $option{writespeed} --disable_e +ject --force_erase_cdrw]); } exit 0; ### Verwerk de configuratiefile. Zet excluded directories ### in @excdirs en geef included directories mee aan &syncDir. sub parseCfg { return if (!$_[0] or /opt/); if (substr($_[0], -1) ne "/") {$_[0] .= "/";} if(/exc/) { $_[0] =~ s/exc - //; push(@excdirs,$_[0]); return; } $_[0] =~ s/inc - //; return $_[0]; } ### Verwerk de configuratiefile. Zet opties in %option. sub parseOpt { while(<DIRLIST>) { chomp; my $dir = $_; if($dir =~ s/opt - //) { my ($name,$value) = split(/ = /,$dir); $option{$name} = $value unless (!$name or !$value); } } } ### Hoofdsub : Open de meegegeven directory. Wanneer er een subdir wor +dt gevonden, ### zet deze in @dirstack. Wanneer er een bestand wordt gevonden, kopi +eer deze ### indien hij niet al bestaat en gelijk is. Wanneer incremental true +is, kopieer ### een bestand wanneer het 31 dagen oud (of jonger) is. sub syncDir { my $dir = $_[0]; return if(!$dir); if( grep { $dir eq $_ } @excdirs) {push @dirmsg,"Directory [$dir] is excluded.";} elsif (! -e $dir) {push @dirmsg, "Directory [$dir] bestaat niet." ;} else { push @dirmsg, "Directory [$dir] wordt geopend..."; $dir .= '/' unless substr($dir, -1) eq "/"; my $currdir = $dir; opendir DIR, $dir or push @errmsg, "Fout bij openen van directory [$ +dir] : $!\n"; foreach my $file(readdir DIR) { if(-d $currdir.$file) #is een directory { $file .= '/' unless substr($file, -1) eq "/"; if( grep { $currdir.$file eq $_ } @excdirs) #staat de dir in black +list? {push @dirmsg,"Directory [$currdir$file] is excluded.";} elsif(!($file =~ /(\.)|(\.\.)/)) { push(@dirstack,$currdir.$file); #zet er bij in de stack... push @dirmsg, "Directory [$currdir$file] gevonden, in de stack ge +zet."; } else {push @dirmsg, "Directory [$currdir$file] is een ongeldige direct +ory.";} } else #is een bestand { my $ncdir = $currdir; $ncdir =~ s/^[a-z]:\///; #haal evt drivenotatie weg if(-e $dest.$ncdir.$file) #bestaat al in destination dir { if(!( (-M $currdir.$file) == (-M $dest.$ncdir.$file)) && !( (-s $ +currdir.$file) == (-s $dest.$ncdir.$file) ) ) # Gelijk in leeftijd en + grootte? { #bestand is gelijk unlink($dest.$ncdir.$file); #Verwijder indien bestand reeds best +aat. copy($currdir.$file,$dest.$ncdir.$file) or push @errmsg, "Fout b +ij kopieren van bestand [$currdir$file] naar [$dest$ncdir$file] : $!\ +n"; push @filemsg, "Bestand [$dest$ncdir$file] is overschreven door +[$currdir$file]."; $totalsize += -s $currdir.$file; $copied++; # Werk $totalsize en + $copied bij } else #bestand is ongelijk {push @filemsg, "Bestand [$currdir$file] is gelijk aan [$dest$nc +dir$file].";} } else # bestaat nog niet in destination dir { mkpath($dest.$ncdir) unless -e $dest.$ncdir; if($option{incremental} && $option{tempdir}) { if(-M $currdir.$file <= $option{incdays}) { copy($currdir.$file,$dest.$ncdir.$file) or push @errmsg, "Fout +bij kopieren van bestand [$currdir$file] naar [$dest$ncdir$file] : $! +\n"; push @filemsg, "Bestand [$currdir$file] is ".(-M $currdir.$file +)." dagen geleden gewijzigd."; }#gewijzigd sinds laatste volledige backup } else {copy($currdir.$file,$dest.$ncdir.$file) or push @errmsg, "Fout +bij kopieren van bestand [$currdir$file] naar [$dest$ncdir$file] : $! +\n";} push @filemsg, "Bestand [$currdir$file] is gekopieerd naar [$dest +$ncdir$file]."; $totalsize += -s $currdir.$file; $copied++; # Werk $totalsize en +$copied bij } } } closedir DIR; } }

In reply to Directory Syncer by jkva

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.