Z
Z
zvuki2011-12-03 19:18:01
MySQL
zvuki, 2011-12-03 19:18:01

How to backup with mysqldump --all-databases so that it does a database_name.sql for each database?

How to backup with mysqldump --all-databases so that it does a database_name.sql for each database?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Andrey Burov, 2011-12-03
@zvuki

for dbname in `echo show databases| mysql -u user -ppass`; do
echo "Dump $dbname..."
mysqldump -u user -ppass $dbname > "$dbname.sql"
done;

N
nikel303, 2011-12-03
@nikel303

There is another such script, you can even put it on cron.

#!/bin/bash

MyUSER="dbuser"
MyPASS="dbpass"
MyHOST="localhost"
 
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"

#MYSQL="/usr/local/bin/mysql"
#MYSQLDUMP="/usr/local/bin/mysqldump"

CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
 
DEST="./db_backup"
 
MBD="$DEST"
HOST="$(hostname)"
NOW="$(date +"%Y%m%d")"
 
FILE=""
DBS=""
 
# DO NOT BACKUP these databases, delemiter SPACE
IGN="information_schema"

# Get all database list first
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"

for db in $DBS
do

    skipdb=-1
    if [ "$IGN" != "" ]; then
    for i in $IGN
    do
      [ "$db" == "$i" ] && skipdb=1 || :
    done
    fi
 
    if [ "$skipdb" == "-1" ] ; then
    MBD="$DEST/$db"    	
    [ ! -d $MBD ] && mkdir -p $MBD || :
    FILE="$MBD/$NOW.sql.gz"
          $MYSQLDUMP --opt -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE
    FNUM="$(find $MBD/* | wc -l)"
    if [ $FNUM -ge 0 ] ; then
      find $MBD/* -type f -mtime 20 -exec rm -rf {} \;
    fi
    fi

done

@
@sledopit, 2011-12-03
_

And further. It doesn't have to be so perverted. You can extract the desired database from a full backup with just one command:
sed -n -e '/CREATE TABLE.*mytable/,/CREATE TABLE/p' full.dump > mytable.dump
Either without even pulling it out, but directly directly like this:
mysql --one-database db_to_restore < full.dump

M
Mixailo, 2011-12-03
@Mixailo

#!/usr/local/bin/bash
DIR="/path/to/backup"
mkdir -p $DIR
LOG="/path/to/log.log"
touch $LOG
TIMENAME=`date +%d.%m.%Y-%H.%M`
db=`mysql -u USERNAME -h localhost -pPASSWORD -Bse 'show databases'`
for n in $db; do
TIMEDUMP=`date '+%T %x'`
echo "backup has been done at $TIMEDUMP : $TIMENAME on db: $n" >> $LOG
mysqldump -u USERNAME -h localhost -pPASSWORD $n | gzip -c > "$DIR/mysql-$n-$TIMENAME-db.dump.gz"

I am using something like this.

Y
YourChief, 2011-12-03
@YourChief

no way. either back up one database at a time, or separate the databases by files dumped into a heap

D
Dim Boy, 2016-03-29
@twix007

cd /home/backup/ && mysql -uappsroot -uUSER -e 'show databases' | while read dbname; do mysqldump -uUSER -pPASS --complete-insert "$dbname"|gzip > "$dbname".sql.gz; done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question