Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 02-13-2008, 07:47 PM
Neyland Neyland is offline
 
Join Date: Oct 2004
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Perl script for csv

I have a number of csv files generated from the admincp->Maint section that I need to push back into a db. These files were generated with the column names, single quote, and comma as the delimiter.

Instead of using myphpadmin to do this one table at a time, I'd just like to write a perl script to loop through the directory contents and call the mysqlimport command passing it the filename.

I'm a relative newb to perl syntax so a little review on that would be helpful, but I need to know what values to pass for the mysqlimport switches --fields-optionally-enclosed-by and --fields-escaped-by.
Code:
#!/usr/bin/perl -w
use strict;
 
my $dir = '/home/DIRNAME/import';
my $dbname = 'MYDBNAME';
my $dbuname = 'DBLOGINNAME';
my $dbpswd = 'DBPASSWORD';
my $importfilename;
 
opendir(DIR, $dir) or die $!;
 
while (my $file = readdir(DIR)) {
    # No Directories
    next unless (-f "$dir/$file");
 
    # Look for files ending in .csv
    next unless ($file =~ m/\.csv$/);
    
    # Provide full path to file we want to import
    $importfilename = "$dir/$file"
  
  # Call mysqlimport
   system('mysqlimport --delete --fields-terminated-by=\',\' --ignore-lines=1 -u $dbuname -p $dbpswd $dbname $importfilename');
}

closedir(DIR);
exit 0;
Thanks!
Reply With Quote
  #2  
Old 02-14-2008, 07:19 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Neyland View Post
I have a number of csv files generated from the admincp->Maint section that I need to push back into a db. These files were generated with the column names, single quote, and comma as the delimiter.
Can you explain this a bit more, i am not aware of any generated CSV file from the Maintenance section of the AdminCP.
Reply With Quote
  #3  
Old 02-15-2008, 12:17 AM
Neyland Neyland is offline
 
Join Date: Oct 2004
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

AdminCP->Maintenance->Database Backup. Bottom of page "CSV backup of single database table".

I got it to work, for those wanting to know, dump the CSV files in a folder along with the attached script. You'll need to edit with your information and make it executable (chmod u+x importer.pl)

Code:
#!/usr/bin/perl -w
use strict;

my $dir = '/home/DOMAINNAME/DIRNAME';
my $dbname = 'DATABASENAME';
my $dbuname = 'DATABASE_LOGIN_NAME';
my $dbpswd = 'DATABASE_PASSWORD';
my $importfilename;
my $cmd;

opendir(DIR, $dir) or die $!;

while (my $file = readdir(DIR)) {
    # No Directories
    next unless (-f "$dir/$file");

    #File names should == table names!! ie access.csv for the access table
    # Look for files ending in .csv
    next unless ($file =~ m/\.csv$/);

    # Provide full path to file we want to import
    $importfilename = "$dir/$file";

   # Call mysqlimport
   # Imports CSVs exported with column names in first row, comma delimted, enclosed by quote
   $cmd ="mysqlimport --local --delete --fields-terminated-by=, --ignore-lines=1 --fields-enclosed-by=\\' --fields-escaped-by=\\\\ --user=$dbuname --password=$dbpswd $dbname $importfilename";

   system($cmd);
}

closedir(DIR);
exit 0;
Attached Files
File Type: zip importer.zip (625 Bytes, 1 views)
Reply With Quote
  #4  
Old 02-15-2008, 09:00 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Okay i see now what you are doing.

Not sure why you have choosen to go the CSV route, that is not a format intended to be used as backup with the intention to restore into the database. Best to use the regular tables dump options (inside AdminCP or even better from SSH commandline).
Reply With Quote
  #5  
Old 02-15-2008, 11:38 AM
Neyland Neyland is offline
 
Join Date: Oct 2004
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Oh I agree totally! I was in a situation where a forum owner got into a spat with the domain owner and didn't have keys to do a full backup and didn't have a writtable web folder before moving on. We even tried to do a VB backup to a writtable folder on a different web server and couldn't do it. The only thing he could do was download each table to csv. Because I ran a VB board myself he asked if I could help migrate him to a new host. So, we uploaded his software to his new host, ran the install, then I FTPd the csv files up, SSH'd in, ran the script and he was back in business.

But, what I found was there wasn't a whole lot of documentation on these csv files. I asked on the VB forums if they could tell me if the files were optionally enclosed or what the escape character was, but never got an answer. With trial and error I found the character you specify to enclose is a full enclose and not optional. This means integer fields are enclosed just like strings. The escape character is '\'.

I was just trying to throw this out there in hopes to save anyone some legwork if they happened to find themselves in a need to import the VB generated CSV files and had more than a few to do.

Reply With Quote
  #6  
Old 02-15-2008, 02:24 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

It does make sense that not many people can give real answers on this. Most people will never use this or even know about it (lol, i forgot about it myself). In most cases a CSV file is only usefull when moving smaller amounts of data between 2 uncompatible systems and not to move from MySQL to MySQL. A lot (well all) of structural information is lost, same probably for all binary data.

Still think a better work around might have been possible, but if you managed to move the data, then i would not spend much time on it anymore unless you still have serious problems.
Reply With Quote
  #7  
Old 02-15-2008, 05:55 PM
cheesegrits's Avatar
cheesegrits cheesegrits is offline
 
Join Date: May 2006
Posts: 500
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I don't think there is any other workable solution in the position he was in, with an uncooperative host. The PHP backup isn't going to work for anything like large tables. Can't do an SQL dump because of having no writeable directory to put it in. Leaving only the CSV option, which lets you download as you go.

-- hugh
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:17 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03945 seconds
  • Memory Usage 2,242KB
  • Queries Executed 12 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (1)postbit_attachment
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • postbit_attachment
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete