Solved

Daily Backup using Perl

Posted on 2009-07-10
5
164 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

0
Comment
Question by:ICT-KevinM
  • 2
  • 2
5 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 24825130
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.
0
 

Author Comment

by:ICT-KevinM
ID: 24825172
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.
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 250 total points
ID: 24828178
Yes, you can use the $DestDirFinal like you have, which uses the strftime function to generate the folder name from the date.  Is this not what you wanted?

0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 250 total points
ID: 24828233
You are best not to mix backslashes and forward slashes.  Much easier to stick with forward slashes so you don't need to make differences between Windows and Unix/Linux systems.

change

$DestDir='e:\budata';
$SourceDir='d:\source';


to

$DestDir="E:/budata";
$SourceDir="D:/source";
0
 

Author Closing Comment

by:ICT-KevinM
ID: 31602113
Thanks, that's great.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl script 4 97
rename outfile before writing 2 71
How to search multiple patterms in a file with perl? 4 79
read an xml file in perl 2 15
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now