Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate last date and check for file

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
I am making the endeavor to learn perl. I have a project where I have a table in mysql that holds the names of servers along with a number indicating how recently they should have backed up. (i.e. servername=mail, days=2)

So I need to take that number of days (2), calculate the date from the current date, then search the system for a file with the word mail in it and if a file exists, check to see if it was created with in the last 2 days. If it has been created, display a Y, if a file doesn't exist or is older than two days, display a N.

I can connect to the database and pull in the data, I just don't know how to go about calculating the date and searching to compare.
 
Hi

proggybilly said:
I can connect to the database and pull in the data, I just don't know how to go about calculating the date and searching to compare.
Personally I prefer to perform the date calculations in the the SQL [tt]select[/tt], for example with the [tt]date_sub()[/tt] function.

Regarding the searching, we probably have code for that here in the forum. Have you checked the old threads ?

Anyway, for the search part of the task some sample data would be helpful.

Feherke.
 
Ok as far as data goes, table holds server name and how often a backup should occur in backup directory, so for example:

web 2

files exists on system in similar format to this:
web200909292242.tbz2

and when you do a ls -l of the directory, you get this:
Sep 30 01:05 web200909292242.tbz2

I want to take the server name, search for a file such as the one above, then check the date attached to the file and see if it is within the number of days indicated from the number 2 from the example, in other words take todays date, calculate the date from two days ago, and see if a file exists between two days ago and today.
 
Here is my perl code thus far:

Code:
#!/usr/bin/perl
use DBI;

$dsn = "DBI:mysql:database=mylog;host=localhost";
$dbh = DBI->connect($dsn, 'root', 'Nix-1a', {'RaiseError' => 1});


$myquery = $dbh->prepare("SELECT server, bu_days FROM master_bu");
$myquery->execute();

while (@row = $myquery->fetchrow_array){
  print "@row\n";
  }

 
Can you rely on the date/time built into the filename by the backup software (probably when the backup started) instead of the date/time held by the file system? This will either be the time it finished or the last time anyone touched the file. You'll note that even in your example the backup started before midnight and finished on the following day, which will probably mess with your calculations...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
The backup is really nothing more than a .tbz2 file that is rsyncd to a server and then backed up to tape from there. Right now just wanting to verify existance on the server. I have been able to do my date calculations.

Right now I need to find a file name that exists between previous date and today.. for example my file name is mail-2009100612345.tbz2(the 12345 is not a static thing, each server has a random set of #'s appended to the filename but all are in tbz2), you can see the date in yyyymmdd format as 20091006. I have a variable that holds today's date as 20091008. I am already changing to the directory where the file is held. What I wanna do is find a file name that is between mail-20091006 and mail-20091008. And if a file exists set a variable = to Y.
 
So all you need to do now is an opendir, readdir through the entries, and see if they match your calculated dates; which part are you stuck on? There is a small piece of example code under perldoc -f readdir.

Annihilannic.
 
The part I was stuck on is what to do, I am kinda learning as I go with PERL.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top