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; } }

Replies are listed 'Best First'.
Re: Directory Syncer
by Aristotle (Chancellor) on Aug 18, 2004 at 12:00 UTC

    Sorry for being a party pooper :-), but — does this do anything rsync doesn't already do better?

    Makeshifts last the longest.

      If you ever want to synchronize in both directions, Unison works really well and has a few other features that rsync doesn't.
      Yep... but until 2 hours ago rsync was yet unknown to me :)
      Thanks for the tip though.

      Detonite
      does this do anything rsync doesn't already do better?

      Hmm. Does rsync provide status/error messages in Dutch?

        No, it doesn't. It isn't even gettextized, unfortunately.

        Retrofitting gettext support and writing a Dutch translation would be a worthy way to invest one's effort if that is an important criteria. :-)

        Makeshifts last the longest.

      To throw another solution on the fire, robocopy works wonders on the Windows platform.

      Existing solutions aside, kudos on the learning initative. I've reinvented many a wheel myself for the sake of learning :)

      -jbWare

        Of course, as I have said many times and will say again: there's nothing wrong with reinventing wheels for the sake of learning, but a lot with subsequently using them in production unless you actually intend to spend the time and effort to invent a rounder wheel than what's already there. That means knowing how existing wheels solve the problem, understanding what issues they have, getting your code out to as many people as possible for testing, and sticking around fixing bugs as they come up. Only then can you be confident that your code is correct and robust enough to withstand abuse from many different users with lots of different needs and scenarios.

        Of course, it is quite enlightening to invent your own wheel, then read an existing one for comparison. There'll be checks and code for all these strange conditions and edge cases you never even thought of. I learned a number of valuable lessons that way.

        And if you do that three or four times, you'll cease trying to invent your own wheels in the first place and start giving advice such as what I have ended up giving. :-)

        Makeshifts last the longest.