LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-27-2011, 10:28 AM   #1
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Rep: Reputation: 10
How to backup a particular Database!! in MYSQL


Hi All,

Thanks in Advance!!

How to backup a particular Databases..through Bash script!!

For example i have 6 databases;

Anish
linux
Software
Questions
Rhce
Google

these are the databases i have from that i want to take "Anish" and "questions" database backup regularly.
through script. Is it possible !!
 
Old 06-27-2011, 11:07 AM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,071

Rep: Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712
Quote:
Originally Posted by anishkumarv View Post
Hi All, Thanks in Advance!!

How to backup a particular Databases..through Bash script!!
For example i have 6 databases;

Anish
linux
Software
Questions
Rhce
Google

these are the databases i have from that i want to take "Anish" and "questions" database backup regularly. through script. Is it possible !!
Yes...did you try to look this up?? It's been covered many, MANY times, on this site, and Google has thousands of documents that tell you how.
Code:
mysqldump -u <user name> <database name> > /path/to/file.sql
You could have easily found this if you had bothered to look.
 
Old 06-27-2011, 11:19 AM   #3
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Original Poster
Rep: Reputation: 10
I got What i expect!!



Code:
#!/bin/bash
date=`date +%Y-%m-%d_%H.%M.%S`
echo Backup Started $date  >> /var/tmp/mysqlbackuplog/mysqldatabasebackup$date.log
###########################################################
#create directory to store mysql database backup
mkdir /var/tmp/mysqldatabasebackup$date
if [ $? -ne 0 ]
then echo "Backup failed with errors" for $date >> /var/tmp/mysqlbackuplog/mysqldatabasebackup$date.log
exit
else
echo “directory mysqldatabasebackup$date created”  >> /var/tmp/mysqlbackuplog/mmysqldatabasebackup$date.log
fi

# Back up MySQL databases 2 database name in the anish.txt file
###########################################################

for db in $(cat /root/Desktop/anish.txt)
do
                mysqldump -u root -proot  $db >> /var/tmp/mysqldatabasebackup$date/$db.sql
       echo DUMPING DATABASE FOR CLIENT ""$db"" >> /var/tmp/mysqlbackuplog/mysqldatabasebackup$date.log
       echo backup copy of $db is moved to /var/tmp/mysqldatabasebackup$date    >> /var/tmp/mysqlbackuplog/mysqldatabasebackup$date.log
        cd  /var/tmp/mysqldatabasebackup$date
        a=`du -sh $db.sql | cut -f1`
        echo THE SIZE OF $db.sql FILE IS $a >> /var/tmp/mysqlbackuplog/mysqldatabasebackup$date.log
done
echo "Backup completed successfully $date" >> /var/tmp/mysqlbackuplog/mysqldatabasebackup$date.log

cat /var/tmp/mysqlbackuplog/mysqldatabasebackup$date.log | mail -s "mysqlbackuplog" 07anis@gmail.com

Actually i know the command and all!! dude..but quite confused how to backup the particular

Two databases alone in Regular basis..okay!! Thanks TB0ne..

Last edited by anishkumarv; 06-27-2011 at 11:21 AM.
 
0 members found this post helpful.
Old 06-27-2011, 12:37 PM   #4
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,071

Rep: Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712
Quote:
Originally Posted by anishkumarv View Post
I got What i expect!!
Actually i know the command and all!!
Then why did you ask the question, if you knew the answer?
Quote:
dude..but quite confused how to backup the particular Two databases alone in Regular basis..okay!! Thanks TB0ne..
If you know how to back up ONE database, you know how to back up ANY databases. Seems like you've already got the script to do it. Does your script WORK, are you asking for help with it?? Not sure why you posed the question, since you said you already knew how to back up a database, and have a script that already seems to (at first glance), work for backing up multiples....

Last edited by TB0ne; 06-27-2011 at 12:39 PM.
 
Old 06-27-2011, 02:00 PM   #5
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi,
checking your script briefly I can say that it doesn't work because of some errors when I tried it on my test server at home.
I'll try to make some corrections (sometimes later) if you want help with your script if it's not working (although marked Solved)?

sorry for 'breaking in' to solved thread.
 
Old 06-27-2011, 05:57 PM   #6
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Original Poster
Rep: Reputation: 10
Hi TB0ne,

(if i wasted your time means...i apologize for that)

Actually my requirement is want to take 2 databases backup through script.. i don't know how to made

changes in my existing script(in my existing script it will take all the databases) ...

after posting this thread to here only one of my friend suggested using for loop we can easily solve

your requirement if you mention your (2 databases) alone in one file name means from that we can read

the db name and we can take easily backup

Quote:
# Back up MySQL databases 2 database name in the anish.txt file
###########################################################

for db in $(cat /root/Desktop/anish.txt)
now its working fine..so i marked this thread as solved..

but still i am not compromised with this method..is it possible to do with out mentioning

a db name in a file...??
 
Old 06-27-2011, 06:23 PM   #7
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
you can define the database names in a variable in the script like:
Code:
# Back up MySQL databases 2 database name in the anish.txt file
###########################################################
DATABASES="my_first_database
my_2nd-database"

for db in $DATABASES
which would be quite appropriate for this.
DATABASES="1st_name <-- this is deliberately written this way separating the names
2nd_database" <--- as this way the "do" loop will execute as many times you write the names to the DATABASES (you can define 10 or whatever you like)

Last edited by lithos; 06-27-2011 at 06:25 PM.
 
1 members found this post helpful.
Old 06-27-2011, 06:52 PM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
The default IFS http://bash.cyberciti.biz/guide/$IFS is <space><tab><newline>, so spaces would work instead of a newline there.

My test
Code:
test="astr bstr"

for str in $test
do
    echo $str
done
output
Code:
astr
bstr
 
1 members found this post helpful.
  


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
how to create backup MYSQL Script to backup my database for every 1hour RMLinux Linux - Newbie 3 11-20-2008 11:13 AM
backup mysql database nawuza Linux - Newbie 17 08-25-2008 03:40 PM
Create mysql user for backup of database timgerr Linux - Server 2 01-04-2008 11:44 AM
Backup software for MySql database blinton25 Linux - Software 5 06-01-2007 04:40 AM
How to import a mysql database from backup WindowBreaker Slackware 6 03-17-2006 12:08 AM


All times are GMT -5. The time now is 05:01 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration