#!/usr/bin/perl #--------------------------------------------------------------------# # Simple Directory Mirror # Date Written: 29-Aug-2001 04:35:58 PM # Last Modified: 31-Aug-2001 12:36:17 PM # Author: Kurt Kincaid (sifukurt@yahoo.com) # # Makes backups of all files in a directory, and only copies the # file again if it has changed. It does not go through subdirectories. # This specifically was a feature I did not want. However, it # shouldn't be too difficult to add that feature if you are so # inclined. # # This is free software and may be redistributed and/or modified # under the same terms as Perl itself. #--------------------------------------------------------------------# use Digest::MD5 qw(md5_hex); use File::Copy; use strict; our ( $work_dir, $bu_dir, $file, $digest, $sleep_time ); our ( @files, @new_files, @deleted, %File ); #--------------------------------------------------------------------# # Configuration options #--------------------------------------------------------------------# $sleep_time = 300; # seconds $work_dir = '/kurt/work'; $bu_dir = '/kurt/perl/backup'; Files( $work_dir, \@files ); foreach $file ( @files ) { if ( -d $file ) { next } open (FILE, "$work_dir\/$file"); binmode(FILE); $File{$file} = Digest::MD5->new->addfile(*FILE)->hexdigest(); close (FILE); copy( "$work_dir\/$file", "$bu_dir\/$file" ); } for (;;) { sleep( $sleep_time ); Files( $work_dir, \@new_files ); @deleted = Compare( \@files, \@new_files ); foreach $file ( @deleted ) { unlink "$bu_dir\/$file"; delete $File{$file}; @files = keys %File; } foreach $file ( @new_files ) { if ( -d $file ) { next } open (FILE, "$work_dir\/$file"); binmode(FILE); $digest = Digest::MD5->new->addfile(*FILE)->hexdigest(); close (FILE); unless ( $digest eq $File{$file} ) { $File{$file} = $digest; copy( "$work_dir\/$file", "$bu_dir\/$file" ); } } } sub Files { my ( $file, $array ) = @_; opendir ITEMS, $file; @$array = grep !/^\./, readdir ITEMS; closedir ITEMS; chomp @$array; return @$array; } sub Compare { my ( $arrayref1, $arrayref2 ) = @_; unless ( defined $arrayref1 && defined $arrayref2 && @$arrayref1 > + 0 && @$arrayref2 > 0 ) { return undef; } my %seen; my @aonly; my $item; foreach $item (@$arrayref2) { $seen{$item} = 1 } foreach $item (@$arrayref1) { unless ( $seen{$item} ) { push ( @aonly, $item ); } } return @aonly; }

In reply to Simple Directory Mirror by sifukurt

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.