Many CGI programs suffer the unfortunate demise of having too many people
try to access it at the same time. When this happens, data files tend to
get erased. Once that data is gone, unless you backed it up to your desk top
computer, it's gone!
One solution is to use a flocking command "flock()" or to use lock and unlock
files. Both of these are good but neither guarantee 100% safety. Therefore
I have developed two utilities that are easily used to backup files and
restore lost data. It's not 100% guaranteed either but when used in conjunction
with one of these other protection methods then it goes a long way to reducing
most of the risk!
- backup.pl : backup([file_abs_path])
- This utility will take an existing file on your Unix server and make a backup
copy of it. It creates a file with an identical name, in the same directory but
the extension is changed to ".bak". I write in the program that every time
a critical file is generated or updated, it also gets backed up. This backup
utility requires only one parameter be passed to it. This parameter is the
absolute path to the file that you want backed up.
- restore.pl : @contents = restore([file_abs_path])
- This utility, when called, will search the directory in question for
a backup file (file same name but extension of ".bak"). If the backup file
is found the utility then restores the original files contents as they were
when it was last backed up. The program then returns from the subroutine the
contents of the file.
As you can see, these two utilities were designed to be used to together
but they don't have to be used together.
Here is an example of the backup of a file:
if(open(BITS, ">$DataPath/parts/catalogue.file")) {
flock(BITS, '2');
print BITS "$entry\n";
close(BITS);
backup("$DataPath/parts/catalogue.bak");
}
if you were using my 'fileadd.pl' utility then it would look like this:
fileadd("$DataPath/parts/catalogue.file", "$entry");
backup("$DataPath/parts/catalogue.file");
This will now make two files in the directory 'parts', they are:
catalogue.file and catalogue.bak
Here is an example of restoring a file. Two examples, one plain coding
and the second using my 'getfile.pl' utility:
if(open(BITS, "<$DataPath/parts/catalogue.file")) {
flock(BITS, '2');
@contents = ;
close(BITS);
if(!$contents[0]) {
@contents = restore("$DataPath/parts/catalogue.file");
}
}
....or....
@contents = getfile("$DataPath/parts/catalogue.file");
if(!$contents[0]) {
@contents = restore("$DataPath/parts/catalogue.file");
}
|