Backup/Restore & Export/Import & Server Move Perl DBM Database Tools
By
Will Bontrager
Imagine opening a database file and manipulating it as easily as you would a normal Perl variable. In fact, you can use the same code as you would if it was not a database file.
It's called tieing a variable to a file. A choice of several Perl modules make this easy to do.
The file the modules create is a DBM database (DBM is an acronym for DataBase Manager).
I use the feature a lot. Files many megabytes in size can be manipulated just as if they were actually residing in the script's memory variables. And you don't have to pay any attention to file updates; once the variable is tied to the file, the module being used does all file updates automatically.
But there is a drawback. Because DBM files are not plain text files, backups and server moves can be a hassle.
When backing up or restoring the DBM files themselves, FTP downloads and uploads must be done as if they were binary files. In the case of a server move, the new server's configuration might be different than the one on the old server, making the moved file useless.
This blog post will provide the tools to back up and restore Perl DBM files and the means to move the information in the files to other servers.
Backing up is exporting the data to a plain text file. Restoring is importing the data into the DBM file. FTP file downloads and uploads of these exported files must be done as ASCII/plain text files.
For server moves, simply export the data, move the plain text file to the new server, then import the data. A similar procedure can copy the data from one DBM file type to another DBM file type on the same server when a third-party script needs access to the DBM database, for example.
If your script uses dbmopen() instead of tie(), and you're unsure which DBM module is utilized, use Master Pre-Installation Tester or other means to determine which DBM modules are on your server. Possibilities are:
SDBM_File
DB_File
GDBM_File
NDBM_File
ODBM_File
(Module SDBM_File comes with perl.)
Once you have a list, make a backup copy of the original DBM files. Then use the backup script (below in the blog post) with each module until you get intelligible data in the exported file. Now you know which DBM module your script uses.
(If your script uses dbmopen() or the AnyDBM_File module, the effective DBM module could change if the hosting company reconfigures Perl or adds/removes other DBM modules.)
When exporting for backups or moves, the data is stored in a flat file database, one line per record. Thus, the return and linefeed characters need to be replaced with something else. The backup and restore scripts (below) allow you to specify what characters to use as replacements.
This script can back up/export the information in a DBM file to a plain text flat file database:
To make a backup file, customize, install, and run the above backup.cgi script.
Download the resulting plain text file for safekeeping. If moving the database to a new server, upload the plain text file to the new server.
This script can restore/import the information in a flat file database created with the restore.cgi script into a DBM database:
To restore the backup file, customize, install, and run the above restore.cgi script.
July 9, 2006
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.
Rate this blog post.
No page reload!