Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Parse MSSQL .bak file

by gryphon (Abbot)
on Dec 04, 2002 at 19:24 UTC ( [id://217564]=perlquestion: print w/replies, xml ) Need Help??

gryphon has asked for the wisdom of the Perl Monks concerning the following question:

Greetings fellow monks,

Due to circumstances beyond my control, I'm now in dire need of being able to parse a Microsoft SQL Server .bak backup file. It's not quite 3 gigs, and ultimately I'd love to convert it into something like XML or (even better) into MySQL SQL. I know I'll loose data relationships and various other things, but I don't care. I need at the data badly and rapidly, and I'm at a loss as to how to get there. I've searched here and on CPAN trying to find a module to help, but I'm out of luck. Any suggestions as to where I can turn? Thanks.

gryphon
code('Perl') || die;

Replies are listed 'Best First'.
Re: Parse MSSQL .bak file
by metadatum (Scribe) on Dec 04, 2002 at 19:33 UTC
    The first and last time I was given this to do, I had to download a trial version of MS SQL Server, install it, restore the bak file, and I exported all table definitions to a file, as well as the data in csv format.

    I then uninstalled MS SQL Server from the machine.

    Good Luck!
Re: Parse MSSQL .bak file
by mpeppler (Vicar) on Dec 04, 2002 at 21:52 UTC
    I assume that by .BAK you mean a database dump, right?

    I've not worked with MSSQL, but Sybase database dumps are binary dumps, using direct page copies from the original database device (and MSSQL is a direct descendant of the Sybase code line, in case you're wondering why this is relevant :-). Parsing such files is seriously non-trivial, unfortunately. Lots of people would love to have such a utility, though, because binary database dumps aren't portable between hardware platforms (or sometimes even between OS versions).

    Personally I'd check to see if MS offers an eval version of MSSQL, and then load the dump into a running server. This will almost certainly be the fastest way of getting at the data...

    This being said, I don't want to be excessively pessimistic. Have you looked at the file with a hex editor to see if there is any structure that you can distinguish?

    Michael

Re: Parse MSSQL .bak file
by Acolyte (Hermit) on Dec 05, 2002 at 18:08 UTC

    From your post I'd guess that you're probably having trouble with access to your primary MS SQL Server and really need to get at this data. You may want to give the MS SQL Server Desktop Engine a try. MS also has a trial version of SQL 2000 available that may suffice long enough for you to get at the data and convert it to another platform/format.

    Best of luck...

    Acolyte
    Studying at the feet of the masters
Re: Parse MSSQL .bak file
by gryphon (Abbot) on Dec 05, 2002 at 21:43 UTC

    In the world of total illogic which is that of our client, the requirement that required the data from this massive 2.9 gig BAK file is no longer required. However, I had nearly solved the problem based on all your suggestions. I downloaded a copy of MSSQL 2K Trial and was in the process of dumping the database into a more negotiable format. I originally tried MSDE, but you're limited to databases smaller than 2 gig.

    Getting back to Perl, though: Looking through the BAK file in a hex editor, I found it difficult to see many patterns. I concure that it would be very non-trivial to write a Perl parser for such files. However, it ought to be do-able. If anyone had the time and interest, I'm sure folks who like MySQL or PostgreSQL would be fans of a utility that would let them easily migrate an MSSQL database into something a little more open source.

    gryphon
    code('Perl') || die;

      Well - if the server is running it should be fairly straightforward to copy the data to flat files. I understand that MS in its infinite wisdom has deprecated the bcp utility, but I've used the following with Sybase:

      #!/usr/bin/perl -w use strict; use Sybase::Simple; my $dbh = new Sybase::Simple 'sa', 'pwd', 'server'; my $tables = $dbh->ArrayOfScalar(" select name from $db..sysobjects where type = 'U'"); foreach my $tab (@$tables) { system("bcp $db..$tab out $tab.bcp -c -Usa -Ppwd -Sserver"); }
      A similar script should be feasible with MS-SQL, either using MS's native tools, or using functionality from the FreeTDS project.

      Michael

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://217564]
Approved by rob_au
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (1)
As of 2024-04-24 16:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found