File: backup.pl - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

#! /usr/bin/perl

use File::Copy;

my  $from = "/var/local/backup/from/";
my  $to = "/var/local/backup/to/";

opendir (H1, $from) || die ("error: cannot read input directory");
opendir (H2, $to) || die ("error: cannot read output directory");

my  @files1 = readdir (H1);
my  %files2 = map { $_ => 1 } readdir (H2);

closedir (H1);
closedir (H2);

foreach $file (@files1)
{
    if ($file != '.' && $file != '..')
    {
        my  $i = 0;

        while ($files2{"$file.$i"})
        {
            $i = $i + 1;
        }

        if ($i == 0 || -s "$from/$file" != -s ("$to/$file." . ($i - 1)))
        {
            copy ("$from/$file", "$to/$file.$i");
            print "backup $file as $file.$i\n";
        }
    }
}