Hello everyone.

I wrote a script which backs up certain files and folders which get modified often. Some of the files or files within folders do not get modified as often as I back them up. I was wondering if checking the last modified time and ignoring those which have the same last modified time, would be faster than just overwriting.

Original script which overwrites

#!/usr/bin/perl use strict; use warnings; use File::Copy qw(copy); use File::Mirror qw(mirror); use Try::Tiny; local $\ = "\n"; if (-e "J:/") { while (<DATA>) { my ($source,$destination) = split(/\|/,$_); chomp($source,$destination); print "Copying $source to $destination"; # I guess at one time the copy failed and made the script die, so +I added # Tiny::Try to keep the script from dying. try { if (-f $source) { copy($source,$destination); } elsif (-d $source) { mirror($source,$destination); } else { print "What did you do wrong?"; } } catch { print "Couldn't copy $source to $destination"; } } } else { print "Insert the thumb drive!"; } __DATA__ C:/Documents and Settings/me/My Documents/home/checkbook2.xls|J:/My Do +cuments/home/checkbook2.xls C:/Documents and Settings/me/Local Settings/Application Data/Microsoft +/Outlook/Outlook.pst|J:/application data/Outlook/Outlook.pst C:/Documents and Settings/me/My Documents/gaming|J:/My Documents/gamin +g C:/Documents and Settings/me/My Documents/fantasy|J:/My Documents/fant +asy C:/Documents and Settings/me/Application Data/Notepad++/stylers.xml|J: +/application data/Notepad++/stylers.xml C:/Documents and Settings/me/Application Data/HexChat|J:/application d +ata/HexChat

Script which checks last modified time (untested)

#!/usr/bin/perl use strict; use warnings; use File::Copy qw(copy); use File::Mirror qw(mirror recursive); use Try::Tiny; # return the last modified time sub get_mod_time { my $file = shift; my @stats = stat($file); my $mod = $stats[9]; return $mod; } # compare the two files' last modified times and return false or true sub same_mod_time { my ($src_file,$dst_file) = @_; my $same_mod = get_mod_time($src_file) != get_mod_time($dst_file) ? +0 : 1; return $same_mod; } local $\ = "\n"; if (-e "J:/") { while (<DATA>) { my ($source,$destination) = split(/\|/,$_); chomp($source,$destination); print "Copying $source to $destination"; # I guess at one time the copy failed and made the script die, so +I added # Tiny::Try to keep the script from dying. try { if (-f $source) { copy($source,$destination) if same_mod_time($source,$destinati +on) == 0; } elsif (-d $source) { # I had to change "mirror" to "recursive" to check for the sam +e mod time. recursive { copy($_[0],$_[1]) if same_mod_time($_[0],$_[1]) == 0; } $source, $destination; } else { print "What did you do wrong?"; } } catch { print "Couldn't copy $source to $destination"; } } } else { print "Insert the thumb drive!"; } __DATA__ C:/Documents and Settings/me/My Documents/home/checkbook2.xls|J:/My Do +cuments/home/checkbook2.xls C:/Documents and Settings/me/Local Settings/Application Data/Microsoft +/Outlook/Outlook.pst|J:/application data/Outlook/Outlook.pst C:/Documents and Settings/me/My Documents/gaming|J:/My Documents/gamin +g C:/Documents and Settings/me/My Documents/fantasy|J:/My Documents/fant +asy C:/Documents and Settings/me/Application Data/Notepad++/stylers.xml|J: +/application data/Notepad++/stylers.xml C:/Documents and Settings/me/Application Data/HexChat|J:/application d +ata/HexChat

So my question is, which is the better way to back up my files? If you have any other suggestions, I would like to know.

(I still need to figure out how to clean out files I deleted in the source which are still in my backups.)

Have a cookie and a very nice day!
Lady Aleena

In reply to Will checking last modified date take more time than just overwriting? by Lady_Aleena

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.