We help IT Professionals succeed at work.

Daily Backup using Perl

ICT-KevinM
ICT-KevinM asked
on
188 Views
Last Modified: 2012-06-21
I need to make a Perl backup script that creates a new dated folder every day, copies data from a source folder to the new dated folder until it reaches 28 days, then deletes the oldest folder and continues. This will have to function in Server 2k3. Below is what I have so far - I started getting confused when generating and naming the folders failed. Any Perl Genius want to help out?
#! C:\Perl\bin
 
use Date::Calc qw(Day_of_Week Week_Number Day_of_Week_to_Text);
use Time::localtime;
 
$tm = localtime;
($day, $month, $year) = ($tm->mday, $tm->mon, $tm->year);
 
###########################
# Set date/time/day of week
 
$year = $year+1900;
$month = $month+1;
$wday = Day_of_Week($year, $month, $day);
$wnum = Week_Number($year, $month, $day);
print "date set\n";
 
###########################
# Create new folders by date
 
use POSIX qw[strftime];
 
$DestDir='e:\budata';
$SourceDir='d:\source';
 
$DateDir=strftime("%a_%b_%d_%Y",localtime());
 
$DestDirFinal="$DestDir/$DateDir";
 
if(-e $DestDirFinal) {
      system("rm -fr $DestDirFinal");
}
 
mkdir($DestDirFinal) or die "Could not mkdir: $!\n";
system("cp -R $SourceDir $DestDirFinal");

Open in new window

Comment
Watch Question

Top Expert 2009

Commented:
Instead of using system, you should use perl commands, as it'll be easier to tell if it was successful.

Instead of "rm -fr" (a unix command - I thought you need win2k3?) to remove directories, you should use the rmtree from the File::Path module.

Instead of "cp -R" (again unix command?) to copy, use File::Copy::Recursive.

using these modules will also make your script more portable, in case it is needed on both unix and windows systems.

Author

Commented:
Thanks Adam - I guess I having been reading so much information on Perl, I have begun to blur the lines between perl and unix commands. Is it possible for me to name folders based on local date using a Perl command? I have searched high and low.
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks, that's great.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.