Gossamer Forum
Home : Products : DBMan : Installation :

Archive DB?

Quote Reply
Archive DB?
I currently have two databases, both with the exact same setup but one is an archive database. At the moment I have to go through and manually edit the db file and move records from the normal database to the archive one. Is there a way that I could just put a button on the admin home page that would allow me to login, click the button, and have dbman move all records that are older than a certain number of days to the archive database.
Quote Reply
Re: Archive DB? In reply to
Yes, you could do it.

It would require creating a subroutine that would automatically do a search, collect the old records and then delete them from the current database and add them to the archive.

This is all off the top of my head, so I'm not promising anything. Read carefully the bolded parts of the code below. You'll need to make some changes based on your database.

Code:
sub archive_records {
#--------------------------------------
my ($days,$output,$archive,$fieldnum,$arch_num);

$days = 30; # Change this to the age of the records you want archived

$term = &get_date(time() - ($days * 86400));

$db_archive_file_name = $db_script_path . "/archive.db"; # Change the the name of your archive file

$fieldnum = 3; # Change to the number of the field that holds the date the record was added

open (DB, "<$db_file_name") or &cgierr("error in search.
unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
(/^#/) and next LINE; # Skip comment Lines.
(/^\s*$/) and next LINE; # Skip blank lines.
$line = $_;
chomp ($line); # Remove trailing new line.
@values = &split_decode($line);

if (&date_to_unix($values[$fieldnum]) < &date_to_unix($term)) {
$archive .= "$line\n";
++$arch_num;
}
else {
$output .= "$line\n";
}
}
close DB;

open (DB, ">$db_file_name") or &cgierr("error in archive_records.
unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
print DB $output;
close DB; # automatically removes file lock

open (DB, ">>$db_archive_file_name") or &cgierr("error in add_record.
unable to open database: $db_archive_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_archive_file_name.\nReason: $!");
}
print DB $archive;
close DB; # automatically removes file lock
&html_home($arch_num);
}

This assumes that there will not be a duplicate key that will be added to the archive database. I would only use this if the key for your main database is a numeric key that is tracked by DBMan.

You'll also need to add a line to db.cgi, sub main

Code:
elsif ($in{'archive_records'}) { if ($per_admin) { &archive_records; } else { &html_unauth; } }

And you'll need a link on your home page (probably) like this:

print qq!<a href="$script_link_url&archive_records=1">Archive old records</a> ! if ($per_admin);

And, if you want to have the number of archived records listed on your home page, you'll need to add to sub html_home, right at the beginning

$message = $_[0];

and somewhere outside of a print statement

Code:
if ($message) {
print "There were $message records archived.";
}

Now, before you do anything, make backups of ALL your files. I haven't tested this, aside from testing for syntax errors. I ain't gonna guarantee nuthin'! Smile

One other thing. You need to have adjusted the get_date subroutine so that it will accept input. Just to be on the safe side, I'll post the subroutine here.

Code:
sub get_date {
# --------------------------------------------------------
# Returns the date in the format "dd-mmm-yyyy".
# Warning: If you change the default format, you must also modify the &date_to_unix
# subroutine below which converts your date format into a unix time in seconds for sorting
# purposes.
my ($time) = $_[0];
($time) or ($time = time());

my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time);
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;

return "$day-$months[$mon]-$year";
}


------------------
JPD







[This message has been edited by JPDeni (edited June 02, 1999).]
Quote Reply
Re: Archive DB? In reply to
Ugh, sorry, if you read what I had posted here earlier. Everything worked once I replaced my get_date that I had gotten from your What's New mod. I do have one question though. I notice the sub has + 1900, does this mean that I have to change this at the end of the year for the year 2000.

[This message has been edited by Helpdesk (edited June 02, 1999).]
Quote Reply
Re: Archive DB? In reply to
Thanks for mentioning the "What's New" mod. I need to go in and fix that. I'm still learning as I go along and sometimes I forget to change some of the old stuff.

Not to worry about Y2K. The year value that is returned from the localtime function is the number of years since 1900. Next year, the year value will be 100. It's all taken care of. Smile

So the archiving thing worked? Cool!


------------------
JPD





Quote Reply
Re: Archive DB? In reply to
...


[This message has been edited by JPDeni (edited April 12, 2000).]
Quote Reply
Re: Archive DB? In reply to
Just wanted to reply and express my thanks for this great mod. I think someone should come up with HOWTO's for DBMan because you can do a lot of neat tricks.

I hate to tell you this, but I have one more question on this topic. Now that I have the archiving working, is it possible to allow users in the main database to search through the archives. For example, at the bottom of the search for there would be a drop down that said

Select Database: Main
Archives
Both

I am sharing a password and auth directory between the two.
Quote Reply
Re: Archive DB? In reply to
You're welcome. Smile

The main "HowTo" with DBMan is reading and understanding the script, coupled with learning Perl. It takes a while to get really proficient, but not too long to be able to start. A year ago, if I'd heard someone say talk about the "Perl" language, I would have thought they were talking about how oysters communicate. Smile I still have a really long way to go, though.

I have some mods made up, but a lot of the things people ask for here on the forum are very specific to their own databases.

Choosing to search one archive or the other wouldn't be much of a problem. On html_view_search (and also html_view_failure), take out the hidden field

<input type=hidden name="db" value="$db_setup">

and add a select field:

Code:
<SELECT NAME="db">
<OPTION VALUE="default">Main
<OPTION VALUE="archive">Archive
</SELECT>

If your the .cfg file for your archive database isn't "archive" change the bold type above.

Setting it to search both would be a whole lot tougher and the little select field thing above wouldn't work either. I'm not sure how you would pass the information to the script to search in both databases, unless you did it by a separate checkbox or something.

Once the information was passed, you'd need to create a new subroutine that would run the search twice and append the results from the second search to the results from the first search. To make it even more complicated, then you'd have to do a sort all over again if you wanted the results sorted together.

Since your database structures are identical, the only difference in the .cfg files is the $db_file_name value, right?

I have some ideas, but I think I'm gonna have to sleep on them before I can write them out. If having just one or the other database (but not both) is okay with you, though, the select field above will work.

Oh, yeah. If there is a problem with the search, and the user searched the archives, the "Archives" option will not be selected on the html_view_failure page. No way I can think of to do that.



------------------
JPD





Quote Reply
Re: Archive DB? In reply to
The ability to search both databases is really what I need. However, I have no idea how to do this, so you're farther than me. I would rather that it be a drop down but you know more than me, so tell me what's possible.
Quote Reply
Re: Archive DB? In reply to
It's odd that there are two questions tonight that really are the same -- yours and the one about "grouping" results. Both would require multiple passes through the query subroutine.

This one has the added problem of searching through two databases and probably sorting the results together. If you are using the short/long display mod, it adds even more problems, since in order to go to the long record display, the database file would have to be defined.

It's possible that I might wake up tomorrow and have it all figured out. But I wouldn't hold my breath.

Sorry.



------------------
JPD





Quote Reply
Re: Archive DB? In reply to
This specific database is not yet using the short/long mod. However, I am planning to implement it when I get the time, probably within the next week or two.