LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-16-2012, 01:42 AM   #1
sanjay87
Member
 
Registered: Oct 2011
Posts: 164

Rep: Reputation: Disabled
How to backup all mysql database individually in single command


Hi
Im trying the backup all my mysql database indidually i could int find the command .Wright now im using off follwoing command to backup all mysql databases.Can any one help me how can i get all individuall backup of single database in single mysql command.

mysqldump -u root -pPASSWORD -routines --all-databases > alldatabases.sql
 
Old 03-16-2012, 06:34 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
You can't do it in a single command into separate files.

This is the script I use to create backups from a MySQL server. In this case the server is a replication slave which is why the options are there to turn off the replication before the backup and turn it back on afterwards.

The databases are listed individually in DBLIST variable to give control over exactly what is backed up.

I use it with the "day name" BACKUPDATE so that I end up with 7 days worth of backups.

Once all the databases have been dumped the SQL files are then compressed.

You can put your username and password etc. into the DUMPOPTIONS variable as needed.

Code:
#!/bin/bash

# Options
# BACKUPDIR     Location to store backups
# DBLIST        Space separated list of database names to backup
# DUMPOPTIONS   Any options you wish mysqldump to use
# BINDIR        Path to executables

BACKUPDIR=/backup
DBLIST="mydatabase1 myforumdatabase mywordpressdatabase"
DUMPOPTIONS="--lock-all-tables"
BINDIR=/usr/bin

## Script Logic Starts Here
echo `date` MySQL Backup START

# Date to append to database names

# Uncomment the line below to use full dates
# Remember to make the day of week line a comment if you want to use the full dates.
#BACKUPDATE=`/bin/date +%Y%m%d`

# The versions gives day of the week backups, eg mydatabase.Mon.sql, mydatabase.Tue.sql
BACKUPDATE=`/bin/date +%a`

# Stop accepting replication while we backup
echo `date` Stop replication
${BINDIR}/mysqladmin stop-slave

# Iterate through the list of databases and take a dump
for DATABASE in ${DBLIST} ; do
  echo `date` Dumping ${DATABASE} START
  ${BINDIR}/mysqldump ${DUMPOPTIONS} ${DATABASE} > ${BACKUPDIR}/${DATABASE}_${BACKUPDATE}.sql
  echo `date` Dumping ${DATABASE} FINISH
done

# Start accepting replication
echo `date` Start replication
${BINDIR}/mysqladmin start-slave

# Compress the backups
echo `date` Compress Backup START
${BINDIR}/gzip ${BACKUPDIR}/*_${BACKUPDATE}.sql
echo `date` Compress Backup FINISH

# Thank you and goodnight
echo `date` MySQL Backup FINISH

Last edited by TenTenths; 03-16-2012 at 06:36 AM.
 
Old 03-16-2012, 04:35 PM   #3
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Yes you can do it in a single command...

mysqldump --opt --all-databases > outputfile.sql
 
Old 03-16-2012, 04:38 PM   #4
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
A note on the above, I NEVER USE THAT!

I usually do a simple for loop so that my database dumps are to seperate files.
Code:
for db in `ls /var/lib/mysql | egrep -v "mysql|otherdbyoudontwanttobackup|anotheroneyoudont|etc.."; do mysqldump --opt $db > /tmp/mysqldumps/$db_$(date +%H-%M_%m-%d-%Y).sql; done
 
Old 03-16-2012, 04:39 PM   #5
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
I have a syntax error in the code, there is a missing "`" after the quote for the egrep command.
 
Old 03-17-2012, 08:48 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by Kustom42 View Post
Yes you can do it in a single command...

mysqldump --opt --all-databases > outputfile.sql
Read the Original Post, he wants individual files per database rather than a single file.
 
Old 03-17-2012, 01:19 PM   #7
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Yea i missed that one my first post. The for loop works perfectly for individual files
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to backup a particular Database!! in MYSQL anishkumarv Linux - Newbie 7 06-27-2011 05:52 PM
how to create backup MYSQL Script to backup my database for every 1hour RMLinux Linux - Newbie 3 11-20-2008 10:13 AM
backup mysql database nawuza Linux - Newbie 17 08-25-2008 02:40 PM
How to Umount single MySQL database KimVette Linux - Software 2 07-13-2005 12:55 PM
MySQL - command - create user/single database dmedici Linux - Software 3 06-21-2004 03:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:41 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
Open Source Consulting | Domain Registration