LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-29-2014, 04:26 PM   #1
kamran.ayub
Member
 
Registered: Jan 2012
Posts: 72

Rep: Reputation: Disabled
bash Script to import data in mysql DB


Dear All,

I want to create a reporting DB with mysql and bash script. I have directories which are growing day by day. I want to store their size in database along with incremental size on day to day basis so that I may retrieve records from database on need basis.

i.e directory listing by command du -sh --block-size=M * is

755M newyork
919M pakistan
197M britain
189M america

Now my Database has following columns. database name=reporting, table name=reporting.

Name, Current_Size, Old_Size, Date

Please guide me to write bash script which insert folder names alongwith size in mysql database mentioned.

Thanks

Regards,
Kamran
 
Old 10-29-2014, 04:53 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 22,115

Rep: Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915
Quote:
Originally Posted by kamran.ayub View Post
Dear All,
I want to create a reporting DB with mysql and bash script. I have directories which are growing day by day. I want to store their size in database along with incremental size on day to day basis so that I may retrieve records from database on need basis.

i.e directory listing by command du -sh --block-size=M * is

755M newyork
919M pakistan
197M britain
189M america

Now my Database has following columns. database name=reporting, table name=reporting. Name, Current_Size, Old_Size, Date
Please guide me to write bash script which insert folder names alongwith size in mysql database mentioned.
We will be glad to 'guide you'...so show us what YOU wrote already, and tell us where you're having problems. We are NOT going to write your script for you, but will be happy to help. There are THOUSANDS of easily-found bash scripting tutorials, many that include importing data into MySQL.

Please show some effort of your own, and don't ask people to write your scripts for you.
 
Old 10-29-2014, 05:04 PM   #3
kamran.ayub
Member
 
Registered: Jan 2012
Posts: 72

Original Poster
Rep: Reputation: Disabled
Hello TbOne,

Yes I have written a script. here are commands which I am using.

#!/bin/bash
cd /recording/web/

du -sh --block-size=M * | awk '{print $2}' > /home/kamran/names
du -sh --block-size=M * | awk '{print $1}' > /home/kamran/size


echo "INSERT INTO 'reporting' (Date) values (NOW());" | mysql -h x.x.x.x -u root -ppassword reporting;done

#################################

Not actually not getting to insert my collected directory names and sizes from mentioned files into mysql columns.
When I tried different commands it give me that columns and rows count does not match.

regards,
kamran

Last edited by kamran.ayub; 10-29-2014 at 05:05 PM. Reason: mistake
 
Old 10-30-2014, 11:30 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 22,115

Rep: Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915Reputation: 5915
Quote:
Originally Posted by kamran.ayub View Post
Hello TbOne,
Yes I have written a script. here are commands which I am using.
Code:
#!/bin/bash
cd /recording/web/

du -sh --block-size=M * | awk '{print $2}' > /home/kamran/names
du -sh --block-size=M * | awk '{print $1}' > /home/kamran/size

echo "INSERT INTO 'reporting' (Date) values (NOW());" | mysql -h x.x.x.x -u root -ppassword reporting;done
Not actually not getting to insert my collected directory names and sizes from mentioned files into mysql columns. When I tried different commands it give me that columns and rows count does not match.
Ok...so that VERY CLEARLY tells you what the problem is. Did you read and understand what you posted in your first post, and consider what the error says??

You said your table has FOUR columns...and your data has TWO. And you also need to read the man page on the mysql command, because you can easily specify the insert on the command line, without the echo. And you're putting data into two different files for some reason, first the name, then the size...and then you insert BOTH of them (somewhere...certainly not in the echo statement) into the database. So why bother to split them up?

Again, you need to look at one of the MANY easily found examples on how to do what you're after.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql import without overwriting existing data surferboy Linux - General 1 12-19-2012 09:51 AM
Import data from MySQL to MSSQL xeon123 Linux - General 2 05-06-2010 12:32 PM
import data back to mysql alaios Linux - General 2 02-27-2007 09:52 AM
import data to mysql server alaios Linux - Software 1 05-25-2005 01:37 AM
mysql data import bijuhpd Linux - Newbie 1 03-05-2005 09:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration