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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
10-29-2014, 04:26 PM
|
#1
|
Member
Registered: Jan 2012
Posts: 72
Rep:
|
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
|
|
|
10-29-2014, 04:53 PM
|
#2
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,425
|
Quote:
Originally Posted by kamran.ayub
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.
|
|
|
10-29-2014, 05:04 PM
|
#3
|
Member
Registered: Jan 2012
Posts: 72
Original Poster
Rep:
|
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
|
|
|
10-30-2014, 11:30 AM
|
#4
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,425
|
Quote:
Originally Posted by kamran.ayub
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.
|
|
|
All times are GMT -5. The time now is 08:41 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|