Lady_Aleena has asked for the wisdom of the Perl Monks concerning the following question:
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Will checking last modified date take more time than just overwriting?
by kcott (Archbishop) on Aug 19, 2013 at 05:56 UTC | |
by Lady_Aleena (Priest) on Aug 19, 2013 at 08:32 UTC | |
by kcott (Archbishop) on Aug 19, 2013 at 09:14 UTC | |
Re: Will checking last modified date take more time than just overwriting?
by stefbv (Curate) on Aug 19, 2013 at 05:56 UTC | |
Re: Will checking last modified date take more time than just overwriting?
by sundialsvc4 (Abbot) on Aug 19, 2013 at 12:57 UTC | |
Re: Will checking last modified date take more time than just overwriting?
by Laurent_R (Canon) on Aug 19, 2013 at 20:24 UTC |
Back to
Seekers of Perl Wisdom