4

Has anyone got a one liner (command line) that will dump all databases to seperate dump files e.g.

forumdb.dump
wikidb.dump
etc.

3 Answers 3

4

Why not, still a long one liner though :)

mkdir mysql-all-dumps ; for x in `mysql -u root -pXXXXXXXX -e "SHOW DATABASES;" | tr -d "| " | grep -vE '(Database|information_schema)'` ; do mysqldump -v -u root -pXXXXXXXXX $x > mysql-all-dumps/$x.sql ; done

Make sure to replace the XXXX with the correct mysql root password and the verbose option in the mysqldump is optional as well.

4
  • Thanks, I can tell you have used this before :P It's well thought out
    – James
    Sep 13, 2011 at 21:08
  • Yes, when I had to backup all the individual databases prior to a mysql upgrade :)
    – SparX
    Sep 13, 2011 at 21:29
  • Nice - how would we go about this if we wanted a backup-user to dump the files as opposed to have root do it (so we don't have to have root passwords in cron files)?
    – morleyc
    Jan 20, 2013 at 20:47
  • The command is same and you just need to replace the username/password combo in the above command (-u root -pXXXXXXXX).
    – SparX
    Jul 12, 2016 at 8:11
4

I don't believe there is a good one-liner, but here is a script fragment. You might be able to adapt this to your usage. If you want to hard-code your database list, you could probably get it down to a one-liner. But I needed something that would be able to handle database adds and drops.

OPTIONS="--verbose --lock-tables --flush-logs"

BACKUPDATE=`date +"%y%m%d%H"`

DBSQL="SELECT SCHEMA_NAME FROM information_schema.SCHEMATA where SCHEMA_NAME!='information_schema' \
       AND SCHEMA_NAME!='performance_schema' order by SCHEMA_NAME"

# retrieve list of databases
DBS=`/usr/bin/mysql --defaults-extra-file=/etc/mysql/credentials.cnf --batch \
                                  --skip-column-names --execute "$DBSQL"`
# cleat it up a bit
DBS=`echo $DBS | tr -d '\n' | sed -e "s/ \+/ /g"`

# backup each database into a separate file
for DB in $DBS; do
  /usr/bin/mysqldump --defaults-extra-file=/etc/mysql/credentials.cnf ${OPTIONS} $DB > /backup_${DB}_${BACKUPDATE}
done
1
  • Nice script - what would one put in credentials.cnf? How would we add a user to MySQL that is only allowed to backup?
    – morleyc
    Jan 20, 2013 at 21:12
4

can do this

cd mysql_data_dir ; for d in $(ls -d */ | xargs basename) ; do mysqldump $d > /path/to/dumps/$d.dump; done

Basically list all the directories in the data directory, and then just dump them. I've done the above in a pinch if I didn't have a jobs set to backup individual files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .